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>
相关推荐
逆旅行天涯1 分钟前
【Threejs】从零开始(六)--GUI调试开发3D效果
前端·javascript·3d
m0_7482552623 分钟前
easyExcel导出大数据量EXCEL文件,前端实现进度条或者遮罩层
前端·excel
长风清留扬43 分钟前
小程序毕业设计-音乐播放器+源码(可播放)下载即用
javascript·小程序·毕业设计·课程设计·毕设·音乐播放器
web147862107231 小时前
C# .Net Web 路由相关配置
前端·c#·.net
m0_748247801 小时前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
飞的肖1 小时前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
青灯文案11 小时前
前端 HTTP 请求由 Nginx 反向代理和 API 网关到后端服务的流程
前端·nginx·http
m0_748254881 小时前
DataX3.0+DataX-Web部署分布式可视化ETL系统
前端·分布式·etl
ZJ_.1 小时前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
GIS开发特训营1 小时前
Vue零基础教程|从前端框架到GIS开发系列课程(七)响应式系统介绍
前端·vue.js·前端框架·gis开发·webgis·三维gis