使用canvas制作画画

<!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>Document</title>
    <style>
        .active {
            color: #fff;
            background-color: orangered;
        }
    </style>
</head>

<body>
    <canvas id="c1" width="800" height="600" style="border:1px solid #d3d3d3;"></canvas>
    <hr/>
    <button id="boldBtn" type="button">粗线条</button>
    <button id="thinBtn" type="button">细线条</button>
    <button id="saveBtn" type="button">保存签名</button>
    <input type="color" name="" id="color">
    <button class="clearBtn" type="button">橡皮檫</button>
    <button id="nullBtn" type="button">清空画布</button>
</body>
<script>
    //1.获取画笔
    var canvas = document.querySelector("#c1");
    var ctx = canvas.getContext("2d");

    //链接处圆润
    ctx.lineJoin = "round";
    // 开端和结束也是圆的
    ctx.lineCap = "round";
    //2.获取输入框和按钮
    //2.1设置画笔的粗细
    //粗线条
    var boldBtn = document.querySelector("#boldBtn");
    // 细线条
    var thinBtn = document.querySelector("#thinBtn");
    // 选择的颜色
    var inputColor = document.querySelector("#color");
    // 保存画布
    var saveBtn = document.querySelector("#saveBtn");
    // 橡皮檫
    var clearBtn = document.querySelector(".clearBtn");
    // 清空画布
    var nullBtn = document.querySelector("#nullBtn");

    // 设置允许绘制的变量
    var isDraw = false;
    // 鼠标移入事件
    canvas.onmousedown = function() {
            isDraw = true;
            ctx.beginPath();
            var x = event.pageX - canvas.offsetLeft;
            var y = event.pageY - canvas.offsetTop;
            ctx.moveTo(x, y);
        }
        // 鼠标离开事件
    canvas.onmouseleave = function() {
            isDraw = true;
            ctx.closePath();
        }
        // 鼠标释放事件
    canvas.onmouseup = function() {
            isDraw = true;
            ctx.closePath();
        }
        // 鼠标离开事件
    canvas.onmousemove = function(event) {
            if (isDraw) {
                var x = event.pageX - canvas.offsetLeft;
                var y = event.pageY - canvas.offsetTop;
                ctx.lineTo(x, y);
                ctx.stroke();
            }
        }
        //粗线条点击事件
    boldBtn.onclick = function() {
            ctx.globalCompositeOperation = 'source-over';
            ctx.lineWidth = 20;
            boldBtn.classList.add('active');
            thinBtn.classList.remove('active');
            clearBtn.classList.remove('active');
        }
        // 细线条事件
    thinBtn.onclick = function() {
        ctx.globalCompositeOperation = 'source-over';
        ctx.lineWidth = 2;
        thinBtn.classList.add('active');
        boldBtn.classList.remove('active');
        clearBtn.classList.remove('active');
    }

    // 橡皮檫点击事件   
    clearBtn.onclick = function() {
            ctx.globalCompositeOperation = 'destination-out';
            ctx.lineWidth = 30;
        }
        // 清空画布点击事件
    nullBtn.onclick = function() {
            ctx.clearRect(0, 0, 800, 600);
        }
        // 保存签名
    saveBtn.onclick = function() {
        var urlData = canvas.toDataURL();
        var img = new Image();
        img.src = urlData;
        // document.body.appendChild(img);
        var downloadA = document.createElement('a');
        downloadA.setAttribute('download', '酷炫签名');
        downloadA.href = urlData;
        downloadA.click();
    }

    inputColor.onchange = function() {
        ctx.strokeStyle = inputColor.value;
    }
</script>

</html>

效果图:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

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

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

打赏作者

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

抵扣说明:

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

余额充值