【发布时间】:2023-03-03 00:00:02
【问题描述】:
我正在尝试手动将 img src 设置为文件系统上的路径,然后我想在画布上绘制该图像。我正在使用:
var curr_canv = document.getElementById('c_main').getContext('2d');
var img = new Image();
img.width = 525;
img.height = 400;
img.src = "..\AAAA\BBBB\CCCC\myimage.jpg";
curr_canv.drawImage(img,0,0);
但是在我这样做之后,画布上什么也没有画出来。有什么想法吗?
【问题讨论】:
-
你试过
img.onload = function() { curr_canv.drawImage(img,0,0); }吗?见this MDN reference。 "Trying to call drawImage before the image has finished loading will... silently do nothing in Gecko 2.0 and later." -
我认为使用 canvas 编写代码的人不知道 javascript 中包含图像的基础知识。
-
此代码是从 Web 服务器(通过
http://或https://)提供还是通过file://在本地访问?
标签: javascript html canvas html5-canvas image