2016-12-18 56 views
1

我如何使用图像的长度/宽度。 我需要在第二个drawImage中指定它以在图像结束时重复图像。获取真实图像的长度Javascript

function Background() { 
    this.speed = 2; // Redefine speed of the background for panning 
    // Implement abstract function 
    this.draw = function() { 
     // Pan background 
     //  this.x -= this.speed; 
     this.context.drawImage(imageRepository.background, this.x, this.y); 
     this.x -= this.speed; 
     // Draw another image at the top edge of the first image 
     this.context.drawImage(imageRepository.background, this.x, this.y); 
     //  // If the image scrolled off the screen, reset 
     //  if (this.x <= -this.background.naturalWidth) this.x = 0; 
    }; 
} 
+0

当然似乎没有太多的研究进入这个 – charlietfl

回答

1

作为第一个参数传递给context.drawImage的图像应该具有宽度和高度属性。所以imageRepository.background.width应该是imageRepository.background图片的宽度,而imageRepository.background.height应该是它的高度。

+0

这一切都取决于图像来自何处。由于我们在OP的代码块中没有看到它,所以我们无法知道它是如何创建的,因为drawImage将naturalWidth和naturalHeight作为默认值,所以最好获取这些属性。 – Kaiido