JS canvas 中的撤销,重做,保存

直接上代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>在canvas上面获取特定位置的base64信息</title>
</head>
<style>
    #origin_canvas {
        width: 200px;
        height: 200px;

    }
</style>

<body>
    <div>
        <canvas id="canvas" width="400" height="400"></canvas>
        <button id="undoBtn">撤销</button>
        <button id="redoBtn">重做</button>
        <button id="saveBtn">保存</button>
        <img id="resultImg">

    </div>
    <script>
        const canvas = document.getElementById('canvas');
        const undoBtn = document.getElementById('undoBtn');
        const redoBtn = document.getElementById('redoBtn');
        const saveBtn = document.getElementById('saveBtn');
        const context = canvas.getContext('2d');
        const resultImg = document.getElementById('resultImg');

        let undoList = []; // 用于保存所有操作,用于撤销和重做
        let redoList = []; // 用于保存所有撤销的操作,用于重做

        let isDrawing = false;
        let currentPath = null;

        // 鼠标按下事件
        canvas.addEventListener('mousedown', e => {
            isDrawing = true;
            currentPath = {
                color: context.strokeStyle,
                width: context.lineWidth,
                points: [{ x: e.offsetX, y: e.offsetY }]
            };
        });

        // 鼠标移动事件
        canvas.addEventListener('mousemove', e => {
            if (!isDrawing) return;
            currentPath.points.push({ x: e.offsetX, y: e.offsetY });
            context.clearRect(0, 0, canvas.width, canvas.height);
            drawPaths([...undoList, currentPath]);
        });

        // 鼠标松开事件
        canvas.addEventListener('mouseup', e => {
            isDrawing = false;
            undoList.push(currentPath);
            redoList = [];
        });

        // 鼠标离开事件
        canvas.addEventListener('mouseleave', e => {
            isDrawing = false;
        });

        // 撤销按钮点击事件
        undoBtn.addEventListener('click', e => {
            if (undoList.length === 0) return;
            const lastPath = undoList.pop();
            redoList.push(lastPath);
            context.clearRect(0, 0, canvas.width, canvas.height);
            drawPaths(undoList);
        });

        // 重做按钮点击事件
        redoBtn.addEventListener('click', e => {
            if (redoList.length === 0) return;
            const lastPath = redoList.pop();
            undoList.push(lastPath);
            context.clearRect(0, 0, canvas.width, canvas.height);
            drawPaths(undoList);
        });

        // 保存按钮点击事件
        saveBtn.addEventListener('click', e => {
            const dataUrl = canvas.toDataURL();
            resultImg.src = dataUrl;
        });

        // 画所有的路径
        function drawPaths(paths) {
            paths.forEach(path => {
                context.beginPath();
                context.strokeStyle = path.color;
                context.lineWidth = path.width;
                context.moveTo(path.points[0].x, path.points[0].y);
                path.points.slice(1).forEach(point => {
                    context.lineTo(point.x, point.y);
                });
                context.stroke();
            });
        }

    </script>
</body>

</html>
上方的代码可以直接粘贴,可以看效果

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_2524963996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值