- 首先寫好
觸摸開始, 觸摸移動(dòng), 結(jié)束觸摸事件
<canvas disable-scroll="true" canvas-id="drawline" class="board" @touchstart="handtouchstart" @touchmove="handtouchmove"
@touchend="handtouchend">
</canvas>
data() {
return {
x: 0, // 渠道開始和移動(dòng)位置
y: 0,
newx: 0,
newy: 0
}
},
var ctx = uni.createCanvasContext('drawline')
- 獲取到用戶的手勢(shì)點(diǎn)擊
x, y軸位置
// 觸摸開始
handtouchstart(e) {
let startX = e.changedTouches[0].x;
let startY = e.changedTouches[0].y;
this.x = startX;
this.y = startY;
},
- 獲取用戶的觸摸移動(dòng)位置, 并根據(jù)位置進(jìn)行繪制
// 觸摸移動(dòng)
handtouchmove(e) {
let moveX = e.changedTouches[0].x;
let moveY = e.changedTouches[0].y;
this.newx = moveX;
this.newy = moveY;
ctx.setLineWidth(10); // 劃線多粗
ctx.setLineCap('round'); // 不中斷
ctx.setStrokeStyle('red')
ctx.moveTo(this.x, this.y)
ctx.lineTo(this.newx, this.newy)
ctx.stroke()
ctx.draw(true) // 保存繪畫內(nèi)容
this.x = moveX
this.y = moveY
this.main(this.x, this.y);
},
// 結(jié)束觸摸
handtouchend(e) {
ctx.draw() // 清空
},
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。