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

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

效果图如下


image.png


项目总计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()

一起来看看最终效果


image.png


完整代码

<!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>
相关文章
|
6月前
|
存储 监控 数据可视化
|
5天前
|
Java
普通玩家也能掌握的Java游戏加点系统,专业到让你无敌!
普通玩家也能掌握的Java游戏加点系统,专业到让你无敌!
15 1
|
5天前
|
图形学
如何在微信小游戏制作工具中做出好看的粒子效果?
如何在微信小游戏制作工具中做出好看的粒子效果?
50 1
|
5天前
|
C# 图形学
【Unity】2D游戏-愤怒的小鸟教学实战(附源码和实现步骤 超详细)
【Unity】2D游戏-愤怒的小鸟教学实战(附源码和实现步骤 超详细)
215 1
|
5天前
在微信小游戏制作工具中制作一个下雨的效果
在微信小游戏制作工具中制作一个下雨的效果
33 0
|
5天前
|
开发者
什么是分享?游戏中的自制关卡如何分享给朋友?
什么是分享?游戏中的自制关卡如何分享给朋友?
27 0
|
9月前
[视频]高端微课|W01.初尝万彩动画——人人都能制作动画版微课!
或许,你也曾想抛弃PPT那万年不变的动画模式!或许,你也想让你的微课提升一个档次!或许,你也羡慕网上那些科普类动画的的炫酷!
68 0
|
10月前
|
小程序
微信小程序项目实例——我有一支画笔(画画)
微信小程序项目实例——我有一支画笔(画画)
|
11月前
|
缓存 移动开发 前端开发
小程序使用 canvas 制作活动分享海报的一点点细节
小程序使用 canvas 制作活动分享海报的一点点细节
108 0
|
Python 容器
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏05之滚动屏幕
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏05之滚动屏幕
111 0