2016-12-18 68 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