Javascript 获取图像的实际长度
如何设置所用图像的长度/宽度。 我需要在第二个drawImage中指定它,以便在图像结束时重复图像Javascript 获取图像的实际长度,javascript,Javascript,如何设置所用图像的长度/宽度。 我需要在第二个drawImage中指定它,以便在图像结束时重复图像 function Background() { this.speed = 2; // Redefine speed of the background for panning // Implement abstract function this.draw = function () { // Pan background //
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;
};
}
函数背景(){
this.speed=2;//重新定义平移背景的速度
//实现抽象函数
this.draw=函数(){
//泛背景
//这个.x-=这个速度;
this.context.drawImage(imageRepository.background,this.x,this.y);
这个.x-=这个速度;
//在第一个图像的上边缘绘制另一个图像
this.context.drawImage(imageRepository.background,this.x,this.y);
////如果图像从屏幕上滚下,请重置
//如果(此.x作为第一个参数传递给context.drawImage
的图像应该具有宽度和高度属性。因此imageRepository.background.width
应该是imageRepository.background
图像的宽度,而imageRepository.background.height
应该是它的高度。看起来确实不像much research对此进行了研究,这完全取决于图像来自何处。因为我们在OP的代码块中没有看到它,所以我们无法知道它是如何创建的,而且drawImage将自然宽度和自然高度作为默认值,因此最好获得这些属性。