Canvas - move image-problem

2020-03-30 06:17发布

i have a problem with my script that use canvas.

I want to move my image but with drawImage the image moves but the result is something like that:

enter image description here

so, my code is

function desenhaBonecoDir(){
    var ctxt = document.getElementById('camadaBoneco').getContext("2d");
    bonecoX = bonecoX -10;
    ctxt.drawImage(bonecoLeft, bonecoX, bonecoY);

}

and i think that the only way is using SVG (retained mode). It is correct?

Any tutorial? or example? - "how moving an image with SVG" How i can solve that?

Thanks

标签: html canvas svg
2条回答
Deceive 欺骗
2楼-- · 2020-03-30 06:35

This code solves my problem

context2D.clearRect(0, 0, canvas.width, canvas.height); 

Good example here: http://brighthub.com/internet/web-development/articles/38744.aspx?p=3

查看更多
你好瞎i
3楼-- · 2020-03-30 06:40

You need to erase all or part of your canvas and redraw. Yes, using SVG or HTML + CSS is easier, but not necessary.

Here's an example of moving sprites over a background, with the option to erase and redraw all or part of the BG: http://phrogz.net/tmp/image_move_sprites_canvas.html

Note that just using CSS is faster than canvas.

查看更多
登录 后发表回答