Skip to content

Commit 93e74cc

Browse files
author
zhaokai
committed
【问题】【fabricLayer在storybook中报错】
1 parent 141c8a7 commit 93e74cc

File tree

2 files changed

+47
-53
lines changed

2 files changed

+47
-53
lines changed

src/mapboxgl/overlay/FabricLayer.js

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @Author: zk
55
* @Date: 2022-05-13 11:22:56
66
* @LastEditors: zk
7-
* @LastEditTime: 2022-05-24 11:23:51
7+
* @LastEditTime: 2022-07-13 13:37:47
88
*/
99
import mapboxgl from '@mapgis/mapbox-gl';
1010
import { PlotMapCoordSys } from './fabric/PlotMapCoordSys';
@@ -19,27 +19,23 @@ export class FabricLayer {
1919
* @param {Object} fabricOptions fabric图层选项
2020
*/
2121
constructor(map, fabricClass, fabricOptions) {
22-
if(!FabricLayer.instance){
23-
const m_fabricOptions = fabricOptions || {};
24-
this.map = map;
25-
this.initDevicePixelRatio();
26-
this.canvas = this._createCanvas();
27-
this.mapContainer = map.getCanvasContainer();
28-
this.mapContainer.appendChild(this.canvas);
29-
30-
this.m_fabricCanvas = this._createFabricCanvas(this.canvas, fabricClass, m_fabricOptions);
31-
this.m_fabricCanvas.setMap(this.map)
32-
// this.mapContainer.style.perspective = this.map.transform.cameraToCenterDistance + 'px';
33-
34-
this.bindEvent();
35-
this._reset();
36-
37-
// set fabric instance
38-
FabricLayer.instance= this
39-
}else{
40-
return FabricLayer.instance
41-
}
42-
22+
const m_fabricOptions = fabricOptions || {};
23+
this.map = map;
24+
this.initDevicePixelRatio();
25+
26+
this.destroy();
27+
this.canvas = this._createCanvas();
28+
this.mapContainer = map.getCanvasContainer();
29+
this.mapContainer.appendChild(this.canvas);
30+
31+
this.m_fabricCanvas = this._createFabricCanvas(this.canvas, fabricClass, m_fabricOptions);
32+
33+
this.m_fabricCanvas.setMap(this.map);
34+
// this.mapContainer.style.perspective = this.map.transform.cameraToCenterDistance + 'px';
35+
36+
this.bindEvent();
37+
this._reset();
38+
FabricLayer.instance=this
4339
}
4440

4541
/**
@@ -68,15 +64,15 @@ export class FabricLayer {
6864
this.map.dragRotate.enable();
6965
this.map.dragPan.enable();
7066
});
71-
return m_fabricCanvas
67+
return m_fabricCanvas;
7268
}
7369
/**
7470
* @function: Module:FabricLayer.pototype.getFabricCanvas
7571
* @description: 获取fabricCanvas
7672
* @return {Object} fabricCanvas
7773
*/
78-
getFabricCanvas(){
79-
return this.m_fabricCanvas
74+
getFabricCanvas() {
75+
return this.m_fabricCanvas;
8076
}
8177

8278
//-----------------------------------Event Methods----------------------------------------
@@ -87,7 +83,7 @@ export class FabricLayer {
8783
bindEvent() {
8884
var map = this.map;
8985
//下面几个是mapboxgl专属事件,clickEvent和mousemoveEvent是mapv内部自带的方法不放出来
90-
this.innerMove= throttle(this.moveEvent,25,this)
86+
this.innerMove = throttle(this.moveEvent, 25, this);
9187
this.innerMoveStart = this.moveStartEvent.bind(this);
9288
this.innerMoveEnd = this.moveEndEvent.bind(this);
9389

@@ -144,7 +140,7 @@ export class FabricLayer {
144140
map.off('moveend', this.innerMoveEnd);
145141
}
146142

147-
moveEvent(){
143+
moveEvent() {
148144
this._reset();
149145
}
150146
moveStartEvent() {
@@ -153,7 +149,7 @@ export class FabricLayer {
153149

154150
moveEndEvent() {
155151
this._reset();
156-
this._visiable()
152+
this._visiable();
157153
}
158154

159155
zoomStartEvent() {
@@ -166,7 +162,6 @@ export class FabricLayer {
166162

167163
rotateStartEvent() {
168164
this._unvisiable();
169-
170165
}
171166
rotateEndEvent() {
172167
this._reset();
@@ -175,7 +170,6 @@ export class FabricLayer {
175170

176171
pitchStartEvent() {
177172
this._unvisiable();
178-
179173
}
180174
pitchEndEvent() {
181175
this._reset();
@@ -189,7 +183,7 @@ export class FabricLayer {
189183
removeEvent() {
190184
try {
191185
this.mapContainer.removeChild(this.canvas);
192-
}catch (e) {}
186+
} catch (e) {}
193187
}
194188

195189
/**
@@ -245,18 +239,18 @@ export class FabricLayer {
245239

246240
/**
247241
* @function: Module:FabricLayer.pototype.resizeCanvas
248-
* @description: 刷新canvas
242+
* @description: 刷新canvas
249243
*/
250244
resizeCanvas() {
251245
this.mapContainer.style.perspective = this.map.transform.cameraToCenterDistance + 'px';
252246
if (this.canvas == undefined || this.canvas == null) return;
253247

254248
const x = parseInt(this.map.getCanvas().style.width) * this.devicePixelRatio;
255249
const y = parseInt(this.map.getCanvas().style.height) * this.devicePixelRatio;
250+
256251
this.m_fabricCanvas.setCanvasDimensionsSize({ height: y, width: x });
257252

258-
259-
const topLeft ={x:0,y:0};
253+
const topLeft = { x: 0, y: 0 };
260254

261255
// 偏移canvas
262256
this._applyPosition(this.m_fabricCanvas.lowerCanvasEl, topLeft);
@@ -280,7 +274,7 @@ export class FabricLayer {
280274
fixPosition() {}
281275

282276
onResize() {}
283-
277+
284278
/**
285279
* @function: Module:FabricLayer.pototype.render
286280
* @description: 渲染
@@ -292,12 +286,17 @@ export class FabricLayer {
292286

293287
/**
294288
* @function: Module:FabricLayer.pototype.destroy
295-
* @description:
289+
* @description:
296290
*/
297291
destroy() {
298-
this.unbindEvent();
299-
this.mapContainer.removeChild(this.canvas);
300-
this.m_fabricCanvas = null;
292+
if (FabricLayer.instance) {
293+
FabricLayer.instance.m_fabricCanvas.dispose();
294+
FabricLayer.instance.unbindEvent();
295+
FabricLayer.instance.m_fabricCanvas = null;
296+
FabricLayer.instance.canvas=null
297+
FabricLayer.instance=null
298+
}
299+
301300
}
302301
}
303302

src/service/2DPlot/PlotLayer2DGroup.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @Author: zk
55
* @Date: 2022-05-13 11:01:10
66
* @LastEditors: zk
7-
* @LastEditTime: 2022-07-06 12:11:07
7+
* @LastEditTime: 2022-07-13 12:47:38
88
*/
99

1010
import { fabric } from 'fabric';
@@ -17,23 +17,18 @@ export const PlotLayer2DGroup = fabric.util.createClass(fabric.Canvas, {
1717
selection: false,
1818
/**
1919
* @function: Module:PlotLayer2DGroup
20-
* @description: 构造
21-
* @param {HTMLCanvasElement} el
20+
* @description: 构造
21+
* @param {HTMLCanvasElement} el
2222
* @param {Object} options
2323
* @return {*}
2424
*/
2525
initialize: function (el, options) {
26-
if (!PlotLayer2DGroup.instance) {
27-
this.callSuper('initialize', el, options);
28-
PlotLayer2DGroup.instance = this;
29-
// 工具图层
30-
this._utilPlotCanvas = new PlotLayer2D();
31-
this._utilPlotCanvas.editable=true
32-
this._plotCanvasLayers = [];
33-
this.addLayer(this._utilPlotCanvas);
34-
} else {
35-
return PlotLayer2DGroup.instance;
36-
}
26+
this.callSuper('initialize', el, options);
27+
// 工具图层
28+
this._utilPlotCanvas = new PlotLayer2D();
29+
this._utilPlotCanvas.editable=true
30+
this._plotCanvasLayers = [];
31+
this.addLayer(this._utilPlotCanvas);
3732
},
3833
setMap(map){
3934
this._map=map

0 commit comments

Comments
 (0)