Vue實(shí)現(xiàn)多點(diǎn)涂鴉效果的示例代碼
效果展示
單點(diǎn)效果
多點(diǎn)效果
創(chuàng)建畫(huà)布
創(chuàng)建畫(huà)布并設(shè)置事件處理器
<canvas ref="board" style="background-color: #2b2d42" width="1000" height="1000" @touchstart="handleStart" @touchmove="handleMove" @touchend="handleEnd" />
獲取畫(huà)布并初始化畫(huà)筆信息
onMounted(() => { initPointer(); }); const board = ref(); const cxt = ref(); const lineWidth = 4; const initPointer = () => { cxt.value = board.value.getContext('2d'); window.MeApi?.mainWindowLoaded(); cxt.value.strokeStyle = 'red'; cxt.value.fillStyle = 'red'; cxt.value.lineWidth = lineWidth; };
觸摸事件處理
基礎(chǔ)知識(shí)
唯一地識(shí)別和觸摸平面接觸的點(diǎn)的值。
這個(gè)值在這根手指(或觸摸筆等)所引發(fā)的所有事件中保持一致,直到它離開(kāi)觸摸平面。
該屬性返回一個(gè)TouchList
:
- 對(duì)于
touchstart
事件,這個(gè)TouchList
對(duì)象列出在此次事件中新增加的觸點(diǎn)。 - 對(duì)于
touchmove
事件,列出和上一次事件相比較,發(fā)生了變化的觸點(diǎn)。 - 對(duì)于 touchend 事件,
changedTouches
是已經(jīng)從觸摸面的離開(kāi)的觸點(diǎn)的集合(也就是說(shuō),手指已經(jīng)離開(kāi)了屏幕/觸摸面)。
拷貝觸摸點(diǎn)
瀏覽器會(huì)復(fù)用觸摸點(diǎn),通過(guò)拷貝只記錄差異點(diǎn)和唯一標(biāo)識(shí)符替換引用整個(gè)對(duì)象的方式進(jìn)行優(yōu)化
type Point = { identifier: number,//觸摸點(diǎn)什么標(biāo)識(shí) //觸摸點(diǎn)坐標(biāo) x: number, y: number }; const copyTouch = (touch: Touch) => { return { identifier: touch.identifier, x: touch.pageX, y: touch.pageY }; };
查找觸摸點(diǎn)
通過(guò)遍歷 activityTouches
數(shù)組來(lái)尋找與給定標(biāo)記相匹配的觸摸點(diǎn),返回該觸摸點(diǎn)在數(shù)組中的下標(biāo)。
const activityTouchIndexById = (idToFind: number) => { for (let i = 0; i < activityTouches.length; i++) { const id = activityTouches[i].identifier; if (id === idToFind) { return i; } } return -1; };
跟蹤所有觸摸點(diǎn)以實(shí)現(xiàn)多點(diǎn)觸控
//跟蹤當(dāng)前存在的所有觸摸點(diǎn) const activityTouches: Point[] = [];
新增觸摸事件
當(dāng)屏幕上出現(xiàn)新的觸摸點(diǎn)touchstart
事件被觸發(fā),handleStart
函數(shù)被觸發(fā)。此時(shí),要收集記錄觸摸點(diǎn)并在觸摸點(diǎn)處畫(huà)圓:
const handleStart = (evt: TouchEvent) => { //獲取所有新增的點(diǎn) const touches = evt.changedTouches; for (let i = 0; i < touches.length; i++) { //收集觸摸點(diǎn) activityTouches.push(copyTouch(touches[i])); //畫(huà)圓 cxt.value.beginPath(); drawCircle(touches[i].clientX, touches[i].clientY) } };
觸摸移動(dòng)時(shí)
當(dāng) touchmove
事件被觸發(fā)時(shí),從而將調(diào)用handleMove()
函數(shù),此時(shí)按照路徑繪制線:
const handleMove = (evt: TouchEvent) => { evt.preventDefault(); const touches = evt.changedTouches; for (let i = 0; i < touches.length; i++) { const indexById = activityTouchIndexById(touches[i].identifier); if (indexById >= 0) { cxt.value.beginPath(); cxt.value.moveTo(activityTouches[indexById].x, activityTouches[indexById].y); cxt.value.lineTo(touches[i].clientX, touches[i].pageY); cxt.value.stroke(); //更新緩存信息 activityTouches.splice(indexById, 1, copyTouch(touches[i])); } } };
首先遍歷所有發(fā)生移動(dòng)的觸摸點(diǎn)。通過(guò)讀取每個(gè)觸摸點(diǎn)的 Touch.identifier
屬性,從緩存中讀取每個(gè)觸摸點(diǎn)在變化前的起點(diǎn)。這樣取得每個(gè)觸摸點(diǎn)之前位置的坐標(biāo),進(jìn)而進(jìn)行繪制。
觸摸結(jié)束處理
通過(guò)調(diào)用 handleEnd()
函數(shù)來(lái)處理觸摸結(jié)束事件:
const handleEnd = (evt: TouchEvent) => { evt.preventDefault(); const touches = evt.changedTouches; for (let i = 0; i < touches.length; i++) { const indexById = activityTouchIndexById(touches[i].identifier); if (indexById >= 0) { cxt.value.beginPath(); cxt.value.moveTo(activityTouches[indexById].x, activityTouches[indexById].y); cxt.value.lineTo(touches[i].clientX, touches[i].clientY); drawCircle(touches[i].clientX, touches[i].clientY) //移除緩存 activityTouches.splice(indexById, 1); } } };
類似handleMove
,首先遍歷所有事件,并讀取緩存。在事件觸發(fā)點(diǎn)畫(huà)個(gè)圓,然后將對(duì)應(yīng)的觸摸對(duì)象從緩存中移除。
其他
移動(dòng)端和PC端所對(duì)應(yīng)的時(shí)間不同。詳情可見(jiàn)鼠標(biāo)事件和觸摸事件文檔。
移動(dòng)端的觸摸點(diǎn)信息被封裝在Touch中,通過(guò)Touch.clientX
和Touch.clientX
讀取當(dāng)前坐標(biāo)
移動(dòng)端的觸摸點(diǎn)信息被封裝在MouseEvent中
完整代碼
<template> <canvas ref="board" style="background-color: #2b2d42" width="1000" height="1000" @touchstart="handleStart" @touchmove="handleMove" @touchend="handleEnd" ></canvas> </template> <script setup lang="ts"> import { onMounted, ref } from 'vue'; const board = ref(); const cxt = ref(); const lineWidth = 4; onMounted(() => { initPointer(); }); const initPointer = () => { cxt.value = board.value.getContext('2d'); window.MeApi?.mainWindowLoaded(); cxt.value.strokeStyle = 'red'; cxt.value.fillStyle = 'red'; cxt.value.lineWidth = lineWidth; }; type Point = { identifier: number, x: number, y: number }; const activityTouches: Point[] = []; const copyTouch = (touch: Touch) => { return { identifier: touch.identifier, x: touch.pageX, y: touch.pageY }; }; const activityTouchIndexById = (idToFind: number) => { for (let i = 0; i < activityTouches.length; i++) { const id = activityTouches[i].identifier; if (id === idToFind) { return i; } } return -1; }; const handleStart = (evt: TouchEvent) => { const touches = evt.changedTouches; for (let i = 0; i < touches.length; i++) { activityTouches.push(copyTouch(touches[i])); cxt.value.beginPath(); drawCircle(touches[i].clientX, touches[i].clientY) } }; const handleMove = (evt: TouchEvent) => { evt.preventDefault(); const touches = evt.changedTouches; for (let i = 0; i < touches.length; i++) { const indexById = activityTouchIndexById(touches[i].identifier); if (indexById >= 0) { cxt.value.beginPath(); cxt.value.moveTo(activityTouches[indexById].x, activityTouches[indexById].y); cxt.value.lineTo(touches[i].clientX, touches[i].pageY); cxt.value.stroke(); activityTouches.splice(indexById, 1, copyTouch(touches[i])); } } }; const handleEnd = (evt: TouchEvent) => { evt.preventDefault(); const touches = evt.changedTouches; for (let i = 0; i < touches.length; i++) { const indexById = activityTouchIndexById(touches[i].identifier); if (indexById >= 0) { cxt.value.beginPath(); cxt.value.moveTo(activityTouches[indexById].x, activityTouches[indexById].y); cxt.value.lineTo(touches[i].clientX, touches[i].clientY); drawCircle(touches[i].clientX, touches[i].clientY) activityTouches.splice(indexById, 1); } } }; const drawCircle = (x:number,y:number) => { cxt.value.arc(x, y, lineWidth / 2, 0, 2 * Math.PI, false); cxt.value.fill(); } </script>
到此這篇關(guān)于Vue實(shí)現(xiàn)多點(diǎn)涂鴉效果的示例代碼的文章就介紹到這了,更多相關(guān)Vue多點(diǎn)涂鴉內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中icon亂碼的問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目中icon亂碼的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12el-select單選時(shí)選擇后輸入框的is-focus狀態(tài)并沒(méi)有取消問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于el-select單選時(shí)選擇后輸入框的is-focus狀態(tài)并沒(méi)有取消問(wèn)題的解決過(guò)程,文中通過(guò)圖文以及代碼示例將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01vue不用window的方式如何刷新當(dāng)前頁(yè)面
這篇文章主要介紹了vue不用window的方式如何刷新當(dāng)前頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Vue+FormData+axios實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家學(xué)習(xí)介紹了Vue如何利用FormData和axios實(shí)現(xiàn)圖片上傳功能,本文為大家整理了詳細(xì)步驟,感興趣的小伙伴可以了解一下2023-08-08Vue+abp微信掃碼登錄的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了Vue+abp微信掃碼登錄的實(shí)現(xiàn)代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01vue?ElementUI級(jí)聯(lián)選擇器回顯問(wèn)題解決
這篇文章主要介紹了vue?ElementUI級(jí)聯(lián)選擇器回顯問(wèn)題解決,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09