简体   繁体   English

将图像移入<canvas>无需重绘整个 canvas?</canvas>

[英]Move image in <canvas> without redrawing entire canvas?

I've made a game board and I want to drag and drop my pieces (.gif's) but I can't find a way to do this without having to redraw the entire board endlessly since I have to clear the piece every time it moves 1 pixel and that clears whatever was under it too (the board and other pieces).我已经制作了一个游戏板,我想拖放我的棋子(.gif),但我无法找到一种方法来做到这一点,而不必无休止地重新绘制整个棋盘,因为我每次移动时都必须清除棋子1 像素,它也清除了它下面的任何东西(板和其他部分)。 Is it even possible?甚至可能吗?

Yes, you do need to redraw the entire board each time.是的,您确实需要每次都重新绘制整个电路板。

One technique you can use is an offscreen canvas:您可以使用的一种技术是屏幕外 canvas:

var board = document.createElement("canvas");
board.width = width;
board.height = height;

var boardContext = board.getContext("2d");

// rendering code for board
// ...

// now draw board onto main canvas
var context = mainCanvas.getContext("2d");

context.drawImage(board, ...);

The board canvas is not visible on the screen, but it will be stored in memory so that each time you need to re-render, it renders very quickly.板子 canvas 在屏幕上是不可见的,但它会存储在 memory 中,这样每次需要重新渲染时,它渲染得非常快。

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

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