用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>
相关文章
开源! ! ! 轻量级多功能按键驱动-LiteButton
开源! ! ! 轻量级多功能按键驱动-LiteButton
|
数据采集 存储 监控
大数据的数据来源 - 数据采集的方式(数据接入的方式)
大数据处理关键技术一般包括:大数据采集、大数据预处理、大数据存储及管理、大数据分析及挖掘、大数据展现和应用(大数据检索、大数据可视化、大数据应用、大数据安全等)。下面主要介绍下大数据采集
7459 0
|
人工智能 数据可视化 数据挖掘
工业零件不良率、残次率的智能数据分析和数字化管理
在传统工业领域,我们通过引入DataV-Note平台,成功实现了企业智能数据分析与数字化管理的初步目标。这一平台不仅显著提升了数据处理的效率和准确性,还为我们的日常运营提供了更加科学、直观的决策支持。然而,这只是智能化转型的第一步。展望未来,我们期望能够进一步深化技术应用,推动企业管理向更高层次的智能化方向迈进。通过持续优化数据分析能力、完善数字化管理体系,我们致力于将企业的运营模式从传统的经验驱动转变为数据驱动,从而全面提升管理效能和市场竞争力,为企业创造更大的长期价值
696 129
|
开发工具 git
git fetch和 pull的区别
`git fetch`和 `git pull`在Git中扮演着不同的角色,了解它们的区别和使用场景对于高效管理代码库至关重要。通过合理使用这两个命令,可以更好地控制代码合并过程,减少冲突,提高团队协作效率。
1195 97
|
前端开发 小程序
扩展uview复选组件库支持自定义图片+自定义内容
扩展uview复选组件库支持自定义图片+自定义内容
685 6
|
应用服务中间件 nginx
Nginx里的root和alias的区别是什么?
Nginx里的root和alias的区别是什么?
1296 2
|
Dart 前端开发 JavaScript
dart-sass与node-sass的区别以及使用dart-sass可能会出现的问题
dart-sass与node-sass的区别以及使用dart-sass可能会出现的问题
2317 0
dart-sass与node-sass的区别以及使用dart-sass可能会出现的问题
|
机器学习/深度学习 计算机视觉
YOLOv8改进 | 2023注意力篇 | EMAttention注意力机制(附多个可添加位置)
YOLOv8改进 | 2023注意力篇 | EMAttention注意力机制(附多个可添加位置)
1549 0
|
存储
uniapp实战 -- 个人信息维护(含选择图片 uni.chooseMedia,上传文件 uni.uploadFile,获取和更新表单数据)
uniapp实战 -- 个人信息维护(含选择图片 uni.chooseMedia,上传文件 uni.uploadFile,获取和更新表单数据)
709 2
|
缓存 安全 网络协议
Windows 安全基础——NetBIOS篇
Windows 安全基础——NetBIOS篇
715 4

热门文章

最新文章