我制作了一个游戏板,我想拖放我的棋子 (.gif's),但我无法找到一种方法来做到这一点,而不必无休止地重新绘制整个棋盘,因为我必须每次都清除棋子时间它移动 1 个像素并清除它下面的任何东西(板和其他部分)。有可能吗?
最佳答案
是的,您确实需要每次都重新绘制整个板。
您可以使用的一种技术是屏幕外 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, ...);
画板 Canvas 在屏幕上是不可见的,但它会存储在内存中,以便每次需要重新渲染时,它都能非常快速地渲染。
关于javascript - 在 <canvas> 中移动图像而不重绘整个 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837144/