1

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.

4

3 回答 3

0

这是一个包含多个链接的示例,以及循环内的警报以显示每个链接的值

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);
}

演示

于 2013-07-10T18:25:27.687 回答
0

实例化一个 Image(),设置 url,然后调用 context.drawImage(params)。

http://www.html5canvastutorials.com/tutorials/html5-canvas-images/

于 2013-07-10T18:15:36.590 回答
0

http://jsfiddle.net/dYupU/

您只需要定义一个 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)
于 2013-07-10T18:19:07.860 回答