Browse Source

升级底图为路径,使用剪切画图

caner 1 year ago
parent
commit
1f2be7fba3
5 changed files with 61 additions and 52 deletions
  1. 0 9
      index/4.svg
  2. 46 35
      index/drawCanvas.js
  3. 9 4
      index/index.js
  4. 1 1
      index/index.wxml
  5. 5 3
      index/index.wxss

+ 0 - 9
index/4.svg

@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<svg width="349.007135px" height="274.00004px" viewBox="0 0 349.007135 274.00004" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
-    <title>编组 6</title>
-    <g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
-        <g id="编组-6" transform="translate(-0.012468, 0.000040)" fill="#FFFFFF" stroke="#979797">
-            <path d="M174.087591,0.514560141 C221.082581,-0.0605323943 258.036942,16.4651639 285.471873,39.3811671 C324.097037,71.644211 343.867653,116.568208 346.20662,144.24652 C350.19951,191.496617 349.953227,225.979727 337.927395,239.136244 C331.547559,246.11592 292.534012,273.500015 156.081032,273.500015 C124.046067,273.249295 88.3649706,267.889441 59.9639811,260.374988 C31.6787425,252.891161 10.4895126,243.339368 7.76631374,234.485863 C0.979316969,215.637975 -3.35222841,169.181427 5.40075043,132.103662 C11.4231177,109.216448 30.4968016,66.3255379 69.7184967,35.7271113 C95.2877064,15.7795392 129.422591,1.06113985 174.087591,0.514560141 Z" id="三台阶"></path>
-        </g>
-    </g>
-</svg>

+ 46 - 35
index/drawCanvas.js

@@ -6,12 +6,9 @@ class DrawCanvas {
     this._executionArr = []
     this._pathArr = []
     this._style = null
-    this.baseMapUrl = null
-    this.baseWidth = null
-    this.baseHeight = null
   }
 
