Skip to content

Commit 0bd23cd

Browse files
author
zhaokai
committed
路径动画
1 parent d3c9f06 commit 0bd23cd

File tree

5 files changed

+198
-42
lines changed

5 files changed

+198
-42
lines changed

src/service/2DPlot/PlotCanvasGroup.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* @Author: zk
55
* @Date: 2022-05-13 11:01:10
66
* @LastEditors: zk
7-
* @LastEditTime: 2022-05-26 15:30:28
7+
* @LastEditTime: 2022-05-27 10:56:43
88
*/
99

1010
import { fabric } from 'fabric';
1111
import PlotCanvas from './PlotCanvas';
12+
import PlotUtilLine from './Shapes/PlotUtilLine';
1213

1314
export const PlotCanvasGroup = fabric.util.createClass(fabric.Canvas, {
1415
selection: false,
@@ -206,6 +207,9 @@ export const PlotCanvasGroup = fabric.util.createClass(fabric.Canvas, {
206207
let t = null;
207208
for (let i = 0; i < this._objects.length; i++) {
208209
const object = this._objects[i];
210+
if(!object.isExtendPlotObject){
211+
continue
212+
}
209213
const elem = object.getElement();
210214
if (elem && elem.getFeatureId() === uid) {
211215
t = object;
@@ -220,19 +224,15 @@ export const PlotCanvasGroup = fabric.util.createClass(fabric.Canvas, {
220224
});
221225
},
222226
addUtilPath(coords, options) {
223-
const coordSys = this.getCoordSys();
224-
const points = coords.map((s) => {
225-
const v = coordSys.dataToPoint([s.x, s.y]);
226-
return { x: v[0], y: v[1] };
227-
});
228-
const polyline = new fabric.Polyline(points, {
229-
fill:'green',
230-
});
231-
// this.add(polyline);
232-
return polyline
227+
const plot = new PlotUtilLine({
228+
coords,
229+
canvas:this
230+
})
231+
this.add(plot);
232+
return plot;
233233
},
234-
removeUtilPath(utilPlotObject){
235-
this.remove(utilPlotObject)
234+
removeUtilPath(utilPlotObject) {
235+
this.remove(utilPlotObject);
236236
},
237237
/**
238238
* @function: Module:PlotCanvasGroup.prototype._renderObjects
@@ -245,17 +245,20 @@ export const PlotCanvasGroup = fabric.util.createClass(fabric.Canvas, {
245245
var i, len;
246246
for (i = 0, len = objects.length; i < len; ++i) {
247247
const object = objects[i];
248-
const element = object.getElement();
249-
const bounds = element.getBounds();
250-
const left = bounds.left;
251-
const bottom = bounds.bottom;
252-
const top = bounds.top;
253-
const right = bounds.right;
254-
255248
let flag = true;
256-
if ((this.width < left && this.height < bottom) || (top < 0 && right < 0)) {
257-
flag = false;
249+
if (object.isExtendPlotObject) {
250+
const element = object.getElement();
251+
const bounds = element.getBounds();
252+
const left = bounds.left;
253+
const bottom = bounds.bottom;
254+
const top = bounds.top;
255+
const right = bounds.right;
256+
257+
if ((this.width < left && this.height < bottom) || (top < 0 && right < 0)) {
258+
flag = false;
259+
}
258260
}
261+
259262
flag && object && object.render(ctx);
260263
}
261264
}

src/service/2DPlot/Shapes/PlotObject.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: your name
33
* @Date: 2021-07-05 11:32:52
4-
* @LastEditTime: 2022-05-26 15:30:59
4+
* @LastEditTime: 2022-05-27 10:53:09
55
* @LastEditors: zk
66
* @Description: In User Settings Edit
77
* @FilePath: \MapGISPlot\src\js\Shapes\PlotObject.js
@@ -18,6 +18,8 @@ const PlotObject = fabric.util.createClass(fabric.Object, {
1818
this._elem.positions = options.positions || [];
1919
this.m_coordsPx = [];
2020
this._elem.propsUpdateSignal.add(this._elemPropsUpdateHandler, this);
21+
22+
this.isExtendPlotObject=true
2123
},
2224
_elemPropsUpdateHandler(event) {
2325
this.set('dirty', true);
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* @Author: your name
3+
* @Date: 2021-07-05 11:32:52
4+
* @LastEditTime: 2022-05-27 11:43:25
5+
* @LastEditors: zk
6+
* @Description: In User Settings Edit
7+
* @FilePath: \MapGISPlot\src\js\Shapes\PlotObject.js
8+
*/
9+
import Point from '../../PlotUtilBase/Geometry/Point';
10+
import { fabric } from 'fabric';
11+
import Bounds from '../../PlotUtilBase/Geometry/Bound';
12+
import { defined } from '../../PlotUtilBase/Check';
13+
14+
const _ = require('lodash');
15+
const PlotUtilLine = fabric.util.createClass(fabric.Object, {
16+
fill: null,
17+
strokeLineJoin: 'round',
18+
strokeWidth:20,
19+
strokeStyle:"#00ff00",
20+
lineWidth:20,
21+
initialize: function initialize(options) {
22+
this.callSuper('initialize', options);
23+
this.canvas = options.canvas;
24+
this.m_coords = options.coords || [];
25+
this.m_coordsPx = [];
26+
this.isExtendPlotObject = false;
27+
},
28+
setPnts(latlngs) {
29+
this.m_coords = latlngs;
30+
this.dataToPoint();
31+
this.set('dirty', true);
32+
},
33+
getCoordSys: function getCoordSys() {
34+
return this.canvas.getCoordSys();
35+
},
36+
dataToPoint: function dataToPoint() {
37+
debugger
38+
const coordSys = this.getCoordSys();
39+
const positions = this.m_coords;
40+
this.m_coordsPx = [];
41+
for (let i = 0; i < positions.length; i += 1) {
42+
const pntPx = coordSys.dataToPoint([positions[i].x, positions[i].y]);
43+
this.m_coordsPx.push(new Point(pntPx[0], pntPx[1]));
44+
}
45+
},
46+
setBounds: function setBounds(ctx) {
47+
this._setBounds(this._calcBounds(ctx));
48+
},
49+
// eslint-disable-next-line no-unused-vars
50+
_calcBounds: function _calcBounds(ctx) {
51+
let xMin = Number.POSITIVE_INFINITY;
52+
let yMin = Number.POSITIVE_INFINITY;
53+
let xMax = Number.NEGATIVE_INFINITY;
54+
let yMax = Number.NEGATIVE_INFINITY;
55+
for (let i = 0; i < this.m_coordsPx.length; i += 1) {
56+
const pnt = this.m_coordsPx[i];
57+
xMin = Math.min(xMin, pnt.x);
58+
yMin = Math.min(yMin, pnt.y);
59+
xMax = Math.max(xMax, pnt.x);
60+
yMax = Math.max(yMax, pnt.y);
61+
}
62+
63+
return new Bounds(xMin, yMin, xMax, yMax);
64+
},
65+
66+
_setBounds: function _setBounds(bounds) {
67+
const halfStrokeWid = this.strokeWidth / 2;
68+
this.set('top', bounds.bottom - halfStrokeWid);
69+
this.set('left', bounds.left - halfStrokeWid);
70+
this.set('width', bounds.right - bounds.left + this.strokeWidth);
71+
this.set('height', bounds.top - bounds.bottom + this.strokeWidth);
72+
73+
this.m_offsetX = -this.left - this.width / 2;
74+
this.m_offsetY = -this.top - this.height / 2;
75+
this.setCoords();
76+
},
77+
// eslint-disable-next-line no-unused-vars
78+
moveBy: function moveBy(moveX, moveY) {
79+
const coordSys = this.getCoordSys();
80+
const coords = [];
81+
const coordPx = this.m_coordsPx;
82+
83+
for (let i = 0; i < coordPx.length; i += 1) {
84+
coordPx[i].x += moveX;
85+
coordPx[i].y += moveY;
86+
}
87+
88+
for (let i = 0; i < coordPx.length; i += 1) {
89+
const latlng = coordSys.pointToData([coordPx[i].x, coordPx[i].y]);
90+
coords.push(new Point(latlng[0], latlng[1]));
91+
}
92+
93+
this.setPnts(coords);
94+
},
95+
getPlotCanvas: function getPlotCanvas() {
96+
return this.canvas;
97+
},
98+
render: function render(ctx) {
99+
if (this.isNotVisible()) {
100+
return;
101+
}
102+
this.dataToPoint();
103+
this.setBounds(ctx);
104+
this.callSuper('render', ctx);
105+
},
106+
_render(ctx) {
107+
if (this.m_coordsPx.length < 2) return;
108+
ctx.beginPath();
109+
ctx.moveTo(this.m_coordsPx[0].x + this.m_offsetX, this.m_coordsPx[0].y + this.m_offsetY);
110+
for (let i = 0; i < this.m_coordsPx.length; i += 1) {
111+
const point = this.m_coordsPx[i];
112+
ctx.lineTo(point.x + this.m_offsetX, point.y+ this.m_offsetY);
113+
}
114+
115+
this._drawPath(ctx,{strokeStyle:"#ff00ff",lineWidth:6})
116+
},
117+
_createStyleObject(){
118+
},
119+
_drawPath(ctx, style) {
120+
Object.keys(style).forEach((key) => {
121+
ctx[key] = style[key];
122+
});
123+
124+
if (defined(style.strokeStyle) && style.strokeStyle !== '' && style.strokeStyle !== 'none') {
125+
ctx.stroke();
126+
}
127+
},
128+
});
129+
fabric.PlotUtilLine = PlotUtilLine;
130+
export default PlotUtilLine;

src/service/PlotBase/Animation/AnimationTypes/PlotExtendAnimation/PlotGrowAnimation/PlotPathAnimation.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import MainLineElement from '../../../../SvgLoader/element/extend/MainLineElemen
88
* @Author: zk
99
* @Date: 2022-04-19 09:59:57
1010
* @LastEditors: zk
11-
* @LastEditTime: 2022-05-26 16:13:31
11+
* @LastEditTime: 2022-05-27 11:00:55
1212
*/
1313
export default class PlotPathAnimation extends PlotCoordsAnimation {
1414
constructor(options) {
@@ -33,10 +33,10 @@ export default class PlotPathAnimation extends PlotCoordsAnimation {
3333
this.AlongTangent = AnimationUtil.defineValue(options.AlongTangent, true);
3434

3535
//绑定id
36-
this.symbolBindId = AnimationUtil.defineValue(options.symbolBindId, null);
36+
this.symbolBindId = AnimationUtil.defineValue(options.symbolBindId, null);
3737

3838
// 路径对象
39-
this.pathWayObject=null
39+
this.pathWayObject = null;
4040
// init geometry
4141
this.geometryInstance = null;
4242
this.geometryAngles = [];
@@ -46,24 +46,29 @@ export default class PlotPathAnimation extends PlotCoordsAnimation {
4646
// 根据路径类型生成几何
4747
const { pathType } = this;
4848

49-
const plot= this.getPlotObjectById(this.symbolBindId)
50-
let coords=null
51-
if(plot){
52-
coords=plot.getElement().positions
53-
}else{
54-
coords=this.animationCoords
49+
const plot = this.getPlotObjectById(this.symbolBindId);
50+
let coords = null;
51+
if (plot) {
52+
coords = plot.getElement().positions;
53+
} else {
54+
coords = this.animationCoords;
5555
}
5656

5757
if (pathType == 'line') {
5858
this.geometryInstance = new Spline(coords);
5959
}
6060

61-
// const main= new MainLineElement()
62-
// main.applyMainGeo(this.geometryInstance)
63-
// const tempCoords=main.getCoords().flat()
64-
65-
// this.pathWayObject=this.addUtilPath(tempCoords)
66-
61+
const main = new MainLineElement();
62+
main.applyMainGeo(this.geometryInstance);
63+
const tempCoords = main.getCoords().flat();
64+
if(this.pathWayObject){
65+
this.pathWayObject.setPnts(tempCoords)
66+
this.pathWayObject=null
67+
}else{
68+
this.pathWayObject = this.addUtilPath(tempCoords);
69+
}
70+
71+
6772
this.geometryAngles = this._plotObjects.map((s) => s.getElement().getGeometryAngle());
6873
}
6974

@@ -79,12 +84,17 @@ export default class PlotPathAnimation extends PlotCoordsAnimation {
7984
const element = s.getElement();
8085
element.setGeometryAngle(this.geometryAngles[i]);
8186
});
87+
88+
if(this.pathWayObject){
89+
this.removeUtilPath(this.pathWayObject)
90+
this.pathWayObject=null
91+
}
8292
}
8393

8494
render(rate) {
85-
if(!this.geometryInstance) return;
95+
if (!this.geometryInstance) return;
8696
const trueRate = this._calcTrueRate(rate);
87-
97+
8898
const v = this.geometryInstance.getTransfromByRate(trueRate);
8999
const pnt = new Point(v[0][0], v[0][1]);
90100

src/service/PlotBase/SvgLoader/element/SvgElement.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: your name
33
* @Date: 2021-08-30 22:20:42
4-
* @LastEditTime: 2022-05-26 16:13:17
4+
* @LastEditTime: 2022-05-26 16:55:50
55
* @LastEditors: zk
66
* @Description: In User Settings Edit
77
* @FilePath: \MapGISPlotBase\src\svg-loader\SvgElement.js
@@ -10,7 +10,11 @@ import RenderedElement from "./RenderedElement";
1010

1111
export default class SvgElement extends RenderedElement {
1212
constructor(node) {
13-
super(node);
13+
if(node){
14+
super(node)
15+
}else{
16+
super(SvgElement.createDefaultNode())
17+
}
1418

1519
this.type = "svg";
1620

@@ -31,4 +35,11 @@ export default class SvgElement extends RenderedElement {
3135
cloneObject.height = this.height;
3236
}
3337
}
38+
SvgElement.createDefaultNode = function () {
39+
// 添加子节点
40+
const svg = document.createElement('svg');
41+
svg.setAttribute('width','200');
42+
svg.setAttribute('height','200');
43+
return svg;
44+
};
3445

0 commit comments

Comments
 (0)