首页 > 其他分享 >canvas绘制手写签名【移动端+web】--支持下载

canvas绘制手写签名【移动端+web】--支持下载

时间:2022-11-26 21:45:14浏览次数:41  
标签:web canvas const name img -- ctx cvs width

一个简单的实例

<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>手写签名2</title> <style> body { margin: 0; } #cvs { display: block; background-color: #dddddd; } .btn { position: absolute; top: 0; } .p2 { left: 50px } </style> </head> <body> <button class="btn" onclick="onRotate()">旋转</button> <button class="btn p2" onclick="onDownload()">下载</button> <canvas id="cvs" width="400" height="300"></canvas> <script> const cvs = document.getElementById('cvs') const ctx = cvs.getContext('2d') let isDrawing = false const onRotate = () => { const { width, height } = cvs const img = new Image() img.src = cvs.toDataURL('image/png') img.onload = () => { cvs.width = height cvs.height = width ctx.translate(cvs.width, 0) ctx.rotate(Math.PI / 180 * 90) ctx.drawImage(img, 0, 0) } } const onDownload = async () => { const a = document.createElement('a') a.setAttribute('download', 'name.png') const res = await fetch(cvs.toDataURL('image/png')) const blob = await res.blob() a.href = URL.createObjectURL(blob) a.click() } const addEvents = (cvs, name, call) => { const isMobile = navigator.userAgent.match(/Mobile/) const mobileNameObj = { 'mousedown': 'touchstart', 'mousemove': 'touchmove', 'mouseup': 'touchend' } if (isMobile) { cvs.addEventListener(mobileNameObj[name], e => { call(e.touches[0]) }) } else { cvs.addEventListener(name, e => { call(e) }) } } addEvents(cvs, 'mousedown', e => { isDrawing = true ctx.moveTo(e.pageX, e.pageY) }) addEvents(cvs, 'mousemove', e => { if (isDrawing) { ctx.lineWidth = 5 ctx.lineTo(e.pageX, e.pageY) ctx.stroke() } }) addEvents(cvs, 'mouseup', e => { isDrawing = false }) </script> </body>

标签:web,canvas,const,name,img,--,ctx,cvs,width
From: https://www.cnblogs.com/Tanguncle/p/16928385.html

相关文章

  • OI生涯回忆录
    简短地梳理一下吧:大抵是从小升初的暑假开始学的。刚学了2个月,自然没有通过NOIP2018普及组的初赛。第二年,压线通过初赛,那时候是个不会分析复杂度的憨憨,在T2想出正解但没......
  • 力扣 leetcode 882. 细分图中的可到达节点
    问题描述给你一个无向图(原始图),图中有n个节点,编号从0到n-1。你决定将图中的每条边细分为一条节点链,每条边之间的新节点数各不相同。图用由边组成的二维数组edg......
  • mybatisPlus笔记
    MybatisPlus:一些容易不知道是什么含义的方法:MybaitsPlus中我们没有进行设置表名,MybaitsPlus是如何进行确定表名的?​MybatisPlus中默认是通过实体类(Ma......
  • 『题解』Codeforces 1702A Round Down the Price
    题意这道题其实就是让你求出当前数字与\(10\)的整数幂次的差值(注意不能向上取,只能向下取)。而且题目也标注了\(1\lek\le9\),所以我们可以让\(i\)从\(0\sim9\)......
  • Failed to load ApplicationContext-myBatis注解与整合
    严重:CaughtexceptionwhileallowingTestExecutionListener[org.springframework.test.context.support.DependencyInjectionTestExecutionListener@21213b92]topre......
  • redis对key和数据库的基本操作
    redis基本数据结构操作:0.基本的常用key操作和数据库操作:keys*查看当前库所有key(匹配:keys*1)127.0.0.1:6379>setusernameqiugeOK127.0.0.1:6379>setage18O......
  • 【Online Assignment】
    11.25(day1)第一天先咕了...11.26(day2)上午语文英语课专注度较低,下午学习效率相对较好数学进入关键期,应该尽快完成圆锥曲线的二级整理,完成数学收官物理进入磁场......
  • 【光通信】用于可见光通信的OFDM发射机和接收机
    1.软件版本MATLAB2013b2.本算法理论知识可见光通信,2篇比较好的论文:http://wenku.baidu.com/view/3c077736a32d7375a4178022.htmlhttp://www.docin.com/p-121488388.ht......
  • 『学习笔记』树状数组
    树状数组其实假如你会线段树,并且比较熟练,你可以直接离开。为什么呢?虽然线段树和树状数组的基础功能是差不多的,但是—树状数组能做到的操作线段树一定能做到,但是线段树......
  • LSTM
    LSTM又叫长短时记忆神经网络,问了解决RNN不能处理从较长的前文中推断下一个单词的问题而提出的从较长的前文中推断下一个单词的问题比如:"IgrewupinFrance…Ispea......