|
|
@@ -1,13 +1,14 @@
|
|
|
+import { lineSplit, points, polygon, pointsWithinPolygon, lineString ,nearestPointOnLine} from "@turf/turf";
|
|
|
+
|
|
|
class DrawCanvas {
|
|
|
|
|
|
constructor() {
|
|
|
this._ctx = null
|
|
|
this._canvas = null
|
|
|
- this._executionArr = { s: 0, e: 0, arr: [], type: 1 } //每移动一笔的数据
|
|
|
+ this._executionArr = { s: 0, e: 0, arr: [], type: 1, outPath: [], splitPath: null } //每移动一笔的数据
|
|
|
this._pathArr = [] //抬起落下为一笔
|
|
|
this._fillStyle = null // 每笔的样式
|
|
|
this._matrix = new DOMMatrix() //旋转矩阵
|
|
|
-
|
|
|
}
|
|
|
|
|
|
_init({ canvas, dicTypePath = '', callBack }) {
|
|
|
@@ -23,12 +24,14 @@ class DrawCanvas {
|
|
|
// 加个背景
|
|
|
this._ctx.fillStyle = "#FFFFFF";
|
|
|
this._ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
+ // 获取path上的点用svg处理
|
|
|
+
|
|
|
// 添加路径底图
|
|
|
const path = this.sPath2cPath(dicTypePath)
|
|
|
if (path) {
|
|
|
this._ctx.stroke(path)
|
|
|
this._ctx.clip(path) //IOS真机裁切后,导致无法画图
|
|
|
- this._path = path
|
|
|
+ // this._executionArr.outPath = outPath
|
|
|
}
|
|
|
// 事件监听
|
|
|
canvas.addEventListener('touchstart', this.touchStart.bind(this))
|
|
|
@@ -45,6 +48,7 @@ class DrawCanvas {
|
|
|
this._executionArr.e = clientY - offsetTop
|
|
|
}
|
|
|
this._executionArr.arr = []
|
|
|
+ console.log('点击');
|
|
|
}
|
|
|
|
|
|
touchMove(event) {
|
|
|
@@ -56,19 +60,26 @@ class DrawCanvas {
|
|
|
}
|
|
|
|
|
|
touchEnd() {
|
|
|
- this._pathArr.push({
|
|
|
- ...this._fillStyle,
|
|
|
- path: this._executionArr
|
|
|
- })
|
|
|
+ console.log('结束', this._fillStyle, this._executionArr,this._pathArr);
|
|
|
+ const { arr: line, outPath, type } = this._executionArr
|
|
|
+ if (line.length > 2) {
|
|
|
+ // 画线才记录数据
|
|
|
+ this._pathArr.push({
|
|
|
+ ...this._fillStyle,
|
|
|
+ path: this._executionArr
|
|
|
+ })
|
|
|
+ // 开始结束自动拟合
|
|
|
+
|
|
|
+ // 画线后切分
|
|
|
+ if (!type && outPath) {
|
|
|
+ const splitPath = lineSplit(lineString(line), outPath)
|
|
|
+ if(!splitPath.features.length) console.log('数据无法切分,请重画');
|
|
|
+ this._executionArr.splitPath = splitPath
|
|
|
+ console.log('画线结束,切割区域', this._pathArr);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 区域检测
|
|
|
- // 当拥有圆圈的坐标和尺寸(半径)信息,我们可以利用数学方式通过简单计算来检测在任意一个圆圈上的点击。我们所需要的就是获取到鼠标点击位置的坐标信息,并且跟所有的圆圈逐一进行相交检测:
|
|
|
- // https://www.cnblogs.com/hanshuai/p/14385286.html
|
|
|
- isIntersect(point, circle) {
|
|
|
- return Math.sqrt((point.x-circle.x) ** 2 + (point.y - circle.y) ** 2) < circle.radius;
|
|
|
- }
|
|
|
-
|
|
|
// 画线
|
|
|
drawLine(x = 0, y = 0) {
|
|
|
if (!this._ctx || this._executionArr.type) return
|
|
|
@@ -79,7 +90,7 @@ class DrawCanvas {
|
|
|
this._ctx.stroke();
|
|
|
this._executionArr.s = x
|
|
|
this._executionArr.e = y
|
|
|
- console.log('画线', this._executionArr);
|
|
|
+ console.log('画线');
|
|
|
}
|
|
|
|
|
|
// svg 路径转 canvas 路径
|
|
|
@@ -102,6 +113,14 @@ class DrawCanvas {
|
|
|
return PATH
|
|
|
}
|
|
|
|
|
|
+ // 获取path上的点
|
|
|
+ getPathPonit(path=''){
|
|
|
+ if(!path) return ''
|
|
|
+ const pathDom = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
|
+ const a = pathDom.getTotalLength()
|
|
|
+ return a
|
|
|
+ }
|
|
|
+
|
|
|
// 销毁
|
|
|
destory() {
|
|
|
this._ctx = null
|
|
|
@@ -110,7 +129,6 @@ class DrawCanvas {
|
|
|
this._pathArr = []
|
|
|
this._style = null
|
|
|
this._matrix = null
|
|
|
- this._path = null
|
|
|
}
|
|
|
}
|
|
|
|