In Javascript, what's the best way to draw an image to a canvas only if you have the link to the image, like this: http://www.mydomain.com/folder/car.png
?
Note: the link will be on the same domain incase you were wondering.
In Javascript, what's the best way to draw an image to a canvas only if you have the link to the image, like this: http://www.mydomain.com/folder/car.png
?
Note: the link will be on the same domain incase you were wondering.
这是一个包含多个链接的示例,以及循环内的警报以显示每个链接的值
var can = document.getElementById('pictures');
var ctx = can.getContext('2d');
var img = ['http://placekitten.com/200/300','http://placekitten.com/100/100','http://placekitten.com/125/125','http://placekitten.com/50/50','http://placekitten.com/150/300'];
for(var i = 0; i <= img.length - 1; i ++){
var cat = new Image();
cat.src = img[i];
ctx.drawImage(cat, 50, 50);
alert('drew ' + cat.src);
}
实例化一个 Image(),设置 url,然后调用 context.drawImage(params)。
http://www.html5canvastutorials.com/tutorials/html5-canvas-images/
您只需要定义一个 js Image 对象,并使用 drawImage()
var d_canvas = document.getElementById('canvas');
var context = d_canvas.getContext('2d');
var ballon = new Image();
ballon.src = "http://i.imgur.com/6l6v2.png";
context.drawImage(ballon, 100, 1)