vue+canvas实现逐字手写效果

在pc端进行逐字手写的功能。用户可以在一个 inputCanvas 上书写单个字,然后在特定时间后将这个字添加到 outputCanvas 上,形成一个逐字的手写效果。用户还可以保存整幅图像或者撤销上一个添加的字。

<template>
  <div class="container" v-if="!disabled">
    <div class="tipCn">
      <div>请您在右侧区域内逐字手写以下文字,全部写完后点击保存!</div>
      <div>{{ ruleForm.sqcn }}</div>
    </div>

    <div style="margin: 0px 20px">
      <span class="dialog-footer">
        <el-button @click="undoChar" type="danger" :icon="RefreshRight">撤销上一个字</el-button>
        <el-button @click="saveImage" type="primary" :icon="Check">保存</el-button>
      </span>
      <canvas
        ref="inputCanvas"
        class="input-canvas"
        :width="canvasWidth"
        :height="canvasHeight"
        @mousedown="startDrawing"
        @mousemove="draw"
        @mouseup="stopDrawing"
        @mouseleave="stopDrawing"
      ></canvas>
    </div>

    <canvas ref="outputCanvas" class="output-canvas" :width="outputWidth" :height="outputHeight"></canvas>
  </div>

  <img class="Signature" v-else :src="resultImg" alt="commitment Image" />
</template>

<script setup>
import { ref, onMounted, nextTick, watch } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import fileService from "@/api/sys/fileService.js";
import { Check, RefreshRight } from "@element-plus/icons-vue";
import knsService from "@/api/sys/kns/knsService";

const canvasWidth = 300;
const canvasHeight = 300;
const isDrawing = ref(false);
const startX = ref(0);
const startY = ref(0);
const charObjects = ref([]);
const timer = ref(null);
const delay = 1000; // 1秒延迟
let outputWidth = 300;
let outputHeight = ref(50);
let resultImg = ref("");
let context = null;
let outputContext = null;

const inputCanvas = ref(null);
const outputCanvas = ref(null);

let ruleForm = ref({});

const emit = defineEmits(["update:modelValue"]);
const props = defineProps({
  modelValue: {
    type: [Number, String],
    default: ""
  },
  disabled: {
    type: Boolean,
    default: false
  }
});

// 当输入框内容变化时触发更新父组件的 value
watch(
  resultImg,
  (newValue) => {
    emit("update:modelValue", newValue);
  },
  { deep: true }
);

watch(
  () => props.modelValue,
  (newValue) => {
    resultImg.value = newValue;
  },
  { deep: true, immediate: true }
);

onMounted(() => {
  if (!props.disabled) {
    getData();
    context = inputCanvas.value.getContext("2d");
    outputContext = outputCanvas.value.getContext("2d");
    context.strokeStyle = "#000000";
    context.lineWidth = 4;
    context.lineCap = "round";
    context.lineJoin = "round";
    outputContext.strokeStyle = "#000000";
    outputContext.lineWidth = 3;
    outputContext.lineCap = "round";
    outputContext.lineJoin = "round";
  }
});

// 获取承诺
const getData = async () => {
  const res = await knsService.getSettingData();
  ruleForm.value = res[0];
};

const startDrawing = (e) => {
  if (timer.value) {
    clearTimeout(timer.value);
    timer.value = null;
  }
  isDrawing.value = true;
  startX.value = e.offsetX;
  startY.value = e.offsetY;
  context.beginPath();
  context.moveTo(startX.value, startY.value);
};

const draw = (e) => {
  if (!isDrawing.value) return;
  context.lineTo(e.offsetX, e.offsetY);
  context.stroke();
};

const stopDrawing = () => {
  if (isDrawing.value) {
    isDrawing.value = false;
    context.closePath();
    timer.value = setTimeout(addChar, delay);
  }
};

const addChar = () => {
  const canvas = inputCanvas.value;
  const dataUrl = canvas.toDataURL("image/png");
  charObjects.value.push(dataUrl);
  clearCanvas();
  redrawOutputCanvas();
};

const clearCanvas = () => {
  const canvas = inputCanvas.value;
  context.clearRect(0, 0, canvas.width, canvas.height);
};

const undoChar = () => {
  if (charObjects.value.length > 0) {
    charObjects.value.pop();
    redrawOutputCanvas();
    if (charObjects.value.length === 0) {
      outputHeight.value = 50; // 如果字符对象为空,则将输出画布高度设置为 50
      outputCanvas.value.height = outputHeight.value; // 更新画布高度
    }
  }
};

