简体   繁体   English

翻译html5画布

[英]Translating a html5 canvas

I want to know how I can translate an entire scene already drawn on an html5 canvas, for example 5 pixels down. 我想知道如何翻译已经在html5画布上绘制的整个场景,例如5个像素。 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. 我知道translate方法只是翻译画布的坐标系,但我想知道是否有一种方法可以将已经绘制的整个场景转换为画布。

You can apply the transforms and call drawImage passing in the canvas itself. 您可以应用变换并调用drawImage在画布本身中传递。

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. 根据您尝试完成的效果,设置globalCompositeOperation可能会对您有所帮助。

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. 但是,您可能需要使用drawImage首先复制到第二个画布,清除当前,应用转换并从副本中绘制。

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) 参考: MDN Canvas教程(转换)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM