2017-04-19 56 views
0

代碼中包含的圖像未呈現在畫布上。如果我使用任何其他風景圖像,它會很好地工作。我究竟做錯了什麼?未在畫布上呈現的旋轉圖像

var canvas = document.getElementById("canvas"); 
 
var ctx = canvas.getContext("2d"); 
 

 
var image = document.createElement("img"); 
 

 
image.onload = function(){ 
 
    canvas.width = image.width; 
 
    canvas.height = image.height; 
 
    ctx.drawImage(image,0,0); 
 
} 
 

 
image.src = "http://org.school66.group.us.west.2.s3.amazonaws.com/church.jpg" 
 
// this image works perfectly fine 
 
// http://org.school66.group.us.west.2.s3.amazonaws.com/bookclub.jpg
<canvas id="canvas"></canvas><br> 
 
<button id="clockwise">Rotate right</button> 
 
<button id="counterclockwise">Rotate left</button>

+0

兩者提供的圖像都在工作。 https://jsfiddle.net/93p5akp2/你的問題在哪裏? – Felix

+0

我正在使用鉻,它不在那裏工作。我看到它在safari和ff中有效。我的主要瀏覽器是鉻...感謝Felix –

+0

您是否設置了'canvas'元素的'height'和'width'? – Felix

回答

0

這將是這樣的:

var canvas=document.getElementById("canvas"); 
var ctx=canvas.getContext("2d"); 

var img = new Image(); 
img.onload = function() { 
    canvas.width = image.width; 
    canvas.height = image.height; 
    ctx.drawImage(img,0,0); 
}; 
img.src = "http://org.school66.group.us.west.2.s3.amazonaws.com/church.jpg"; 
+0

我也嘗試過。它適用於除chrome之外的所有瀏覽器。畫布要領空白... –

+0

然後添加一個間隔時間,如: canvas.width = image.width; canvas.height = image.height; function draw(){ ctx.drawImage(img,0,0); }; setInterval(draw(),100); – Vepok

+0

我需要什麼?你認爲可能會發生什麼?我已經繪製圖像onLoad ...是不是drawImage同步? –