const redrawOutputCanvas = () => {
  outputContext.clearRect(0, 0, outputWidth, outputHeight.value);

  const charSize = 50; // 调整字符大小
  const charSpacing = 50; // 调整字符间距
  const maxCharsPerRow = Math.floor(outputWidth / charSize); // 每行最大字符数
  const numRows = Math.ceil(charObjects.value.length / maxCharsPerRow); // 计算行数
  const newOutputHeight = numRows * charSize; // 动态计算输出画布的高度

  if (newOutputHeight !== outputHeight.value) {
    outputHeight.value = newOutputHeight;
    outputCanvas.value.height = outputHeight.value; // 更新画布高度
  }

  nextTick(() => {
    charObjects.value.forEach((char, index) => {
      const rowIndex = Math.floor(index / maxCharsPerRow); // 当前字符的行索引
      const colIndex = index % maxCharsPerRow; // 当前字符的列索引
      const img = new Image();
      img.onload = () => {
        outputContext.drawImage(img, colIndex * charSpacing, rowIndex * charSpacing, charSize, charSize); // 绘制字符图片到输出画布上
      };
      img.src = char;
    });
  });
};

const saveImage = () => {
  if (charObjects.value.length === 0) {
    ElMessage.error("请输入!");
    return false;
  }

  const canvas = outputCanvas.value;
  const dataUrl = canvas.toDataURL("image/png");
  console.log(dataUrl, "dataUrldataUrldataUrl"); // 您可以将此图片上传或保存

  // 生成带有当前日期和时间的文件名
  const now = new Date();
  const filename = `承诺-${now.getFullYear()}${padZero(now.getMonth() + 1)}${padZero(now.getDate())}${padZero(
    now.getHours()
  )}${padZero(now.getMinutes())}${padZero(now.getSeconds())}.jpg`;

  const blob = dataURLtoBlob(dataUrl);
  const tofile = blobToFile(blob, filename);
  setTimeout(async () => {
    const formData = new FormData();
    formData.append("file", tofile, tofile.name);
    formData.append("fileType", 9);
    console.log(formData, "formDataformData");
    const res2 = await fileService.uploadFile(formData);
    resultImg.value = res2;
    console.log(resultImg.value, "resultImg.value");
  });

  ElMessage.success("保存成功!");
};

const dataURLtoBlob = (dataurl) => {
  const arr = dataurl.split(",");
  const mime = arr[0].match(/:(.*?);/)[1];
  const bstr = atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
};

const blobToFile = (theBlob, fileName) => {
  theBlob.lastModifiedDate = new Date();
  theBlob.name = fileName;
  return theBlob;
};

const padZero = (num) => {
  return num < 10 ? "0" + num : num;
};
</script>

<style scoped lang="scss">
.container {
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;

  .output-canvas {
    border: 1px solid #ddd;
  }
  img {
    width: 50px;
    height: 50px;
    margin: 1px;
  }
  .input-canvas {
    border-radius: 5px;
    border: 1px dashed #dddee1;
  }
  .dialog-footer {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }

  .tipCn {
    div:nth-child(1) {
      color: #ff6f77;
      font-size: 12px;
    }
    div:nth-child(2) {
      background-color: #ecf5ff;
      padding: 0px 10px;
      border-radius: 4px;
      color: #3c9cff;
      font-size: 14px;
      text-align: left;
    }
  }
}
.Signature {
    width: 500px;
    height: 150px;
    margin-top: 10px;
    border: 1px solid #dddee1;
  }
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现拓扑图可以使用 Vue3 和 Canvas 结合来完成。下面是一个简单的实现过程: 1. 首先,需要安装 Vue3 和 Canvas 库: ``` bash npm install vue@next npm install canvas --save ``` 2. 在 Vue3 中创建一个组件,用于渲染拓扑图。在组件中引入 Canvas 库: ``` javascript <template> <canvas ref="canvas"></canvas> </template> <script> import { onMounted, ref } from 'vue' import Canvas from 'canvas' export default { setup() { const canvasRef = ref(null) onMounted(() => { const canvas = canvasRef.value const ctx = canvas.getContext('2d') // 在这里进行绘制 }) return { canvasRef } } } </script> ``` 3. 在组件的 `onMounted` 钩子函数中,获取 Canvas 的上下文对象 `ctx`,并进行绘制。可以使用 Canvas 的 API 画出线条、圆形等形状,也可以使用外部库来绘制更复杂的图形。 4. 在绘制时,可以将节点和线条信息存储在数组中,以方便后续的更新和交互。例如: ``` javascript // 存储节点和线条信息的数组 const nodes = [ {x: 100, y: 100, r: 20, color: '#ff0000'}, {x: 200, y: 200, r: 30, color: '#00ff00'} ] const links = [ {source: 0, target: 1}, {source: 1, target: 2} ] // 绘制节点 nodes.forEach(node => { ctx.beginPath() ctx.arc(node.x, node.y, node.r, 0, Math.PI * 2) ctx.fillStyle = node.color ctx.fill() }) // 绘制线条 links.forEach(link => { const source = nodes[link.source] const target = nodes[link.target] ctx.beginPath() ctx.moveTo(source.x, source.y) ctx.lineTo(target.x, target.y) ctx.stroke() }) ``` 以上就是一个简单的 Vue3 和 Canvas 实现拓扑图的过程。需要注意的是,Vue3 的模板中不能直接使用 Canvas,需要通过 `ref` 引用实现。另外,绘制时需要注意节点和线条的位置信息,以及 Canvas 的坐标系。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值