Vue實現(xiàn)多點涂鴉效果的示例代碼
效果展示
單點效果
多點效果
創(chuàng)建畫布
創(chuàng)建畫布并設置事件處理器
<canvas ref="board" style="background-color: #2b2d42" width="1000" height="1000" @touchstart="handleStart" @touchmove="handleMove" @touchend="handleEnd" />
獲取畫布并初始化畫筆信息
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; };
觸摸事件處理
基礎知識
唯一地識別和觸摸平面接觸的點的值。
這個值在這根手指(或觸摸筆等)所引發(fā)的所有事件中保持一致,直到它離開觸摸平面。
該屬性返回一個TouchList
:
- 對于
touchstart
事件,這個TouchList
對象列出在此次事件中新增加的觸點。 - 對于
touchmove
事件,列出和上一次事件相比較,發(fā)生了變化的觸點。 - 對于 touchend 事件,
changedTouches
是已經(jīng)從觸摸面的離開的觸點的集合(也就是說,手指已經(jīng)離開了屏幕/觸摸面)。
拷貝觸摸點
瀏覽器會復用觸摸點,通過拷貝只記錄差異點和唯一標識符替換引用整個對象的方式進行優(yōu)化
type Point = { identifier: number,//觸摸點什么標識 //觸摸點坐標 x: number, y: number }; const copyTouch = (touch: Touch) => { return { identifier: touch.identifier, x: touch.pageX, y: touch.pageY }; };
查找觸摸點
通過遍歷 activityTouches
數(shù)組來尋找與給定標記相匹配的觸摸點,返回該觸摸點在數(shù)組中的下標。
const activityTouchIndexById = (idToFind: number) => { for (let i = 0; i < activityTouches.length; i++) { const id = activityTouches[i].identifier; if (id === idToFind) { return i; } } return -1; };
跟蹤所有觸摸點以實現(xiàn)多點觸控
//跟蹤當前存在的所有觸摸點 const activityTouches: Point[] = [];
新增觸摸事件
當屏幕上出現(xiàn)新的觸摸點touchstart
事件被觸發(fā),handleStart
函數(shù)被觸發(fā)。此時,要收集記錄觸摸點并在觸摸點處畫圓:
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) } };
觸摸移動時
當 touchmove
事件被觸發(fā)時,從而將調用handleMove()
函數(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ā)生移動的觸摸點。通過讀取每個觸摸點的 Touch.identifier
屬性,從緩存中讀取每個觸摸點在變化前的起點。這樣取得每個觸摸點之前位置的坐標,進而進行繪制。
觸摸結束處理
通過調用 handleEnd()
函數(shù)來處理觸摸結束事件:
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ā)點畫個圓,然后將對應的觸摸對象從緩存中移除。
其他
移動端和PC端所對應的時間不同。詳情可見鼠標事件和觸摸事件文檔。
移動端的觸摸點信息被封裝在Touch中,通過Touch.clientX
和Touch.clientX
讀取當前坐標
移動端的觸摸點信息被封裝在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>
到此這篇關于Vue實現(xiàn)多點涂鴉效果的示例代碼的文章就介紹到這了,更多相關Vue多點涂鴉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
el-select單選時選擇后輸入框的is-focus狀態(tài)并沒有取消問題解決
這篇文章主要給大家介紹了關于el-select單選時選擇后輸入框的is-focus狀態(tài)并沒有取消問題的解決過程,文中通過圖文以及代碼示例將解決的辦法介紹的非常詳細,需要的朋友可以參考下2024-01-01Vue+FormData+axios實現(xiàn)圖片上傳功能
這篇文章主要為大家學習介紹了Vue如何利用FormData和axios實現(xiàn)圖片上傳功能,本文為大家整理了詳細步驟,感興趣的小伙伴可以了解一下2023-08-08vue?ElementUI級聯(lián)選擇器回顯問題解決
這篇文章主要介紹了vue?ElementUI級聯(lián)選擇器回顯問題解決,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09