|
| 1 | +/* |
| 2 | + * @class: Module:FabricLayer |
| 3 | + * @Description: fabric-地图框架联动图层 |
| 4 | + * @Author: zk |
| 5 | + * @Date: 2022-05-13 11:22:56 |
| 6 | + * @LastEditors: zk |
| 7 | + * @LastEditTime: 2022-05-24 11:23:51 |
| 8 | + */ |
| 9 | +import mapboxgl from '@mapgis/mapbox-gl'; |
| 10 | +import { PlotMapCoordSys } from './fabric/PlotMapCoordSys'; |
| 11 | +import { throttle } from '../util/Util'; |
| 12 | + |
| 13 | +export class FabricLayer { |
| 14 | + /** |
| 15 | + * @function: Module:FabricLayer |
| 16 | + * @description: 构造 |
| 17 | + * @param {Object} map mapboxglmap |
| 18 | + * @param {Function} fabricClass fabric图层 |
| 19 | + * @param {Object} fabricOptions fabric图层选项 |
| 20 | + */ |
| 21 | + 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 | + |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @function: Module:FabricLayer.pototype.initDevicePixelRatio |
| 47 | + * @description: 初始化dpi |
| 48 | + */ |
| 49 | + initDevicePixelRatio() { |
| 50 | + this.devicePixelRatio = window.devicePixelRatio || 1; |
| 51 | + } |
| 52 | + /** |
| 53 | + * @function: Module:FabricLayer.pototype._createFabricCanvas |
| 54 | + * @description: 创建fabric图层 |
| 55 | + * @param {Object} fabricClass fabric canvas类 |
| 56 | + * @param {Object} fabricOptions fabric options |
| 57 | + * @return {Object} fabric图层 |
| 58 | + */ |
| 59 | + _createFabricCanvas(canvas, fabricClass, fabricOptions) { |
| 60 | + const m_fabricCanvas = new fabricClass(canvas, fabricOptions); |
| 61 | + m_fabricCanvas.setCoordSys(new PlotMapCoordSys(this.map)); |
| 62 | + |
| 63 | + m_fabricCanvas.on('selection:created', () => { |
| 64 | + this.map.dragRotate.disable(); |
| 65 | + this.map.dragPan.disable(); |
| 66 | + }); |
| 67 | + m_fabricCanvas.on('selection:cleared', () => { |
| 68 | + this.map.dragRotate.enable(); |
| 69 | + this.map.dragPan.enable(); |
| 70 | + }); |
| 71 | + return m_fabricCanvas |
| 72 | + } |
| 73 | + /** |
| 74 | + * @function: Module:FabricLayer.pototype.getFabricCanvas |
| 75 | + * @description: 获取fabricCanvas |
| 76 | + * @return {Object} fabricCanvas |
| 77 | + */ |
| 78 | + getFabricCanvas(){ |
| 79 | + return this.m_fabricCanvas |
| 80 | + } |
| 81 | + |
| 82 | + //-----------------------------------Event Methods---------------------------------------- |
| 83 | + /** |
| 84 | + * @function: Module:FabricLayer.pototype.bindEvent |
| 85 | + * @description: 绑定地图事件 |
| 86 | + */ |
| 87 | + bindEvent() { |
| 88 | + var map = this.map; |
| 89 | + //下面几个是mapboxgl专属事件,clickEvent和mousemoveEvent是mapv内部自带的方法不放出来 |
| 90 | + this.innerMove= throttle(this.moveEvent,25,this) |
| 91 | + this.innerMoveStart = this.moveStartEvent.bind(this); |
| 92 | + this.innerMoveEnd = this.moveEndEvent.bind(this); |
| 93 | + |
| 94 | + this.innnerZoomStart = this.zoomStartEvent.bind(this); |
| 95 | + this.innnerZoomEnd = this.zoomEndEvent.bind(this); |
| 96 | + |
| 97 | + this.innnerRotateStart = this.rotateStartEvent.bind(this); |
| 98 | + this.innnerRotateEnd = this.rotateEndEvent.bind(this); |
| 99 | + |
| 100 | + this.innnerPitchStart = this.pitchStartEvent.bind(this); |
| 101 | + this.innnerPitchEnd = this.pitchEndEvent.bind(this); |
| 102 | + |
| 103 | + this.innerResize = this.resizeEvent.bind(this); |
| 104 | + |
| 105 | + this.innerRemove = this.removeEvent.bind(this); |
| 106 | + |
| 107 | + map.on('resize', this.innerResize); |
| 108 | + |
| 109 | + map.on('zoomstart', this.innnerZoomStart); |
| 110 | + map.on('zoomend', this.innnerZoomEnd); |
| 111 | + |
| 112 | + map.on('rotatestart', this.innnerRotateStart); |
| 113 | + map.on('rotateend', this.innnerRotateEnd); |
| 114 | + |
| 115 | + map.on('pitchstart', this.innnerPitchStart); |
| 116 | + map.on('pitchend', this.innnerPitchEnd); |
| 117 | + |
| 118 | + // map.on('move',this.innerMove) |
| 119 | + map.on('movestart', this.innerMoveStart); |
| 120 | + map.on('moveend', this.innerMoveEnd); |
| 121 | + |
| 122 | + this.map.on('remove', this.innerRemove); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @function: Module:FabricLayer.pototype.unbindEvent |
| 127 | + * @description: 解绑地图事件 |
| 128 | + */ |
| 129 | + unbindEvent() { |
| 130 | + var map = this.map; |
| 131 | + map.off('resize', this.innerResize); |
| 132 | + |
| 133 | + map.off('zoomstart', this.innnerZoomStart); |
| 134 | + map.off('zoomend', this.innnerZoomEnd); |
| 135 | + |
| 136 | + map.off('rotatestart', this.innnerRotateStart); |
| 137 | + map.off('rotateend', this.innnerRotateEnd); |
| 138 | + |
| 139 | + map.off('pitchstart', this.innnerPitchStart); |
| 140 | + map.off('pitchend', this.innnerPitchEnd); |
| 141 | + |
| 142 | + // map.off('move',this.innerMove) |
| 143 | + map.off('movestart', this.innerMoveStart); |
| 144 | + map.off('moveend', this.innerMoveEnd); |
| 145 | + } |
| 146 | + |
| 147 | + moveEvent(){ |
| 148 | + this._reset(); |
| 149 | + } |
| 150 | + moveStartEvent() { |
| 151 | + this._unvisiable(); |
| 152 | + } |
| 153 | + |
| 154 | + moveEndEvent() { |
| 155 | + this._reset(); |
| 156 | + this._visiable() |
| 157 | + } |
| 158 | + |
| 159 | + zoomStartEvent() { |
| 160 | + this._unvisiable(); |
| 161 | + } |
| 162 | + zoomEndEvent() { |
| 163 | + this._reset(); |
| 164 | + this._visiable(); |
| 165 | + } |
| 166 | + |
| 167 | + rotateStartEvent() { |
| 168 | + this._unvisiable(); |
| 169 | + |
| 170 | + } |
| 171 | + rotateEndEvent() { |
| 172 | + this._reset(); |
| 173 | + this._visiable(); |
| 174 | + } |
| 175 | + |
| 176 | + pitchStartEvent() { |
| 177 | + this._unvisiable(); |
| 178 | + |
| 179 | + } |
| 180 | + pitchEndEvent() { |
| 181 | + this._reset(); |
| 182 | + this._visiable(); |
| 183 | + } |
| 184 | + |
| 185 | + resizeEvent() { |
| 186 | + this._reset(); |
| 187 | + this._visiable(); |
| 188 | + } |
| 189 | + removeEvent() { |
| 190 | + this.mapContainer.removeChild(this.canvas); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @function: Module:FabricLayer.pototype._createCanvas |
| 195 | + * @description: 创建canvas |
| 196 | + * @return {HTMLCanvasElement} |
| 197 | + */ |
| 198 | + _createCanvas() { |
| 199 | + var canvas = document.createElement('canvas'); |
| 200 | + var devicePixelRatio = this.devicePixelRatio; |
| 201 | + canvas.style.position = 'absolute'; |
| 202 | + canvas.style.top = '0px'; |
| 203 | + canvas.style.left = '0px'; |
| 204 | + canvas.width = parseInt(this.map.getCanvas().style.width) * devicePixelRatio; |
| 205 | + canvas.height = parseInt(this.map.getCanvas().style.height) * devicePixelRatio; |
| 206 | + canvas.getContext('2d').scale(devicePixelRatio, devicePixelRatio); |
| 207 | + canvas.style.width = this.map.getCanvas().style.width; |
| 208 | + canvas.style.height = this.map.getCanvas().style.height; |
| 209 | + return canvas; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @function: Module:FabricLayer.pototype._reset |
| 214 | + * @description: 重置视图 |
| 215 | + */ |
| 216 | + _reset() { |
| 217 | + if (this.canvas === null || this.m_fabricCanvas === null) { |
| 218 | + return; |
| 219 | + } |
| 220 | + this.resizeCanvas(); |
| 221 | + this.fixPosition(); |
| 222 | + this.onResize(); |
| 223 | + this.render(); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * @function: Module:FabricLayer.pototype.draw |
| 228 | + * @description: 绘制 |
| 229 | + */ |
| 230 | + draw() { |
| 231 | + return this._reset(); |
| 232 | + } |
| 233 | + |
| 234 | + _visiable() { |
| 235 | + this.canvas.style.display = 'block'; |
| 236 | + return this; |
| 237 | + } |
| 238 | + |
| 239 | + _unvisiable() { |
| 240 | + this.canvas.style.display = 'none'; |
| 241 | + return this; |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * @function: Module:FabricLayer.pototype.resizeCanvas |
| 246 | + * @description: 刷新canvas |
| 247 | + */ |
| 248 | + resizeCanvas() { |
| 249 | + this.mapContainer.style.perspective = this.map.transform.cameraToCenterDistance + 'px'; |
| 250 | + if (this.canvas == undefined || this.canvas == null) return; |
| 251 | + |
| 252 | + const x = parseInt(this.map.getCanvas().style.width) * this.devicePixelRatio; |
| 253 | + const y = parseInt(this.map.getCanvas().style.height) * this.devicePixelRatio; |
| 254 | + this.m_fabricCanvas.setCanvasDimensionsSize({ height: y, width: x }); |
| 255 | + |
| 256 | + |
| 257 | + const topLeft ={x:0,y:0}; |
| 258 | + |
| 259 | + // 偏移canvas |
| 260 | + this._applyPosition(this.m_fabricCanvas.lowerCanvasEl, topLeft); |
| 261 | + this._applyPosition(this.m_fabricCanvas.upperCanvasEl, topLeft); |
| 262 | + |
| 263 | + this.m_fabricCanvas.renderAll(); |
| 264 | + } |
| 265 | + /** |
| 266 | + * @function: Module:FabricLayer.pototype._applyPosition |
| 267 | + * @description: 偏移fabriccanvas |
| 268 | + * @param {HTMLCanvasElement} canvas |
| 269 | + * @param {{x:number,y:number}} origin |
| 270 | + */ |
| 271 | + _applyPosition(canvas, origin) { |
| 272 | + if (!canvas) return; |
| 273 | + const temp = canvas; |
| 274 | + temp.style.left = origin.x + 'px'; |
| 275 | + temp.style.top = origin.y + 'px'; |
| 276 | + } |
| 277 | + |
| 278 | + fixPosition() {} |
| 279 | + |
| 280 | + onResize() {} |
| 281 | + |
| 282 | + /** |
| 283 | + * @function: Module:FabricLayer.pototype.render |
| 284 | + * @description: 渲染 |
| 285 | + */ |
| 286 | + render() { |
| 287 | + if (!this.canvas || !this.m_fabricCanvas) return; |
| 288 | + this.m_fabricCanvas.requestRenderAll(); |
| 289 | + } |
| 290 | + |
| 291 | + /** |
| 292 | + * @function: Module:FabricLayer.pototype.destroy |
| 293 | + * @description: |
| 294 | + */ |
| 295 | + destroy() { |
| 296 | + this.unbindEvent(); |
| 297 | + this.mapContainer.removeChild(this.canvas); |
| 298 | + this.m_fabricCanvas = null; |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +mapboxgl.zondy.FabricLayer = FabricLayer; |
0 commit comments