@ohuoy/easymap 1.0.21 → 1.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.js +80 -47
- package/dist/example - /345/211/257/346/234/254/bundle.js" +80 -47
- package/dist/example - /345/211/257/346/234/254/index.html" +33 -3
- package/index.js +195 -7
- package/lib/threebox-plugin/src/three.module.js +1 -0
- package/package.json +2 -2
- package/src/components/EasyMapMarker.js +7 -3
- package/src/components/control/TilesBar.js +87 -12
- package/src/components/control/Toobars.js +63 -24
- package/src/components/index.js +9 -2
- package/src/components/layer/AlarmLayer.js +13 -12
- package/src/components/layer/AnimationBarbsLayer.js +13 -12
- package/src/components/layer/AnimationLayer.js +38 -21
- package/src/components/layer/CustomIconLayer.js +6 -5
- package/src/components/layer/ExtrusionLayer.js +71 -66
- package/src/components/layer/MarkerAreaLayer.js +10 -9
- package/src/components/layer/PathLineLayer.js +30 -14
- package/src/components/layer/ScanWallLayer.js +467 -0
- package/src/components/layer/ThreeBoxWallLayer.js +253 -0
- package/src/components/layer/ThreeScanLayer.js +63 -37
- package/src/components/layer/ThreeWallLayer copy 2.js +1104 -0
- package/src/components/layer/ThreeWallLayer copy.js +517 -0
- package/src/components/layer/ThreeWallLayer.js +231 -256
- package/src/components/layer/WallLayer.js +3 -2
- package/src/components/layer/WebGlWallLayer.js +320 -0
- package/src/components/mapOutScreen.js +15 -3
- package/src/utils/CameraSync.js +6 -3
- package/src/utils/mapWatch.js +70 -0
- package/src/utils/util.js +3 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
|
|
2
|
+
import {hasProperty,transform} from '../../utils/util'
|
|
3
|
+
import mapboxgl from '../../../lib/mapbox-gl/dist/mapbox-gl.js';
|
|
4
|
+
import { lnglatsToWorld ,normalizeVertices,flattenVectors,projectToWorld} from '../../utils/mapUtil';
|
|
5
|
+
export default class ScanLayer{
|
|
6
|
+
drawArray = []
|
|
7
|
+
defaultDrawInfo = {
|
|
8
|
+
id:'',
|
|
9
|
+
colors:[],
|
|
10
|
+
coords:[],
|
|
11
|
+
refreshTime:null
|
|
12
|
+
}
|
|
13
|
+
opacity = 0.8
|
|
14
|
+
scale = 2
|
|
15
|
+
limit = 6
|
|
16
|
+
colorFun = ()=>{
|
|
17
|
+
return [0,0,0,0]
|
|
18
|
+
}
|
|
19
|
+
addedFun = null
|
|
20
|
+
constructor(id,_scanInfo) {
|
|
21
|
+
this.id = id;
|
|
22
|
+
this.opacity = Number(config.opacity)
|
|
23
|
+
if(config.colorFun){
|
|
24
|
+
this.colorFun = config.colorFun
|
|
25
|
+
}
|
|
26
|
+
this.type = 'custom';
|
|
27
|
+
this.renderingMode = '2d';
|
|
28
|
+
this.addedFun = config.addedFun
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
onAdd(map, gl) {
|
|
32
|
+
// create GLSL source for vertex shader
|
|
33
|
+
const vertexSource = `
|
|
34
|
+
uniform mat4 u_matrix;
|
|
35
|
+
attribute vec4 a_pos;
|
|
36
|
+
attribute vec4 a_color;
|
|
37
|
+
varying vec4 v_color;
|
|
38
|
+
void main() {
|
|
39
|
+
gl_Position = u_matrix * vec4(a_pos);
|
|
40
|
+
v_color = a_color;
|
|
41
|
+
}`;
|
|
42
|
+
|
|
43
|
+
// create GLSL source for fragment shader
|
|
44
|
+
const fragmentSource = `
|
|
45
|
+
precision mediump float;
|
|
46
|
+
varying vec4 v_color;
|
|
47
|
+
void main() {
|
|
48
|
+
gl_FragColor = v_color;
|
|
49
|
+
}`;
|
|
50
|
+
|
|
51
|
+
// create a vertex shader
|
|
52
|
+
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
|
|
53
|
+
gl.shaderSource(vertexShader, vertexSource);
|
|
54
|
+
gl.compileShader(vertexShader);
|
|
55
|
+
|
|
56
|
+
// create a fragment shader
|
|
57
|
+
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
|
|
58
|
+
gl.shaderSource(fragmentShader, fragmentSource);
|
|
59
|
+
gl.compileShader(fragmentShader);
|
|
60
|
+
|
|
61
|
+
// link the two shaders into a WebGL program
|
|
62
|
+
this.program = gl.createProgram();
|
|
63
|
+
gl.attachShader(this.program, vertexShader);
|
|
64
|
+
gl.attachShader(this.program, fragmentShader);
|
|
65
|
+
gl.linkProgram(this.program);
|
|
66
|
+
|
|
67
|
+
this.aPos = gl.getAttribLocation(this.program, 'a_pos');
|
|
68
|
+
this.gl = gl;
|
|
69
|
+
this.map = map;
|
|
70
|
+
// define vertices of the triangle to be rendered in the custom style layer
|
|
71
|
+
|
|
72
|
+
// create and initialize a WebGLBuffer to store vertex and color data
|
|
73
|
+
// this.buffer = gl.createBuffer();
|
|
74
|
+
// gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
|
|
75
|
+
// gl.bufferData(
|
|
76
|
+
// gl.ARRAY_BUFFER,
|
|
77
|
+
// new Float32Array(),
|
|
78
|
+
// gl.STATIC_DRAW
|
|
79
|
+
// );
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// method fired on each animation frame
|
|
83
|
+
// https://docs.mapbox.com/mapbox-gl-js/api/#map.event:render
|
|
84
|
+
render(gl, matrix) {
|
|
85
|
+
gl.useProgram(this.program);
|
|
86
|
+
gl.uniformMatrix4fv(
|
|
87
|
+
gl.getUniformLocation(this.program, 'u_matrix'),
|
|
88
|
+
false,
|
|
89
|
+
matrix
|
|
90
|
+
);
|
|
91
|
+
//gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
|
|
92
|
+
// gl.enableVertexAttribArray(this.aPos);
|
|
93
|
+
// gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, 0, 0);
|
|
94
|
+
// gl.enable(gl.BLEND);
|
|
95
|
+
// gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
96
|
+
//gl.drawArrays(gl.TRIANGLE_STRIP, 0, 12);
|
|
97
|
+
this.draw()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//
|
|
101
|
+
createBufferData(item){
|
|
102
|
+
let height = 0.0000001
|
|
103
|
+
let arrs = []
|
|
104
|
+
//let colors = []
|
|
105
|
+
for(let cr in item.coords){
|
|
106
|
+
let _next = 1
|
|
107
|
+
if(cr !=0 ){
|
|
108
|
+
let p1 = mapboxgl.MercatorCoordinate.fromLngLat({
|
|
109
|
+
lng: item.coords[cr-_next][0],
|
|
110
|
+
lat: item.coords[cr-_next][1]
|
|
111
|
+
}, 10)
|
|
112
|
+
let p2 = mapboxgl.MercatorCoordinate.fromLngLat({
|
|
113
|
+
lng: item.coords[cr][0],
|
|
114
|
+
lat: item.coords[cr][1]
|
|
115
|
+
}, 10)
|
|
116
|
+
if(p1.x == p2.x && p1.y == p2.y && p1.z== p2.z){
|
|
117
|
+
_next +=1
|
|
118
|
+
}else{
|
|
119
|
+
//创建矩形区域
|
|
120
|
+
for(let _c in item.colors[cr]){
|
|
121
|
+
let _height = height + height *Number(_c)
|
|
122
|
+
//创建垂直矩形区域
|
|
123
|
+
// let rect = [
|
|
124
|
+
// p1.x,p1.y,p1.z,
|
|
125
|
+
// p2.x,p2.y,p2.z,
|
|
126
|
+
// p1.x,p1.y,_height,
|
|
127
|
+
// p2.x,p2.y,_height,
|
|
128
|
+
// ]
|
|
129
|
+
let _colorinfo = item.colors[cr][_c]
|
|
130
|
+
let _color = [_colorinfo[0]/255,_colorinfo[1]/255,_colorinfo[2]/255,_colorinfo[3]]
|
|
131
|
+
arrs.push(p1.x)
|
|
132
|
+
arrs.push(p1.y)
|
|
133
|
+
arrs.push(p1.z)
|
|
134
|
+
arrs.push(_color[0])
|
|
135
|
+
arrs.push(_color[1])
|
|
136
|
+
arrs.push(_color[2])
|
|
137
|
+
arrs.push(_color[3])
|
|
138
|
+
arrs.push(p2.x)
|
|
139
|
+
arrs.push(p2.y)
|
|
140
|
+
arrs.push(p2.z)
|
|
141
|
+
arrs.push(_color[0])
|
|
142
|
+
arrs.push(_color[1])
|
|
143
|
+
arrs.push(_color[2])
|
|
144
|
+
arrs.push(_color[3])
|
|
145
|
+
arrs.push(p1.x)
|
|
146
|
+
arrs.push(p1.y)
|
|
147
|
+
arrs.push(_height)
|
|
148
|
+
arrs.push(_color[0])
|
|
149
|
+
arrs.push(_color[1])
|
|
150
|
+
arrs.push(_color[2])
|
|
151
|
+
arrs.push(_color[3])
|
|
152
|
+
arrs.push(p2.x)
|
|
153
|
+
arrs.push(p2.y)
|
|
154
|
+
arrs.push(_height)
|
|
155
|
+
arrs.push(_color[0])
|
|
156
|
+
arrs.push(_color[1])
|
|
157
|
+
arrs.push(_color[2])
|
|
158
|
+
arrs.push(_color[3])
|
|
159
|
+
}
|
|
160
|
+
_next=1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return arrs;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
createData(dataList){
|
|
168
|
+
let distance = dataList[0].distance;
|
|
169
|
+
let noDataLenth = distance[0] / distance[1]
|
|
170
|
+
let limitIndex = this.limit / distance[1]
|
|
171
|
+
limitIndex = limitIndex >= distance[2] -1 ? distance[2] -1 : limitIndex
|
|
172
|
+
// dataList = dataList.slice(0,5)
|
|
173
|
+
let len = limitIndex + noDataLenth;
|
|
174
|
+
let index = 0;
|
|
175
|
+
for(let i in dataList){
|
|
176
|
+
i = Number(i)
|
|
177
|
+
let item = dataList[i]
|
|
178
|
+
let lng = item.lng;
|
|
179
|
+
let lat = item.lat;
|
|
180
|
+
let position = projectToWorld([lng,lat])
|
|
181
|
+
if(i < dataList.length -1){
|
|
182
|
+
let next = dataList[i+1]
|
|
183
|
+
if(lng == next.lng && lat == next.lat){
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
for(let j in item.data){
|
|
187
|
+
j = Number(j)
|
|
188
|
+
if(j > -noDataLenth && j <= limitIndex){
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
begin(dataList){
|
|
197
|
+
this.createData(dataList)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
drawArrayItem(item){
|
|
201
|
+
if(!item?.coords || !item.colors){
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
// if(!item?.text){
|
|
205
|
+
// this.removeText(item)
|
|
206
|
+
// }
|
|
207
|
+
// if(item?.text?.type == 'layer'){
|
|
208
|
+
// this.drawTextByLayer(item)
|
|
209
|
+
// }
|
|
210
|
+
// if(item?.text){
|
|
211
|
+
// this.drawText(item)
|
|
212
|
+
// }
|
|
213
|
+
let gl = this.gl;
|
|
214
|
+
if(!item?.buffer) {
|
|
215
|
+
item.buffer = gl.createBuffer();
|
|
216
|
+
let _data = this.createBufferData(item);
|
|
217
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer);
|
|
218
|
+
let bufferDataArray = new Float32Array(_data);
|
|
219
|
+
item.bufferDataLength = _data.length;
|
|
220
|
+
gl.bufferData(
|
|
221
|
+
gl.ARRAY_BUFFER,
|
|
222
|
+
bufferDataArray,
|
|
223
|
+
gl.STATIC_DRAW
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
item.bufferColor = gl.createBuffer();
|
|
227
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, item.bufferColor);
|
|
228
|
+
gl.bufferData(
|
|
229
|
+
gl.ARRAY_BUFFER,
|
|
230
|
+
bufferDataArray,
|
|
231
|
+
gl.STATIC_DRAW
|
|
232
|
+
);
|
|
233
|
+
item.byteSize = bufferDataArray.BYTES_PER_ELEMENT
|
|
234
|
+
item.aColor = gl.getAttribLocation(this.program, 'a_color');
|
|
235
|
+
// 每帧都要绑定 VBO,并启用 vertexAttributes、设置 vertexAttributes 的参数
|
|
236
|
+
//gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer)
|
|
237
|
+
// gl.enableVertexAttribArray(this.aPos)
|
|
238
|
+
// gl.enableVertexAttribArray(this.aUv)
|
|
239
|
+
// gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, 20, 0)
|
|
240
|
+
// gl.vertexAttribPointer(this.aUv, 2, gl.FLOAT, false, 20, 12)
|
|
241
|
+
}
|
|
242
|
+
// item.buffer = gl.createBuffer()
|
|
243
|
+
// let bufferData = this.createBufferData(item)
|
|
244
|
+
// item.dataLength = bufferData.length
|
|
245
|
+
// gl.bufferData(
|
|
246
|
+
// gl.ARRAY_BUFFER,
|
|
247
|
+
// new Float32Array([0.8250514363610296,
|
|
248
|
+
// 0.41078121183273425,
|
|
249
|
+
// 2.99947200815501e-7,
|
|
250
|
+
// 0.8250521974715219,
|
|
251
|
+
// 0.40078065150822717,
|
|
252
|
+
// 2.999477853115788e-7,
|
|
253
|
+
// 0.8250514363610296,
|
|
254
|
+
// 0.41078121183273425,
|
|
255
|
+
// 0.02,
|
|
256
|
+
// 0.8250521974715219,
|
|
257
|
+
// 0.40078065150822717,
|
|
258
|
+
// 0.02]),
|
|
259
|
+
// gl.STATIC_DRAW
|
|
260
|
+
// )
|
|
261
|
+
|
|
262
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer);
|
|
263
|
+
gl.enableVertexAttribArray(this.aPos);
|
|
264
|
+
gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, item.byteSize *7, 0);
|
|
265
|
+
|
|
266
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, item.bufferColor);
|
|
267
|
+
gl.enableVertexAttribArray(item.aColor);
|
|
268
|
+
gl.vertexAttribPointer(item.aColor, 4, gl.FLOAT, false, item.byteSize * 7, item.byteSize*3);
|
|
269
|
+
|
|
270
|
+
gl.enable(gl.BLEND);
|
|
271
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
272
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, item.bufferDataLength);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// render(gl, matrix) {
|
|
276
|
+
// if(this.drawArray.length == 0 || !this.program) return;
|
|
277
|
+
// gl.useProgram(this.program)
|
|
278
|
+
// // 每帧都要传递 uniform
|
|
279
|
+
// gl.uniformMatrix4fv(
|
|
280
|
+
// gl.getUniformLocation(this.program, 'u_matrix'),
|
|
281
|
+
// false,
|
|
282
|
+
// matrix
|
|
283
|
+
// )
|
|
284
|
+
// //gl.uniform1f(gl.getUniformLocation(this.program, 'opa'),this.opacity)
|
|
285
|
+
// this.draw()
|
|
286
|
+
|
|
287
|
+
// // gl.useProgram(this.program)
|
|
288
|
+
// // // 每帧都要传递 uniform
|
|
289
|
+
// // gl.uniformMatrix4fv(
|
|
290
|
+
// // gl.getUniformLocation(this.program, 'u_matrix'),
|
|
291
|
+
// // false,
|
|
292
|
+
// // matrix
|
|
293
|
+
// // )
|
|
294
|
+
// // // 每帧都要绑定纹理参数
|
|
295
|
+
// // gl.bindTexture(gl.TEXTURE_2D, this.texture)
|
|
296
|
+
// // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
|
297
|
+
// // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
|
298
|
+
// // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
|
299
|
+
// // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
|
300
|
+
|
|
301
|
+
// // // 每帧都要绑定 VBO,并启用 vertexAttributes、设置 vertexAttributes 的参数
|
|
302
|
+
// // gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
// // 如果你用不着透明度,可以不执行这两行
|
|
306
|
+
// // gl.enable(gl.BLEND)
|
|
307
|
+
// // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
|
|
308
|
+
|
|
309
|
+
// // 触发绘制
|
|
310
|
+
// //gl.drawArrays(gl.TRIANGLES, 0, 6)
|
|
311
|
+
// // 每帧都要指定用哪个着色器程序
|
|
312
|
+
// if(this.renderBack!=null) {
|
|
313
|
+
// if(this.renderBack) this.renderBack()
|
|
314
|
+
// }
|
|
315
|
+
// //this.map.triggerRepaint();
|
|
316
|
+
// }
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
@@ -12,7 +12,7 @@ const mapOutScreen = class {
|
|
|
12
12
|
progress=()=>{}
|
|
13
13
|
constructor(center,distance,config = {
|
|
14
14
|
target:null,
|
|
15
|
-
style: 'http://
|
|
15
|
+
style: 'http://dbcenter.gyquantum.com/easymap/styles/light',
|
|
16
16
|
type:'blob',
|
|
17
17
|
pix:{
|
|
18
18
|
w:1024,
|
|
@@ -29,7 +29,7 @@ const mapOutScreen = class {
|
|
|
29
29
|
h:1024
|
|
30
30
|
},
|
|
31
31
|
opacity:0.7,
|
|
32
|
-
style: 'http://
|
|
32
|
+
style: 'http://dbcenter.gyquantum.com/easymap/styles/light',
|
|
33
33
|
callback:()=>{},
|
|
34
34
|
},config)
|
|
35
35
|
|
|
@@ -77,6 +77,7 @@ const mapOutScreen = class {
|
|
|
77
77
|
})
|
|
78
78
|
})
|
|
79
79
|
this.map = scene.map;
|
|
80
|
+
|
|
80
81
|
return new Promise((resolve, reject) => {
|
|
81
82
|
scene.map.on('load',async ()=>{
|
|
82
83
|
if(!imageList || imageList.length == 0){
|
|
@@ -169,7 +170,18 @@ const mapOutScreen = class {
|
|
|
169
170
|
imgItemEl.onload = (b)=>{
|
|
170
171
|
this.ctx.drawImage(b.currentTarget,0,0,this.canvas.width,this.canvas.height)
|
|
171
172
|
this.ctx.font = point.addTime.font ? point.addTime.font : '32px SimHei'
|
|
172
|
-
this.ctx.fillText(point.addTime.text,point.addTime.left,point.addTime.top)
|
|
173
|
+
this.ctx.fillText(point.addTime.text,point.addTime.left,point.addTime.top)
|
|
174
|
+
if(point.rect && point.rect.x && point.rect.y) {
|
|
175
|
+
this.ctx.strokeStyle = 'red';
|
|
176
|
+
this.ctx.lineWidth = 5;
|
|
177
|
+
this.ctx.globalAlpha = 1;
|
|
178
|
+
let x = (point.distance * 1000 + point.rect.x *1)/point.distance /2000 *this.canvas.width ;
|
|
179
|
+
let y = (point.distance * 1000 - point.rect.y *1 )/point.distance / 2000 *this.canvas.height ;
|
|
180
|
+
let size = point.rect.size?point.rect.size:80;
|
|
181
|
+
let startX = x -size/2,startY = y - size/2
|
|
182
|
+
this.ctx.strokeRect(startX,startY,size,size)
|
|
183
|
+
this.ctx.save()
|
|
184
|
+
}
|
|
173
185
|
resolve(this.canvas.toDataURL())
|
|
174
186
|
}
|
|
175
187
|
})
|
package/src/utils/CameraSync.js
CHANGED
|
@@ -185,17 +185,20 @@ export default class {
|
|
|
185
185
|
let y = t.y || t.point.y;
|
|
186
186
|
translateMap.makeTranslation(-x, y, 0);
|
|
187
187
|
rotateMap.makeRotationZ(Math.PI);
|
|
188
|
-
|
|
189
188
|
this.world.matrix = new THREE.Matrix4()
|
|
190
189
|
.premultiply(rotateMap)
|
|
191
190
|
.premultiply(this.state.translateCenter)
|
|
192
191
|
.premultiply(scale)
|
|
193
192
|
.premultiply(translateMap)
|
|
194
193
|
|
|
194
|
+
// console.log(nearZ,farZ)
|
|
195
|
+
// console.log(this.camera.position)
|
|
195
196
|
// utils.prettyPrintMatrix(this.camera.projectionMatrix.elements);
|
|
196
|
-
this.map.fire('CameraSynced', { detail: { nearZ: nearZ, farZ: farZ, pitch: t._pitch, angle: t.angle, furthestDistance: furthestDistance, cameraToCenterDistance: this.cameraToCenterDistance, t: this.map.transform, tbProjMatrix: this.camera.projectionMatrix.elements, tbWorldMatrix: this.world.matrix.elements, cameraSyn: this } });
|
|
197
|
+
this.map.fire('CameraSynced', { detail: { nearZ: nearZ, farZ: farZ , pitch: t._pitch, angle: t.angle, furthestDistance: furthestDistance, cameraToCenterDistance: this.cameraToCenterDistance, t: this.map.transform, tbProjMatrix: this.camera.projectionMatrix.elements, tbWorldMatrix: this.world.matrix.elements, cameraSyn: this } });
|
|
197
198
|
|
|
198
199
|
}
|
|
199
200
|
|
|
200
201
|
|
|
201
|
-
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
|
|
2
|
+
var watchList = [];
|
|
3
|
+
|
|
4
|
+
let cb;
|
|
5
|
+
const targetMap = new WeakMap();
|
|
6
|
+
/**
|
|
7
|
+
* @description 实现依赖收集,为属性和其对应的回调函数建立关联
|
|
8
|
+
* @param {Object} target 目标对象
|
|
9
|
+
* @param {any} key 当前被收集的属性
|
|
10
|
+
*/
|
|
11
|
+
function collect(target, key) {
|
|
12
|
+
const effect = cb; // 从全局获取当前依赖对应的回调
|
|
13
|
+
if (effect) {
|
|
14
|
+
// 建立target,key和effect之间映射关系
|
|
15
|
+
let depMap = targetMap.get(target);
|
|
16
|
+
// 初始化时,该对象所对应的键值对还不存在,则创建一个map
|
|
17
|
+
if (!depMap) {
|
|
18
|
+
depMap = new Map();
|
|
19
|
+
targetMap.set(target, depMap);
|
|
20
|
+
}
|
|
21
|
+
// 获取该属性对应的的Set
|
|
22
|
+
let deps = depMap.get(key);
|
|
23
|
+
if (!deps) {
|
|
24
|
+
deps = new Set();
|
|
25
|
+
depMap.set(key, deps);
|
|
26
|
+
}
|
|
27
|
+
deps.add(effect); // 给当前属性添加一个回调
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
function update(target, key) {
|
|
33
|
+
// 根据target和key获取对应的set
|
|
34
|
+
// 循环执行
|
|
35
|
+
const depMap = targetMap.get(target);
|
|
36
|
+
if (!depMap) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const deps = depMap.get(key);
|
|
40
|
+
deps.forEach((dep) => dep());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function observe(obj){
|
|
44
|
+
if (typeof obj !== "object") {
|
|
45
|
+
return obj;
|
|
46
|
+
}
|
|
47
|
+
return new Proxy(obj,{
|
|
48
|
+
get(target, key, receiver) {
|
|
49
|
+
//console.log(key + '被收集');
|
|
50
|
+
const res = Reflect.get(target, key, receiver);
|
|
51
|
+
// 依赖收集
|
|
52
|
+
collect(target, key);
|
|
53
|
+
// 深层响应化
|
|
54
|
+
return typeof obj === "object" ? observe(res) : res;
|
|
55
|
+
},
|
|
56
|
+
set(target, key, val, receiver) {
|
|
57
|
+
//console.log("set", key);
|
|
58
|
+
const res = Reflect.set(target, key, val, receiver);
|
|
59
|
+
// 侦听到数据变化,执行相应回调
|
|
60
|
+
update(target, key);
|
|
61
|
+
return res;
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function esMapWatchEffect(fn) {
|
|
67
|
+
cb = fn;
|
|
68
|
+
fn();// 触发依赖收集
|
|
69
|
+
cb = null;
|
|
70
|
+
}
|