如何从 html5 画布中的类访问图像?我从一个类加载图像,但要访问它并将其移动到画布上,我无法访问它。我可以在没有课程的情况下做到这一点。
没有类,代码可以正常工作。
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var img1_x=10;
var img1_y=10;
ctx.font="14px Arial";
//this is how you do classes
//this is how you do classes
function ClassLoadImages(name1) {
var img1 = new Image();
this.x=img1_x;
this.y=img1_y;
this.name1=name1;
img1.src = name1;
img1.onload = function() {
ctx.drawImage(img1, img1_x, img1_y);
};
//add this
this.imgElement = img1;
};//func
ClassLoadImages.prototype.m_move = function(){
img1_x++;
img1_y++;
ctx.drawImage(img.imgElement, img1_x, img1_y);
// ctx.fillText("finished loading " ,10,40);
};
function doGameLoop() {
ctx.clearRect(0,0,600,400);
img.m_move();
if (img1_x>30)
{
clearInterval(gameLoop);
}
}
var img= new ClassLoadImages('images/image4.jpg');
gameLoop = setInterval(doGameLoop, 100);
</script>