-  _init({ width, height, canvas, baseMapUrl = '' }) {
+  _init({ width, height, canvas, dicTypePath = '' }) {
     if (!width || !height || !canvas) return
     const ctx = canvas.getContext('2d')
     const dpr = wx.getSystemInfoSync().pixelRatio
@@ -22,10 +19,12 @@ class DrawCanvas {
     this._ctx = ctx
     this._executionArr = [] //每移动一笔的数据
     this._pathArr = [] //抬起落下为一笔
-    this.baseHeight = height
-    this.baseWidth = width
-    this.baseMapUrl = baseMapUrl
-    if (baseMapUrl) this.addBaseMap()
+    if (dicTypePath) {
+      // 添加路径底图
+      const path = this.sPath2cPath(dicTypePath)
+      this._ctx.stroke(path)
+      this._ctx.clip(path)
+    }
   }
 
   mouseStart() {
@@ -50,34 +49,18 @@ class DrawCanvas {
   mouseBack() {
     // 删除每个移动数据
     if (this._pathArr.length) {
-      this._ctx.globalCompositeOperation = "source-over" //覆盖
       // 清空画布
       this._ctx.clearRect(0, 0, this._ctx.canvas.width, this._ctx.canvas.height)
       this._pathArr.pop()
-      // 画上底图
-      this.addBaseMap(() => {
-        // 画上路径
-        for (let k = 0; k < this._pathArr.length; k++) {
-          const el = this._pathArr[k];
-          for (let j = 0; j < el.path.length; j++) {
-            const es = el.path[j];
-            this._ctx.fillStyle = el.fillStyle
-            this._ctx.fillRect(es[0] - 5, es[1] - 5, 10, 10)
-          }
+      for (let k = 0; k < this._pathArr.length; k++) {
+        const el = this._pathArr[k];
+        for (let j = 0; j < el.path.length; j++) {
+          const es = el.path[j];
+          this._ctx.fillStyle = el.fillStyle
+          this._ctx.fillRect(es[0] - 5, es[1] - 5, 10, 10)
         }
-      })
-    }
-  }
-
-  // 添加底图
-  addBaseMap(callback) {
-    const img = this._canvas.createImage()
-    img.onload = () => {
-      this._ctx.drawImage(img, 0, 0, this.baseWidth, this.baseHeight)
-      this._ctx.globalCompositeOperation = "source-atop" //画笔重叠部分覆盖
-      if (callback) callback()
+      }
     }
-    img.src = this.baseMapUrl
   }
 
   // 切换画笔
@@ -90,18 +73,46 @@ class DrawCanvas {
     img.src = url
   }
 
+  // svg 路径转 canvas 路径
+  sPath2cPath(path = '') {
+    if (!path || !this._canvas) return
+    const arr = path.split(/(^|\s+)(?=[A-Z])/).filter(el => el !== ' ')
+    const PATH = this._canvas.createPath2D();
+    for (let k = 0; k < arr.length; k++) {
+      const el = arr[k].replace(/\s+/g, ',');
+      const key = el.replace(/[^A-Z]/g, '')
+      const value = el.replace(/[A-Z]/g, '').split(',')
+      console.log(77, key, value);
+      if (key === 'M') {
+        PATH.moveTo(value[0], value[1]);
+      } else if (key === 'C') {
+        PATH.bezierCurveTo(value[0], value[1], value[2], value[3], value[4], value[5]);
+      } else if (key === 'L') {
+        PATH.lineTo(value[0], value[1]);
+      }
+    }
+    return PATH
+  }
+
   // 清除画布
-  destory() {
+  clear() {
     // 清空画布
     this._ctx.clearRect(0, 0, this._ctx.canvas.width, this._ctx.canvas.height)
-    this._ctx.globalCompositeOperation = "source-over" //覆盖
-    // 画上底图
-    this.addBaseMap()
+    // this._ctx.globalCompositeOperation = "source-over" //覆盖
     // 清除路径数据
     this._pathArr = []
     this._executionArr = []
   }
 
+  // 销毁
+  destory() {
+    this._ctx = null
+    this._canvas = null
+    this._executionArr = []
+    this._pathArr = []
+    this._style = null
+  }
+
   // 保存
   save() {
     wx.canvasToTempFilePath({

+ 9 - 4
index/index.js

@@ -11,11 +11,16 @@ Page({
         name: '2',
         url: './2.png'
       },
-    ]
+    ],
+    dicTypePath: {
+      2: 'M174.087591,0.514560141 C221.082581,-0.0605323943 258.036942,16.4651639 285.471873,39.3811671 C324.097037,71.644211 343.867653,116.568208 346.20662,144.24652 C350.19951,191.496617 349.953227,225.979727 337.927395,239.136244 C331.547559,246.11592 292.534012,273.500015 156.081032,273.500015 C124.046067,273.249295 88.3649706,267.889441 59.9639811,260.374988 C31.6787425,252.891161 10.4895126,243.339368 7.76631374,234.485863 C0.979316969,215.637975 -3.35222841,169.181427 5.40075043,132.103662 C11.4231177,109.216448 30.4968016,66.3255379 69.7184967,35.7271113 C95.2877064,15.7795392 129.422591,1.06113985 174.087591,0.514560141 Z',
+      1: 'M74.5687033,39.0988067 C103.126959,16.3660403 140.601506,-0.229902063 187.512565,0.524738835 C236.745333,1.31672823 274.595793,17.4503114 302.795795,39.6405029 C342.604241,70.9652189 363.185816,114.34989 369.416086,143.644689 C374.911511,169.484239 376.854407,192.671434 374.545889,210.933889 C372.400553,227.905431 366.593006,240.62113 356.481554,247.164644 C356.083446,247.422274 355.666331,247.696637 355.22572,247.986461 C343.683165,255.57889 315.933614,273.789334 190.568062,279.499721 C98.8356514,278.682689 21.1251422,255.234726 10.5947186,240.294075 C-0.7422251,224.209125 -2.04541832,184.937269 4.21868782,146.408753 C8.01362465,123.067308 30.8546268,73.8958162 74.5687033,39.0988067 Z',
+      0: 'M186.999991,0.5 C237.942268,0.5 284.114101,20.9396581 317.773259,54.0709856 C351.489638,87.2586372 372.650842,133.181116 373.474962,184.04672 L373.474962,184.04672 L373.49,186.000085 L373.499915,244.5 L0.499991046,244.5 L0.499991046,186.002992 C0.806967785,134.709026 21.789529,88.3201013 55.5288158,54.7612385 C89.2466165,21.2237469 135.704937,0.5 186.999991,0.5 Z'
+    }
   },
   drawCanvas: new DrawCanvas(),
   onLoad() {
-    console.log(111,this.drawCanvas);
+    console.log(111, this.drawCanvas);
     // 通过 SelectorQuery 获取 Canvas 节点
     wx.createSelectorQuery()
       .select('#canvas')
@@ -30,7 +35,7 @@ Page({
     const width = res[0].width
     const height = res[0].height
     const canvas = res[0].node
-    this.drawCanvas._init({ width, height, canvas, baseMapUrl:'./4.svg'})
+    this.drawCanvas._init({ width, height, canvas, dicTypePath: this.data.dicTypePath[1] })
   },
 
   start() {
@@ -51,7 +56,7 @@ Page({
   },
 
   clear() {
-    this.drawCanvas.destory()
+    this.drawCanvas.clear()
   },
 
   changeTool(e) {

+ 1 - 1
index/index.wxml

@@ -1,4 +1,4 @@
-<canvas type="2d" id="canvas" disable-scroll bindtouchmove="move" bindtouchend="end"  bindtouchstart="start" style="width: 100vw;height: 600rpx;"></canvas>
+<canvas type="2d" id="canvas" disable-scroll bindtouchmove="move" bindtouchend="end"  bindtouchstart="start"></canvas>
 
 <view class="tool">
   <block wx:for="{{list}}" wx:key="item">

+ 5 - 3
index/index.wxss

@@ -1,6 +1,8 @@
-/* canvas{
-  background: grey;
-} */
+canvas{
+  margin: 0 auto;
+  width: 698rpx;
+  height: 548rpx;
+}
 .tool {
   display: flex;
   justify-content: center;