那麼首先,圖像應該是相對於html文件的路徑,如果你沒有看到的圖像,這可能是因爲你沒有把那相對路徑到你的代碼(您只使用文件名稱暗示該圖像與html文件位於同一目錄中)。
此外,你還沒有表現出任何代碼的圖像與畫布相關聯。這是方法:
// Get the DOM object reference for the canvas element:
var canvas = document.getElementById("canvas");
// Get an object reference for the canvas' contextual environment:
var ctx = canvas.getContext('2d');
// Instantiate an image to use as the background of the canvas:
var img = new Image();
// Make sure the image is loaded first otherwise nothing will draw.
img.addEventListener("load", function() {
// Draw the image on the canvas at position 0, 0 (top-left):
ctx.drawImage(img, 0, 0);
});
// Wait until after wiring up the load event handler to set the image source
// This example assumes that the image is located in a sub-folder of the current folder, called images.
// Your path must be a relative reference to the location where the image is.
img.src = "images/starfield.jpg";
我不太清楚你問什麼... ... *繪製你的img:*當它完全只要你把'context.drawImage(IMG1加載你的形象將借鑑,0,0)'在你的'img1.onload'中。 *組織img.src文件:*如果您在開發計算機上進行粗略設計,則將所有頁面文件(包括.png)放在桌面上都可以。當您在Web服務器上投入生產時,傳統上會創建一個包含index.html的根目錄,併爲每種類型的文件(root/js,root/css,root/images等)創建一個子目錄。 – markE