| <template> |
| <view class="draw-page"> |
| <view class="draw-content"> |
| <canvas |
| style="width: 100%; height: 100%" |
| ref="sign" |
| canvas-id="sign" |
| id="sign" |
| disable-scroll |
| @touchstart="touchstart" |
| @touchmove="touchmove" |
| ></canvas> |
| </view> |
| <view class="actions"> |
| <button @click="background()" type="warn">清空</button> |
| <button @click="save" type="primary">保存</button> |
| </view> |
| </view> |
| </template> |
| |
| <script> |
| export default { |
| data() { |
| return { |
| lineColor: "#000", |
| lineWidth: 2, |
| }; |
| }, |
| onReady() { |
| const ctx = uni.createCanvasContext("sign"); |
| this.ctx = ctx; |
| this.ctx.setStrokeStyle(this.lineColor); |
| this.ctx.setLineWidth(this.lineWidth); |
| |
| |
| const query = uni.createSelectorQuery().in(this); |
| query.select("#sign").boundingClientRect(); |
| query.exec((res) => { |
| if (res && res.length) { |
| this.width = res[0].width; |
| this.height = res[0].height; |
| this.background(); |
| } |
| }); |
| }, |
| methods: { |
| save() { |
| uni.canvasToTempFilePath({ |
| canvasId: "sign", |
| success(res) { |
| |
| const url = res.tempFilePath; |
| console.log(url); |
| }, |
| fail(err) { |
| console.error(err); |
| }, |
| }); |
| }, |
| |
| background(color = "#f8f8f8") { |
| this.ctx.fillStyle = color; |
| this.ctx.fillRect(0, 0, this.width, this.height); |
| this.ctx.draw(); |
| }, |
| touchstart(e) { |
| if (e.touches && e.touches.length) { |
| const t = e.touches[0]; |
| this.mouseX = t.x; |
| this.mouseY = t.y; |
| this.pmouseX = this.mouseX; |
| this.pmouseY = this.mouseY; |
| } |
| }, |
| |
| touchmove(e) { |
| if (e.touches && e.touches.length) { |
| const t = e.touches[0]; |
| this.pmouseX = this.mouseX; |
| this.pmouseY = this.mouseY; |
| this.mouseX = t.x; |
| this.mouseY = t.y; |
| this.draw(); |
| } |
| }, |
| draw() { |
| this.ctx.moveTo(this.pmouseX, this.pmouseY); |
| this.ctx.lineTo(this.mouseX, this.mouseY); |
| this.ctx.stroke(); |
| this.ctx.draw(true); |
| }, |
| }, |
| }; |
| </script> |
| |
| <style lang="scss"> |
| .draw-page { |
| height: 100vh; |
| display: flex; |
| flex-direction: column; |
| } |
| |
| .draw-content { |
| flex: 1; |
| } |
| |
| .actions { |
| display: flex; |
| |
| button { |
| flex: 1; |
| } |
| } |
| |
| |
| |
| |
| </style> |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
2020-08-26 js 位掩码