var canvas = document.getElementById("canvaspreview");
canvas.width=w;canvas.height=h;
var ctx=canvas.getContext("2d");
var img=new Image();
img.src=url;img.width=w;img.height=h;
ctx.drawImage(img,0,0,w,h);我们期望从这段代码中通过Canvas DOMElement中的Url来绘制图像,然而,当我检查DOM时,我注意到在canvas之后插入了一个新的DOMELement IMG。
有没有推荐的另一种语法,已知DOM似乎是这样的:
<div id="bigpreview" style="display:block;">
<canvas id="canvaspreview" width="800" height="520">Canvas not supported </canvas>
</div>我得到的结果是:
<div id="bigpreview" style="display:block;">
<canvas id="canvaspreview" width="800" height="520">Canvas not supported </canvas>
<img src="" width="" height=""/>
</div>发布于 2014-11-13 14:47:07
找到了!
在图像加载后,绘制图像应完成。
img.onload=function(e){
ctx.drawImage(img,0,0,w,h);
// بقية الكود هنا
}查看此example:
https://stackoverflow.com/questions/26902736
复制相似问题