vue canvas 线上签名
<template>
<div id="box">
<div class="main" id='main'>
<canvas ref="saveCanvas" width="500" height="400" @mousedown="ctxDown($event)" @mousemove="ctxMove($event)" @mouseup="ctxUp" @mouseleave="ctxLeave" />
</div>
</div>
</template>
<script>
export default {
data() {
return {
canvas: null,
ctx: null,
isDown: false,
main:{}
}
},
mounted(){
//签名
this.canvas = this.$refs.saveCanvas
this.ctx = this.canvas.getContext('2d')
this.ctx.lineWidth = 1
this.ctx.strokeStyle="red";
this.ctx.lineCap = 'round' //线的连接处的形式
this.main = document.getElementById("main");
},
methods: {
ctxDown(e){
console.log('ctxDown',e)
this.isDown = true
const len = 50
const canvasX = e.clientX - this.main.offsetLeft
const canvasY = e.clientY - this.main.offsetTop
this.ctx.beginPath()
this.ctx.moveTo(canvasX, canvasY)
},
ctxMove(e){
console.log('ctxMove',e,e.target.offsetLeft)
if (this.isDown) {
const len = 50
const canvasX = e.clientX - this.main.offsetLeft
const canvasY = e.clientY - this.main.offsetTop
this.ctx.lineTo(canvasX, canvasY)
this.ctx.stroke()
}
},
ctxUp(){
console.log('ctxUp')
this.isDown = false
this.ctx.closePath()
},
ctxLeave(){
console.log('ctxLeave')
this.isDown = false
}
},
}
</script>
<style scoped lang="scss">
</style>
<div id="box">
<div class="main" id='main'>
<canvas ref="saveCanvas" width="500" height="400" @mousedown="ctxDown($event)" @mousemove="ctxMove($event)" @mouseup="ctxUp" @mouseleave="ctxLeave" />
</div>
</div>
</template>
<script>
export default {
data() {
return {
canvas: null,
ctx: null,
isDown: false,
main:{}
}
},
mounted(){
//签名
this.canvas = this.$refs.saveCanvas
this.ctx = this.canvas.getContext('2d')
this.ctx.lineWidth = 1
this.ctx.strokeStyle="red";
this.ctx.lineCap = 'round' //线的连接处的形式
this.main = document.getElementById("main");
},
methods: {
ctxDown(e){
console.log('ctxDown',e)
this.isDown = true
const len = 50
const canvasX = e.clientX - this.main.offsetLeft
const canvasY = e.clientY - this.main.offsetTop
this.ctx.beginPath()
this.ctx.moveTo(canvasX, canvasY)
},
ctxMove(e){
console.log('ctxMove',e,e.target.offsetLeft)
if (this.isDown) {
const len = 50
const canvasX = e.clientX - this.main.offsetLeft
const canvasY = e.clientY - this.main.offsetTop
this.ctx.lineTo(canvasX, canvasY)
this.ctx.stroke()
}
},
ctxUp(){
console.log('ctxUp')
this.isDown = false
this.ctx.closePath()
},
ctxLeave(){
console.log('ctxLeave')
this.isDown = false
}
},
}
</script>
<style scoped lang="scss">
</style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
2021-10-28 vue 下载文件 插件