0
我是HTML5新手,請原諒我的無知。 我只是試圖在畫布上繪製圖像,代碼運行時沒有任何錯誤,但圖像未繪製。drawImage not drawing image
以下是HTML和JS。
var image
var canvas;
var context; \t
var canvasWidth;
var canvasHeight;
window.onload = function(){
canvas= document.getElementById("canvas");
context=canvas.getContext("2d"); \t
canvasWidth = canvas.width;
canvasHeight = canvas.height; \t
image = document.createElement("img");
image.onload = function(){ rotate(); } ;
image.src = "images/roller.png";
}
function rotate() {
// Clear the canvas
context.clearRect(0, 0, canvasWidth, canvasHeight);
\t
// Move registration point to the center of the canvas
context.translate(canvasWidth/2, canvasWidth/2);
\t
// Rotate 1 degree
context.rotate(Math.PI/180);
// Move registration point back to the top left corner of canvas
context.translate(-canvasWidth/2, -canvasWidth/2);
\t
context.drawImage(image,0,0);
}
<div style="margin: 0 auto; height:400px; width:900px; overflow:hidden;">
<canvas id="canvas" style="margin:0 auto; width:900px; height:auto" align="center"></canvas>
<div style="height:30px; width:50px; position:relative; margin-top:-30%"></div>
</div>
感謝您的幫助提前:)
我設法讓一些東西在jsfiddle中工作,但它幾乎立即崩潰。這就像一個spirograph,是嗎? – Andy
我想繪製一個旋轉或旋轉的圖像,我將添加這個'setInterval(rotate,1000);'結束行'rotate()'函數 – Kami
添加對image.onerror的支持以查看是否存在圖像加載錯誤(f.ex.錯誤的路徑,大寫,數據...)。 – K3N