From 5bf094f7c26ebe2730f258b2ac5cef1ca804767d Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Mon, 6 Apr 2015 17:45:21 -0700 Subject: [PATCH] implement `symbol-sort` --- js/data/symbol_bucket.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/js/data/symbol_bucket.js b/js/data/symbol_bucket.js index ba3f5adc805..8ae55cdc35f 100644 --- a/js/data/symbol_bucket.js +++ b/js/data/symbol_bucket.js @@ -26,6 +26,7 @@ function SymbolBucket(buffers, layoutProperties, collision, overscaling, collisi this.collision = collision; this.overscaling = overscaling; this.collisionDebug = collisionDebug; + this.sortedAngle = undefined; this.symbolInstances = []; @@ -191,6 +192,22 @@ SymbolBucket.prototype.placeFeatures = function(buffers, collisionDebug) { var textAlongLine = layout['text-rotation-alignment'] === 'map' && layout['symbol-placement'] === 'line'; var iconAlongLine = layout['icon-rotation-alignment'] === 'map' && layout['symbol-placement'] === 'line'; + if (layout['symbol-sort'] !== undefined && this.sortedAngle !== this.collision.angle) { + + var sortAngle = layout['symbol-sort'] / 180 * Math.PI; + var angle = this.collision.angle - sortAngle; + var sin = Math.sin(angle), + cos = Math.cos(angle); + + this.symbolInstances.sort(function(a, b) { + var aRotated = sin * a.x + cos * a.y; + var bRotated = sin * b.x + cos * b.y; + return bRotated - aRotated; + }); + + this.sortedAngle = this.collision.angle; + } + for (var p = 0; p < this.symbolInstances.length; p++) { var symbolInstance = this.symbolInstances[p]; var hasText = symbolInstance.hasText; @@ -397,6 +414,9 @@ function SymbolInstance(anchor, line, shapedText, shapedIcon, layout, inside, textBoxScale, textPadding, textAlongLine, iconBoxScale, iconPadding, iconAlongLine) { + this.x = anchor.x; + this.y = anchor.y; + this.hasText = !!shapedText; this.hasIcon = !!shapedIcon; @@ -409,4 +429,5 @@ function SymbolInstance(anchor, line, shapedText, shapedIcon, layout, inside, this.iconQuads = inside ? getIconQuads(anchor, shapedIcon, iconBoxScale, line, layout, iconAlongLine) : []; this.iconCollisionFeature = new CollisionFeature(line, anchor, shapedIcon, iconBoxScale, iconPadding, iconAlongLine); } + }