canvas绘画线条

复制代码
<template>
  <canvas
    ref="canvas"
    :width="width"
    :height="height"
    @mousedown="startDraw"
    @mousemove="draw"
    @mouseup="endDraw"
    @mouseleave="endDraw"
  ></canvas>
  <div class="canvasButton">
    <a-button @click="save" type="primary">保存</a-button>
    <a-button @click="clear" style="margin-left: 15px;" danger>清除</a-button>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { Button as AButton, } from "ant-design-vue"

const canvas = ref(null);
const ctx = ref(null);
const isDrawing = ref(false);
const width = 650;
const height = 360;

const startDraw = (event) => {
  isDrawing.value = true;
  const rect = canvas.value.getBoundingClientRect();
  const x = event.clientX - rect.left;
  const y = event.clientY - rect.top;
  ctx.value.beginPath();
  ctx.value.moveTo(x, y);
};

const draw = (event) => {
  if (!isDrawing.value) return;
  const rect = canvas.value.getBoundingClientRect();
  const x = event.clientX - rect.left;
  const y = event.clientY - rect.top;
  ctx.value.lineTo(x, y);
  ctx.value.stroke();
};

const endDraw = () => {
  isDrawing.value = false;
};

onMounted(() => {
  ctx.value = canvas.value.getContext('2d');
});

const clear = () => {
  ctx.value.clearRect(0, 0, width, height);
}
const save = () => {
  // console.log(canvas.value.toDataURL('image/png'))
  let imgBase64 = canvas.value.toDataURL('image/png')
  console.log(imgBase64)
}
</script>

<style scoped>
canvas {
  /* border: 1px solid #000; */
  touch-action: none;
}

.canvasButton {
  display: flex;

}
</style>

canvas融合

复制代码
<template>
  <canvas
    ref="canvas"
    :width="width"
    :height="height"
    @mousedown="startDraw"
    @mousemove="draw"
    @mouseup="endDraw"
    @mouseleave="endDraw"
    style="background: none;"
  ></canvas>
  <canvas
     ref="canvas1"
    :width="width"
    :height="height" style="display: none; pointer-events: none;position: absolute;"></canvas>
  <div class="canvasButton">
    <a-button @click="save" type="primary">保存</a-button>
    <a-button @click="makeImg" type="primary">生成图片</a-button>
    <a-button @click="clear" style="margin-left: 0px;" danger>清除</a-button>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { Button as AButton, } from "ant-design-vue"

const canvas = ref(null);
const canvas1 = ref(null);
const ctx = ref(null);
const ctx1 = ref(null);
const isDrawing = ref(false);
const width = 650;
const height = 360;

const emits =  defineEmits(['generateBase64'])

const startDraw = (event) => {
  isDrawing.value = true;
  const rect = canvas.value.getBoundingClientRect();
  const x = event.clientX - rect.left;
  const y = event.clientY - rect.top;;
  ctx.value.beginPath();
  ctx.value.lineWidth = 20;
  ctx.value.strokeStyle="#fff";
  ctx.value.moveTo(x, y);
};

const draw = (event) => {
  if (!isDrawing.value) return;
  const rect = canvas.value.getBoundingClientRect();
  const x = event.clientX - rect.left;
  const y = event.clientY - rect.top;
  ctx.value.lineTo(x, y);
  ctx.value.stroke();
};

const endDraw = () => {
  isDrawing.value = false;
};

onMounted(() => {
  ctx.value = canvas.value.getContext('2d');

  ctx1.value = canvas1.value.getContext('2d');
  ctx1.value.fillStyle = "rgba(0, 0, 0, 1)"
  ctx1.value.fillRect(0, 0, width, height);
});

const clear = () => {
  ctx.value.clearRect(0, 0, width, height);
}
const save = () => {
  let canvasMerged = document.createElement('canvas');
  canvasMerged.width = 650;
  canvasMerged.height = 360;
  let ctxMerged = canvasMerged.getContext('2d');
  ctxMerged.drawImage(canvas1.value, 0, 0, width, height);
  ctxMerged.drawImage(canvas.value, 0, 0, width, height);
  console.log(ctxMerged.canvas.toDataURL('image/png'))
  emits('generateBase64', ctxMerged.canvas.toDataURL('image/png'))

  // let imgBase64 = canvas.value.toDataURL('image/png')
  // console.log(imgBase64)
  // emits('generateBase64', imgBase64)
}

const makeImg = () => {
  let canvasMerged = document.createElement('canvas');
  canvasMerged.width = 650;
  canvasMerged.height = 360;
  let ctxMerged = canvasMerged.getContext('2d');
  ctxMerged.drawImage(canvas1.value, 0, 0, width, height);
  ctxMerged.drawImage(canvas.value, 0, 0, width, height);
  var link = document.createElement('a');
  link.href = ctxMerged.canvas.toDataURL('image/png');
  link.download = "download.png";
  link.click();
}



defineExpose({
  save
});
</script>

<style scoped>
canvas {
  /* border: 1px solid #000; */
  touch-action: none;
  background: none;
}

.canvasButton {
  display: flex;

}
</style>
相关推荐
Panda__Panda20 小时前
docker项目打包演示项目(数字排序服务)
运维·javascript·python·docker·容器·c#
10年前端老司机21 小时前
Promise 常见面试题(持续更新中)
前端·javascript
潘小安21 小时前
跟着 AI 学 (一)- shell 脚本
前端·ci/cd·vibecoding
clownAdam1 天前
Chrome性能优化秘籍
前端·chrome·性能优化
@Kerry~1 天前
phpstudy .htaccess 文件内容
java·开发语言·前端
nueroamazing1 天前
PPT-EA:PPT自动生成器
vue.js·python·语言模型·flask·大模型·项目·ppt
WebDesign_Mu1 天前
为了庆祝2025英雄联盟全球总决赛开启,我用HTML+CSS+JS制作了LOL官方网站
javascript·css·html
@PHARAOH1 天前
WHAT - 前端性能指标(交互和响应性能指标)
前端·交互
噢,我明白了1 天前
前端js 常见算法面试题目详解
前端·javascript·算法
im_AMBER1 天前
Web 开发 30
前端·笔记·后端·学习·web