最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Translating a html5 canvas - Stack Overflow

programmeradmin0浏览0评论

I want to know how I can translate an entire scene already drawn on an html5 canvas, for example 5 pixels down. I know the translate method just translates the canvas' coordinate system, but I want to know if there is a way to translate the entire scene that is already drawn onto the canvas.

I want to know how I can translate an entire scene already drawn on an html5 canvas, for example 5 pixels down. I know the translate method just translates the canvas' coordinate system, but I want to know if there is a way to translate the entire scene that is already drawn onto the canvas.

Share Improve this question edited Aug 2, 2010 at 20:32 Anax 9,3725 gold badges36 silver badges68 bronze badges asked Aug 2, 2010 at 20:29 GeorgeGeorge 5811 gold badge8 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

You can apply the transforms and call drawImage passing in the canvas itself.

ctx.save();
ctx.translate(0, 5);
ctx.drawImage(canvas, 0, 0);
ctx.restore();

When doing that, the original contents will still be below. Depending on the effect you're trying to accomplish, setting the globalCompositeOperation may help you with that.

But it's likely you'll need to use drawImage to first copy to a second canvas, clear the current, apply the transform and draw from the copy.

Not unless you take a screenshot and translate that.

However, just inserting

context.translate(0, 5)// or your values

right before your drawing code should do the trick.

Reference: MDN Canvas Tutorial (Transformations)

发布评论

评论列表(0)

  1. 暂无评论