Vue实现多点涂鸦效果的示例代码

 更新时间:2023年03月28日 09:34:29   作者:白瑞德  
这篇文章主要为大家详细介绍了如何利用Vue实现多点涂鸦效果,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以跟随小编一起学习一下

效果展示

单点效果

多点效果

创建画布

创建画布并设置事件处理器

 <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;
};

触摸事件处理

基础知识

Touch.identifier

唯一地识别和触摸平面接触的点的值。

这个值在这根手指(或触摸笔等)所引发的所有事件中保持一致,直到它离开触摸平面。

TouchEvent.changedTouches

该属性返回一个TouchList:

  • 对于 touchstart 事件,这个 TouchList 对象列出在此次事件中新增加的触点。
  • 对于 touchmove 事件,列出和上一次事件相比较,发生了变化的触点。
  • 对于 touchend 事件,changedTouches 是已经从触摸面的离开的触点的集合(也就是说,手指已经离开了屏幕/触摸面)。

拷贝触摸点

浏览器会复用触摸点,通过拷贝只记录差异点和唯一标识符替换引用整个对象的方式进行优化

type Point = {
  identifier: number,//触摸点什么标识
  //触摸点坐标
  x: number,
  y: number
};

const copyTouch = (touch: Touch) => {
  return {
    identifier: touch.identifier,
    x: touch.pageX,
    y: touch.pageY
  };
};

查找触摸点

通过遍历 activityTouches 数组来寻找与给定标记相匹配的触摸点,返回该触摸点在数组中的下标。

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 activityTouches: Point[] = [];

新增触摸事件

当屏幕上出现新的触摸点touchstart事件被触发,handleStart 函数被触发。此时,要收集记录触摸点并在触摸点处画圆:

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 事件被触发时,从而将调用handleMove() 函数,此时按照路径绘制线:

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]));
    }
  }
};

首先遍历所有发生移动的触摸点。通过读取每个触摸点的 Touch.identifier 属性,从缓存中读取每个触摸点在变化前的起点。这样取得每个触摸点之前位置的坐标,进而进行绘制。

触摸结束处理

通过调用 handleEnd() 函数来处理触摸结束事件:

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 ,首先遍历所有事件,并读取缓存。在事件触发点画个圆,然后将对应的触摸对象从缓存中移除。

其他

移动端和PC端所对应的时间不同。详情可见鼠标事件触摸事件文档。

移动端的触摸点信息被封装在Touch中,通过Touch.clientXTouch.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实现多点涂鸦效果的示例代码的文章就介绍到这了,更多相关Vue多点涂鸦内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue项目中icon乱码的问题及解决

    vue项目中icon乱码的问题及解决

    这篇文章主要介绍了vue项目中icon乱码的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • el-select单选时选择后输入框的is-focus状态并没有取消问题解决

    el-select单选时选择后输入框的is-focus状态并没有取消问题解决

    这篇文章主要给大家介绍了关于el-select单选时选择后输入框的is-focus状态并没有取消问题的解决过程,文中通过图文以及代码示例将解决的办法介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • vue不用window的方式如何刷新当前页面

    vue不用window的方式如何刷新当前页面

    这篇文章主要介绍了vue不用window的方式如何刷新当前页面,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 讲解vue-router之什么是嵌套路由

    讲解vue-router之什么是嵌套路由

    这篇文章主要介绍了讲解vue-router之什么是嵌套路由,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Vue+FormData+axios实现图片上传功能

    Vue+FormData+axios实现图片上传功能

    这篇文章主要为大家学习介绍了Vue如何利用FormData和axios实现图片上传功能,本文为大家整理了详细步骤,感兴趣的小伙伴可以了解一下
    2023-08-08
  • Vue+abp微信扫码登录的实现代码示例

    Vue+abp微信扫码登录的实现代码示例

    这篇文章主要介绍了Vue+abp微信扫码登录的实现代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Vue extend学习示例讲解

    Vue extend学习示例讲解

    这篇文章主要介绍了Vue.extend使用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • vue ElementUI级联选择器回显问题解决

    vue ElementUI级联选择器回显问题解决

    这篇文章主要介绍了vue ElementUI级联选择器回显问题解决,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Vue中比较流行且好用的组件使用示例

    Vue中比较流行且好用的组件使用示例

    这篇文章主要介绍了Vue中比较流行且好用的一些组件使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Vue3中局部组件和全局组件的使用教程详解

    Vue3中局部组件和全局组件的使用教程详解

    这篇文章主要为大家学习介绍了Vue3中局部组件和全局组件的使用方法,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以学习一下
    2023-07-07

最新评论