用Canvas自己寫個(gè)小畫板

  • 首先寫好觸摸開始, 觸摸移動(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
            }
        },
  • 創(chuàng)建 canvas 繪圖上下文
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é)束觸摸 可清空原本繪制的圖像 也可不
            // 結(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 作者:?緝熙Soyaine 簡(jiǎn)介:JavaScript30 是 Wes Bos 推出的一個(gè) 30 天挑戰(zhàn)。項(xiàng)目免...
    未枝丫閱讀 3,224評(píng)論 1 3
  • 一、實(shí)現(xiàn)思路 (非觸屏設(shè)備) 監(jiān)聽鼠標(biāo)事件①按下鼠標(biāo):onmousedown;滑動(dòng)鼠標(biāo):onmousemove;松...
    darkTi閱讀 1,260評(píng)論 0 1
  • 書中代碼示例效果展示:Core HTML5 Canvas Examples 基礎(chǔ)知識(shí) canvas元素 canva...
    szu_bee閱讀 3,044評(píng)論 2 28
  • 本文為原創(chuàng)文章,如需轉(zhuǎn)載請(qǐng)注明出處,謝謝! 最近項(xiàng)目中添加了白板涂鴉的功能,需求是手指在屏幕上滑動(dòng)需要繪制出光滑曲...
    Android_ZzT閱讀 2,712評(píng)論 1 11
  • 項(xiàng)目上需要一個(gè)對(duì)當(dāng)前頁(yè)面批注并且可以添加文字的功能,最后生成一張圖片附帶到流程中。便于審批的領(lǐng)導(dǎo)明確提出對(duì)哪一點(diǎn)不...
    滴水1238閱讀 1,681評(píng)論 1 2

友情鏈接更多精彩內(nèi)容