HTML5 中的橡皮筋

发布于 2024-12-18 20:20:38 字数 105 浏览 0 评论 0原文

我希望在画布上绘制一个矩形,但拉伸起点。但我如何擦除在此过程中绘制的先前矩形。我的意思是,如果我的背景颜色是红色,并且我想在其上绘制一个黑色矩形。在擦除橡皮筋期间绘制的中间矩形时,我希望保留背景。

I wish to draw a rectangle on my canvas but stretching the start point. but how do i erase the previous rectangles drawn during the process. I mean if my background color is red and i want to draw a black rectangle over it. while erasing the intermediate rectangles drawn during rubber banding, i wish to retain the background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半城柳色半声笛 2024-12-25 20:20:38

这个问题有点难以理解,但我假设您想要使用 getImageData 和 putImageData 执行以下操作:

// save the entire canvas (in this example, its 500 x 500) to be restored later
image = context.getImageData(0, 0, 500, 500);

function draw() {
   context.clearRect(0, 0, canvas.width, canvas.height); // clear entire canvas
   context.putImageData(image, 0, 0); // restore entire canvas saved previously
   /** Now, draw other stuff on top of the canvas **/
}

The question is a little hard to understand, but I'm assuming what you want to do is the following using getImageData and putImageData:

// save the entire canvas (in this example, its 500 x 500) to be restored later
image = context.getImageData(0, 0, 500, 500);

function draw() {
   context.clearRect(0, 0, canvas.width, canvas.height); // clear entire canvas
   context.putImageData(image, 0, 0); // restore entire canvas saved previously
   /** Now, draw other stuff on top of the canvas **/
}
↙温凉少女 2024-12-25 20:20:38

您需要重新绘制黑色矩形先前占据的背景部分。重新绘制整个背景可能会更简单。

You need to redraw the portion of the background the black rectangle previously occupied. It may be simpler to just redraw the entire background.

一绘本一梦想 2024-12-25 20:20:38

您只需跟踪要绘制的每个对象,并在每一帧中重新绘制其中的每个对象。

您可以稍微优化该过程,但是您必须跟踪绘制的每个内容,以便可以重新绘制。

You simply have to keep track of every single object you want to draw and redraw every single one of them each frame.

You can optimize the process a bit, but you must keep track of each thing drawn so you can redraw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文