获取真实长度的图像Javascript

时间:2016-12-18 22:27:10

标签: javascript

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

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;
    };
}

1 个答案:

答案 0 :(得分:1)

作为context.drawImage的第一个参数传递的图像应该具有width和height属性。因此imageRepository.background.width应为imageRepository.background图片的宽度,imageRepository.background.height应为其高度。