接上一篇文章:踩坑小程序canvas,uni + vite + vue3 搞起来
小程序搜索【涂图了】,可进行体验。
轨迹自动播放实现
在画布上绘制笔迹的时候,通过touchmove
事件将每一次绘制的点坐标都记录起来,同时也要将当前笔迹的颜色、宽度等数据记录起来。最终画布上所有的笔迹都转换成了一个数据列表,当播放的时候,再将列表里面的数据一条一条的在canvas
上重新绘制出来,通过setTimeout
进行自动循环不断的去绘制。
看代码:
class Paint {
// ...省略...
/**
* 从头绘制路径
* @param path 路径
* @param completed 完成回调
* @returns
*/
playPath(path: Path[], completed?: () => void) {
if (!path.length) return Promise.resolve();
this.path = path;
// 初始化
this.row = 0;
this.column = 0;
this.stop = false;
this.isComplete = false;
const {
pos, color, width } = path[0];
this.start(pos[0], color, width