当前位置: 首页 > news >正文

使用canvas做了一个最简单的网页版画板,5分钟学会

画板实现的效果:可以切换画笔的粗细,颜色,还可以使用橡皮擦,还可以清除画布,然后将画的内容保存下载成一张图片:

具体用到的canvas功能有:画笔的粗细调整lineWidth,开始一个新的画笔路径beginPath,结束一个画笔路径closePath,这个可以保证不影响之前画的效果,重新开始一个画笔路径。 还有橡皮擦使用的ctx.globalCompositeOperation = 'destination-out'属性,清空画布使用的:ctx.clearRect(0, 0, canvas.width, canvas.height),保存图片使用的是let url = canvas.toDataURL('image/png')。

完整的代码如下:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>像素操作</title><style>.active {background-color: #f2a1a1;}</style></head><body><div>创建一个画布,可以使用画笔随意画画</div><div style="width: 800px; margin-top: 6px"><button class="bold">粗线条</button><button class="thin">细线条</button><input id="color" type="color" /><button class="del">橡皮擦</button><button class="clear">清空画布</button><button class="save">保存图片</button><hr /><canvas id="myCanvas" width="800" height="600"></canvas></div><script>// 获取画布const canvas = document.getElementById('myCanvas')// 获取画笔const ctx = canvas.getContext('2d')// 让画笔的拐弯处更加圆润,没有锯齿感ctx.lineCap = 'round'ctx.lineJoin = 'round'// 获取控制按钮const bold = document.querySelector('.bold')const thin = document.querySelector('.thin')const color = document.querySelector('#color')const del = document.querySelector('.del')const clear = document.querySelector('.clear')const save = document.querySelector('.save')// 添加点击事件bold.onclick = function () {ctx.lineWidth = 20bold.classList.add('active')thin.classList.remove('active')del.classList.remove('active')clear.classList.remove('active')save.classList.remove('active')}thin.onclick = function () {ctx.lineWidth = 5thin.classList.add('active')bold.classList.remove('active')del.classList.remove('active')clear.classList.remove('active')save.classList.remove('active')}color.onchange = function (e) {console.log('颜色改变了:', e.target.value)ctx.strokeStyle = e.target.value}del.onclick = function () {console.log('橡皮擦')ctx.globalCompositeOperation = 'destination-out'ctx.lineWidth = 30del.classList.add('active')bold.classList.remove('active')thin.classList.remove('active')clear.classList.remove('active')save.classList.remove('active')}clear.onclick = function () {console.log('清空画布')ctx.clearRect(0, 0, canvas.width, canvas.height)}// 保存图片save.onclick = function () {console.log('保存图片')let url = canvas.toDataURL('image/png')let a = document.createElement('a')a.href = urla.download = 'canvas.png'a.click()}// 监听画布画画事件let mouseDown = false// 鼠标按下将变量设置为truecanvas.onmousedown = function (e) {ctx.beginPath()mouseDown = truectx.moveTo(e.offsetX, e.offsetY)}// 鼠标抬起将变量设置为falsecanvas.onmouseup = function () {mouseDown = falsectx.closePath()ctx.globalCompositeOperation = 'source-over'}canvas.onmouseleave = function () {mouseDown = falsectx.closePath()}// 鼠标移动canvas.onmousemove = function (e) {if (mouseDown) {console.log('鼠标移动')ctx.lineTo(e.offsetX, e.offsetY)ctx.stroke()}}</script></body>
</html>

http://www.lryc.cn/news/204486.html

相关文章:

  • 自组织映射Python实现
  • 如何避免阿里云对象储存OSS被盗刷
  • 产品研发团队协作神器!10款提效工具大盘点!
  • LSTM 与 GRU
  • 代码评审CheckList
  • [尚硅谷React笔记]——第5章 React 路由
  • 如何去掉不够优雅的IF-ELSE
  • Python中defaultdict的使用
  • 【ccc3.8】虚拟列表
  • 【23种设计模式】单一职责原则
  • DNS入门学习:什么是TTL值?如何设置合适的TTL值?
  • ilr normalize isometric log-ratio transformation
  • el表单的简单查询方法
  • 【USRP】通信总的分支有哪些
  • 关于服务器网络代理解决方案(1024)
  • Linux下 /etc/shadow内容详解
  • Go学习第二章——变量与数据类型
  • 【剑指Offer】:循环有序列表的插入(涉及链表的知识)
  • 【Django 04】Django-DRF(ModelViewSet)
  • ubuntu命令
  • C++学习之强制类型转换
  • 在Linux中,可以使用以下命令来查看进程
  • 【算法训练-动态规划 一】【应用DP问题】零钱兑换、爬楼梯、买卖股票的最佳时机I、打家劫舍
  • 2023年中职组“网络安全”赛项云南省竞赛任务书
  • Modeling Deep Learning Accelerator Enabled GPUs
  • 《动手学深度学习 Pytorch版》 9.5 机器翻译与数据集
  • 网络入门基础
  • Towards a Rigorous Evaluation of Time-series Anomaly Detection(论文翻译)
  • 理解Python装饰器
  • VR智慧景区,为游客开启智慧旅游新时代