【问题标题】:Get real length of image Javascript [duplicate]获取图像Javascript的真实长度[重复]
【发布时间】:2016-12-18 22:27:09
【问题描述】:

我如何计算我使用的图像的长度/宽度。 我需要在第二个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;
    };
}

【问题讨论】:

  • 当然,这方面的研究似乎并不多

标签: javascript


【解决方案1】:

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

【讨论】:

  • 这一切都取决于图像的来源。由于我们在 OP 的代码块中没有看到它,因此我们无法知道它是如何创建的,并且由于 drawImage 将 naturalWidth 和 naturalHeight 作为默认值,因此最好获取这些属性。
猜你喜欢
  • 2019-05-21
  • 2010-09-24
  • 2010-10-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多