Skip to content

Commit fc6e8f2

Browse files
authored
code clean up (#1322)
* code clean up * happy lint
1 parent a1993b1 commit fc6e8f2

File tree

8 files changed

+64
-108
lines changed

8 files changed

+64
-108
lines changed

src/core/Browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IS_NODE } from './util';
1+
import { IS_NODE } from './util/env';
22

33
let Browser = {};
44

src/geo/Position.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNil } from '../core/util';
1+
import { isNil } from '../core/util/common';
22

33
/**
44
* Abstract parent class for Point and Coordinate

src/geo/Size.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Point from './Point';
2-
import { isNumber } from '../core/util';
2+
import { isNumber } from '../core/util/common';
33

44
/**
55
* Represents a size.

src/map/Map.Camera.js

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -214,77 +214,56 @@ Map.include(/** @lends Map.prototype */{
214214
* @private
215215
* @function
216216
*/
217-
_pointToContainerPoint: function () {
217+
_pointToContainerPoint: function (point, zoom, altitude = 0, out) {
218+
if (!out) {
219+
out = new Point(0, 0);
220+
}
221+
point = this._pointToPoint(point, zoom, out);
222+
const isTransforming = this.isTransforming();
223+
const res = this._getResolution(zoom) / this._getResolution();
224+
let centerPoint;
225+
if (!isTransforming && !altitude) {
226+
centerPoint = this._prjToPoint(this._getPrjCenter(), undefined, TEMP_COORD);
227+
}
228+
this._toContainerPoint(out, isTransforming, res, altitude, centerPoint);
229+
return out;
230+
},
231+
232+
/**
233+
*Batch conversion for better performance
234+
*/
235+
_pointsToContainerPoints: function (points, zoom, altitudes = []) {
236+
const altitudeIsArray = Array.isArray(altitudes);
237+
const isTransforming = this.isTransforming();
238+
const res = this._getResolution(zoom) / this._getResolution();
239+
const centerPoint = this._prjToPoint(this._getPrjCenter(), undefined, TEMP_COORD);
240+
const pts = [];
241+
for (let i = 0, len = points.length; i < len; i++) {
242+
const point = points[i].copy()._multi(res);
243+
const altitude = altitudeIsArray ? (altitudes[i] || 0) : altitudes;
244+
this._toContainerPoint(point, isTransforming, res, altitude, centerPoint);
245+
pts.push(point);
246+
}
247+
return pts;
248+
},
249+
250+
_toContainerPoint: function () {
218251
const a = [0, 0, 0];
219-
return function (point, zoom, altitude = 0, out) {
220-
point = this._pointToPoint(point, zoom, out);
221-
if (this.isTransforming() || altitude) {
222-
//convert altitude at zoom to current zoom
223-
altitude *= this._getResolution(zoom) / this._getResolution();
252+
return function (out, isTransforming, res, altitude, centerPoint) {
253+
const w2 = this.width / 2, h2 = this.height / 2;
254+
if (isTransforming || altitude) {
255+
altitude *= res;
224256
const scale = this._glScale;
225-
set(a, point.x * scale, point.y * scale, altitude * scale);
226-
257+
set(a, out.x * scale, out.y * scale, altitude * scale);
227258
const t = this._projIfBehindCamera(a, this.cameraPosition, this.cameraForward);
228259
applyMatrix(t, t, this.projViewMatrix);
229-
230-
const w2 = this.width / 2, h2 = this.height / 2;
231-
t[0] = (t[0] * w2) + w2;
232-
t[1] = -(t[1] * h2) + h2;
233-
if (out) {
234-
out.x = t[0];
235-
out.y = t[1];
236-
return out;
237-
}
238-
return new Point(t[0], t[1]);
260+
out.x = (t[0] * w2) + w2;
261+
out.y = -(t[1] * h2) + h2;
239262
} else {
240-
const centerPoint = this._prjToPoint(this._getPrjCenter(), undefined, TEMP_COORD);
241-
if (out) {
242-
out.x = point.x;
243-
out.y = point.y;
244-
} else {
245-
out = point;
246-
}
247263
out._sub(centerPoint.x, centerPoint.y);
248264
out.set(out.x, -out.y);
249-
return out._add(this.width / 2, this.height / 2);
250-
}
251-
};
252-
}(),
253-
254-
/**
255-
*Batch conversion for better performance
256-
*/
257-
_pointsToContainerPoints: function () {
258-
const a = [0, 0, 0];
259-
return function (points, zoom, altitudes = []) {
260-
const altitudeIsArray = Array.isArray(altitudes);
261-
const isTransforming = this.isTransforming();
262-
const res = this._getResolution(zoom) / this._getResolution();
263-
const w2 = this.width / 2, h2 = this.height / 2;
264-
const centerPoint = this._prjToPoint(this._getPrjCenter(), undefined, TEMP_COORD);
265-
const pts = [];
266-
for (let i = 0, len = points.length; i < len; i++) {
267-
const point = points[i].copy()._multi(res);
268-
let altitude = altitudeIsArray ? (altitudes[i] || 0) : altitudes;
269-
if (isTransforming || altitude) {
270-
altitude *= res;
271-
const scale = this._glScale;
272-
set(a, point.x * scale, point.y * scale, altitude * scale);
273-
const t = this._projIfBehindCamera(a, this.cameraPosition, this.cameraForward);
274-
applyMatrix(t, t, this.projViewMatrix);
275-
t[0] = (t[0] * w2) + w2;
276-
t[1] = -(t[1] * h2) + h2;
277-
point.x = t[0];
278-
point.y = t[1];
279-
pts.push(point);
280-
} else {
281-
const out = point;
282-
out._sub(centerPoint.x, centerPoint.y);
283-
out.set(out.x, -out.y);
284-
pts.push(out._add(w2, h2));
285-
}
265+
out._add(w2, h2);
286266
}
287-
return pts;
288267
};
289268
}(),
290269

src/map/Map.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import Coordinate from '../geo/Coordinate';
2222
import Layer from '../layer/Layer';
2323
import Renderable from '../renderer/Renderable';
2424
import SpatialReference from './spatial-reference/SpatialReference';
25-
import { set } from '../core/util/vec3';
26-
import { applyMatrix } from '../core/util/math';
2725

2826
const TEMP_COORD = new Coordinate(0, 0);
2927
/**
@@ -2162,35 +2160,15 @@ Map.include(/** @lends Map.prototype */{
21622160
const resolution = this._getResolution(zoom);
21632161
const res = resolution / this._getResolution();
21642162
const projection = this.getProjection();
2165-
const tempOut = [0, 0, 0];
21662163
const prjOut = new Coordinate(0, 0);
21672164
const isTransforming = this.isTransforming();
21682165
const centerPoint = this._prjToPoint(this._getPrjCenter(), undefined, TEMP_COORD);
2169-
const w = this.width / 2, h = this.height / 2;
21702166
for (let i = 0, len = coordinates.length; i < len; i++) {
21712167
const pCoordinate = projection.project(coordinates[i], prjOut);
21722168
let point = transformation.transform(pCoordinate, resolution);
21732169
point = point._multi(res);
2174-
if (isTransforming) {
2175-
//convert altitude at zoom to current zoom
2176-
// altitude=0;
2177-
const scale = this._glScale;
2178-
set(tempOut, point.x * scale, point.y * scale, 0);
2179-
const t = this._projIfBehindCamera(tempOut, this.cameraPosition, this.cameraForward);
2180-
applyMatrix(t, t, this.projViewMatrix);
2181-
2182-
const w2 = w, h2 = h;
2183-
t[0] = (t[0] * w2) + w2;
2184-
t[1] = -(t[1] * h2) + h2;
2185-
point.x = t[0];
2186-
point.y = t[1];
2187-
pts.push(point);
2188-
} else {
2189-
const out = point;
2190-
out._sub(centerPoint.x, centerPoint.y);
2191-
out.set(out.x, -out.y);
2192-
pts.push(out._add(w, h));
2193-
}
2170+
this._toContainerPoint(point, isTransforming, res, 0, centerPoint);
2171+
pts.push(point);
21942172
}
21952173
return pts;
21962174
};

src/renderer/geometry/Painter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class Painter extends Class {
324324
TEMP_BBOX.miny = miny;
325325
TEMP_BBOX.maxx = maxx;
326326
TEMP_BBOX.maxy = maxy;
327-
this.__bbox = TEMP_BBOX;
327+
this._containerBbox = TEMP_BBOX;
328328
return cPoints;
329329
}
330330

src/renderer/geometry/VectorRenderer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ const TEMP_WITHIN = {
1616
};
1717
// bbox in pixel
1818
function isWithinPixel(painter) {
19-
if (!painter || (!painter.__bbox)) {
19+
if (!painter || !painter._containerBbox) {
2020
TEMP_WITHIN.within = false;
2121
} else {
2222
TEMP_WITHIN.within = false;
23-
const { minx, miny, maxx, maxy } = painter.__bbox;
23+
const { minx, miny, maxx, maxy } = painter._containerBbox;
2424
const offsetx = Math.abs(maxx - minx);
2525
const offsety = Math.abs(maxy - miny);
2626
if (offsetx <= 1 && offsety <= 1) {
2727
TEMP_WITHIN.within = true;
2828
TEMP_WITHIN.center[0] = (minx + maxx) / 2;
2929
TEMP_WITHIN.center[1] = (miny + maxy) / 2;
3030
}
31-
delete painter.__bbox;
31+
delete painter._containerBbox;
3232
}
3333
return TEMP_WITHIN;
3434
}

src/renderer/layer/vectorlayer/VectorLayerCanvasRenderer.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ const TEMP_EXTENT = new PointExtent();
88
const TEMP_VEC3 = [];
99
const TEMP_FIXEDEXTENT = new PointExtent();
1010
const PLACEMENT_CENTER = 'center';
11-
const TEMP_POINTS = [];
12-
const TEMP_ALTITUDES = [];
13-
const TEMP_GEOS = [];
11+
1412
/**
1513
* @classdesc
1614
* Renderer class based on HTML5 Canvas2D for VectorLayers
@@ -248,8 +246,9 @@ class VectorLayerRenderer extends OverlayLayerCanvasRenderer {
248246
// 优化前 11fps
249247
// 优化后 15fps
250248
_batchConversionMarkers(glZoom) {
251-
//减少临时变量的使用
252-
TEMP_ALTITUDES.length = TEMP_GEOS.length = TEMP_POINTS.length = 0;
249+
const cPoints = [];
250+
const markers = [];
251+
const altitudes = [];
253252
const altitudeCache = {};
254253
//Traverse all Geo
255254
let idx = 0;
@@ -264,25 +263,25 @@ class VectorLayerRenderer extends OverlayLayerCanvasRenderer {
264263
const point = painter.getRenderPoints(PLACEMENT_CENTER)[0][0];
265264
const altitude = geo.getAltitude();
266265
//减少方法的调用
267-
if (altitudeCache[altitude] == null) {
266+
if (altitudeCache[altitude] === undefined) {
268267
altitudeCache[altitude] = painter.getAltitude();
269268
}
270-
TEMP_POINTS[idx] = point;
271-
TEMP_ALTITUDES[idx] = altitudeCache[altitude];
272-
TEMP_GEOS[idx] = geo;
269+
cPoints[idx] = point;
270+
altitudes[idx] = altitudeCache[altitude];
271+
markers[idx] = geo;
273272
idx++;
274273
}
275274
}
276275
if (idx === 0) {
277276
return [];
278277
}
279278
const map = this.getMap();
280-
const pts = map._pointsToContainerPoints(TEMP_POINTS, glZoom, TEMP_ALTITUDES);
279+
const pts = map._pointsToContainerPoints(cPoints, glZoom, altitudes);
281280
const containerExtent = map.getContainerExtent();
282281
const { xmax, ymax, xmin, ymin } = containerExtent;
283-
const symbolkeyMap = {};
284-
for (let i = 0, len = TEMP_GEOS.length; i < len; i++) {
285-
const geo = TEMP_GEOS[i];
282+
const extentCache = {};
283+
for (let i = 0, len = markers.length; i < len; i++) {
284+
const geo = markers[i];
286285
geo._cPoint = pts[i];
287286
const { x, y } = pts[i];
288287
//Is the point in view
@@ -293,7 +292,7 @@ class VectorLayerRenderer extends OverlayLayerCanvasRenderer {
293292
let fixedExtent;
294293
if (symbolkey) {
295294
//相同的symbol 不要重复计算
296-
fixedExtent = symbolkeyMap[symbolkey] = (symbolkeyMap[symbolkey] || geo._painter.getFixedExtent());
295+
fixedExtent = extentCache[symbolkey] = (extentCache[symbolkey] || geo._painter.getFixedExtent());
297296
} else {
298297
fixedExtent = geo._painter.getFixedExtent();
299298
}

0 commit comments

Comments
 (0)