canvas画布 可修改画笔颜色、粗细、返回到上一步、清除画布、保存图片

直接上代码,canvas真有趣

参考https://blog.csdn.net/zemprogram/article/details/89077403

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画图板</title>
</head>

<body>
    <div style="width:506px;margin:50px auto;">
        <input type="color" id="color">
        <select name="" id="line-width">
            <option value="1">--请选择线条粗细(像素)--</option>
            <option value="2">2px</option>
            <option value="5">5px</option>
            <option value="8">8px</option>
            <option value="12">12px</option>
            <option value="15">15px</option>
        </select>
        <input type="button" value="清除画布" id="btnClear">
        <input type="button" value="返回上一步" id="return">
        <input type="button" value="保存" id="save">
        <canvas id="canvas" width="500" height="500" style="display: block;border: 3px black solid;margin-top: 10px;"></canvas>
    </div>

    <script type="text/javascript">
        //获取画布元素
        let canvas = document.getElementById("canvas");
        let context = canvas.getContext("2d");

        //将所有像素点信息存储在数组中,后续返回上一步时可用到
        let restore = [context.getImageData(0, 0, 500, 500)];

        //获取画笔颜色
        let color = document.getElementById("color");
        //获取画笔宽度
        let lineWidth = document.getElementById("line-width");
        let btn = document.getElementById("btnClear");
        let returnStep = document.getElementById("return");
        let save = document.getElementById("save");
        isDraw = false;
        //绘图
        //鼠标摁下时
        canvas.onmousedown = function(event) {
                isDraw = true;

                context.lineWidth = lineWidth.value;
                context.strokeStyle = color.value;

                context.beginPath();
                context.moveTo(event.offsetX, event.offsetY);
            }
            //鼠标开始移动时绘图
        canvas.onmousemove = function(event) {
                if (isDraw) {
                    context.lineTo(event.offsetX, event.offsetY);
                    context.stroke();
                }
            }
            //鼠标松开时停止绘图
        canvas.onmouseup = function() {
                isDraw = false;
                //将此时绘制的信息存储到restore数组中
                restore[restore.length] = context.getImageData(0, 0, canvas.width, canvas.height);
            }
            //鼠标移出canvas后停止绘图
        canvas.onmouseout = function() {
                isDraw = false;
            }
            //点击按钮清除画布
        btn.onclick = function() {
                context.clearRect(0, 0, canvas.width, canvas.height);
                restore[restore.length] = context.getImageData(0, 0, canvas.width, canvas.height);
            }
            //返回上一步
        returnStep.onclick = function() {
                if (restore.length > 1) {
                    context.putImageData(restore[restore.length - 2], 0, 0);
                    restore.length--;
                }
            }
            //保存图片
        save.onclick = function() {
            let name = prompt("请输入要保存的图片名称", "canvas绘制图片");
            if (name === "") {
                alert("图片名字不能为空")
            } else {
                let href = canvas.toDataURL();
                let a = document.createElement('a');
                a.download = name;
                a.href = href;
                a.dispatchEvent(new MouseEvent('click'));
            }
        }
    </script>
</body>

</html>

 

好的,我理解了您的要求。下面是使用Canvas实现的画板的示例代码,您可以使用这个代码来实现您需要的画板功能: ``` <!DOCTYPE html> <html> <head> <title>Canvas Paint Board</title> </head> <body> <canvas id="canvas" width="600" height="400" style="border: 1px solid black;"></canvas> <label>Color:</label> <input type="color" id="colorPicker"> <label>Brush Size:</label> <input type="range" id="brushSize" value="10" min="1" max="50"> <button id="undoBtn">Undo</button> <button id="redoBtn">Redo</button> <button id="clearBtn">Clear</button> <script> // 获取画布画笔 var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // 画笔颜色粗细 var brushColor = document.getElementById("colorPicker").value; var brushSize = document.getElementById("brushSize").value; // 保存历史记录的栈 var undoStack = []; var redoStack = []; // 绘制初始画面 drawCanvas(); // 监听画布点击事件 canvas.addEventListener("mousedown", startDrawing); // 监听画笔颜色粗细的变化 document.getElementById("colorPicker").addEventListener("input", changeColor); document.getElementById("brushSize").addEventListener("input", changeBrushSize); // 监听撤销、重做和清空按钮的点击事件 document.getElementById("undoBtn").addEventListener("click", undoDrawing); document.getElementById("redoBtn").addEventListener("click", redoDrawing); document.getElementById("clearBtn").addEventListener("click", clearCanvas); // 鼠标按下,准备开始绘制 function startDrawing(event) { // 添加绘制历史记录 undoStack.push(canvas.toDataURL()); // 获取鼠标在画布上的位置 var x = event.clientX - canvas.offsetLeft; var y = event.clientY - canvas.offsetTop; canvas.addEventListener("mousemove", drawLine); canvas.addEventListener("mouseup", stopDrawing); // 绘制初始点 ctx.beginPath(); ctx.moveTo(x, y); } // 绘制线条 function drawLine(event) { // 获取鼠标在画布上的位置 var x = event.clientX - canvas.offsetLeft; var y = event.clientY - canvas.offsetTop; // 设置画笔颜色粗细 ctx.strokeStyle = brushColor; ctx.lineWidth = brushSize; // 绘制线条 ctx.lineTo(x, y); ctx.stroke(); } // 鼠标抬起,结束绘制 function stopDrawing() { canvas.removeEventListener("mousemove", drawLine); canvas.removeEventListener("mouseup", stopDrawing); } // 改变画笔颜色 function changeColor(event) { brushColor = event.target.value; } // 改变画笔粗细 function changeBrushSize(event) { brushSize = event.target.value; } // 撤销绘制 function undoDrawing() { if (undoStack.length > 0) { // 添加重做历史记录 redoStack.push(canvas.toDataURL()); var lastDraw = undoStack.pop(); var img = new Image(); img.onload = function () { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(img, 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height); }; img.src = lastDraw; } } // 重做绘制 function redoDrawing() { if (redoStack.length > 0) { // 添加撤销历史记录 undoStack.push(canvas.toDataURL()); var lastDraw = redoStack.pop(); var img = new Image(); img.onload = function () { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(img, 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height); }; img.src = lastDraw; } } // 清空画布 function clearCanvas() { // 添加撤销历史记录 undoStack.push(canvas.toDataURL()); ctx.clearRect(0, 0, canvas.width, canvas.height); } // 绘制初始画面 function drawCanvas() { ctx.fillStyle = "white"; ctx.fillRect(0, 0, canvas.width, canvas.height); } </script> </body> </html> ``` 这段代码使用了Canvas API来绘制图形,同时实现了一些常见的画板功能,包括: - 绘制线条 - 改变画笔颜色粗细 - 撤销、重做和清空画布 您可以在上面的示例代码基础上进行修改,添加自己需要的功能。同时,注意保护好用户的隐私和安全,不要泄露用户的任何信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值