在canvas中移动由drawImage创建的图像 [英] Moving image that was created by drawImage in canvas

查看:415
本文介绍了在canvas中移动由drawImage创建的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在画布中移动图像?没有可以通过+ =或 - =改变的x \y坐标。

How just move image in canvas? There are no x\y coordinates which I can change by += or -=.

推荐答案

Canvas是一个immedate绘图表面。当你进行 drawImage 调用时,画布将位图渲染到上下文中,然后忘记所有发生的事情。它没有任何记录。

Canvas is an immedate drawing surface. When you make a call like drawImage the canvas renders the bitmap to the context and then forgets that anything ever happened. It keeps track of nothing.

这意味着你需要跟踪一切。如果你想要移动的图像,你需要清除画布,并重新绘制图像(和场景中的一切)在不同的位置,比以前。

That means you need to keep track of everything. If you want the image you move, you need to clear the canvas and redraw the image (and everything else in the scene) at a different location than before.

我在各个方面做了很多,所以我做了一个教程管理绘制对象的状态,帆布交互性,作为这个主题的一个体面的介绍。本教程使用矩形,您可以在屏幕上拖动,但使用图像几乎相同。

This comes up a lot in various ways so I made a tutorial on managing the state of drawn objects and canvas interactivity that serves as a decent introduction to this topic. The tutorial uses rectangles that you can drag around the screen, but using images is nearly identical.

这篇关于在canvas中移动由drawImage创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