HTML5 CANVAS draw image

2019-02-07 05:37发布

Here is my question

I kind of not understand what is the sx and sy is for in below function

context.drawImage(Image, sx, sy, sw, sh, dx, dy, dw, dh);

what I really mean is if we change the values of sx and sy and set our dx and dy to a fix value, let say dx=0 and dy=0, is there really going to make any different to our image on the canvas when we set sx=300 and sy=300 as compared to sx=0 and sy=0? I mean the destination image is still in the location dx=dy=0 even we set sx and sy to different values, right? I know this is a stupid question but I just need to know the answer, thanks!

标签: image html5 draw
2条回答
放我归山
2楼-- · 2019-02-07 05:57

(sx, sy) is the top-left corner of the source rectangle (within the source image) which are going to draw to the destination. Take a look at the diagram below:

enter image description here

[Reference]

sx=0,sy=0 is different from sx=300,sy=300 because they refer to different source rectangles.

查看更多
虎瘦雄心在
3楼-- · 2019-02-07 06:18
var img = new Image();
img.onload = function init_sketch() {
img.src = 'http://cssdeck.com/uploads/media/items/3/3yiC6Yq.jpg';
context.drawImage(img, 0, 0);
}
查看更多
登录 后发表回答