用canvas实现在线画板,带你找回童年乐趣

目录

项目起步

实现功能

完整代码


效果图如下

 项目总计6大功能

项目起步

首先准备好项目所用到的元素

 <canvas id="c1" width="1000" height="400"></canvas>
    <hr>
    <button id="strong">粗线条</button>
    <button id="thin">细线条</button>
    <button id="save">保存签名</button>
    <input type="color" id="color" value="">
    <button id="clearBtn">橡皮擦</button>
    <button id="null">清空画布</button>
/** 
         * @type {HTMLCanvasElement}
         */

        const c1 = document.querySelector('#c1')
        const ctx = c1.getContext('2d')

        let isDraw = false
        let lineWidth = 10
        //连接处圆润
        ctx.lineJoin = 'round'
        //开头和结尾圆润
        ctx.lineCap = 'round'
        c1.onmousedown = function () {
            ctx.beginPath()
            let x = event.pageX
            let y = event.pageY
            ctx.moveTo(x, y)
            isDraw = true
        }
        c1.onmouseup = function () {
            isDraw = false
            ctx.closePath()
        }
        c1.onmouseleave = function () {
            isDraw = false
            ctx.closePath()
        }
        c1.onmousemove = function () {
            if (isDraw) {
                let x = event.pageX
                let y = event.pageY
                ctx.lineTo(x, y)
                ctx.lineWidth = lineWidth
                ctx.stroke()
            }
        }

通过监听鼠标事件,我们可以在canvas元素中绘制出我们想要的效果



实现功能

1.线段的粗细

改变的是线条的粗细,根据 lineWidth调整即可

2.橡皮擦功能

当我们再擦除的时候,其实是利用的是合成属性,让合成的部分隐藏起来

注意擦除后,我们再绘制的时候需要让元素显示出来需要设置source-over

  ctx.globalCompositeOperation = 'destination-out'

3.清除画布

利用clearRect 即可清除想要清除的画布区域

ctx.clearRect(0,0, 1000, 400)

4.颜色改变

我们读取取色器的value然后赋值给strokeStyle即可

  ctx.strokeStyle = color.value

5.保存签名

这一步我们可以将画好的画布下载下来

利用画布的toDataURL属性,将其转化成一个url, 接着让一个a标签的href指向其,添加download属性即可

const imgUrl = c1.toDataURL()

            let a = document.createElement('a')
            a.setAttribute('download', '酷炫前面')
            a.href = imgUrl
            a.click()

一起来看看最终效果


完整代码

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active {
            border: 1px solid #ffcccc;
            background-color: #ffcccc;
            color: white;
        }
    </style>
</head>

<body>
    <canvas id="c1" width="1000" height="400"></canvas>
    <hr>
    <button id="strong">粗线条</button>
    <button id="thin">细线条</button>
    <button id="save">保存签名</button>
    <input type="color" id="color" value="">
    <button id="clearBtn">橡皮擦</button>
    <button id="null">清空画布</button>

    <script>

        /** 
         * @type {HTMLCanvasElement}
         */

        const c1 = document.querySelector('#c1')
        const ctx = c1.getContext('2d')

        let isDraw = false
        let lineWidth = 10
        //连接处圆润
        ctx.lineJoin = 'round'
        //开头和结尾圆润
        ctx.lineCap = 'round'
        c1.onmousedown = function () {
            ctx.beginPath()
            let x = event.pageX
            let y = event.pageY
            ctx.moveTo(x, y)
            isDraw = true
        }
        c1.onmouseup = function () {
            isDraw = false
            ctx.closePath()
        }
        c1.onmouseleave = function () {
            isDraw = false
            ctx.closePath()
        }
        c1.onmousemove = function () {
            if (isDraw) {
                let x = event.pageX
                let y = event.pageY
                ctx.lineTo(x, y)
                ctx.lineWidth = lineWidth
                ctx.stroke()
            }
        }


        const strong = document.querySelector('#strong')

        strong.onclick = function () {
            ctx.globalCompositeOperation = 'source-over'
            strong.classList.add('active')
            thin.classList.remove('active')
            clearBtn.classList.remove('active')
            lineWidth = 20
        }
        const thin = document.querySelector('#thin')

        thin.onclick = function () {
            ctx.globalCompositeOperation = 'source-over'
            thin.classList.add('active')
            strong.classList.remove('active')
            clearBtn.classList.remove('active')
            lineWidth = 2
        }
        const save = document.querySelector('#save')

        //保存图片
        save.onclick = function () {
            const imgUrl = c1.toDataURL()

            let a = document.createElement('a')
            a.setAttribute('download', '酷炫前面')
            a.href = imgUrl
            a.click()

        }


        const color = document.querySelector('#color')

        color.onchange = function () {
            ctx.strokeStyle = color.value
        }

        const clearBtn = document.querySelector('#clearBtn')

        clearBtn.onclick = function () {
            clearBtn.classList.add('active')
            thin.classList.remove('active')
            strong.classList.remove('active')
            ctx.globalCompositeOperation = 'destination-out'

            lineWidth = 50
        }

        const nullCanvas = document.querySelector('#null')

        nullCanvas.onclick = function () {
            ctx.clearRect(0, 0, 1000, 400)
        }
    </script>
</body>

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值