different performance optimizations

This commit is contained in:
mourner 2011-02-27 22:31:46 +02:00
parent 7b205c4d66
commit 047b5c6d1d
6 changed files with 28 additions and 20 deletions

View File

@ -29,7 +29,6 @@ Controls:
Known issues to be fixed:
- some IE7/8 glitches
- show scaled background until tiles are loaded even with animation disabled
## Browser support

View File

@ -62,16 +62,10 @@ L.Util.extend(L.LineUtil, {
},
_sqPointToSegmentDist: function(p, p1, p2) {
var l2 = this._sqDist(p1, p2);
if (l2 == 0) return this._sqDist(p, p1);
var x1 = p.x - p1.x,
x2 = p2.x - p1.x,
y1 = p.y - p1.y,
var x2 = p2.x - p1.x,
y2 = p2.y - p1.y,
dot = x1 * x2 + y1 * y2,
t = dot / l2;
dot = (p.x - p1.x) * x2 + (p.y - p1.y) * y2,
t = dot / this._sqDist(p1, p2);
if (t < 0) return this._sqDist(p, p1);
if (t > 1) return this._sqDist(p, p2);
@ -84,13 +78,16 @@ L.Util.extend(L.LineUtil, {
L.Util.extend(L.LineUtil, {
// Cohen-Sutherland line segment clipping algorithm
// it's considered the fastest in case most clippings are trivial accepts or rejects
clipSegment: function(a, b, bounds) {
clipSegment: function(a, b, bounds, useLastCode) {
var min = bounds.min,
max = bounds.max;
var codeA = this._getBitCode(a, bounds),
var codeA = useLastCode ? this._lastCode : this._getBitCode(a, bounds),
codeB = this._getBitCode(b, bounds);
// save 2nd code to avoid calculating it on the next segment
this._lastCode = codeB;
while (true) {
if (!(codeA | codeB)) {
return [a, b];

View File

@ -13,7 +13,14 @@ L.Point.prototype = {
},
subtract: function(point) {
return new L.Point(this.x - point.x, this.y - point.y);
return this.clone()._subtract(point);
},
// destructive subtract (faster)
_subtract: function(point) {
this.x -= point.x;
this.y -= point.y;
return this;
},
divideBy: function(num, round) {

View File

@ -10,11 +10,16 @@ L.Transformation = L.Class.extend({
this._d = d;
},
transform: function(/*Point*/ point, /*Number*/ scale) /*-> Point*/ {
transform: function(point, scale) {
return this._transform(point.clone(), scale);
},
// destructive transform (faster)
_transform: function(/*Point*/ point, /*Number*/ scale) /*-> Point*/ {
scale = scale || 1;
return new L.Point(
scale * (this._a * point.x + this._b),
scale * (this._c * point.y + this._d));
point.x = scale * (this._a * point.x + this._b);
point.y = scale * (this._c * point.y + this._d);
return point;
},
untransform: function(/*Point*/ point, /*Number*/ scale) /*-> Point*/ {

View File

@ -58,7 +58,7 @@ L.Polyline = L.Path.extend({
this._parts = [];
for (i = 0, k = 0; i < len - 1; i++) {
segment = L.LineUtil.clipSegment(points[i], points[i+1], this._map._pathViewport);
segment = L.LineUtil.clipSegment(points[i], points[i+1], this._map._pathViewport, i);
if (!segment) continue;
this._parts[k] = this._parts[k] || [];

View File

@ -230,13 +230,13 @@ L.Map = L.Class.extend({
},
latLngToLayerPoint: function(latlng) {
return this.project(latlng).subtract(this._initialTopLeftPoint);
return this.project(latlng)._subtract(this._initialTopLeftPoint);
},
project: function(/*Object*/ coord, /*(optional) Number*/ zoom)/*-> Point*/ {
var projectedPoint = this.options.projection.project(coord),
scale = this.options.scaling(isNaN(zoom) ? this._zoom : zoom);
return this.options.transformation.transform(projectedPoint, scale);
return this.options.transformation._transform(projectedPoint, scale);
},
unproject: function(/*Point*/ point, /*(optional) Number*/ zoom, /*(optional) Boolean*/ unbounded)/*-> Object*/ {