Last active
November 9, 2022 01:45
-
-
Save andrewharvey/01006319700c5352deaad3b58ec53b8c to your computer and use it in GitHub Desktop.
Mapbox GL JS Marker Offset Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title></title> | |
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
<script src='mapbox-gl-dev.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.21.0/mapbox-gl.css' rel='stylesheet' /> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoiYWxhbnRnZW8tcHJlc2FsZXMiLCJhIjoiY2lweHA5bTN6MHdpMGZvbTI2ZjU3bHQycSJ9.6Co8WMXUf6EYDj7eRzf-gw'; | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/streets-v9', | |
center: [151.20665426363286, -33.85372532383168], | |
zoom: 19 | |
}); | |
var img = document.createElement('img'); | |
img.src = 'marker.png'; | |
img.style.width = '100px'; | |
img.style.height = '142px'; | |
var marker = new mapboxgl.Marker(img, { | |
offset: { | |
x: -50, | |
y: -142 | |
} | |
}) | |
.setLngLat([151.20665426363286, -33.85372532383168]) | |
.addTo(map); | |
var img2 = document.createElement('img'); | |
img2.src = 'marker.png'; | |
img2.style.width = '100px'; | |
img2.style.height = '142px'; | |
var marker2 = new mapboxgl.Marker(img2, { | |
offset: [-50, -142] | |
}) | |
.setLngLat([151.20555818080902, -33.85412098182466]) | |
.addTo(map); | |
</script> | |
</body> | |
</html> |
This file has been truncated, but you can view the full file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mapboxgl = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | |
'use strict'; | |
var featureFilter = require('feature-filter'); | |
var Buffer = require('./buffer'); | |
var util = require('../util/util'); | |
var StructArrayType = require('../util/struct_array'); | |
var VertexArrayObject = require('../render/vertex_array_object'); | |
var assert = require('assert'); | |
module.exports = Bucket; | |
/** | |
* Instantiate the appropriate subclass of `Bucket` for `options`. | |
* @private | |
* @param options See `Bucket` constructor options | |
* @returns {Bucket} | |
*/ | |
Bucket.create = function(options) { | |
var Classes = { | |
fill: require('./bucket/fill_bucket'), | |
line: require('./bucket/line_bucket'), | |
circle: require('./bucket/circle_bucket'), | |
symbol: require('./bucket/symbol_bucket') | |
}; | |
return new Classes[options.layer.type](options); | |
}; | |
/** | |
* The maximum extent of a feature that can be safely stored in the buffer. | |
* In practice, all features are converted to this extent before being added. | |
* | |
* Positions are stored as signed 16bit integers. | |
* One bit is lost for signedness to support featuers extending past the left edge of the tile. | |
* One bit is lost because the line vertex buffer packs 1 bit of other data into the int. | |
* One bit is lost to support features extending past the extent on the right edge of the tile. | |
* This leaves us with 2^13 = 8192 | |
* | |
* @private | |
* @readonly | |
*/ | |
Bucket.EXTENT = 8192; | |
/** | |
* The maximum size of a vertex array. This limit is imposed by WebGL's 16 bit | |
* addressing of vertex buffers. | |
* @private | |
* @readonly | |
*/ | |
Bucket.MAX_VERTEX_ARRAY_LENGTH = Math.pow(2, 16) - 1; | |
/** | |
* The `Bucket` class is the single point of knowledge about turning vector | |
* tiles into WebGL buffers. | |
* | |
* `Bucket` is an abstract class. A subclass exists for each Mapbox GL | |
* style spec layer type. Because `Bucket` is an abstract class, | |
* instances should be created via the `Bucket.create` method. | |
* | |
* @class Bucket | |
* @private | |
* @param options | |
* @param {number} options.zoom Zoom level of the buffers being built. May be | |
* a fractional zoom level. | |
* @param options.layer A Mapbox style layer object | |
* @param {Object.<string, Buffer>} options.buffers The set of `Buffer`s being | |
* built for this tile. This object facilitates sharing of `Buffer`s be | |
between `Bucket`s. | |
*/ | |
function Bucket(options) { | |
this.zoom = options.zoom; | |
this.overscaling = options.overscaling; | |
this.layer = options.layer; | |
this.childLayers = options.childLayers; | |
this.type = this.layer.type; | |
this.features = []; | |
this.id = this.layer.id; | |
this.index = options.index; | |
this.sourceLayer = this.layer.sourceLayer; | |
this.sourceLayerIndex = options.sourceLayerIndex; | |
this.minZoom = this.layer.minzoom; | |
this.maxZoom = this.layer.maxzoom; | |
this.paintAttributes = createPaintAttributes(this); | |
if (options.arrays) { | |
var childLayers = this.childLayers; | |
var programInterfaces = this.programInterfaces; | |
this.bufferGroups = util.mapObject(options.arrays, function(programArrayGroups, programName) { | |
var programInterface = programInterfaces[programName]; | |
var paintVertexArrayTypes = options.paintVertexArrayTypes[programName]; | |
return programArrayGroups.map(function(arrayGroup) { | |
var group = { | |
layoutVertexBuffer: new Buffer(arrayGroup.layoutVertexArray, | |
programInterface.layoutVertexArrayType.serialize(), Buffer.BufferType.VERTEX), | |
paintVertexBuffers: util.mapObject(arrayGroup.paintVertexArrays, function(array, name) { | |
return new Buffer(array, paintVertexArrayTypes[name], Buffer.BufferType.VERTEX); | |
}), | |
vaos: {} | |
}; | |
if (arrayGroup.elementArray) { | |
group.elementBuffer = new Buffer(arrayGroup.elementArray, | |
programInterface.elementArrayType.serialize(), Buffer.BufferType.ELEMENT); | |
} | |
if (arrayGroup.elementArray2) { | |
group.elementBuffer2 = new Buffer(arrayGroup.elementArray2, | |
programInterface.elementArrayType2.serialize(), Buffer.BufferType.ELEMENT); | |
group.secondVaos = {}; | |
} | |
for (var l = 0; l < childLayers.length; l++) { | |
var layerName = childLayers[l].id; | |
group.vaos[layerName] = new VertexArrayObject(); | |
if (arrayGroup.elementArray2) group.secondVaos[layerName] = new VertexArrayObject(); | |
} | |
return group; | |
}); | |
}); | |
} | |
} | |
/** | |
* Build the arrays! Features are set directly to the `features` property. | |
* @private | |
*/ | |
Bucket.prototype.populateArrays = function() { | |
this.createArrays(); | |
this.recalculateStyleLayers(); | |
for (var i = 0; i < this.features.length; i++) { | |
this.addFeature(this.features[i]); | |
} | |
this.trimArrays(); | |
}; | |
/** | |
* Check if there is enough space available in the current array group for | |
* `vertexLength` vertices. If not, append a new array group. Should be called | |
* by `populateArrays` and its callees. | |
* | |
* Array groups are added to this.arrayGroups[programName]. | |
* An individual array group looks like: | |
* { | |
* index: number, | |
* layoutVertexArray: VertexArrayType, | |
* ?elementArray: ElementArrayType, | |
* ?elementArray2: ElementArrayType2, | |
* paintVertexArrays: { [layerName]: PaintArrayType, ... } | |
* } | |
* | |
* | |
* @private | |
* @param {string} programName the name of the program associated with the buffer that will receive the vertices | |
* @param {number} vertexLength The number of vertices that will be inserted to the buffer. | |
* @returns The current array group | |
*/ | |
Bucket.prototype.prepareArrayGroup = function(programName, numVertices) { | |
var groups = this.arrayGroups[programName]; | |
var currentGroup = groups.length && groups[groups.length - 1]; | |
if (!currentGroup || currentGroup.layoutVertexArray.length + numVertices > Bucket.MAX_VERTEX_ARRAY_LENGTH) { | |
var programInterface = this.programInterfaces[programName]; | |
var LayoutVertexArrayType = programInterface.layoutVertexArrayType; | |
currentGroup = { | |
index: groups.length, | |
layoutVertexArray: new LayoutVertexArrayType(), | |
paintVertexArrays: {} | |
}; | |
var ElementArrayType = programInterface.elementArrayType; | |
if (ElementArrayType) currentGroup.elementArray = new ElementArrayType(); | |
var ElementArrayType2 = programInterface.elementArrayType2; | |
if (ElementArrayType2) currentGroup.elementArray2 = new ElementArrayType2(); | |
var paintVertexArrayTypes = this.paintVertexArrayTypes[programName]; | |
for (var i = 0; i < this.childLayers.length; i++) { | |
var layerName = this.childLayers[i].id; | |
var PaintVertexArrayType = paintVertexArrayTypes[layerName]; | |
currentGroup.paintVertexArrays[layerName] = new PaintVertexArrayType(); | |
} | |
groups.push(currentGroup); | |
} | |
return currentGroup; | |
}; | |
/** | |
* Sets up `this.paintVertexArrayTypes` as { [programName]: { [layerName]: PaintArrayType, ... }, ... } | |
* | |
* And `this.arrayGroups` as { [programName]: [], ... }; these get populated | |
* with array group structure over in `prepareArrayGroup`. | |
* | |
* @private | |
*/ | |
Bucket.prototype.createArrays = function() { | |
this.arrayGroups = {}; | |
this.paintVertexArrayTypes = {}; | |
for (var programName in this.programInterfaces) { | |
this.arrayGroups[programName] = []; | |
var paintVertexArrayTypes = this.paintVertexArrayTypes[programName] = {}; | |
var layerPaintAttributes = this.paintAttributes[programName]; | |
for (var layerName in layerPaintAttributes) { | |
paintVertexArrayTypes[layerName] = new Bucket.VertexArrayType(layerPaintAttributes[layerName].attributes); | |
} | |
} | |
}; | |
Bucket.prototype.destroy = function(gl) { | |
for (var programName in this.bufferGroups) { | |
var programBufferGroups = this.bufferGroups[programName]; | |
for (var i = 0; i < programBufferGroups.length; i++) { | |
var bufferGroup = programBufferGroups[i]; | |
bufferGroup.layoutVertexBuffer.destroy(gl); | |
if (bufferGroup.elementBuffer) { | |
bufferGroup.elementBuffer.destroy(gl); | |
} | |
if (bufferGroup.elementBuffer2) { | |
bufferGroup.elementBuffer2.destroy(gl); | |
} | |
for (var n in bufferGroup.paintVertexBuffers) { | |
bufferGroup.paintVertexBuffers[n].destroy(gl); | |
} | |
for (var j in bufferGroup.vaos) { | |
bufferGroup.vaos[j].destroy(gl); | |
} | |
for (var k in bufferGroup.secondVaos) { | |
bufferGroup.secondVaos[k].destroy(gl); | |
} | |
} | |
} | |
}; | |
Bucket.prototype.trimArrays = function() { | |
for (var programName in this.arrayGroups) { | |
var arrayGroups = this.arrayGroups[programName]; | |
for (var i = 0; i < arrayGroups.length; i++) { | |
var arrayGroup = arrayGroups[i]; | |
arrayGroup.layoutVertexArray.trim(); | |
if (arrayGroup.elementArray) { | |
arrayGroup.elementArray.trim(); | |
} | |
if (arrayGroup.elementArray2) { | |
arrayGroup.elementArray2.trim(); | |
} | |
for (var paintArray in arrayGroup.paintVertexArrays) { | |
arrayGroup.paintVertexArrays[paintArray].trim(); | |
} | |
} | |
} | |
}; | |
Bucket.prototype.isEmpty = function() { | |
for (var programName in this.arrayGroups) { | |
var arrayGroups = this.arrayGroups[programName]; | |
for (var i = 0; i < arrayGroups.length; i++) { | |
var arrayGroup = arrayGroups[i]; | |
if (arrayGroup.layoutVertexArray.length > 0) { | |
return false; | |
} | |
} | |
} | |
return true; | |
}; | |
Bucket.prototype.getTransferables = function(transferables) { | |
for (var programName in this.arrayGroups) { | |
var arrayGroups = this.arrayGroups[programName]; | |
for (var i = 0; i < arrayGroups.length; i++) { | |
var arrayGroup = arrayGroups[i]; | |
transferables.push(arrayGroup.layoutVertexArray.arrayBuffer); | |
if (arrayGroup.elementArray) { | |
transferables.push(arrayGroup.elementArray.arrayBuffer); | |
} | |
if (arrayGroup.elementArray2) { | |
transferables.push(arrayGroup.elementArray2.arrayBuffer); | |
} | |
for (var paintArray in arrayGroup.paintVertexArrays) { | |
transferables.push(arrayGroup.paintVertexArrays[paintArray].arrayBuffer); | |
} | |
} | |
} | |
}; | |
Bucket.prototype.setUniforms = function(gl, programName, program, layer, globalProperties) { | |
var uniforms = this.paintAttributes[programName][layer.id].uniforms; | |
for (var i = 0; i < uniforms.length; i++) { | |
var uniform = uniforms[i]; | |
var uniformLocation = program[uniform.name]; | |
gl['uniform' + uniform.components + 'fv'](uniformLocation, uniform.getValue(layer, globalProperties)); | |
} | |
}; | |
Bucket.prototype.serialize = function() { | |
return { | |
layerId: this.layer.id, | |
zoom: this.zoom, | |
arrays: util.mapObject(this.arrayGroups, function(programArrayGroups) { | |
return programArrayGroups.map(function(arrayGroup) { | |
return { | |
layoutVertexArray: arrayGroup.layoutVertexArray.serialize(), | |
elementArray: arrayGroup.elementArray && arrayGroup.elementArray.serialize(), | |
elementArray2: arrayGroup.elementArray2 && arrayGroup.elementArray2.serialize(), | |
paintVertexArrays: util.mapObject(arrayGroup.paintVertexArrays, function(array) { | |
return array.serialize(); | |
}) | |
}; | |
}); | |
}), | |
paintVertexArrayTypes: util.mapObject(this.paintVertexArrayTypes, function(arrayTypes) { | |
return util.mapObject(arrayTypes, function(arrayType) { | |
return arrayType.serialize(); | |
}); | |
}), | |
childLayerIds: this.childLayers.map(function(layer) { | |
return layer.id; | |
}) | |
}; | |
}; | |
Bucket.prototype.createFilter = function() { | |
if (!this.filter) { | |
this.filter = featureFilter(this.layer.filter); | |
} | |
}; | |
var FAKE_ZOOM_HISTORY = { lastIntegerZoom: Infinity, lastIntegerZoomTime: 0, lastZoom: 0 }; | |
Bucket.prototype.recalculateStyleLayers = function() { | |
for (var i = 0; i < this.childLayers.length; i++) { | |
this.childLayers[i].recalculate(this.zoom, FAKE_ZOOM_HISTORY); | |
} | |
}; | |
Bucket.prototype.populatePaintArrays = function(interfaceName, globalProperties, featureProperties, startGroup, startIndex) { | |
for (var l = 0; l < this.childLayers.length; l++) { | |
var layer = this.childLayers[l]; | |
var groups = this.arrayGroups[interfaceName]; | |
for (var g = startGroup.index; g < groups.length; g++) { | |
var group = groups[g]; | |
var length = group.layoutVertexArray.length; | |
var paintArray = group.paintVertexArrays[layer.id]; | |
paintArray.resize(length); | |
var attributes = this.paintAttributes[interfaceName][layer.id].attributes; | |
for (var m = 0; m < attributes.length; m++) { | |
var attribute = attributes[m]; | |
var value = attribute.getValue(layer, globalProperties, featureProperties); | |
var multiplier = attribute.multiplier || 1; | |
var components = attribute.components || 1; | |
var start = g === startGroup.index ? startIndex : 0; | |
for (var i = start; i < length; i++) { | |
var vertex = paintArray.get(i); | |
for (var c = 0; c < components; c++) { | |
var memberName = components > 1 ? (attribute.name + c) : attribute.name; | |
vertex[memberName] = value[c] * multiplier; | |
} | |
} | |
} | |
} | |
} | |
}; | |
Bucket.VertexArrayType = function (members) { | |
return new StructArrayType({ | |
members: members, | |
alignment: Buffer.VERTEX_ATTRIBUTE_ALIGNMENT | |
}); | |
}; | |
Bucket.ElementArrayType = function (components) { | |
return new StructArrayType({ | |
members: [{ | |
type: Buffer.ELEMENT_ATTRIBUTE_TYPE, | |
name: 'vertices', | |
components: components || 3 | |
}] | |
}); | |
}; | |
function createPaintAttributes(bucket) { | |
var attributes = {}; | |
for (var interfaceName in bucket.programInterfaces) { | |
var layerPaintAttributes = attributes[interfaceName] = {}; | |
for (var c = 0; c < bucket.childLayers.length; c++) { | |
var childLayer = bucket.childLayers[c]; | |
layerPaintAttributes[childLayer.id] = { | |
attributes: [], | |
uniforms: [], | |
defines: [], | |
vertexPragmas: { define: {}, initialize: {} }, | |
fragmentPragmas: { define: {}, initialize: {} } | |
}; | |
} | |
var interface_ = bucket.programInterfaces[interfaceName]; | |
if (!interface_.paintAttributes) continue; | |
// These tokens are replaced by arguments to the pragma | |
// https://github.com/mapbox/mapbox-gl-shaders#pragmas | |
var attributePrecision = '{precision}'; | |
var attributeType = '{type}'; | |
for (var i = 0; i < interface_.paintAttributes.length; i++) { | |
var attribute = interface_.paintAttributes[i]; | |
attribute.multiplier = attribute.multiplier || 1; | |
for (var j = 0; j < bucket.childLayers.length; j++) { | |
var layer = bucket.childLayers[j]; | |
var paintAttributes = layerPaintAttributes[layer.id]; | |
var attributeInputName = attribute.name; | |
assert(attribute.name.slice(0, 2) === 'a_'); | |
var attributeInnerName = attribute.name.slice(2); | |
var attributeVaryingDefinition; | |
paintAttributes.fragmentPragmas.initialize[attributeInnerName] = ''; | |
if (layer.isPaintValueFeatureConstant(attribute.paintProperty)) { | |
paintAttributes.uniforms.push(attribute); | |
paintAttributes.fragmentPragmas.define[attributeInnerName] = paintAttributes.vertexPragmas.define[attributeInnerName] = [ | |
'uniform', | |
attributePrecision, | |
attributeType, | |
attributeInputName | |
].join(' ') + ';'; | |
paintAttributes.fragmentPragmas.initialize[attributeInnerName] = paintAttributes.vertexPragmas.initialize[attributeInnerName] = [ | |
attributePrecision, | |
attributeType, | |
attributeInnerName, | |
'=', | |
attributeInputName | |
].join(' ') + ';\n'; | |
} else if (layer.isPaintValueZoomConstant(attribute.paintProperty)) { | |
paintAttributes.attributes.push(util.extend({}, attribute, { | |
name: attributeInputName | |
})); | |
attributeVaryingDefinition = [ | |
'varying', | |
attributePrecision, | |
attributeType, | |
attributeInnerName | |
].join(' ') + ';\n'; | |
var attributeAttributeDefinition = [ | |
paintAttributes.fragmentPragmas.define[attributeInnerName], | |
'attribute', | |
attributePrecision, | |
attributeType, | |
attributeInputName | |
].join(' ') + ';\n'; | |
paintAttributes.fragmentPragmas.define[attributeInnerName] = attributeVaryingDefinition; | |
paintAttributes.vertexPragmas.define[attributeInnerName] = attributeVaryingDefinition + attributeAttributeDefinition; | |
paintAttributes.vertexPragmas.initialize[attributeInnerName] = [ | |
attributeInnerName, | |
'=', | |
attributeInputName, | |
'/', | |
attribute.multiplier.toFixed(1) | |
].join(' ') + ';\n'; | |
} else { | |
var tName = 'u_' + attributeInputName.slice(2) + '_t'; | |
var zoomLevels = layer.getPaintValueStopZoomLevels(attribute.paintProperty); | |
// Pick the index of the first offset to add to the buffers. | |
// Find the four closest stops, ideally with two on each side of the zoom level. | |
var numStops = 0; | |
while (numStops < zoomLevels.length && zoomLevels[numStops] < bucket.zoom) numStops++; | |
var stopOffset = Math.max(0, Math.min(zoomLevels.length - 4, numStops - 2)); | |
var fourZoomLevels = []; | |
for (var s = 0; s < 4; s++) { | |
fourZoomLevels.push(zoomLevels[Math.min(stopOffset + s, zoomLevels.length - 1)]); | |
} | |
attributeVaryingDefinition = [ | |
'varying', | |
attributePrecision, | |
attributeType, | |
attributeInnerName | |
].join(' ') + ';\n'; | |
paintAttributes.vertexPragmas.define[attributeInnerName] = attributeVaryingDefinition + [ | |
'uniform', | |
'lowp', | |
'float', | |
tName | |
].join(' ') + ';\n'; | |
paintAttributes.fragmentPragmas.define[attributeInnerName] = attributeVaryingDefinition; | |
paintAttributes.uniforms.push(util.extend({}, attribute, { | |
name: tName, | |
getValue: createGetUniform(attribute, stopOffset), | |
components: 1 | |
})); | |
var components = attribute.components; | |
if (components === 1) { | |
paintAttributes.attributes.push(util.extend({}, attribute, { | |
getValue: createFunctionGetValue(attribute, fourZoomLevels), | |
isFunction: true, | |
components: components * 4 | |
})); | |
paintAttributes.vertexPragmas.define[attributeInnerName] += [ | |
'attribute', | |
attributePrecision, | |
'vec4', | |
attributeInputName | |
].join(' ') + ';\n'; | |
paintAttributes.vertexPragmas.initialize[attributeInnerName] = [ | |
attributeInnerName, | |
'=', | |
'evaluate_zoom_function_1(' + attributeInputName + ', ' + tName + ')', | |
'/', | |
attribute.multiplier.toFixed(1) | |
].join(' ') + ';\n'; | |
} else { | |
var attributeInputNames = []; | |
for (var k = 0; k < 4; k++) { | |
attributeInputNames.push(attributeInputName + k); | |
paintAttributes.attributes.push(util.extend({}, attribute, { | |
getValue: createFunctionGetValue(attribute, [fourZoomLevels[k]]), | |
isFunction: true, | |
name: attributeInputName + k | |
})); | |
paintAttributes.vertexPragmas.define[attributeInnerName] += [ | |
'attribute', | |
attributePrecision, | |
attributeType, | |
attributeInputName + k | |
].join(' ') + ';\n'; | |
} | |
paintAttributes.vertexPragmas.initialize[attributeInnerName] = [ | |
attributeInnerName, | |
' = ', | |
'evaluate_zoom_function_4(' + attributeInputNames.join(', ') + ', ' + tName + ')', | |
'/', | |
attribute.multiplier.toFixed(1) | |
].join(' ') + ';\n'; | |
} | |
} | |
} | |
} | |
} | |
return attributes; | |
} | |
function createFunctionGetValue(attribute, stopZoomLevels) { | |
return function(layer, globalProperties, featureProperties) { | |
if (stopZoomLevels.length === 1) { | |
// return one multi-component value like color0 | |
return attribute.getValue(layer, util.extend({}, globalProperties, { zoom: stopZoomLevels[0] }), featureProperties); | |
} else { | |
// pack multiple single-component values into a four component attribute | |
var values = []; | |
for (var z = 0; z < stopZoomLevels.length; z++) { | |
var stopZoomLevel = stopZoomLevels[z]; | |
values.push(attribute.getValue(layer, util.extend({}, globalProperties, { zoom: stopZoomLevel }), featureProperties)[0]); | |
} | |
return values; | |
} | |
}; | |
} | |
function createGetUniform(attribute, stopOffset) { | |
return function(layer, globalProperties) { | |
// stopInterp indicates which stops need to be interpolated. | |
// If stopInterp is 3.5 then interpolate half way between stops 3 and 4. | |
var stopInterp = layer.getPaintInterpolationT(attribute.paintProperty, globalProperties.zoom); | |
// We can only store four stop values in the buffers. stopOffset is the number of stops that come | |
// before the stops that were added to the buffers. | |
return [Math.max(0, Math.min(4, stopInterp - stopOffset))]; | |
}; | |
} | |
},{"../render/vertex_array_object":28,"../util/struct_array":106,"../util/util":108,"./bucket/circle_bucket":2,"./bucket/fill_bucket":3,"./bucket/line_bucket":4,"./bucket/symbol_bucket":5,"./buffer":6,"assert":110,"feature-filter":124}],2:[function(require,module,exports){ | |
'use strict'; | |
var Bucket = require('../bucket'); | |
var util = require('../../util/util'); | |
var loadGeometry = require('../load_geometry'); | |
var EXTENT = Bucket.EXTENT; | |
module.exports = CircleBucket; | |
/** | |
* Circles are represented by two triangles. | |
* | |
* Each corner has a pos that is the center of the circle and an extrusion | |
* vector that is where it points. | |
* @private | |
*/ | |
function CircleBucket() { | |
Bucket.apply(this, arguments); | |
} | |
CircleBucket.prototype = util.inherit(Bucket, {}); | |
CircleBucket.prototype.addCircleVertex = function(layoutVertexArray, x, y, extrudeX, extrudeY) { | |
return layoutVertexArray.emplaceBack( | |
(x * 2) + ((extrudeX + 1) / 2), | |
(y * 2) + ((extrudeY + 1) / 2)); | |
}; | |
CircleBucket.prototype.programInterfaces = { | |
circle: { | |
layoutVertexArrayType: new Bucket.VertexArrayType([{ | |
name: 'a_pos', | |
components: 2, | |
type: 'Int16' | |
}]), | |
elementArrayType: new Bucket.ElementArrayType(), | |
paintAttributes: [{ | |
name: 'a_color', | |
components: 4, | |
type: 'Uint8', | |
getValue: function(layer, globalProperties, featureProperties) { | |
return layer.getPaintValue("circle-color", globalProperties, featureProperties); | |
}, | |
multiplier: 255, | |
paintProperty: 'circle-color' | |
}, { | |
name: 'a_radius', | |
components: 1, | |
type: 'Uint16', | |
isLayerConstant: false, | |
getValue: function(layer, globalProperties, featureProperties) { | |
return [layer.getPaintValue("circle-radius", globalProperties, featureProperties)]; | |
}, | |
multiplier: 10, | |
paintProperty: 'circle-radius' | |
}, { | |
name: 'a_blur', | |
components: 1, | |
type: 'Uint16', | |
isLayerConstant: false, | |
getValue: function(layer, globalProperties, featureProperties) { | |
return [layer.getPaintValue("circle-blur", globalProperties, featureProperties)]; | |
}, | |
multiplier: 10, | |
paintProperty: 'circle-blur' | |
}, { | |
name: 'a_opacity', | |
components: 1, | |
type: 'Uint16', | |
isLayerConstant: false, | |
getValue: function(layer, globalProperties, featureProperties) { | |
return [layer.getPaintValue("circle-opacity", globalProperties, featureProperties)]; | |
}, | |
multiplier: 255, | |
paintProperty: 'circle-opacity' | |
}] | |
} | |
}; | |
CircleBucket.prototype.addFeature = function(feature) { | |
var globalProperties = {zoom: this.zoom}; | |
var geometries = loadGeometry(feature); | |
var startGroup = this.prepareArrayGroup('circle', 0); | |
var startIndex = startGroup.layoutVertexArray.length; | |
for (var j = 0; j < geometries.length; j++) { | |
for (var k = 0; k < geometries[j].length; k++) { | |
var x = geometries[j][k].x; | |
var y = geometries[j][k].y; | |
// Do not include points that are outside the tile boundaries. | |
if (x < 0 || x >= EXTENT || y < 0 || y >= EXTENT) continue; | |
// this geometry will be of the Point type, and we'll derive | |
// two triangles from it. | |
// | |
// ┌─────────┐ | |
// │ 3 2 │ | |
// │ │ | |
// │ 0 1 │ | |
// └─────────┘ | |
var group = this.prepareArrayGroup('circle', 4); | |
var layoutVertexArray = group.layoutVertexArray; | |
var index = this.addCircleVertex(layoutVertexArray, x, y, -1, -1); | |
this.addCircleVertex(layoutVertexArray, x, y, 1, -1); | |
this.addCircleVertex(layoutVertexArray, x, y, 1, 1); | |
this.addCircleVertex(layoutVertexArray, x, y, -1, 1); | |
group.elementArray.emplaceBack(index, index + 1, index + 2); | |
group.elementArray.emplaceBack(index, index + 3, index + 2); | |
} | |
} | |
this.populatePaintArrays('circle', globalProperties, feature.properties, startGroup, startIndex); | |
}; | |
},{"../../util/util":108,"../bucket":1,"../load_geometry":8}],3:[function(require,module,exports){ | |
'use strict'; | |
var Bucket = require('../bucket'); | |
var util = require('../../util/util'); | |
var loadGeometry = require('../load_geometry'); | |
var earcut = require('earcut'); | |
var classifyRings = require('../../util/classify_rings'); | |
var EARCUT_MAX_RINGS = 500; | |
module.exports = FillBucket; | |
function FillBucket() { | |
Bucket.apply(this, arguments); | |
} | |
FillBucket.prototype = util.inherit(Bucket, {}); | |
FillBucket.prototype.programInterfaces = { | |
fill: { | |
layoutVertexArrayType: new Bucket.VertexArrayType([{ | |
name: 'a_pos', | |
components: 2, | |
type: 'Int16' | |
}]), | |
elementArrayType: new Bucket.ElementArrayType(1), | |
elementArrayType2: new Bucket.ElementArrayType(2), | |
paintAttributes: [{ | |
name: 'a_color', | |
components: 4, | |
type: 'Uint8', | |
getValue: function(layer, globalProperties, featureProperties) { | |
return layer.getPaintValue("fill-color", globalProperties, featureProperties); | |
}, | |
multiplier: 255, | |
paintProperty: 'fill-color' | |
}, { | |
name: 'a_outline_color', | |
components: 4, | |
type: 'Uint8', | |
getValue: function(layer, globalProperties, featureProperties) { | |
return layer.getPaintValue("fill-outline-color", globalProperties, featureProperties); | |
}, | |
multiplier: 255, | |
paintProperty: 'fill-outline-color' | |
}, { | |
name: 'a_opacity', | |
components: 1, | |
type: 'Uint8', | |
getValue: function(layer, globalProperties, featureProperties) { | |
return [layer.getPaintValue("fill-opacity", globalProperties, featureProperties)]; | |
}, | |
multiplier: 255, | |
paintProperty: 'fill-opacity' | |
}] | |
} | |
}; | |
FillBucket.prototype.addFeature = function(feature) { | |
var lines = loadGeometry(feature); | |
var polygons = classifyRings(lines, EARCUT_MAX_RINGS); | |
var startGroup = this.prepareArrayGroup('fill', 0); | |
var startIndex = startGroup.layoutVertexArray.length; | |
for (var i = 0; i < polygons.length; i++) { | |
this.addPolygon(polygons[i]); | |
} | |
this.populatePaintArrays('fill', {zoom: this.zoom}, feature.properties, startGroup, startIndex); | |
}; | |
FillBucket.prototype.addPolygon = function(polygon) { | |
var numVertices = 0; | |
for (var k = 0; k < polygon.length; k++) { | |
numVertices += polygon[k].length; | |
} | |
var group = this.prepareArrayGroup('fill', numVertices); | |
var flattened = []; | |
var holeIndices = []; | |
var startIndex = group.layoutVertexArray.length; | |
for (var r = 0; r < polygon.length; r++) { | |
var ring = polygon[r]; | |
if (r > 0) holeIndices.push(flattened.length / 2); | |
for (var v = 0; v < ring.length; v++) { | |
var vertex = ring[v]; | |
var index = group.layoutVertexArray.emplaceBack(vertex.x, vertex.y); | |
if (v >= 1) { | |
group.elementArray2.emplaceBack(index - 1, index); | |
} | |
// convert to format used by earcut | |
flattened.push(vertex.x); | |
flattened.push(vertex.y); | |
} | |
} | |
var triangleIndices = earcut(flattened, holeIndices); | |
for (var i = 0; i < triangleIndices.length; i++) { | |
group.elementArray.emplaceBack(triangleIndices[i] + startIndex); | |
} | |
}; | |
},{"../../util/classify_rings":97,"../../util/util":108,"../bucket":1,"../load_geometry":8,"earcut":123}],4:[function(require,module,exports){ | |
'use strict'; | |
var Bucket = require('../bucket'); | |
var util = require('../../util/util'); | |
var loadGeometry = require('../load_geometry'); | |
var EXTENT = Bucket.EXTENT; | |
// NOTE ON EXTRUDE SCALE: | |
// scale the extrusion vector so that the normal length is this value. | |
// contains the "texture" normals (-1..1). this is distinct from the extrude | |
// normals for line joins, because the x-value remains 0 for the texture | |
// normal array, while the extrude normal actually moves the vertex to create | |
// the acute/bevelled line join. | |
var EXTRUDE_SCALE = 63; | |
/* | |
* Sharp corners cause dashed lines to tilt because the distance along the line | |
* is the same at both the inner and outer corners. To improve the appearance of | |
* dashed lines we add extra points near sharp corners so that a smaller part | |
* of the line is tilted. | |
* | |
* COS_HALF_SHARP_CORNER controls how sharp a corner has to be for us to add an | |
* extra vertex. The default is 75 degrees. | |
* | |
* The newly created vertices are placed SHARP_CORNER_OFFSET pixels from the corner. | |
*/ | |
var COS_HALF_SHARP_CORNER = Math.cos(75 / 2 * (Math.PI / 180)); | |
var SHARP_CORNER_OFFSET = 15; | |
// The number of bits that is used to store the line distance in the buffer. | |
var LINE_DISTANCE_BUFFER_BITS = 15; | |
// We don't have enough bits for the line distance as we'd like to have, so | |
// use this value to scale the line distance (in tile units) down to a smaller | |
// value. This lets us store longer distances while sacrificing precision. | |
var LINE_DISTANCE_SCALE = 1 / 2; | |
// The maximum line distance, in tile units, that fits in the buffer. | |
var MAX_LINE_DISTANCE = Math.pow(2, LINE_DISTANCE_BUFFER_BITS - 1) / LINE_DISTANCE_SCALE; | |
module.exports = LineBucket; | |
/** | |
* @private | |
*/ | |
function LineBucket() { | |
Bucket.apply(this, arguments); | |
} | |
LineBucket.prototype = util.inherit(Bucket, {}); | |
LineBucket.prototype.addLineVertex = function(layoutVertexBuffer, point, extrude, tx, ty, dir, linesofar) { | |
return layoutVertexBuffer.emplaceBack( | |
// a_pos | |
(point.x << 1) | tx, | |
(point.y << 1) | ty, | |
// a_data | |
// add 128 to store an byte in an unsigned byte | |
Math.round(EXTRUDE_SCALE * extrude.x) + 128, | |
Math.round(EXTRUDE_SCALE * extrude.y) + 128, | |
// Encode the -1/0/1 direction value into the first two bits of .z of a_data. | |
// Combine it with the lower 6 bits of `linesofar` (shifted by 2 bites to make | |
// room for the direction value). The upper 8 bits of `linesofar` are placed in | |
// the `w` component. `linesofar` is scaled down by `LINE_DISTANCE_SCALE` so that | |
// we can store longer distances while sacrificing precision. | |
((dir === 0 ? 0 : (dir < 0 ? -1 : 1)) + 1) | (((linesofar * LINE_DISTANCE_SCALE) & 0x3F) << 2), | |
(linesofar * LINE_DISTANCE_SCALE) >> 6); | |
}; | |
LineBucket.prototype.programInterfaces = { | |
line: { | |
layoutVertexArrayType: new Bucket.VertexArrayType([{ | |
name: 'a_pos', | |
components: 2, | |
type: 'Int16' | |
}, { | |
name: 'a_data', | |
components: 4, | |
type: 'Uint8' | |
}]), | |
elementArrayType: new Bucket.ElementArrayType() | |
} | |
}; | |
LineBucket.prototype.addFeature = function(feature) { | |
var lines = loadGeometry(feature, LINE_DISTANCE_BUFFER_BITS); | |
for (var i = 0; i < lines.length; i++) { | |
this.addLine( | |
lines[i], | |
this.layer.layout['line-join'], | |
this.layer.layout['line-cap'], | |
this.layer.layout['line-miter-limit'], | |
this.layer.layout['line-round-limit'] | |
); | |
} | |
}; | |
LineBucket.prototype.addLine = function(vertices, join, cap, miterLimit, roundLimit) { | |
var len = vertices.length; | |
// If the line has duplicate vertices at the end, adjust length to remove them. | |
while (len > 2 && vertices[len - 1].equals(vertices[len - 2])) { | |
len--; | |
} | |
// a line must have at least two vertices | |
if (vertices.length < 2) return; | |
if (join === 'bevel') miterLimit = 1.05; | |
var sharpCornerOffset = SHARP_CORNER_OFFSET * (EXTENT / (512 * this.overscaling)); | |
var firstVertex = vertices[0], | |
lastVertex = vertices[len - 1], | |
closed = firstVertex.equals(lastVertex); | |
// we could be more precise, but it would only save a negligible amount of space | |
this.prepareArrayGroup('line', len * 10); | |
// a line may not have coincident points | |
if (len === 2 && closed) return; | |
this.distance = 0; | |
var beginCap = cap, | |
endCap = closed ? 'butt' : cap, | |
startOfLine = true, | |
currentVertex, prevVertex, nextVertex, prevNormal, nextNormal, offsetA, offsetB; | |
// the last three vertices added | |
this.e1 = this.e2 = this.e3 = -1; | |
if (closed) { | |
currentVertex = vertices[len - 2]; | |
nextNormal = firstVertex.sub(currentVertex)._unit()._perp(); | |
} | |
for (var i = 0; i < len; i++) { | |
nextVertex = closed && i === len - 1 ? | |
vertices[1] : // if the line is closed, we treat the last vertex like the first | |
vertices[i + 1]; // just the next vertex | |
// if two consecutive vertices exist, skip the current one | |
if (nextVertex && vertices[i].equals(nextVertex)) continue; | |
if (nextNormal) prevNormal = nextNormal; | |
if (currentVertex) prevVertex = currentVertex; | |
currentVertex = vertices[i]; | |
// Calculate the normal towards the next vertex in this line. In case | |
// there is no next vertex, pretend that the line is continuing straight, | |
// meaning that we are just using the previous normal. | |
nextNormal = nextVertex ? nextVertex.sub(currentVertex)._unit()._perp() : prevNormal; | |
// If we still don't have a previous normal, this is the beginning of a | |
// non-closed line, so we're doing a straight "join". | |
prevNormal = prevNormal || nextNormal; | |
// Determine the normal of the join extrusion. It is the angle bisector | |
// of the segments between the previous line and the next line. | |
var joinNormal = prevNormal.add(nextNormal)._unit(); | |
/* joinNormal prevNormal | |
* ↖ ↑ | |
* .________. prevVertex | |
* | | |
* nextNormal ← | currentVertex | |
* | | |
* nextVertex ! | |
* | |
*/ | |
// Calculate the length of the miter (the ratio of the miter to the width). | |
// Find the cosine of the angle between the next and join normals | |
// using dot product. The inverse of that is the miter length. | |
var cosHalfAngle = joinNormal.x * nextNormal.x + joinNormal.y * nextNormal.y; | |
var miterLength = 1 / cosHalfAngle; | |
var isSharpCorner = cosHalfAngle < COS_HALF_SHARP_CORNER && prevVertex && nextVertex; | |
if (isSharpCorner && i > 0) { | |
var prevSegmentLength = currentVertex.dist(prevVertex); | |
if (prevSegmentLength > 2 * sharpCornerOffset) { | |
var newPrevVertex = currentVertex.sub(currentVertex.sub(prevVertex)._mult(sharpCornerOffset / prevSegmentLength)._round()); | |
this.distance += newPrevVertex.dist(prevVertex); | |
this.addCurrentVertex(newPrevVertex, this.distance, prevNormal.mult(1), 0, 0, false); | |
prevVertex = newPrevVertex; | |
} | |
} | |
// The join if a middle vertex, otherwise the cap. | |
var middleVertex = prevVertex && nextVertex; | |
var currentJoin = middleVertex ? join : nextVertex ? beginCap : endCap; | |
if (middleVertex && currentJoin === 'round') { | |
if (miterLength < roundLimit) { | |
currentJoin = 'miter'; | |
} else if (miterLength <= 2) { | |
currentJoin = 'fakeround'; | |
} | |
} | |
if (currentJoin === 'miter' && miterLength > miterLimit) { | |
currentJoin = 'bevel'; | |
} | |
if (currentJoin === 'bevel') { | |
// The maximum extrude length is 128 / 63 = 2 times the width of the line | |
// so if miterLength >= 2 we need to draw a different type of bevel where. | |
if (miterLength > 2) currentJoin = 'flipbevel'; | |
// If the miterLength is really small and the line bevel wouldn't be visible, | |
// just draw a miter join to save a triangle. | |
if (miterLength < miterLimit) currentJoin = 'miter'; | |
} | |
// Calculate how far along the line the currentVertex is | |
if (prevVertex) this.distance += currentVertex.dist(prevVertex); | |
if (currentJoin === 'miter') { | |
joinNormal._mult(miterLength); | |
this.addCurrentVertex(currentVertex, this.distance, joinNormal, 0, 0, false); | |
} else if (currentJoin === 'flipbevel') { | |
// miter is too big, flip the direction to make a beveled join | |
if (miterLength > 100) { | |
// Almost parallel lines | |
joinNormal = nextNormal.clone(); | |
} else { | |
var direction = prevNormal.x * nextNormal.y - prevNormal.y * nextNormal.x > 0 ? -1 : 1; | |
var bevelLength = miterLength * prevNormal.add(nextNormal).mag() / prevNormal.sub(nextNormal).mag(); | |
joinNormal._perp()._mult(bevelLength * direction); | |
} | |
this.addCurrentVertex(currentVertex, this.distance, joinNormal, 0, 0, false); | |
this.addCurrentVertex(currentVertex, this.distance, joinNormal.mult(-1), 0, 0, false); | |
} else if (currentJoin === 'bevel' || currentJoin === 'fakeround') { | |
var lineTurnsLeft = (prevNormal.x * nextNormal.y - prevNormal.y * nextNormal.x) > 0; | |
var offset = -Math.sqrt(miterLength * miterLength - 1); | |
if (lineTurnsLeft) { | |
offsetB = 0; | |
offsetA = offset; | |
} else { | |
offsetA = 0; | |
offsetB = offset; | |
} | |
// Close previous segment with a bevel | |
if (!startOfLine) { | |
this.addCurrentVertex(currentVertex, this.distance, prevNormal, offsetA, offsetB, false); | |
} | |
if (currentJoin === 'fakeround') { | |
// The join angle is sharp enough that a round join would be visible. | |
// Bevel joins fill the gap between segments with a single pie slice triangle. | |
// Create a round join by adding multiple pie slices. The join isn't actually round, but | |
// it looks like it is at the sizes we render lines at. | |
// Add more triangles for sharper angles. | |
// This math is just a good enough approximation. It isn't "correct". | |
var n = Math.floor((0.5 - (cosHalfAngle - 0.5)) * 8); | |
var approxFractionalJoinNormal; | |
for (var m = 0; m < n; m++) { | |
approxFractionalJoinNormal = nextNormal.mult((m + 1) / (n + 1))._add(prevNormal)._unit(); | |
this.addPieSliceVertex(currentVertex, this.distance, approxFractionalJoinNormal, lineTurnsLeft); | |
} | |
this.addPieSliceVertex(currentVertex, this.distance, joinNormal, lineTurnsLeft); | |
for (var k = n - 1; k >= 0; k--) { | |
approxFractionalJoinNormal = prevNormal.mult((k + 1) / (n + 1))._add(nextNormal)._unit(); | |
this.addPieSliceVertex(currentVertex, this.distance, approxFractionalJoinNormal, lineTurnsLeft); | |
} | |
} | |
// Start next segment | |
if (nextVertex) { | |
this.addCurrentVertex(currentVertex, this.distance, nextNormal, -offsetA, -offsetB, false); | |
} | |
} else if (currentJoin === 'butt') { | |
if (!startOfLine) { | |
// Close previous segment with a butt | |
this.addCurrentVertex(currentVertex, this.distance, prevNormal, 0, 0, false); | |
} | |
// Start next segment with a butt | |
if (nextVertex) { | |
this.addCurrentVertex(currentVertex, this.distance, nextNormal, 0, 0, false); | |
} | |
} else if (currentJoin === 'square') { | |
if (!startOfLine) { | |
// Close previous segment with a square cap | |
this.addCurrentVertex(currentVertex, this.distance, prevNormal, 1, 1, false); | |
// The segment is done. Unset vertices to disconnect segments. | |
this.e1 = this.e2 = -1; | |
} | |
// Start next segment | |
if (nextVertex) { | |
this.addCurrentVertex(currentVertex, this.distance, nextNormal, -1, -1, false); | |
} | |
} else if (currentJoin === 'round') { | |
if (!startOfLine) { | |
// Close previous segment with butt | |
this.addCurrentVertex(currentVertex, this.distance, prevNormal, 0, 0, false); | |
// Add round cap or linejoin at end of segment | |
this.addCurrentVertex(currentVertex, this.distance, prevNormal, 1, 1, true); | |
// The segment is done. Unset vertices to disconnect segments. | |
this.e1 = this.e2 = -1; | |
} | |
// Start next segment with a butt | |
if (nextVertex) { | |
// Add round cap before first segment | |
this.addCurrentVertex(currentVertex, this.distance, nextNormal, -1, -1, true); | |
this.addCurrentVertex(currentVertex, this.distance, nextNormal, 0, 0, false); | |
} | |
} | |
if (isSharpCorner && i < len - 1) { | |
var nextSegmentLength = currentVertex.dist(nextVertex); | |
if (nextSegmentLength > 2 * sharpCornerOffset) { | |
var newCurrentVertex = currentVertex.add(nextVertex.sub(currentVertex)._mult(sharpCornerOffset / nextSegmentLength)._round()); | |
this.distance += newCurrentVertex.dist(currentVertex); | |
this.addCurrentVertex(newCurrentVertex, this.distance, nextNormal.mult(1), 0, 0, false); | |
currentVertex = newCurrentVertex; | |
} | |
} | |
startOfLine = false; | |
} | |
}; | |
/** | |
* Add two vertices to the buffers. | |
* | |
* @param {Object} currentVertex the line vertex to add buffer vertices for | |
* @param {number} distance the distance from the beginning of the line to the vertex | |
* @param {number} endLeft extrude to shift the left vertex along the line | |
* @param {number} endRight extrude to shift the left vertex along the line | |
* @param {boolean} round whether this is a round cap | |
* @private | |
*/ | |
LineBucket.prototype.addCurrentVertex = function(currentVertex, distance, normal, endLeft, endRight, round) { | |
var tx = round ? 1 : 0; | |
var extrude; | |
var arrayGroup = this.arrayGroups.line[this.arrayGroups.line.length - 1]; | |
var layoutVertexArray = arrayGroup.layoutVertexArray; | |
var elementArray = arrayGroup.elementArray; | |
extrude = normal.clone(); | |
if (endLeft) extrude._sub(normal.perp()._mult(endLeft)); | |
this.e3 = this.addLineVertex(layoutVertexArray, currentVertex, extrude, tx, 0, endLeft, distance); | |
if (this.e1 >= 0 && this.e2 >= 0) { | |
elementArray.emplaceBack(this.e1, this.e2, this.e3); | |
} | |
this.e1 = this.e2; | |
this.e2 = this.e3; | |
extrude = normal.mult(-1); | |
if (endRight) extrude._sub(normal.perp()._mult(endRight)); | |
this.e3 = this.addLineVertex(layoutVertexArray, currentVertex, extrude, tx, 1, -endRight, distance); | |
if (this.e1 >= 0 && this.e2 >= 0) { | |
elementArray.emplaceBack(this.e1, this.e2, this.e3); | |
} | |
this.e1 = this.e2; | |
this.e2 = this.e3; | |
// There is a maximum "distance along the line" that we can store in the buffers. | |
// When we get close to the distance, reset it to zero and add the vertex again with | |
// a distance of zero. The max distance is determined by the number of bits we allocate | |
// to `linesofar`. | |
if (distance > MAX_LINE_DISTANCE / 2) { | |
this.distance = 0; | |
this.addCurrentVertex(currentVertex, this.distance, normal, endLeft, endRight, round); | |
} | |
}; | |
/** | |
* Add a single new vertex and a triangle using two previous vertices. | |
* This adds a pie slice triangle near a join to simulate round joins | |
* | |
* @param {Object} currentVertex the line vertex to add buffer vertices for | |
* @param {number} distance the distance from the beggining of the line to the vertex | |
* @param {Object} extrude the offset of the new vertex from the currentVertex | |
* @param {boolean} whether the line is turning left or right at this angle | |
* @private | |
*/ | |
LineBucket.prototype.addPieSliceVertex = function(currentVertex, distance, extrude, lineTurnsLeft) { | |
var ty = lineTurnsLeft ? 1 : 0; | |
extrude = extrude.mult(lineTurnsLeft ? -1 : 1); | |
var arrayGroup = this.arrayGroups.line[this.arrayGroups.line.length - 1]; | |
var layoutVertexArray = arrayGroup.layoutVertexArray; | |
var elementArray = arrayGroup.elementArray; | |
this.e3 = this.addLineVertex(layoutVertexArray, currentVertex, extrude, 0, ty, 0, distance); | |
if (this.e1 >= 0 && this.e2 >= 0) { | |
elementArray.emplaceBack(this.e1, this.e2, this.e3); | |
} | |
if (lineTurnsLeft) { | |
this.e2 = this.e3; | |
} else { | |
this.e1 = this.e3; | |
} | |
}; | |
},{"../../util/util":108,"../bucket":1,"../load_geometry":8}],5:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
var Bucket = require('../bucket'); | |
var Anchor = require('../../symbol/anchor'); | |
var getAnchors = require('../../symbol/get_anchors'); | |
var resolveTokens = require('../../util/token'); | |
var Quads = require('../../symbol/quads'); | |
var Shaping = require('../../symbol/shaping'); | |
var resolveText = require('../../symbol/resolve_text'); | |
var mergeLines = require('../../symbol/mergelines'); | |
var clipLine = require('../../symbol/clip_line'); | |
var util = require('../../util/util'); | |
var loadGeometry = require('../load_geometry'); | |
var CollisionFeature = require('../../symbol/collision_feature'); | |
var shapeText = Shaping.shapeText; | |
var shapeIcon = Shaping.shapeIcon; | |
var getGlyphQuads = Quads.getGlyphQuads; | |
var getIconQuads = Quads.getIconQuads; | |
var EXTENT = Bucket.EXTENT; | |
module.exports = SymbolBucket; | |
function SymbolBucket(options) { | |
Bucket.apply(this, arguments); | |
this.showCollisionBoxes = options.showCollisionBoxes; | |
this.overscaling = options.overscaling; | |
this.collisionBoxArray = options.collisionBoxArray; | |
this.symbolQuadsArray = options.symbolQuadsArray; | |
this.symbolInstancesArray = options.symbolInstancesArray; | |
this.sdfIcons = options.sdfIcons; | |
this.iconsNeedLinear = options.iconsNeedLinear; | |
this.adjustedTextSize = options.adjustedTextSize; | |
this.adjustedIconSize = options.adjustedIconSize; | |
this.fontstack = options.fontstack; | |
} | |
SymbolBucket.prototype = util.inherit(Bucket, {}); | |
SymbolBucket.prototype.serialize = function() { | |
var serialized = Bucket.prototype.serialize.apply(this); | |
serialized.sdfIcons = this.sdfIcons; | |
serialized.iconsNeedLinear = this.iconsNeedLinear; | |
serialized.adjustedTextSize = this.adjustedTextSize; | |
serialized.adjustedIconSize = this.adjustedIconSize; | |
serialized.fontstack = this.fontstack; | |
return serialized; | |
}; | |
var layoutVertexArrayType = new Bucket.VertexArrayType([{ | |
name: 'a_pos', | |
components: 2, | |
type: 'Int16' | |
}, { | |
name: 'a_offset', | |
components: 2, | |
type: 'Int16' | |
}, { | |
name: 'a_data1', | |
components: 4, | |
type: 'Uint8' | |
}, { | |
name: 'a_data2', | |
components: 2, | |
type: 'Uint8' | |
}]); | |
var elementArrayType = new Bucket.ElementArrayType(); | |
function addVertex(array, x, y, ox, oy, tx, ty, minzoom, maxzoom, labelminzoom, labelangle) { | |
return array.emplaceBack( | |
// pos | |
x, | |
y, | |
// offset | |
Math.round(ox * 64), // use 1/64 pixels for placement | |
Math.round(oy * 64), | |
// data1 | |
tx / 4, // tex | |
ty / 4, // tex | |
(labelminzoom || 0) * 10, // labelminzoom | |
labelangle, // labelangle | |
// data2 | |
(minzoom || 0) * 10, // minzoom | |
Math.min(maxzoom || 25, 25) * 10); // minzoom | |
} | |
SymbolBucket.prototype.addCollisionBoxVertex = function(layoutVertexArray, point, extrude, maxZoom, placementZoom) { | |
return layoutVertexArray.emplaceBack( | |
// pos | |
point.x, | |
point.y, | |
// extrude | |
Math.round(extrude.x), | |
Math.round(extrude.y), | |
// data | |
maxZoom * 10, | |
placementZoom * 10); | |
}; | |
SymbolBucket.prototype.programInterfaces = { | |
glyph: { | |
layoutVertexArrayType: layoutVertexArrayType, | |
elementArrayType: elementArrayType | |
}, | |
icon: { | |
layoutVertexArrayType: layoutVertexArrayType, | |
elementArrayType: elementArrayType | |
}, | |
collisionBox: { | |
layoutVertexArrayType: new Bucket.VertexArrayType([{ | |
name: 'a_pos', | |
components: 2, | |
type: 'Int16' | |
}, { | |
name: 'a_extrude', | |
components: 2, | |
type: 'Int16' | |
}, { | |
name: 'a_data', | |
components: 2, | |
type: 'Uint8' | |
}]) | |
} | |
}; | |
SymbolBucket.prototype.populateArrays = function(collisionTile, stacks, icons) { | |
// To reduce the number of labels that jump around when zooming we need | |
// to use a text-size value that is the same for all zoom levels. | |
// This calculates text-size at a high zoom level so that all tiles can | |
// use the same value when calculating anchor positions. | |
var zoomHistory = { lastIntegerZoom: Infinity, lastIntegerZoomTime: 0, lastZoom: 0 }; | |
this.adjustedTextMaxSize = this.layer.getLayoutValue('text-size', {zoom: 18, zoomHistory: zoomHistory}); | |
this.adjustedTextSize = this.layer.getLayoutValue('text-size', {zoom: this.zoom + 1, zoomHistory: zoomHistory}); | |
this.adjustedIconMaxSize = this.layer.getLayoutValue('icon-size', {zoom: 18, zoomHistory: zoomHistory}); | |
this.adjustedIconSize = this.layer.getLayoutValue('icon-size', {zoom: this.zoom + 1, zoomHistory: zoomHistory}); | |
var tileSize = 512 * this.overscaling; | |
this.tilePixelRatio = EXTENT / tileSize; | |
this.compareText = {}; | |
this.iconsNeedLinear = false; | |
this.symbolInstancesStartIndex = this.symbolInstancesArray.length; | |
var layout = this.layer.layout; | |
var features = this.features; | |
var textFeatures = this.textFeatures; | |
var horizontalAlign = 0.5, | |
verticalAlign = 0.5; | |
switch (layout['text-anchor']) { | |
case 'right': | |
case 'top-right': | |
case 'bottom-right': | |
horizontalAlign = 1; | |
break; | |
case 'left': | |
case 'top-left': | |
case 'bottom-left': | |
horizontalAlign = 0; | |
break; | |
} | |
switch (layout['text-anchor']) { | |
case 'bottom': | |
case 'bottom-right': | |
case 'bottom-left': | |
verticalAlign = 1; | |
break; | |
case 'top': | |
case 'top-right': | |
case 'top-left': | |
verticalAlign = 0; | |
break; | |
} | |
var justify = layout['text-justify'] === 'right' ? 1 : | |
layout['text-justify'] === 'left' ? 0 : | |
0.5; | |
var oneEm = 24; | |
var lineHeight = layout['text-line-height'] * oneEm; | |
var maxWidth = layout['symbol-placement'] !== 'line' ? layout['text-max-width'] * oneEm : 0; | |
var spacing = layout['text-letter-spacing'] * oneEm; | |
var textOffset = [layout['text-offset'][0] * oneEm, layout['text-offset'][1] * oneEm]; | |
var fontstack = this.fontstack = layout['text-font'].join(','); | |
var geometries = []; | |
for (var g = 0; g < features.length; g++) { | |
geometries.push(loadGeometry(features[g])); | |
} | |
if (layout['symbol-placement'] === 'line') { | |
// Merge adjacent lines with the same text to improve labelling. | |
// It's better to place labels on one long line than on many short segments. | |
var merged = mergeLines(features, textFeatures, geometries); | |
geometries = merged.geometries; | |
features = merged.features; | |
textFeatures = merged.textFeatures; | |
} | |
var shapedText, shapedIcon; | |
for (var k = 0; k < features.length; k++) { | |
if (!geometries[k]) continue; | |
if (textFeatures[k]) { | |
shapedText = shapeText(textFeatures[k], stacks[fontstack], maxWidth, | |
lineHeight, horizontalAlign, verticalAlign, justify, spacing, textOffset); | |
} else { | |
shapedText = null; | |
} | |
if (layout['icon-image']) { | |
var iconName = resolveTokens(features[k].properties, layout['icon-image']); | |
var image = icons[iconName]; | |
shapedIcon = shapeIcon(image, layout); | |
if (image) { | |
if (this.sdfIcons === undefined) { | |
this.sdfIcons = image.sdf; | |
} else if (this.sdfIcons !== image.sdf) { | |
util.warnOnce('Style sheet warning: Cannot mix SDF and non-SDF icons in one buffer'); | |
} | |
if (image.pixelRatio !== 1) { | |
this.iconsNeedLinear = true; | |
} else if (layout['icon-rotate'] !== 0 || !this.layer.isLayoutValueFeatureConstant('icon-rotate')) { | |
this.iconsNeedLinear = true; | |
} | |
} | |
} else { | |
shapedIcon = null; | |
} | |
if (shapedText || shapedIcon) { | |
this.addFeature(geometries[k], shapedText, shapedIcon, features[k]); | |
} | |
} | |
this.symbolInstancesEndIndex = this.symbolInstancesArray.length; | |
this.placeFeatures(collisionTile, this.showCollisionBoxes); | |
this.trimArrays(); | |
}; | |
SymbolBucket.prototype.addFeature = function(lines, shapedText, shapedIcon, feature) { | |
var layout = this.layer.layout; | |
var glyphSize = 24; | |
var fontScale = this.adjustedTextSize / glyphSize, | |
textMaxSize = this.adjustedTextMaxSize !== undefined ? this.adjustedTextMaxSize : this.adjustedTextSize, | |
textBoxScale = this.tilePixelRatio * fontScale, | |
textMaxBoxScale = this.tilePixelRatio * textMaxSize / glyphSize, | |
iconBoxScale = this.tilePixelRatio * this.adjustedIconSize, | |
symbolMinDistance = this.tilePixelRatio * layout['symbol-spacing'], | |
avoidEdges = layout['symbol-avoid-edges'], | |
textPadding = layout['text-padding'] * this.tilePixelRatio, | |
iconPadding = layout['icon-padding'] * this.tilePixelRatio, | |
textMaxAngle = layout['text-max-angle'] / 180 * Math.PI, | |
textAlongLine = layout['text-rotation-alignment'] === 'map' && layout['symbol-placement'] === 'line', | |
iconAlongLine = layout['icon-rotation-alignment'] === 'map' && layout['symbol-placement'] === 'line', | |
mayOverlap = layout['text-allow-overlap'] || layout['icon-allow-overlap'] || | |
layout['text-ignore-placement'] || layout['icon-ignore-placement'], | |
isLine = layout['symbol-placement'] === 'line', | |
textRepeatDistance = symbolMinDistance / 2; | |
if (isLine) { | |
lines = clipLine(lines, 0, 0, EXTENT, EXTENT); | |
} | |
for (var i = 0; i < lines.length; i++) { | |
var line = lines[i]; | |
// Calculate the anchor points around which you want to place labels | |
var anchors; | |
if (isLine) { | |
anchors = getAnchors( | |
line, | |
symbolMinDistance, | |
textMaxAngle, | |
shapedText, | |
shapedIcon, | |
glyphSize, | |
textMaxBoxScale, | |
this.overscaling, | |
EXTENT | |
); | |
} else { | |
anchors = [ new Anchor(line[0].x, line[0].y, 0) ]; | |
} | |
// For each potential label, create the placement features used to check for collisions, and the quads use for rendering. | |
for (var j = 0, len = anchors.length; j < len; j++) { | |
var anchor = anchors[j]; | |
if (shapedText && isLine) { | |
if (this.anchorIsTooClose(shapedText.text, textRepeatDistance, anchor)) { | |
continue; | |
} | |
} | |
var inside = !(anchor.x < 0 || anchor.x > EXTENT || anchor.y < 0 || anchor.y > EXTENT); | |
if (avoidEdges && !inside) continue; | |
// Normally symbol layers are drawn across tile boundaries. Only symbols | |
// with their anchors within the tile boundaries are added to the buffers | |
// to prevent symbols from being drawn twice. | |
// | |
// Symbols in layers with overlap are sorted in the y direction so that | |
// symbols lower on the canvas are drawn on top of symbols near the top. | |
// To preserve this order across tile boundaries these symbols can't | |
// be drawn across tile boundaries. Instead they need to be included in | |
// the buffers for both tiles and clipped to tile boundaries at draw time. | |
var addToBuffers = inside || mayOverlap; | |
this.addSymbolInstance(anchor, line, shapedText, shapedIcon, this.layer, | |
addToBuffers, this.symbolInstancesArray.length, this.collisionBoxArray, feature.index, this.sourceLayerIndex, this.index, | |
textBoxScale, textPadding, textAlongLine, | |
iconBoxScale, iconPadding, iconAlongLine, {zoom: this.zoom}, feature.properties); | |
} | |
} | |
}; | |
SymbolBucket.prototype.anchorIsTooClose = function(text, repeatDistance, anchor) { | |
var compareText = this.compareText; | |
if (!(text in compareText)) { | |
compareText[text] = []; | |
} else { | |
var otherAnchors = compareText[text]; | |
for (var k = otherAnchors.length - 1; k >= 0; k--) { | |
if (anchor.dist(otherAnchors[k]) < repeatDistance) { | |
// If it's within repeatDistance of one anchor, stop looking | |
return true; | |
} | |
} | |
} | |
// If anchor is not within repeatDistance of any other anchor, add to array | |
compareText[text].push(anchor); | |
return false; | |
}; | |
SymbolBucket.prototype.placeFeatures = function(collisionTile, showCollisionBoxes) { | |
this.recalculateStyleLayers(); | |
// Calculate which labels can be shown and when they can be shown and | |
// create the bufers used for rendering. | |
this.createArrays(); | |
var layout = this.layer.layout; | |
var maxScale = collisionTile.maxScale; | |
var textAlongLine = layout['text-rotation-alignment'] === 'map' && layout['symbol-placement'] === 'line'; | |
var iconAlongLine = layout['icon-rotation-alignment'] === 'map' && layout['symbol-placement'] === 'line'; | |
var mayOverlap = layout['text-allow-overlap'] || layout['icon-allow-overlap'] || | |
layout['text-ignore-placement'] || layout['icon-ignore-placement']; | |
// Sort symbols by their y position on the canvas so that the lower symbols | |
// are drawn on top of higher symbols. | |
// Don't sort symbols that won't overlap because it isn't necessary and | |
// because it causes more labels to pop in and out when rotating. | |
if (mayOverlap) { | |
// Only need the symbol instances from the current tile to sort, so convert those instances into an array | |
// of `StructType`s to enable sorting | |
var symbolInstancesStructTypeArray = this.symbolInstancesArray.toArray(this.symbolInstancesStartIndex, this.symbolInstancesEndIndex); | |
var angle = collisionTile.angle; | |
var sin = Math.sin(angle), | |
cos = Math.cos(angle); | |
this.sortedSymbolInstances = symbolInstancesStructTypeArray.sort(function(a, b) { | |
var aRotated = (sin * a.anchorPointX + cos * a.anchorPointY) | 0; | |
var bRotated = (sin * b.anchorPointX + cos * b.anchorPointY) | 0; | |
return (aRotated - bRotated) || (b.index - a.index); | |
}); | |
} | |
for (var p = this.symbolInstancesStartIndex; p < this.symbolInstancesEndIndex; p++) { | |
var symbolInstance = this.sortedSymbolInstances ? this.sortedSymbolInstances[p - this.symbolInstancesStartIndex] : this.symbolInstancesArray.get(p); | |
var textCollisionFeature = { | |
boxStartIndex: symbolInstance.textBoxStartIndex, | |
boxEndIndex: symbolInstance.textBoxEndIndex | |
}; | |
var iconCollisionFeature = { | |
boxStartIndex: symbolInstance.iconBoxStartIndex, | |
boxEndIndex: symbolInstance.iconBoxEndIndex | |
}; | |
var hasText = !(symbolInstance.textBoxStartIndex === symbolInstance.textBoxEndIndex); | |
var hasIcon = !(symbolInstance.iconBoxStartIndex === symbolInstance.iconBoxEndIndex); | |
var iconWithoutText = layout['text-optional'] || !hasText, | |
textWithoutIcon = layout['icon-optional'] || !hasIcon; | |
// Calculate the scales at which the text and icon can be placed without collision. | |
var glyphScale = hasText ? | |
collisionTile.placeCollisionFeature(textCollisionFeature, | |
layout['text-allow-overlap'], layout['symbol-avoid-edges']) : | |
collisionTile.minScale; | |
var iconScale = hasIcon ? | |
collisionTile.placeCollisionFeature(iconCollisionFeature, | |
layout['icon-allow-overlap'], layout['symbol-avoid-edges']) : | |
collisionTile.minScale; | |
// Combine the scales for icons and text. | |
if (!iconWithoutText && !textWithoutIcon) { | |
iconScale = glyphScale = Math.max(iconScale, glyphScale); | |
} else if (!textWithoutIcon && glyphScale) { | |
glyphScale = Math.max(iconScale, glyphScale); | |
} else if (!iconWithoutText && iconScale) { | |
iconScale = Math.max(iconScale, glyphScale); | |
} | |
// Insert final placement into collision tree and add glyphs/icons to buffers | |
if (hasText) { | |
collisionTile.insertCollisionFeature(textCollisionFeature, glyphScale, layout['text-ignore-placement']); | |
if (glyphScale <= maxScale) { | |
this.addSymbols('glyph', symbolInstance.glyphQuadStartIndex, symbolInstance.glyphQuadEndIndex, glyphScale, layout['text-keep-upright'], textAlongLine, collisionTile.angle); | |
} | |
} | |
if (hasIcon) { | |
collisionTile.insertCollisionFeature(iconCollisionFeature, iconScale, layout['icon-ignore-placement']); | |
if (iconScale <= maxScale) { | |
this.addSymbols('icon', symbolInstance.iconQuadStartIndex, symbolInstance.iconQuadEndIndex, iconScale, layout['icon-keep-upright'], iconAlongLine, collisionTile.angle); | |
} | |
} | |
} | |
if (showCollisionBoxes) this.addToDebugBuffers(collisionTile); | |
}; | |
SymbolBucket.prototype.addSymbols = function(programName, quadsStart, quadsEnd, scale, keepUpright, alongLine, placementAngle) { | |
var group = this.prepareArrayGroup(programName, 4 * (quadsEnd - quadsStart)); | |
var elementArray = group.elementArray; | |
var layoutVertexArray = group.layoutVertexArray; | |
var zoom = this.zoom; | |
var placementZoom = Math.max(Math.log(scale) / Math.LN2 + zoom, 0); | |
for (var k = quadsStart; k < quadsEnd; k++) { | |
var symbol = this.symbolQuadsArray.get(k).SymbolQuad; | |
// drop upside down versions of glyphs | |
var a = (symbol.anchorAngle + placementAngle + Math.PI) % (Math.PI * 2); | |
if (keepUpright && alongLine && (a <= Math.PI / 2 || a > Math.PI * 3 / 2)) continue; | |
var tl = symbol.tl, | |
tr = symbol.tr, | |
bl = symbol.bl, | |
br = symbol.br, | |
tex = symbol.tex, | |
anchorPoint = symbol.anchorPoint, | |
minZoom = Math.max(zoom + Math.log(symbol.minScale) / Math.LN2, placementZoom), | |
maxZoom = Math.min(zoom + Math.log(symbol.maxScale) / Math.LN2, 25); | |
if (maxZoom <= minZoom) continue; | |
// Lower min zoom so that while fading out the label it can be shown outside of collision-free zoom levels | |
if (minZoom === placementZoom) minZoom = 0; | |
// Encode angle of glyph | |
var glyphAngle = Math.round((symbol.glyphAngle / (Math.PI * 2)) * 256); | |
var index = addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, tl.x, tl.y, tex.x, tex.y, minZoom, maxZoom, placementZoom, glyphAngle); | |
addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, tr.x, tr.y, tex.x + tex.w, tex.y, minZoom, maxZoom, placementZoom, glyphAngle); | |
addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, bl.x, bl.y, tex.x, tex.y + tex.h, minZoom, maxZoom, placementZoom, glyphAngle); | |
addVertex(layoutVertexArray, anchorPoint.x, anchorPoint.y, br.x, br.y, tex.x + tex.w, tex.y + tex.h, minZoom, maxZoom, placementZoom, glyphAngle); | |
elementArray.emplaceBack(index, index + 1, index + 2); | |
elementArray.emplaceBack(index + 1, index + 2, index + 3); | |
} | |
}; | |
SymbolBucket.prototype.updateIcons = function(icons) { | |
this.recalculateStyleLayers(); | |
var iconValue = this.layer.layout['icon-image']; | |
if (!iconValue) return; | |
for (var i = 0; i < this.features.length; i++) { | |
var iconName = resolveTokens(this.features[i].properties, iconValue); | |
if (iconName) | |
icons[iconName] = true; | |
} | |
}; | |
SymbolBucket.prototype.updateFont = function(stacks) { | |
this.recalculateStyleLayers(); | |
var fontName = this.layer.layout['text-font'], | |
stack = stacks[fontName] = stacks[fontName] || {}; | |
this.textFeatures = resolveText(this.features, this.layer.layout, stack); | |
}; | |
SymbolBucket.prototype.addToDebugBuffers = function(collisionTile) { | |
var group = this.prepareArrayGroup('collisionBox', 0); | |
var layoutVertexArray = group.layoutVertexArray; | |
var angle = -collisionTile.angle; | |
var yStretch = collisionTile.yStretch; | |
for (var j = this.symbolInstancesStartIndex; j < this.symbolInstancesEndIndex; j++) { | |
var symbolInstance = this.symbolInstancesArray.get(j); | |
symbolInstance.textCollisionFeature = {boxStartIndex: symbolInstance.textBoxStartIndex, boxEndIndex: symbolInstance.textBoxEndIndex}; | |
symbolInstance.iconCollisionFeature = {boxStartIndex: symbolInstance.iconBoxStartIndex, boxEndIndex: symbolInstance.iconBoxEndIndex}; | |
for (var i = 0; i < 2; i++) { | |
var feature = symbolInstance[i === 0 ? 'textCollisionFeature' : 'iconCollisionFeature']; | |
if (!feature) continue; | |
for (var b = feature.boxStartIndex; b < feature.boxEndIndex; b++) { | |
var box = this.collisionBoxArray.get(b); | |
var anchorPoint = box.anchorPoint; | |
var tl = new Point(box.x1, box.y1 * yStretch)._rotate(angle); | |
var tr = new Point(box.x2, box.y1 * yStretch)._rotate(angle); | |
var bl = new Point(box.x1, box.y2 * yStretch)._rotate(angle); | |
var br = new Point(box.x2, box.y2 * yStretch)._rotate(angle); | |
var maxZoom = Math.max(0, Math.min(25, this.zoom + Math.log(box.maxScale) / Math.LN2)); | |
var placementZoom = Math.max(0, Math.min(25, this.zoom + Math.log(box.placementScale) / Math.LN2)); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, tl, maxZoom, placementZoom); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, tr, maxZoom, placementZoom); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, tr, maxZoom, placementZoom); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, br, maxZoom, placementZoom); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, br, maxZoom, placementZoom); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, bl, maxZoom, placementZoom); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, bl, maxZoom, placementZoom); | |
this.addCollisionBoxVertex(layoutVertexArray, anchorPoint, tl, maxZoom, placementZoom); | |
} | |
} | |
} | |
}; | |
SymbolBucket.prototype.addSymbolInstance = function(anchor, line, shapedText, shapedIcon, layer, addToBuffers, index, collisionBoxArray, featureIndex, sourceLayerIndex, bucketIndex, | |
textBoxScale, textPadding, textAlongLine, | |
iconBoxScale, iconPadding, iconAlongLine, globalProperties, featureProperties) { | |
var glyphQuadStartIndex, glyphQuadEndIndex, iconQuadStartIndex, iconQuadEndIndex, textCollisionFeature, iconCollisionFeature, glyphQuads, iconQuads; | |
if (shapedText) { | |
glyphQuads = addToBuffers ? getGlyphQuads(anchor, shapedText, textBoxScale, line, layer, textAlongLine) : []; | |
textCollisionFeature = new CollisionFeature(collisionBoxArray, line, anchor, featureIndex, sourceLayerIndex, bucketIndex, shapedText, textBoxScale, textPadding, textAlongLine, false); | |
} | |
glyphQuadStartIndex = this.symbolQuadsArray.length; | |
if (glyphQuads && glyphQuads.length) { | |
for (var i = 0; i < glyphQuads.length; i++) { | |
this.addSymbolQuad(glyphQuads[i]); | |
} | |
} | |
glyphQuadEndIndex = this.symbolQuadsArray.length; | |
var textBoxStartIndex = textCollisionFeature ? textCollisionFeature.boxStartIndex : this.collisionBoxArray.length; | |
var textBoxEndIndex = textCollisionFeature ? textCollisionFeature.boxEndIndex : this.collisionBoxArray.length; | |
if (shapedIcon) { | |
iconQuads = addToBuffers ? getIconQuads(anchor, shapedIcon, iconBoxScale, line, layer, iconAlongLine, shapedText, globalProperties, featureProperties) : []; | |
iconCollisionFeature = new CollisionFeature(collisionBoxArray, line, anchor, featureIndex, sourceLayerIndex, bucketIndex, shapedIcon, iconBoxScale, iconPadding, iconAlongLine, true); | |
} | |
iconQuadStartIndex = this.symbolQuadsArray.length; | |
if (iconQuads && iconQuads.length === 1) { | |
this.addSymbolQuad(iconQuads[0]); | |
} | |
iconQuadEndIndex = this.symbolQuadsArray.length; | |
var iconBoxStartIndex = iconCollisionFeature ? iconCollisionFeature.boxStartIndex : this.collisionBoxArray.length; | |
var iconBoxEndIndex = iconCollisionFeature ? iconCollisionFeature.boxEndIndex : this.collisionBoxArray.length; | |
return this.symbolInstancesArray.emplaceBack( | |
textBoxStartIndex, | |
textBoxEndIndex, | |
iconBoxStartIndex, | |
iconBoxEndIndex, | |
glyphQuadStartIndex, | |
glyphQuadEndIndex, | |
iconQuadStartIndex, | |
iconQuadEndIndex, | |
anchor.x, | |
anchor.y, | |
index); | |
}; | |
SymbolBucket.prototype.addSymbolQuad = function(symbolQuad) { | |
return this.symbolQuadsArray.emplaceBack( | |
// anchorPoints | |
symbolQuad.anchorPoint.x, | |
symbolQuad.anchorPoint.y, | |
// corners | |
symbolQuad.tl.x, | |
symbolQuad.tl.y, | |
symbolQuad.tr.x, | |
symbolQuad.tr.y, | |
symbolQuad.bl.x, | |
symbolQuad.bl.y, | |
symbolQuad.br.x, | |
symbolQuad.br.y, | |
// texture | |
symbolQuad.tex.h, | |
symbolQuad.tex.w, | |
symbolQuad.tex.x, | |
symbolQuad.tex.y, | |
//angle | |
symbolQuad.anchorAngle, | |
symbolQuad.glyphAngle, | |
// scales | |
symbolQuad.maxScale, | |
symbolQuad.minScale); | |
}; | |
},{"../../symbol/anchor":58,"../../symbol/clip_line":60,"../../symbol/collision_feature":62,"../../symbol/get_anchors":64,"../../symbol/mergelines":67,"../../symbol/quads":68,"../../symbol/resolve_text":69,"../../symbol/shaping":70,"../../util/token":107,"../../util/util":108,"../bucket":1,"../load_geometry":8,"point-geometry":177}],6:[function(require,module,exports){ | |
'use strict'; | |
module.exports = Buffer; | |
/** | |
* The `Buffer` class turns a `StructArray` into a WebGL buffer. Each member of the StructArray's | |
* Struct type is converted to a WebGL atribute. | |
* | |
* @class Buffer | |
* @private | |
* @param {object} array A serialized StructArray. | |
* @param {object} arrayType A serialized StructArrayType. | |
* @param {BufferType} type | |
*/ | |
function Buffer(array, arrayType, type) { | |
this.arrayBuffer = array.arrayBuffer; | |
this.length = array.length; | |
this.attributes = arrayType.members; | |
this.itemSize = arrayType.bytesPerElement; | |
this.type = type; | |
this.arrayType = arrayType; | |
} | |
/** | |
* Bind this buffer to a WebGL context. | |
* @private | |
* @param gl The WebGL context | |
*/ | |
Buffer.prototype.bind = function(gl) { | |
var type = gl[this.type]; | |
if (!this.buffer) { | |
this.buffer = gl.createBuffer(); | |
gl.bindBuffer(type, this.buffer); | |
gl.bufferData(type, this.arrayBuffer, gl.STATIC_DRAW); | |
// dump array buffer once it's bound to gl | |
this.arrayBuffer = null; | |
} else { | |
gl.bindBuffer(type, this.buffer); | |
} | |
}; | |
/** | |
* @enum {string} AttributeType | |
* @private | |
* @readonly | |
*/ | |
var AttributeType = { | |
Int8: 'BYTE', | |
Uint8: 'UNSIGNED_BYTE', | |
Int16: 'SHORT', | |
Uint16: 'UNSIGNED_SHORT' | |
}; | |
/** | |
* Set the attribute pointers in a WebGL context | |
* @private | |
* @param gl The WebGL context | |
* @param program The active WebGL program | |
*/ | |
Buffer.prototype.setVertexAttribPointers = function(gl, program) { | |
for (var j = 0; j < this.attributes.length; j++) { | |
var member = this.attributes[j]; | |
var attribIndex = program[member.name]; | |
if (attribIndex !== undefined) { | |
gl.vertexAttribPointer( | |
attribIndex, | |
member.components, | |
gl[AttributeType[member.type]], | |
false, | |
this.arrayType.bytesPerElement, | |
member.offset | |
); | |
} | |
} | |
}; | |
/** | |
* Destroy the GL buffer bound to the given WebGL context | |
* @private | |
* @param gl The WebGL context | |
*/ | |
Buffer.prototype.destroy = function(gl) { | |
if (this.buffer) { | |
gl.deleteBuffer(this.buffer); | |
} | |
}; | |
/** | |
* @enum {string} BufferType | |
* @private | |
* @readonly | |
*/ | |
Buffer.BufferType = { | |
VERTEX: 'ARRAY_BUFFER', | |
ELEMENT: 'ELEMENT_ARRAY_BUFFER' | |
}; | |
/** | |
* An `BufferType.ELEMENT` buffer holds indicies of a corresponding `BufferType.VERTEX` buffer. | |
* These indicies are stored in the `BufferType.ELEMENT` buffer as `UNSIGNED_SHORT`s. | |
* | |
* @private | |
* @readonly | |
*/ | |
Buffer.ELEMENT_ATTRIBUTE_TYPE = 'Uint16'; | |
/** | |
* WebGL performs best if vertex attribute offsets are aligned to 4 byte boundaries. | |
* @private | |
* @readonly | |
*/ | |
Buffer.VERTEX_ATTRIBUTE_ALIGNMENT = 4; | |
},{}],7:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
var loadGeometry = require('./load_geometry'); | |
var EXTENT = require('./bucket').EXTENT; | |
var featureFilter = require('feature-filter'); | |
var StructArrayType = require('../util/struct_array'); | |
var Grid = require('grid-index'); | |
var DictionaryCoder = require('../util/dictionary_coder'); | |
var vt = require('vector-tile'); | |
var Protobuf = require('pbf'); | |
var GeoJSONFeature = require('../util/vectortile_to_geojson'); | |
var arraysIntersect = require('../util/util').arraysIntersect; | |
var intersection = require('../util/intersection_tests'); | |
var multiPolygonIntersectsBufferedMultiPoint = intersection.multiPolygonIntersectsBufferedMultiPoint; | |
var multiPolygonIntersectsMultiPolygon = intersection.multiPolygonIntersectsMultiPolygon; | |
var multiPolygonIntersectsBufferedMultiLine = intersection.multiPolygonIntersectsBufferedMultiLine; | |
var FeatureIndexArray = new StructArrayType({ | |
members: [ | |
// the index of the feature in the original vectortile | |
{ type: 'Uint32', name: 'featureIndex' }, | |
// the source layer the feature appears in | |
{ type: 'Uint16', name: 'sourceLayerIndex' }, | |
// the bucket the feature appears in | |
{ type: 'Uint16', name: 'bucketIndex' } | |
]}); | |
module.exports = FeatureIndex; | |
function FeatureIndex(coord, overscaling, collisionTile) { | |
if (coord.grid) { | |
var serialized = coord; | |
var rawTileData = overscaling; | |
coord = serialized.coord; | |
overscaling = serialized.overscaling; | |
this.grid = new Grid(serialized.grid); | |
this.featureIndexArray = new FeatureIndexArray(serialized.featureIndexArray); | |
this.rawTileData = rawTileData; | |
this.bucketLayerIDs = serialized.bucketLayerIDs; | |
} else { | |
this.grid = new Grid(EXTENT, 16, 0); | |
this.featureIndexArray = new FeatureIndexArray(); | |
} | |
this.coord = coord; | |
this.overscaling = overscaling; | |
this.x = coord.x; | |
this.y = coord.y; | |
this.z = coord.z - Math.log(overscaling) / Math.LN2; | |
this.setCollisionTile(collisionTile); | |
} | |
FeatureIndex.prototype.insert = function(feature, featureIndex, sourceLayerIndex, bucketIndex) { | |
var key = this.featureIndexArray.length; | |
this.featureIndexArray.emplaceBack(featureIndex, sourceLayerIndex, bucketIndex); | |
var geometry = loadGeometry(feature); | |
for (var r = 0; r < geometry.length; r++) { | |
var ring = geometry[r]; | |
var bbox = [Infinity, Infinity, -Infinity, -Infinity]; | |
for (var i = 0; i < ring.length; i++) { | |
var p = ring[i]; | |
bbox[0] = Math.min(bbox[0], p.x); | |
bbox[1] = Math.min(bbox[1], p.y); | |
bbox[2] = Math.max(bbox[2], p.x); | |
bbox[3] = Math.max(bbox[3], p.y); | |
} | |
this.grid.insert(key, bbox[0], bbox[1], bbox[2], bbox[3]); | |
} | |
}; | |
FeatureIndex.prototype.setCollisionTile = function(collisionTile) { | |
this.collisionTile = collisionTile; | |
}; | |
FeatureIndex.prototype.serialize = function() { | |
var data = { | |
coord: this.coord, | |
overscaling: this.overscaling, | |
grid: this.grid.toArrayBuffer(), | |
featureIndexArray: this.featureIndexArray.serialize(), | |
bucketLayerIDs: this.bucketLayerIDs | |
}; | |
return { | |
data: data, | |
transferables: [data.grid, data.featureIndexArray.arrayBuffer] | |
}; | |
}; | |
function translateDistance(translate) { | |
return Math.sqrt(translate[0] * translate[0] + translate[1] * translate[1]); | |
} | |
// Finds features in this tile at a particular position. | |
FeatureIndex.prototype.query = function(args, styleLayers) { | |
if (!this.vtLayers) { | |
this.vtLayers = new vt.VectorTile(new Protobuf(new Uint8Array(this.rawTileData))).layers; | |
this.sourceLayerCoder = new DictionaryCoder(this.vtLayers ? Object.keys(this.vtLayers).sort() : ['_geojsonTileLayer']); | |
} | |
var result = {}; | |
var params = args.params || {}, | |
pixelsToTileUnits = EXTENT / args.tileSize / args.scale, | |
filter = featureFilter(params.filter); | |
// Features are indexed their original geometries. The rendered geometries may | |
// be buffered, translated or offset. Figure out how much the search radius needs to be | |
// expanded by to include these features. | |
var additionalRadius = 0; | |
for (var id in styleLayers) { | |
var styleLayer = styleLayers[id]; | |
var paint = styleLayer.paint; | |
var styleLayerDistance = 0; | |
if (styleLayer.type === 'line') { | |
styleLayerDistance = getLineWidth(paint) / 2 + Math.abs(paint['line-offset']) + translateDistance(paint['line-translate']); | |
} else if (styleLayer.type === 'fill') { | |
styleLayerDistance = translateDistance(paint['fill-translate']); | |
} else if (styleLayer.type === 'circle') { | |
styleLayerDistance = paint['circle-radius'] + translateDistance(paint['circle-translate']); | |
} | |
additionalRadius = Math.max(additionalRadius, styleLayerDistance * pixelsToTileUnits); | |
} | |
var queryGeometry = args.queryGeometry.map(function(q) { | |
return q.map(function(p) { | |
return new Point(p.x, p.y); | |
}); | |
}); | |
var minX = Infinity; | |
var minY = Infinity; | |
var maxX = -Infinity; | |
var maxY = -Infinity; | |
for (var i = 0; i < queryGeometry.length; i++) { | |
var ring = queryGeometry[i]; | |
for (var k = 0; k < ring.length; k++) { | |
var p = ring[k]; | |
minX = Math.min(minX, p.x); | |
minY = Math.min(minY, p.y); | |
maxX = Math.max(maxX, p.x); | |
maxY = Math.max(maxY, p.y); | |
} | |
} | |
var matching = this.grid.query(minX - additionalRadius, minY - additionalRadius, maxX + additionalRadius, maxY + additionalRadius); | |
matching.sort(topDownFeatureComparator); | |
this.filterMatching(result, matching, this.featureIndexArray, queryGeometry, filter, params.layers, styleLayers, args.bearing, pixelsToTileUnits); | |
var matchingSymbols = this.collisionTile.queryRenderedSymbols(minX, minY, maxX, maxY, args.scale); | |
matchingSymbols.sort(); | |
this.filterMatching(result, matchingSymbols, this.collisionTile.collisionBoxArray, queryGeometry, filter, params.layers, styleLayers, args.bearing, pixelsToTileUnits); | |
return result; | |
}; | |
function topDownFeatureComparator(a, b) { | |
return b - a; | |
} | |
function getLineWidth(paint) { | |
if (paint['line-gap-width'] > 0) { | |
return paint['line-gap-width'] + 2 * paint['line-width']; | |
} else { | |
return paint['line-width']; | |
} | |
} | |
FeatureIndex.prototype.filterMatching = function(result, matching, array, queryGeometry, filter, filterLayerIDs, styleLayers, bearing, pixelsToTileUnits) { | |
var previousIndex; | |
for (var k = 0; k < matching.length; k++) { | |
var index = matching[k]; | |
// don't check the same feature more than once | |
if (index === previousIndex) continue; | |
previousIndex = index; | |
var match = array.get(index); | |
var layerIDs = this.bucketLayerIDs[match.bucketIndex]; | |
if (filterLayerIDs && !arraysIntersect(filterLayerIDs, layerIDs)) continue; | |
var sourceLayerName = this.sourceLayerCoder.decode(match.sourceLayerIndex); | |
var sourceLayer = this.vtLayers[sourceLayerName]; | |
var feature = sourceLayer.feature(match.featureIndex); | |
if (!filter(feature)) continue; | |
var geometry = null; | |
for (var l = 0; l < layerIDs.length; l++) { | |
var layerID = layerIDs[l]; | |
if (filterLayerIDs && filterLayerIDs.indexOf(layerID) < 0) { | |
continue; | |
} | |
var styleLayer = styleLayers[layerID]; | |
if (!styleLayer) continue; | |
var translatedPolygon; | |
if (styleLayer.type !== 'symbol') { | |
// all symbols already match the style | |
if (!geometry) geometry = loadGeometry(feature); | |
var paint = styleLayer.paint; | |
if (styleLayer.type === 'line') { | |
translatedPolygon = translate(queryGeometry, | |
paint['line-translate'], paint['line-translate-anchor'], | |
bearing, pixelsToTileUnits); | |
var halfWidth = getLineWidth(paint) / 2 * pixelsToTileUnits; | |
if (paint['line-offset']) { | |
geometry = offsetLine(geometry, paint['line-offset'] * pixelsToTileUnits); | |
} | |
if (!multiPolygonIntersectsBufferedMultiLine(translatedPolygon, geometry, halfWidth)) continue; | |
} else if (styleLayer.type === 'fill') { | |
translatedPolygon = translate(queryGeometry, | |
paint['fill-translate'], paint['fill-translate-anchor'], | |
bearing, pixelsToTileUnits); | |
if (!multiPolygonIntersectsMultiPolygon(translatedPolygon, geometry)) continue; | |
} else if (styleLayer.type === 'circle') { | |
translatedPolygon = translate(queryGeometry, | |
paint['circle-translate'], paint['circle-translate-anchor'], | |
bearing, pixelsToTileUnits); | |
var circleRadius = paint['circle-radius'] * pixelsToTileUnits; | |
if (!multiPolygonIntersectsBufferedMultiPoint(translatedPolygon, geometry, circleRadius)) continue; | |
} | |
} | |
var geojsonFeature = new GeoJSONFeature(feature, this.z, this.x, this.y); | |
geojsonFeature.layer = styleLayer.serialize({ | |
includeRefProperties: true | |
}); | |
var layerResult = result[layerID]; | |
if (layerResult === undefined) { | |
layerResult = result[layerID] = []; | |
} | |
layerResult.push(geojsonFeature); | |
} | |
} | |
}; | |
function translate(queryGeometry, translate, translateAnchor, bearing, pixelsToTileUnits) { | |
if (!translate[0] && !translate[1]) { | |
return queryGeometry; | |
} | |
translate = Point.convert(translate); | |
if (translateAnchor === "viewport") { | |
translate._rotate(-bearing); | |
} | |
var translated = []; | |
for (var i = 0; i < queryGeometry.length; i++) { | |
var ring = queryGeometry[i]; | |
var translatedRing = []; | |
for (var k = 0; k < ring.length; k++) { | |
translatedRing.push(ring[k].sub(translate._mult(pixelsToTileUnits))); | |
} | |
translated.push(translatedRing); | |
} | |
return translated; | |
} | |
function offsetLine(rings, offset) { | |
var newRings = []; | |
var zero = new Point(0, 0); | |
for (var k = 0; k < rings.length; k++) { | |
var ring = rings[k]; | |
var newRing = []; | |
for (var i = 0; i < ring.length; i++) { | |
var a = ring[i - 1]; | |
var b = ring[i]; | |
var c = ring[i + 1]; | |
var aToB = i === 0 ? zero : b.sub(a)._unit()._perp(); | |
var bToC = i === ring.length - 1 ? zero : c.sub(b)._unit()._perp(); | |
var extrude = aToB._add(bToC)._unit(); | |
var cosHalfAngle = extrude.x * bToC.x + extrude.y * bToC.y; | |
extrude._mult(1 / cosHalfAngle); | |
newRing.push(extrude._mult(offset)._add(b)); | |
} | |
newRings.push(newRing); | |
} | |
return newRings; | |
} | |
},{"../util/dictionary_coder":99,"../util/intersection_tests":103,"../util/struct_array":106,"../util/util":108,"../util/vectortile_to_geojson":109,"./bucket":1,"./load_geometry":8,"feature-filter":124,"grid-index":145,"pbf":175,"point-geometry":177,"vector-tile":187}],8:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var EXTENT = require('./bucket').EXTENT; | |
var assert = require('assert'); | |
// These bounds define the minimum and maximum supported coordinate values. | |
// While visible coordinates are within [0, EXTENT], tiles may theoretically | |
// contain cordinates within [-Infinity, Infinity]. Our range is limited by the | |
// number of bits used to represent the coordinate. | |
function createBounds(bits) { | |
return { | |
min: -1 * Math.pow(2, bits - 1), | |
max: Math.pow(2, bits - 1) - 1 | |
}; | |
} | |
var boundsLookup = { | |
15: createBounds(15), | |
16: createBounds(16) | |
}; | |
/** | |
* Loads a geometry from a VectorTileFeature and scales it to the common extent | |
* used internally. | |
* @param {VectorTileFeature} feature | |
* @param {number} [bits=16] The number of signed integer bits available to store | |
* each coordinate. A warning will be issued if any coordinate will not fits | |
* in the specified number of bits. | |
* @private | |
*/ | |
module.exports = function loadGeometry(feature, bits) { | |
var bounds = boundsLookup[bits || 16]; | |
assert(bounds); | |
var scale = EXTENT / feature.extent; | |
var geometry = feature.loadGeometry(); | |
for (var r = 0; r < geometry.length; r++) { | |
var ring = geometry[r]; | |
for (var p = 0; p < ring.length; p++) { | |
var point = ring[p]; | |
// round here because mapbox-gl-native uses integers to represent | |
// points and we need to do the same to avoid renering differences. | |
point.x = Math.round(point.x * scale); | |
point.y = Math.round(point.y * scale); | |
if (point.x < bounds.min || point.x > bounds.max || point.y < bounds.min || point.y > bounds.max) { | |
util.warnOnce('Geometry exceeds allowed extent, reduce your vector tile buffer size'); | |
} | |
} | |
} | |
return geometry; | |
}; | |
},{"../util/util":108,"./bucket":1,"assert":110}],9:[function(require,module,exports){ | |
'use strict'; | |
module.exports = Coordinate; | |
/** | |
* A coordinate is a column, row, zoom combination, often used | |
* as the data component of a tile. | |
* | |
* @param {number} column | |
* @param {number} row | |
* @param {number} zoom | |
* @private | |
*/ | |
function Coordinate(column, row, zoom) { | |
this.column = column; | |
this.row = row; | |
this.zoom = zoom; | |
} | |
Coordinate.prototype = { | |
/** | |
* Create a clone of this coordinate that can be mutated without | |
* changing the original coordinate | |
* | |
* @returns {Coordinate} clone | |
* @private | |
* var coord = new Coordinate(0, 0, 0); | |
* var c2 = coord.clone(); | |
* // since coord is cloned, modifying a property of c2 does | |
* // not modify it. | |
* c2.zoom = 2; | |
*/ | |
clone: function() { | |
return new Coordinate(this.column, this.row, this.zoom); | |
}, | |
/** | |
* Zoom this coordinate to a given zoom level. This returns a new | |
* coordinate object, not mutating the old one. | |
* | |
* @param {number} zoom | |
* @returns {Coordinate} zoomed coordinate | |
* @private | |
* @example | |
* var coord = new Coordinate(0, 0, 0); | |
* var c2 = coord.zoomTo(1); | |
* c2 // equals new Coordinate(0, 0, 1); | |
*/ | |
zoomTo: function(zoom) { return this.clone()._zoomTo(zoom); }, | |
/** | |
* Subtract the column and row values of this coordinate from those | |
* of another coordinate. The other coordinat will be zoomed to the | |
* same level as `this` before the subtraction occurs | |
* | |
* @param {Coordinate} c other coordinate | |
* @returns {Coordinate} result | |
* @private | |
*/ | |
sub: function(c) { return this.clone()._sub(c); }, | |
_zoomTo: function(zoom) { | |
var scale = Math.pow(2, zoom - this.zoom); | |
this.column *= scale; | |
this.row *= scale; | |
this.zoom = zoom; | |
return this; | |
}, | |
_sub: function(c) { | |
c = c.zoomTo(this.zoom); | |
this.column -= c.column; | |
this.row -= c.row; | |
return this; | |
} | |
}; | |
},{}],10:[function(require,module,exports){ | |
'use strict'; | |
module.exports = LngLat; | |
var wrap = require('../util/util').wrap; | |
/** | |
* A `LngLat` object represents a given longitude and latitude coordinate, measured in degrees. | |
* | |
* Mapbox GL uses longitude, latitude coordinate order (as opposed to latitude, longitude) to match GeoJSON. | |
* | |
* Note that any Mapbox GL method that accepts a `LngLat` object as an argument or option | |
* can also accept an `Array` of two numbers and will perform an implicit conversion. | |
* This flexible type is documented as [`LngLatLike`](#LngLatLike). | |
* | |
* @class LngLat | |
* @param {number} lng Longitude, measured in degrees. | |
* @param {number} lat Latitude, measured in degrees. | |
* @example | |
* var ll = new mapboxgl.LngLat(-73.9749, 40.7736); | |
*/ | |
function LngLat(lng, lat) { | |
if (isNaN(lng) || isNaN(lat)) { | |
throw new Error('Invalid LngLat object: (' + lng + ', ' + lat + ')'); | |
} | |
this.lng = +lng; | |
this.lat = +lat; | |
if (this.lat > 90 || this.lat < -90) { | |
throw new Error('Invalid LngLat latitude value: must be between -90 and 90'); | |
} | |
} | |
/** | |
* Returns a new `LngLat` object whose longitude is wrapped to the range (-180, 180). | |
* | |
* @returns {LngLat} The wrapped `LngLat` object. | |
* @example | |
* var ll = new mapboxgl.LngLat(286.0251, 40.7736); | |
* var wrapped = ll.wrap(); | |
* wrapped.lng; // = -73.9749 | |
*/ | |
LngLat.prototype.wrap = function () { | |
return new LngLat(wrap(this.lng, -180, 180), this.lat); | |
}; | |
/** | |
* Returns the coordinates represented as an array of two numbers. | |
* | |
* @returns {Array<number>} The coordinates represeted as an array of longitude and latitude. | |
* @example | |
* var ll = new mapboxgl.LngLat(-73.9749, 40.7736); | |
* ll.toArray(); // = [-73.9749, 40.7736] | |
*/ | |
LngLat.prototype.toArray = function () { | |
return [this.lng, this.lat]; | |
}; | |
/** | |
* Returns the coordinates represent as a string. | |
* | |
* @returns {string} The coordinates represented as a string of the format `'LngLat(lng, lat)'`. | |
* @example | |
* var ll = new mapboxgl.LngLat(-73.9749, 40.7736); | |
* ll.toString(); // = "LngLat(-73.9749, 40.7736)" | |
*/ | |
LngLat.prototype.toString = function () { | |
return 'LngLat(' + this.lng + ', ' + this.lat + ')'; | |
}; | |
/** | |
* Converts an array of two numbers to a `LngLat` object. | |
* | |
* If a `LngLat` object is passed in, the function returns it unchanged. | |
* | |
* @param {LngLatLike} input An array of two numbers to convert, or a `LngLat` object to return. | |
* @returns {LngLat} A new `LngLat` object, if a conversion occurred, or the original `LngLat` object. | |
* @example | |
* var arr = [-73.9749, 40.7736]; | |
* var ll = mapboxgl.LngLat.convert(arr); | |
* ll; // = LngLat {lng: -73.9749, lat: 40.7736} | |
*/ | |
LngLat.convert = function (input) { | |
if (input instanceof LngLat) { | |
return input; | |
} | |
if (Array.isArray(input)) { | |
return new LngLat(input[0], input[1]); | |
} | |
return input; | |
}; | |
},{"../util/util":108}],11:[function(require,module,exports){ | |
'use strict'; | |
module.exports = LngLatBounds; | |
var LngLat = require('./lng_lat'); | |
/** | |
* A `LngLatBounds` object represents a geographical bounding box, | |
* defined by its southwest and northeast points in longitude and latitude. | |
* | |
* If no arguments are provided to the constructor, a `null` bounding box is created. | |
* | |
* Note that any Mapbox GL method that accepts a `LngLatBounds` object as an argument or option | |
* can also accept an `Array` of two [`LngLatLike`](#LngLatLike) constructs and will perform an implicit conversion. | |
* This flexible type is documented as [`LngLatBoundsLike`](#LngLatBoundsLike). | |
* | |
* @class LngLatBounds | |
* @param {LngLatLike} sw The southwest corner of the bounding box. | |
* @param {LngLatLike} ne The northeast corner of the bounding box. | |
* @example | |
* var sw = new mapboxgl.LngLat(-73.9876, 40.7661); | |
* var ne = new mapboxgl.LngLat(-73.9397, 40.8002); | |
* var llb = new mapboxgl.LngLatBounds(sw, ne); | |
*/ | |
function LngLatBounds(sw, ne) { | |
if (!sw) { | |
return; | |
} else if (ne) { | |
this.extend(sw).extend(ne); | |
} else if (sw.length === 4) { | |
this.extend([sw[0], sw[1]]).extend([sw[2], sw[3]]); | |
} else { | |
this.extend(sw[0]).extend(sw[1]); | |
} | |
} | |
LngLatBounds.prototype = { | |
/** | |
* Extends the bounding box to include an area represented by a `LngLat` or `LngLatBounds`. | |
* | |
* @param {LngLatLike|LngLatBoundsLike} obj The area that the bounding box will extend to include. | |
* @returns {LngLatBounds} `this` | |
*/ | |
extend: function(obj) { | |
var sw = this._sw, | |
ne = this._ne, | |
sw2, ne2; | |
if (obj instanceof LngLat) { | |
sw2 = obj; | |
ne2 = obj; | |
} else if (obj instanceof LngLatBounds) { | |
sw2 = obj._sw; | |
ne2 = obj._ne; | |
if (!sw2 || !ne2) return this; | |
} else { | |
return obj ? this.extend(LngLat.convert(obj) || LngLatBounds.convert(obj)) : this; | |
} | |
if (!sw && !ne) { | |
this._sw = new LngLat(sw2.lng, sw2.lat); | |
this._ne = new LngLat(ne2.lng, ne2.lat); | |
} else { | |
sw.lng = Math.min(sw2.lng, sw.lng); | |
sw.lat = Math.min(sw2.lat, sw.lat); | |
ne.lng = Math.max(ne2.lng, ne.lng); | |
ne.lat = Math.max(ne2.lat, ne.lat); | |
} | |
return this; | |
}, | |
/** | |
* Returns the geographical coordinate equidistant from the bounding box's corners. | |
* | |
* @returns {LngLat} The bounding box's center. | |
* @example | |
* var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]); | |
* llb.getCenter(); // = LngLat {lng: -73.96365, lat: 40.78315} | |
*/ | |
getCenter: function() { | |
return new LngLat((this._sw.lng + this._ne.lng) / 2, (this._sw.lat + this._ne.lat) / 2); | |
}, | |
/** | |
* Returns the southwest corner of the bounding box. | |
* | |
* @returns {LngLat} The southwest corner of the bounding box. | |
*/ | |
getSouthWest: function() { return this._sw; }, | |
/** | |
* Returns the northeast corner of the bounding box. | |
* | |
* @returns {LngLat} The northeast corner of the bounding box. | |
*/ | |
getNorthEast: function() { return this._ne; }, | |
/** | |
* Returns the northwest corner of the bounding box. | |
* | |
* @returns {LngLat} The northwest corner of the bounding box. | |
*/ | |
getNorthWest: function() { return new LngLat(this.getWest(), this.getNorth()); }, | |
/** | |
* Returns the southeast corner of the bounding box. | |
* | |
* @returns {LngLat} The southeast corner of the bounding box. | |
*/ | |
getSouthEast: function() { return new LngLat(this.getEast(), this.getSouth()); }, | |
/** | |
* Returns the west edge of the bounding box. | |
* | |
* @returns {LngLat} The west edge of the bounding box. | |
*/ | |
getWest: function() { return this._sw.lng; }, | |
/** | |
* Returns the south edge of the bounding box. | |
* | |
* @returns {LngLat} The south edge of the bounding box. | |
*/ | |
getSouth: function() { return this._sw.lat; }, | |
/** | |
* Returns the east edge of the bounding box. | |
* | |
* @returns {LngLat} The east edge of the bounding box. | |
*/ | |
getEast: function() { return this._ne.lng; }, | |
/** | |
* Returns the north edge of the bounding box. | |
* | |
* @returns {LngLat} The north edge of the bounding box. | |
*/ | |
getNorth: function() { return this._ne.lat; }, | |
/** | |
* Returns the bounding box represented as an array. | |
* | |
* @returns {Array<Array<number>>} The bounding box represented as an array, consisting of the | |
* southwest and northeast coordinates of the bounding represented as arrays of numbers. | |
* @example | |
* var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]); | |
* llb.toArray(); // = [[-73.9876, 40.7661], [-73.9397, 40.8002]] | |
*/ | |
toArray: function () { | |
return [this._sw.toArray(), this._ne.toArray()]; | |
}, | |
/** | |
* Return the bounding box represented as a string. | |
* | |
* @returns {string} The bounding box represents as a string of the format | |
* `'LngLatBounds(LngLat(lng, lat), LngLat(lng, lat))'`. | |
* @example | |
* var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]); | |
* llb.toString(); // = "LngLatBounds(LngLat(-73.9876, 40.7661), LngLat(-73.9397, 40.8002))" | |
*/ | |
toString: function () { | |
return 'LngLatBounds(' + this._sw.toString() + ', ' + this._ne.toString() + ')'; | |
} | |
}; | |
/** | |
* Converts an array to a `LngLatBounds` object. | |
* | |
* If a `LngLatBounds` object is passed in, the function returns it unchanged. | |
* | |
* Internally, the function calls `LngLat#convert` to convert arrays to `LngLat` values. | |
* | |
* @param {LngLatBoundsLike} input An array of two coordinates to convert, or a `LngLatBounds` object to return. | |
* @returns {LngLatBounds} A new `LngLatBounds` object, if a conversion occurred, or the original `LngLatBounds` object. | |
* @example | |
* var arr = [[-73.9876, 40.7661], [-73.9397, 40.8002]]; | |
* var llb = mapboxgl.LngLatBounds.convert(arr); | |
* llb; // = LngLatBounds {_sw: LngLat {lng: -73.9876, lat: 40.7661}, _ne: LngLat {lng: -73.9397, lat: 40.8002}} | |
*/ | |
LngLatBounds.convert = function (input) { | |
if (!input || input instanceof LngLatBounds) return input; | |
return new LngLatBounds(input); | |
}; | |
},{"./lng_lat":10}],12:[function(require,module,exports){ | |
'use strict'; | |
var LngLat = require('./lng_lat'), | |
Point = require('point-geometry'), | |
Coordinate = require('./coordinate'), | |
wrap = require('../util/util').wrap, | |
interp = require('../util/interpolate'), | |
TileCoord = require('../source/tile_coord'), | |
EXTENT = require('../data/bucket').EXTENT, | |
glmatrix = require('gl-matrix'); | |
var vec4 = glmatrix.vec4, | |
mat4 = glmatrix.mat4, | |
mat2 = glmatrix.mat2; | |
module.exports = Transform; | |
/** | |
* A single transform, generally used for a single tile to be | |
* scaled, rotated, and zoomed. | |
* | |
* @param {number} minZoom | |
* @param {number} maxZoom | |
* @private | |
*/ | |
function Transform(minZoom, maxZoom) { | |
this.tileSize = 512; // constant | |
this._minZoom = minZoom || 0; | |
this._maxZoom = maxZoom || 22; | |
this.latRange = [-85.05113, 85.05113]; | |
this.width = 0; | |
this.height = 0; | |
this._center = new LngLat(0, 0); | |
this.zoom = 0; | |
this.angle = 0; | |
this._altitude = 1.5; | |
this._pitch = 0; | |
this._unmodified = true; | |
} | |
Transform.prototype = { | |
get minZoom() { return this._minZoom; }, | |
set minZoom(zoom) { | |
if (this._minZoom === zoom) return; | |
this._minZoom = zoom; | |
this.zoom = Math.max(this.zoom, zoom); | |
}, | |
get maxZoom() { return this._maxZoom; }, | |
set maxZoom(zoom) { | |
if (this._maxZoom === zoom) return; | |
this._maxZoom = zoom; | |
this.zoom = Math.min(this.zoom, zoom); | |
}, | |
get worldSize() { | |
return this.tileSize * this.scale; | |
}, | |
get centerPoint() { | |
return this.size._div(2); | |
}, | |
get size() { | |
return new Point(this.width, this.height); | |
}, | |
get bearing() { | |
return -this.angle / Math.PI * 180; | |
}, | |
set bearing(bearing) { | |
var b = -wrap(bearing, -180, 180) * Math.PI / 180; | |
if (this.angle === b) return; | |
this._unmodified = false; | |
this.angle = b; | |
this._calcMatrices(); | |
// 2x2 matrix for rotating points | |
this.rotationMatrix = mat2.create(); | |
mat2.rotate(this.rotationMatrix, this.rotationMatrix, this.angle); | |
}, | |
get pitch() { | |
return this._pitch / Math.PI * 180; | |
}, | |
set pitch(pitch) { | |
var p = Math.min(60, pitch) / 180 * Math.PI; | |
if (this._pitch === p) return; | |
this._unmodified = false; | |
this._pitch = p; | |
this._calcMatrices(); | |
}, | |
get altitude() { | |
return this._altitude; | |
}, | |
set altitude(altitude) { | |
var a = Math.max(0.75, altitude); | |
if (this._altitude === a) return; | |
this._unmodified = false; | |
this._altitude = a; | |
this._calcMatrices(); | |
}, | |
get zoom() { return this._zoom; }, | |
set zoom(zoom) { | |
var z = Math.min(Math.max(zoom, this.minZoom), this.maxZoom); | |
if (this._zoom === z) return; | |
this._unmodified = false; | |
this._zoom = z; | |
this.scale = this.zoomScale(z); | |
this.tileZoom = Math.floor(z); | |
this.zoomFraction = z - this.tileZoom; | |
this._calcMatrices(); | |
this._constrain(); | |
}, | |
get center() { return this._center; }, | |
set center(center) { | |
if (center.lat === this._center.lat && center.lng === this._center.lng) return; | |
this._unmodified = false; | |
this._center = center; | |
this._calcMatrices(); | |
this._constrain(); | |
}, | |
resize: function(width, height) { | |
this.width = width; | |
this.height = height; | |
this.pixelsToGLUnits = [2 / width, -2 / height]; | |
this._calcMatrices(); | |
this._constrain(); | |
}, | |
get unmodified() { return this._unmodified; }, | |
zoomScale: function(zoom) { return Math.pow(2, zoom); }, | |
scaleZoom: function(scale) { return Math.log(scale) / Math.LN2; }, | |
project: function(lnglat, worldSize) { | |
return new Point( | |
this.lngX(lnglat.lng, worldSize), | |
this.latY(lnglat.lat, worldSize)); | |
}, | |
unproject: function(point, worldSize) { | |
return new LngLat( | |
this.xLng(point.x, worldSize), | |
this.yLat(point.y, worldSize)); | |
}, | |
get x() { return this.lngX(this.center.lng); }, | |
get y() { return this.latY(this.center.lat); }, | |
get point() { return new Point(this.x, this.y); }, | |
/** | |
* latitude to absolute x coord | |
* @param {number} lon | |
* @param {number} [worldSize=this.worldSize] | |
* @returns {number} pixel coordinate | |
* @private | |
*/ | |
lngX: function(lng, worldSize) { | |
return (180 + lng) * (worldSize || this.worldSize) / 360; | |
}, | |
/** | |
* latitude to absolute y coord | |
* @param {number} lat | |
* @param {number} [worldSize=this.worldSize] | |
* @returns {number} pixel coordinate | |
* @private | |
*/ | |
latY: function(lat, worldSize) { | |
var y = 180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360)); | |
return (180 - y) * (worldSize || this.worldSize) / 360; | |
}, | |
xLng: function(x, worldSize) { | |
return x * 360 / (worldSize || this.worldSize) - 180; | |
}, | |
yLat: function(y, worldSize) { | |
var y2 = 180 - y * 360 / (worldSize || this.worldSize); | |
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90; | |
}, | |
panBy: function(offset) { | |
var point = this.centerPoint._add(offset); | |
this.center = this.pointLocation(point); | |
}, | |
setLocationAtPoint: function(lnglat, point) { | |
var c = this.locationCoordinate(lnglat); | |
var coordAtPoint = this.pointCoordinate(point); | |
var coordCenter = this.pointCoordinate(this.centerPoint); | |
var translate = coordAtPoint._sub(c); | |
this._unmodified = false; | |
this.center = this.coordinateLocation(coordCenter._sub(translate)); | |
}, | |
/** | |
* Given a location, return the screen point that corresponds to it | |
* @param {LngLat} lnglat location | |
* @returns {Point} screen point | |
* @private | |
*/ | |
locationPoint: function(lnglat) { | |
return this.coordinatePoint(this.locationCoordinate(lnglat)); | |
}, | |
/** | |
* Given a point on screen, return its lnglat | |
* @param {Point} p screen point | |
* @returns {LngLat} lnglat location | |
* @private | |
*/ | |
pointLocation: function(p) { | |
return this.coordinateLocation(this.pointCoordinate(p)); | |
}, | |
/** | |
* Given a geographical lnglat, return an unrounded | |
* coordinate that represents it at this transform's zoom level and | |
* worldsize. | |
* @param {LngLat} lnglat | |
* @returns {Coordinate} | |
* @private | |
*/ | |
locationCoordinate: function(lnglat) { | |
var k = this.zoomScale(this.tileZoom) / this.worldSize, | |
ll = LngLat.convert(lnglat); | |
return new Coordinate( | |
this.lngX(ll.lng) * k, | |
this.latY(ll.lat) * k, | |
this.tileZoom); | |
}, | |
/** | |
* Given a Coordinate, return its geographical position. | |
* @param {Coordinate} coord | |
* @returns {LngLat} lnglat | |
* @private | |
*/ | |
coordinateLocation: function(coord) { | |
var worldSize = this.zoomScale(coord.zoom); | |
return new LngLat( | |
this.xLng(coord.column, worldSize), | |
this.yLat(coord.row, worldSize)); | |
}, | |
pointCoordinate: function(p) { | |
var targetZ = 0; | |
// since we don't know the correct projected z value for the point, | |
// unproject two points to get a line and then find the point on that | |
// line with z=0 | |
var coord0 = [p.x, p.y, 0, 1]; | |
var coord1 = [p.x, p.y, 1, 1]; | |
vec4.transformMat4(coord0, coord0, this.pixelMatrixInverse); | |
vec4.transformMat4(coord1, coord1, this.pixelMatrixInverse); | |
var w0 = coord0[3]; | |
var w1 = coord1[3]; | |
var x0 = coord0[0] / w0; | |
var x1 = coord1[0] / w1; | |
var y0 = coord0[1] / w0; | |
var y1 = coord1[1] / w1; | |
var z0 = coord0[2] / w0; | |
var z1 = coord1[2] / w1; | |
var t = z0 === z1 ? 0 : (targetZ - z0) / (z1 - z0); | |
var scale = this.worldSize / this.zoomScale(this.tileZoom); | |
return new Coordinate( | |
interp(x0, x1, t) / scale, | |
interp(y0, y1, t) / scale, | |
this.tileZoom); | |
}, | |
/** | |
* Given a coordinate, return the screen point that corresponds to it | |
* @param {Coordinate} coord | |
* @returns {Point} screen point | |
* @private | |
*/ | |
coordinatePoint: function(coord) { | |
var scale = this.worldSize / this.zoomScale(coord.zoom); | |
var p = [coord.column * scale, coord.row * scale, 0, 1]; | |
vec4.transformMat4(p, p, this.pixelMatrix); | |
return new Point(p[0] / p[3], p[1] / p[3]); | |
}, | |
/** | |
* Calculate the posMatrix that, given a tile coordinate, would be used to display the tile on a map. | |
* @param {TileCoord|Coordinate} coord | |
* @param {Number} maxZoom maximum source zoom to account for overscaling | |
* @private | |
*/ | |
calculatePosMatrix: function(coord, maxZoom) { | |
if (maxZoom === undefined) maxZoom = Infinity; | |
if (coord instanceof TileCoord) coord = coord.toCoordinate(maxZoom); | |
// Initialize model-view matrix that converts from the tile coordinates to screen coordinates. | |
// if z > maxzoom then the tile is actually a overscaled maxzoom tile, | |
// so calculate the matrix the maxzoom tile would use. | |
var z = Math.min(coord.zoom, maxZoom); | |
var scale = this.worldSize / Math.pow(2, z); | |
var posMatrix = new Float64Array(16); | |
mat4.identity(posMatrix); | |
mat4.translate(posMatrix, posMatrix, [coord.column * scale, coord.row * scale, 0]); | |
mat4.scale(posMatrix, posMatrix, [ scale / EXTENT, scale / EXTENT, 1 ]); | |
mat4.multiply(posMatrix, this.projMatrix, posMatrix); | |
return new Float32Array(posMatrix); | |
}, | |
_constrain: function() { | |
if (!this.center || !this.width || !this.height || this._constraining) return; | |
this._constraining = true; | |
var minY, maxY, minX, maxX, sy, sx, x2, y2, | |
size = this.size, | |
unmodified = this._unmodified; | |
if (this.latRange) { | |
minY = this.latY(this.latRange[1]); | |
maxY = this.latY(this.latRange[0]); | |
sy = maxY - minY < size.y ? size.y / (maxY - minY) : 0; | |
} | |
if (this.lngRange) { | |
minX = this.lngX(this.lngRange[0]); | |
maxX = this.lngX(this.lngRange[1]); | |
sx = maxX - minX < size.x ? size.x / (maxX - minX) : 0; | |
} | |
// how much the map should scale to fit the screen into given latitude/longitude ranges | |
var s = Math.max(sx || 0, sy || 0); | |
if (s) { | |
this.center = this.unproject(new Point( | |
sx ? (maxX + minX) / 2 : this.x, | |
sy ? (maxY + minY) / 2 : this.y)); | |
this.zoom += this.scaleZoom(s); | |
this._unmodified = unmodified; | |
this._constraining = false; | |
return; | |
} | |
if (this.latRange) { | |
var y = this.y, | |
h2 = size.y / 2; | |
if (y - h2 < minY) y2 = minY + h2; | |
if (y + h2 > maxY) y2 = maxY - h2; | |
} | |
if (this.lngRange) { | |
var x = this.x, | |
w2 = size.x / 2; | |
if (x - w2 < minX) x2 = minX + w2; | |
if (x + w2 > maxX) x2 = maxX - w2; | |
} | |
// pan the map if the screen goes off the range | |
if (x2 !== undefined || y2 !== undefined) { | |
this.center = this.unproject(new Point( | |
x2 !== undefined ? x2 : this.x, | |
y2 !== undefined ? y2 : this.y)); | |
} | |
this._unmodified = unmodified; | |
this._constraining = false; | |
}, | |
_calcMatrices: function() { | |
if (!this.height) return; | |
// Find the distance from the center point to the center top in altitude units using law of sines. | |
var halfFov = Math.atan(0.5 / this.altitude); | |
var topHalfSurfaceDistance = Math.sin(halfFov) * this.altitude / Math.sin(Math.PI / 2 - this._pitch - halfFov); | |
// Calculate z value of the farthest fragment that should be rendered. | |
var farZ = Math.cos(Math.PI / 2 - this._pitch) * topHalfSurfaceDistance + this.altitude; | |
// matrix for conversion from location to GL coordinates (-1 .. 1) | |
var m = new Float64Array(16); | |
mat4.perspective(m, 2 * Math.atan((this.height / 2) / this.altitude), this.width / this.height, 0.1, farZ); | |
mat4.translate(m, m, [0, 0, -this.altitude]); | |
// After the rotateX, z values are in pixel units. Convert them to | |
// altitude units. 1 altitude unit = the screen height. | |
mat4.scale(m, m, [1, -1, 1 / this.height]); | |
mat4.rotateX(m, m, this._pitch); | |
mat4.rotateZ(m, m, this.angle); | |
mat4.translate(m, m, [-this.x, -this.y, 0]); | |
this.projMatrix = m; | |
// matrix for conversion from location to screen coordinates | |
m = mat4.create(); | |
mat4.scale(m, m, [this.width / 2, -this.height / 2, 1]); | |
mat4.translate(m, m, [1, -1, 0]); | |
this.pixelMatrix = mat4.multiply(new Float64Array(16), m, this.projMatrix); | |
// inverse matrix for conversion from screen coordinaes to location | |
m = mat4.invert(new Float64Array(16), this.pixelMatrix); | |
if (!m) throw new Error("failed to invert matrix"); | |
this.pixelMatrixInverse = m; | |
} | |
}; | |
},{"../data/bucket":1,"../source/tile_coord":36,"../util/interpolate":102,"../util/util":108,"./coordinate":9,"./lng_lat":10,"gl-matrix":135,"point-geometry":177}],13:[function(require,module,exports){ | |
'use strict'; | |
// Font data From Hershey Simplex Font | |
// http://paulbourke.net/dataformats/hershey/ | |
var simplexFont = { | |
" ": [16, []], | |
"!": [10, [5, 21, 5, 7, -1, -1, 5, 2, 4, 1, 5, 0, 6, 1, 5, 2]], | |
"\"": [16, [4, 21, 4, 14, -1, -1, 12, 21, 12, 14]], | |
"#": [21, [11, 25, 4, -7, -1, -1, 17, 25, 10, -7, -1, -1, 4, 12, 18, 12, -1, -1, 3, 6, 17, 6]], | |
"$": [20, [8, 25, 8, -4, -1, -1, 12, 25, 12, -4, -1, -1, 17, 18, 15, 20, 12, 21, 8, 21, 5, 20, 3, 18, 3, 16, 4, 14, 5, 13, 7, 12, 13, 10, 15, 9, 16, 8, 17, 6, 17, 3, 15, 1, 12, 0, 8, 0, 5, 1, 3, 3]], | |
"%": [24, [21, 21, 3, 0, -1, -1, 8, 21, 10, 19, 10, 17, 9, 15, 7, 14, 5, 14, 3, 16, 3, 18, 4, 20, 6, 21, 8, 21, 10, 20, 13, 19, 16, 19, 19, 20, 21, 21, -1, -1, 17, 7, 15, 6, 14, 4, 14, 2, 16, 0, 18, 0, 20, 1, 21, 3, 21, 5, 19, 7, 17, 7]], | |
"&": [26, [23, 12, 23, 13, 22, 14, 21, 14, 20, 13, 19, 11, 17, 6, 15, 3, 13, 1, 11, 0, 7, 0, 5, 1, 4, 2, 3, 4, 3, 6, 4, 8, 5, 9, 12, 13, 13, 14, 14, 16, 14, 18, 13, 20, 11, 21, 9, 20, 8, 18, 8, 16, 9, 13, 11, 10, 16, 3, 18, 1, 20, 0, 22, 0, 23, 1, 23, 2]], | |
"'": [10, [5, 19, 4, 20, 5, 21, 6, 20, 6, 18, 5, 16, 4, 15]], | |
"(": [14, [11, 25, 9, 23, 7, 20, 5, 16, 4, 11, 4, 7, 5, 2, 7, -2, 9, -5, 11, -7]], | |
")": [14, [3, 25, 5, 23, 7, 20, 9, 16, 10, 11, 10, 7, 9, 2, 7, -2, 5, -5, 3, -7]], | |
"*": [16, [8, 21, 8, 9, -1, -1, 3, 18, 13, 12, -1, -1, 13, 18, 3, 12]], | |
"+": [26, [13, 18, 13, 0, -1, -1, 4, 9, 22, 9]], | |
",": [10, [6, 1, 5, 0, 4, 1, 5, 2, 6, 1, 6, -1, 5, -3, 4, -4]], | |
"-": [26, [4, 9, 22, 9]], | |
".": [10, [5, 2, 4, 1, 5, 0, 6, 1, 5, 2]], | |
"/": [22, [20, 25, 2, -7]], | |
"0": [20, [9, 21, 6, 20, 4, 17, 3, 12, 3, 9, 4, 4, 6, 1, 9, 0, 11, 0, 14, 1, 16, 4, 17, 9, 17, 12, 16, 17, 14, 20, 11, 21, 9, 21]], | |
"1": [20, [6, 17, 8, 18, 11, 21, 11, 0]], | |
"2": [20, [4, 16, 4, 17, 5, 19, 6, 20, 8, 21, 12, 21, 14, 20, 15, 19, 16, 17, 16, 15, 15, 13, 13, 10, 3, 0, 17, 0]], | |
"3": [20, [5, 21, 16, 21, 10, 13, 13, 13, 15, 12, 16, 11, 17, 8, 17, 6, 16, 3, 14, 1, 11, 0, 8, 0, 5, 1, 4, 2, 3, 4]], | |
"4": [20, [13, 21, 3, 7, 18, 7, -1, -1, 13, 21, 13, 0]], | |
"5": [20, [15, 21, 5, 21, 4, 12, 5, 13, 8, 14, 11, 14, 14, 13, 16, 11, 17, 8, 17, 6, 16, 3, 14, 1, 11, 0, 8, 0, 5, 1, 4, 2, 3, 4]], | |
"6": [20, [16, 18, 15, 20, 12, 21, 10, 21, 7, 20, 5, 17, 4, 12, 4, 7, 5, 3, 7, 1, 10, 0, 11, 0, 14, 1, 16, 3, 17, 6, 17, 7, 16, 10, 14, 12, 11, 13, 10, 13, 7, 12, 5, 10, 4, 7]], | |
"7": [20, [17, 21, 7, 0, -1, -1, 3, 21, 17, 21]], | |
"8": [20, [8, 21, 5, 20, 4, 18, 4, 16, 5, 14, 7, 13, 11, 12, 14, 11, 16, 9, 17, 7, 17, 4, 16, 2, 15, 1, 12, 0, 8, 0, 5, 1, 4, 2, 3, 4, 3, 7, 4, 9, 6, 11, 9, 12, 13, 13, 15, 14, 16, 16, 16, 18, 15, 20, 12, 21, 8, 21]], | |
"9": [20, [16, 14, 15, 11, 13, 9, 10, 8, 9, 8, 6, 9, 4, 11, 3, 14, 3, 15, 4, 18, 6, 20, 9, 21, 10, 21, 13, 20, 15, 18, 16, 14, 16, 9, 15, 4, 13, 1, 10, 0, 8, 0, 5, 1, 4, 3]], | |
":": [10, [5, 14, 4, 13, 5, 12, 6, 13, 5, 14, -1, -1, 5, 2, 4, 1, 5, 0, 6, 1, 5, 2]], | |
";": [10, [5, 14, 4, 13, 5, 12, 6, 13, 5, 14, -1, -1, 6, 1, 5, 0, 4, 1, 5, 2, 6, 1, 6, -1, 5, -3, 4, -4]], | |
"<": [24, [20, 18, 4, 9, 20, 0]], | |
"=": [26, [4, 12, 22, 12, -1, -1, 4, 6, 22, 6]], | |
">": [24, [4, 18, 20, 9, 4, 0]], | |
"?": [18, [3, 16, 3, 17, 4, 19, 5, 20, 7, 21, 11, 21, 13, 20, 14, 19, 15, 17, 15, 15, 14, 13, 13, 12, 9, 10, 9, 7, -1, -1, 9, 2, 8, 1, 9, 0, 10, 1, 9, 2]], | |
"@": [27, [18, 13, 17, 15, 15, 16, 12, 16, 10, 15, 9, 14, 8, 11, 8, 8, 9, 6, 11, 5, 14, 5, 16, 6, 17, 8, -1, -1, 12, 16, 10, 14, 9, 11, 9, 8, 10, 6, 11, 5, -1, -1, 18, 16, 17, 8, 17, 6, 19, 5, 21, 5, 23, 7, 24, 10, 24, 12, 23, 15, 22, 17, 20, 19, 18, 20, 15, 21, 12, 21, 9, 20, 7, 19, 5, 17, 4, 15, 3, 12, 3, 9, 4, 6, 5, 4, 7, 2, 9, 1, 12, 0, 15, 0, 18, 1, 20, 2, 21, 3, -1, -1, 19, 16, 18, 8, 18, 6, 19, 5]], | |
"A": [18, [9, 21, 1, 0, -1, -1, 9, 21, 17, 0, -1, -1, 4, 7, 14, 7]], | |
"B": [21, [4, 21, 4, 0, -1, -1, 4, 21, 13, 21, 16, 20, 17, 19, 18, 17, 18, 15, 17, 13, 16, 12, 13, 11, -1, -1, 4, 11, 13, 11, 16, 10, 17, 9, 18, 7, 18, 4, 17, 2, 16, 1, 13, 0, 4, 0]], | |
"C": [21, [18, 16, 17, 18, 15, 20, 13, 21, 9, 21, 7, 20, 5, 18, 4, 16, 3, 13, 3, 8, 4, 5, 5, 3, 7, 1, 9, 0, 13, 0, 15, 1, 17, 3, 18, 5]], | |
"D": [21, [4, 21, 4, 0, -1, -1, 4, 21, 11, 21, 14, 20, 16, 18, 17, 16, 18, 13, 18, 8, 17, 5, 16, 3, 14, 1, 11, 0, 4, 0]], | |
"E": [19, [4, 21, 4, 0, -1, -1, 4, 21, 17, 21, -1, -1, 4, 11, 12, 11, -1, -1, 4, 0, 17, 0]], | |
"F": [18, [4, 21, 4, 0, -1, -1, 4, 21, 17, 21, -1, -1, 4, 11, 12, 11]], | |
"G": [21, [18, 16, 17, 18, 15, 20, 13, 21, 9, 21, 7, 20, 5, 18, 4, 16, 3, 13, 3, 8, 4, 5, 5, 3, 7, 1, 9, 0, 13, 0, 15, 1, 17, 3, 18, 5, 18, 8, -1, -1, 13, 8, 18, 8]], | |
"H": [22, [4, 21, 4, 0, -1, -1, 18, 21, 18, 0, -1, -1, 4, 11, 18, 11]], | |
"I": [8, [4, 21, 4, 0]], | |
"J": [16, [12, 21, 12, 5, 11, 2, 10, 1, 8, 0, 6, 0, 4, 1, 3, 2, 2, 5, 2, 7]], | |
"K": [21, [4, 21, 4, 0, -1, -1, 18, 21, 4, 7, -1, -1, 9, 12, 18, 0]], | |
"L": [17, [4, 21, 4, 0, -1, -1, 4, 0, 16, 0]], | |
"M": [24, [4, 21, 4, 0, -1, -1, 4, 21, 12, 0, -1, -1, 20, 21, 12, 0, -1, -1, 20, 21, 20, 0]], | |
"N": [22, [4, 21, 4, 0, -1, -1, 4, 21, 18, 0, -1, -1, 18, 21, 18, 0]], | |
"O": [22, [9, 21, 7, 20, 5, 18, 4, 16, 3, 13, 3, 8, 4, 5, 5, 3, 7, 1, 9, 0, 13, 0, 15, 1, 17, 3, 18, 5, 19, 8, 19, 13, 18, 16, 17, 18, 15, 20, 13, 21, 9, 21]], | |
"P": [21, [4, 21, 4, 0, -1, -1, 4, 21, 13, 21, 16, 20, 17, 19, 18, 17, 18, 14, 17, 12, 16, 11, 13, 10, 4, 10]], | |
"Q": [22, [9, 21, 7, 20, 5, 18, 4, 16, 3, 13, 3, 8, 4, 5, 5, 3, 7, 1, 9, 0, 13, 0, 15, 1, 17, 3, 18, 5, 19, 8, 19, 13, 18, 16, 17, 18, 15, 20, 13, 21, 9, 21, -1, -1, 12, 4, 18, -2]], | |
"R": [21, [4, 21, 4, 0, -1, -1, 4, 21, 13, 21, 16, 20, 17, 19, 18, 17, 18, 15, 17, 13, 16, 12, 13, 11, 4, 11, -1, -1, 11, 11, 18, 0]], | |
"S": [20, [17, 18, 15, 20, 12, 21, 8, 21, 5, 20, 3, 18, 3, 16, 4, 14, 5, 13, 7, 12, 13, 10, 15, 9, 16, 8, 17, 6, 17, 3, 15, 1, 12, 0, 8, 0, 5, 1, 3, 3]], | |
"T": [16, [8, 21, 8, 0, -1, -1, 1, 21, 15, 21]], | |
"U": [22, [4, 21, 4, 6, 5, 3, 7, 1, 10, 0, 12, 0, 15, 1, 17, 3, 18, 6, 18, 21]], | |
"V": [18, [1, 21, 9, 0, -1, -1, 17, 21, 9, 0]], | |
"W": [24, [2, 21, 7, 0, -1, -1, 12, 21, 7, 0, -1, -1, 12, 21, 17, 0, -1, -1, 22, 21, 17, 0]], | |
"X": [20, [3, 21, 17, 0, -1, -1, 17, 21, 3, 0]], | |
"Y": [18, [1, 21, 9, 11, 9, 0, -1, -1, 17, 21, 9, 11]], | |
"Z": [20, [17, 21, 3, 0, -1, -1, 3, 21, 17, 21, -1, -1, 3, 0, 17, 0]], | |
"[": [14, [4, 25, 4, -7, -1, -1, 5, 25, 5, -7, -1, -1, 4, 25, 11, 25, -1, -1, 4, -7, 11, -7]], | |
"\\": [14, [0, 21, 14, -3]], | |
"]": [14, [9, 25, 9, -7, -1, -1, 10, 25, 10, -7, -1, -1, 3, 25, 10, 25, -1, -1, 3, -7, 10, -7]], | |
"^": [16, [6, 15, 8, 18, 10, 15, -1, -1, 3, 12, 8, 17, 13, 12, -1, -1, 8, 17, 8, 0]], | |
"_": [16, [0, -2, 16, -2]], | |
"`": [10, [6, 21, 5, 20, 4, 18, 4, 16, 5, 15, 6, 16, 5, 17]], | |
"a": [19, [15, 14, 15, 0, -1, -1, 15, 11, 13, 13, 11, 14, 8, 14, 6, 13, 4, 11, 3, 8, 3, 6, 4, 3, 6, 1, 8, 0, 11, 0, 13, 1, 15, 3]], | |
"b": [19, [4, 21, 4, 0, -1, -1, 4, 11, 6, 13, 8, 14, 11, 14, 13, 13, 15, 11, 16, 8, 16, 6, 15, 3, 13, 1, 11, 0, 8, 0, 6, 1, 4, 3]], | |
"c": [18, [15, 11, 13, 13, 11, 14, 8, 14, 6, 13, 4, 11, 3, 8, 3, 6, 4, 3, 6, 1, 8, 0, 11, 0, 13, 1, 15, 3]], | |
"d": [19, [15, 21, 15, 0, -1, -1, 15, 11, 13, 13, 11, 14, 8, 14, 6, 13, 4, 11, 3, 8, 3, 6, 4, 3, 6, 1, 8, 0, 11, 0, 13, 1, 15, 3]], | |
"e": [18, [3, 8, 15, 8, 15, 10, 14, 12, 13, 13, 11, 14, 8, 14, 6, 13, 4, 11, 3, 8, 3, 6, 4, 3, 6, 1, 8, 0, 11, 0, 13, 1, 15, 3]], | |
"f": [12, [10, 21, 8, 21, 6, 20, 5, 17, 5, 0, -1, -1, 2, 14, 9, 14]], | |
"g": [19, [15, 14, 15, -2, 14, -5, 13, -6, 11, -7, 8, -7, 6, -6, -1, -1, 15, 11, 13, 13, 11, 14, 8, 14, 6, 13, 4, 11, 3, 8, 3, 6, 4, 3, 6, 1, 8, 0, 11, 0, 13, 1, 15, 3]], | |
"h": [19, [4, 21, 4, 0, -1, -1, 4, 10, 7, 13, 9, 14, 12, 14, 14, 13, 15, 10, 15, 0]], | |
"i": [8, [3, 21, 4, 20, 5, 21, 4, 22, 3, 21, -1, -1, 4, 14, 4, 0]], | |
"j": [10, [5, 21, 6, 20, 7, 21, 6, 22, 5, 21, -1, -1, 6, 14, 6, -3, 5, -6, 3, -7, 1, -7]], | |
"k": [17, [4, 21, 4, 0, -1, -1, 14, 14, 4, 4, -1, -1, 8, 8, 15, 0]], | |
"l": [8, [4, 21, 4, 0]], | |
"m": [30, [4, 14, 4, 0, -1, -1, 4, 10, 7, 13, 9, 14, 12, 14, 14, 13, 15, 10, 15, 0, -1, -1, 15, 10, 18, 13, 20, 14, 23, 14, 25, 13, 26, 10, 26, 0]], | |
"n": [19, [4, 14, 4, 0, -1, -1, 4, 10, 7, 13, 9, 14, 12, 14, 14, 13, 15, 10, 15, 0]], | |
"o": [19, [8, 14, 6, 13, 4, 11, 3, 8, 3, 6, 4, 3, 6, 1, 8, 0, 11, 0, 13, 1, 15, 3, 16, 6, 16, 8, 15, 11, 13, 13, 11, 14, 8, 14]], | |
"p": [19, [4, 14, 4, -7, -1, -1, 4, 11, 6, 13, 8, 14, 11, 14, 13, 13, 15, 11, 16, 8, 16, 6, 15, 3, 13, 1, 11, 0, 8, 0, 6, 1, 4, 3]], | |
"q": [19, [15, 14, 15, -7, -1, -1, 15, 11, 13, 13, 11, 14, 8, 14, 6, 13, 4, 11, 3, 8, 3, 6, 4, 3, 6, 1, 8, 0, 11, 0, 13, 1, 15, 3]], | |
"r": [13, [4, 14, 4, 0, -1, -1, 4, 8, 5, 11, 7, 13, 9, 14, 12, 14]], | |
"s": [17, [14, 11, 13, 13, 10, 14, 7, 14, 4, 13, 3, 11, 4, 9, 6, 8, 11, 7, 13, 6, 14, 4, 14, 3, 13, 1, 10, 0, 7, 0, 4, 1, 3, 3]], | |
"t": [12, [5, 21, 5, 4, 6, 1, 8, 0, 10, 0, -1, -1, 2, 14, 9, 14]], | |
"u": [19, [4, 14, 4, 4, 5, 1, 7, 0, 10, 0, 12, 1, 15, 4, -1, -1, 15, 14, 15, 0]], | |
"v": [16, [2, 14, 8, 0, -1, -1, 14, 14, 8, 0]], | |
"w": [22, [3, 14, 7, 0, -1, -1, 11, 14, 7, 0, -1, -1, 11, 14, 15, 0, -1, -1, 19, 14, 15, 0]], | |
"x": [17, [3, 14, 14, 0, -1, -1, 14, 14, 3, 0]], | |
"y": [16, [2, 14, 8, 0, -1, -1, 14, 14, 8, 0, 6, -4, 4, -6, 2, -7, 1, -7]], | |
"z": [17, [14, 14, 3, 0, -1, -1, 3, 14, 14, 14, -1, -1, 3, 0, 14, 0]], | |
"{": [14, [9, 25, 7, 24, 6, 23, 5, 21, 5, 19, 6, 17, 7, 16, 8, 14, 8, 12, 6, 10, -1, -1, 7, 24, 6, 22, 6, 20, 7, 18, 8, 17, 9, 15, 9, 13, 8, 11, 4, 9, 8, 7, 9, 5, 9, 3, 8, 1, 7, 0, 6, -2, 6, -4, 7, -6, -1, -1, 6, 8, 8, 6, 8, 4, 7, 2, 6, 1, 5, -1, 5, -3, 6, -5, 7, -6, 9, -7]], | |
"|": [8, [4, 25, 4, -7]], | |
"}": [14, [5, 25, 7, 24, 8, 23, 9, 21, 9, 19, 8, 17, 7, 16, 6, 14, 6, 12, 8, 10, -1, -1, 7, 24, 8, 22, 8, 20, 7, 18, 6, 17, 5, 15, 5, 13, 6, 11, 10, 9, 6, 7, 5, 5, 5, 3, 6, 1, 7, 0, 8, -2, 8, -4, 7, -6, -1, -1, 8, 8, 6, 6, 6, 4, 7, 2, 8, 1, 9, -1, 9, -3, 8, -5, 7, -6, 5, -7]], | |
"~": [24, [3, 6, 3, 8, 4, 11, 6, 12, 8, 12, 10, 11, 14, 8, 16, 7, 18, 7, 20, 8, 21, 10, -1, -1, 3, 8, 4, 10, 6, 11, 8, 11, 10, 10, 14, 7, 16, 6, 18, 6, 20, 7, 21, 10, 21, 12]] | |
}; | |
module.exports = function textVertices(text, left, baseline, scale) { | |
scale = scale || 1; | |
var strokes = [], | |
i, len, j, len2, glyph, x, y, prev; | |
for (i = 0, len = text.length; i < len; i++) { | |
glyph = simplexFont[text[i]]; | |
if (!glyph) continue; | |
prev = null; | |
for (j = 0, len2 = glyph[1].length; j < len2; j += 2) { | |
if (glyph[1][j] === -1 && glyph[1][j + 1] === -1) { | |
prev = null; | |
} else { | |
x = left + glyph[1][j] * scale; | |
y = baseline - glyph[1][j + 1] * scale; | |
if (prev) { | |
strokes.push(prev.x, prev.y, x, y); | |
} | |
prev = {x: x, y: y}; | |
} | |
} | |
left += glyph[0] * scale; | |
} | |
return strokes; | |
}; | |
},{}],14:[function(require,module,exports){ | |
'use strict'; | |
// jshint -W079 | |
var mapboxgl = module.exports = {}; | |
mapboxgl.version = require('../package.json').version; | |
mapboxgl.Map = require('./ui/map'); | |
mapboxgl.Control = require('./ui/control/control'); | |
mapboxgl.Navigation = require('./ui/control/navigation'); | |
mapboxgl.Geolocate = require('./ui/control/geolocate'); | |
mapboxgl.Attribution = require('./ui/control/attribution'); | |
mapboxgl.Popup = require('./ui/popup'); | |
mapboxgl.Marker = require('./ui/marker'); | |
mapboxgl.GeoJSONSource = require('./source/geojson_source'); | |
mapboxgl.VideoSource = require('./source/video_source'); | |
mapboxgl.ImageSource = require('./source/image_source'); | |
mapboxgl.Style = require('./style/style'); | |
mapboxgl.LngLat = require('./geo/lng_lat'); | |
mapboxgl.LngLatBounds = require('./geo/lng_lat_bounds'); | |
mapboxgl.Point = require('point-geometry'); | |
mapboxgl.Evented = require('./util/evented'); | |
mapboxgl.util = require('./util/util'); | |
mapboxgl.supported = require('./util/browser').supported; | |
var ajax = require('./util/ajax'); | |
mapboxgl.util.getJSON = ajax.getJSON; | |
mapboxgl.util.getArrayBuffer = ajax.getArrayBuffer; | |
var config = require('./util/config'); | |
mapboxgl.config = config; | |
Object.defineProperty(mapboxgl, 'accessToken', { | |
get: function() { return config.ACCESS_TOKEN; }, | |
set: function(token) { config.ACCESS_TOKEN = token; } | |
}); | |
},{"../package.json":196,"./geo/lng_lat":10,"./geo/lng_lat_bounds":11,"./source/geojson_source":29,"./source/image_source":31,"./source/video_source":39,"./style/style":45,"./ui/control/attribution":76,"./ui/control/control":77,"./ui/control/geolocate":78,"./ui/control/navigation":79,"./ui/map":88,"./ui/marker":89,"./ui/popup":90,"./util/ajax":92,"./util/browser":93,"./util/config":98,"./util/evented":100,"./util/util":108,"point-geometry":177}],15:[function(require,module,exports){ | |
'use strict'; | |
var assert = require('assert'); | |
module.exports = function(uniforms) { | |
var pragmas = { define: {}, initialize: {} }; | |
for (var i = 0; i < uniforms.length; i++) { | |
var uniform = uniforms[i]; | |
assert(uniform.name.slice(0, 2) === 'u_'); | |
var type = '{precision} ' + (uniform.components === 1 ? 'float' : 'vec' + uniform.components); | |
pragmas.define[uniform.name.slice(2)] = 'uniform ' + type + ' ' + uniform.name + ';\n'; | |
pragmas.initialize[uniform.name.slice(2)] = type + ' ' + uniform.name.slice(2) + ' = ' + uniform.name + ';\n'; | |
} | |
return pragmas; | |
}; | |
},{"assert":110}],16:[function(require,module,exports){ | |
'use strict'; | |
var TilePyramid = require('../source/tile_pyramid'); | |
var pyramid = new TilePyramid({ tileSize: 512 }); | |
var pixelsToTileUnits = require('../source/pixels_to_tile_units'); | |
var createUniformPragmas = require('./create_uniform_pragmas'); | |
module.exports = drawBackground; | |
function drawBackground(painter, source, layer) { | |
var gl = painter.gl; | |
var transform = painter.transform; | |
var color = layer.paint['background-color']; | |
var image = layer.paint['background-pattern']; | |
var opacity = layer.paint['background-opacity']; | |
var program; | |
var imagePosA = image ? painter.spriteAtlas.getPosition(image.from, true) : null; | |
var imagePosB = image ? painter.spriteAtlas.getPosition(image.to, true) : null; | |
painter.setDepthSublayer(0); | |
if (imagePosA && imagePosB) { | |
if (painter.isOpaquePass) return; | |
// Draw texture fill | |
program = painter.useProgram('pattern'); | |
gl.uniform1i(program.u_image, 0); | |
gl.uniform2fv(program.u_pattern_tl_a, imagePosA.tl); | |
gl.uniform2fv(program.u_pattern_br_a, imagePosA.br); | |
gl.uniform2fv(program.u_pattern_tl_b, imagePosB.tl); | |
gl.uniform2fv(program.u_pattern_br_b, imagePosB.br); | |
gl.uniform1f(program.u_opacity, opacity); | |
gl.uniform1f(program.u_mix, image.t); | |
gl.uniform2fv(program.u_pattern_size_a, imagePosA.size); | |
gl.uniform2fv(program.u_pattern_size_b, imagePosB.size); | |
gl.uniform1f(program.u_scale_a, image.fromScale); | |
gl.uniform1f(program.u_scale_b, image.toScale); | |
gl.activeTexture(gl.TEXTURE0); | |
painter.spriteAtlas.bind(gl, true); | |
painter.tileExtentPatternVAO.bind(gl, program, painter.tileExtentBuffer); | |
} else { | |
// Draw filling rectangle. | |
if (painter.isOpaquePass !== (color[3] === 1)) return; | |
var pragmas = createUniformPragmas([ | |
{name: 'u_color', components: 4}, | |
{name: 'u_opacity', components: 1} | |
]); | |
program = painter.useProgram('fill', [], pragmas, pragmas); | |
gl.uniform4fv(program.u_color, color); | |
gl.uniform1f(program.u_opacity, opacity); | |
painter.tileExtentVAO.bind(gl, program, painter.tileExtentBuffer); | |
} | |
gl.disable(gl.STENCIL_TEST); | |
// We need to draw the background in tiles in order to use calculatePosMatrix | |
// which applies the projection matrix (transform.projMatrix). Otherwise | |
// the depth and stencil buffers get into a bad state. | |
// This can be refactored into a single draw call once earcut lands and | |
// we don't have so much going on in the stencil buffer. | |
var coords = pyramid.coveringTiles(transform); | |
for (var c = 0; c < coords.length; c++) { | |
var coord = coords[c]; | |
var tileSize = 512; | |
// var pixelsToTileUnitsBound = pixelsToTileUnits.bind({coord:coord, tileSize: tileSize}); | |
if (imagePosA && imagePosB) { | |
var tile = {coord:coord, tileSize: tileSize}; | |
gl.uniform1f(program.u_tile_units_to_pixels, 1 / pixelsToTileUnits(tile, 1, painter.transform.tileZoom)); | |
var tileSizeAtNearestZoom = tile.tileSize * Math.pow(2, painter.transform.tileZoom - tile.coord.z); | |
var pixelX = tileSizeAtNearestZoom * (tile.coord.x + coord.w * Math.pow(2, tile.coord.z)); | |
var pixelY = tileSizeAtNearestZoom * tile.coord.y; | |
// split the pixel coord into two pairs of 16 bit numbers. The glsl spec only guarantees 16 bits of precision. | |
gl.uniform2f(program.u_pixel_coord_upper, pixelX >> 16, pixelY >> 16); | |
gl.uniform2f(program.u_pixel_coord_lower, pixelX & 0xFFFF, pixelY & 0xFFFF); | |
} | |
gl.uniformMatrix4fv(program.u_matrix, false, painter.transform.calculatePosMatrix(coord)); | |
gl.drawArrays(gl.TRIANGLE_STRIP, 0, painter.tileExtentBuffer.length); | |
} | |
gl.stencilMask(0x00); | |
gl.stencilFunc(gl.EQUAL, 0x80, 0x80); | |
} | |
},{"../source/pixels_to_tile_units":32,"../source/tile_pyramid":37,"./create_uniform_pragmas":15}],17:[function(require,module,exports){ | |
'use strict'; | |
var browser = require('../util/browser'); | |
module.exports = drawCircles; | |
function drawCircles(painter, source, layer, coords) { | |
if (painter.isOpaquePass) return; | |
var gl = painter.gl; | |
painter.setDepthSublayer(0); | |
painter.depthMask(false); | |
// Allow circles to be drawn across boundaries, so that | |
// large circles are not clipped to tiles | |
gl.disable(gl.STENCIL_TEST); | |
for (var i = 0; i < coords.length; i++) { | |
var coord = coords[i]; | |
var tile = source.getTile(coord); | |
var bucket = tile.getBucket(layer); | |
if (!bucket) continue; | |
var bufferGroups = bucket.bufferGroups.circle; | |
if (!bufferGroups) continue; | |
var programOptions = bucket.paintAttributes.circle[layer.id]; | |
var program = painter.useProgram( | |
'circle', | |
programOptions.defines, | |
programOptions.vertexPragmas, | |
programOptions.fragmentPragmas | |
); | |
if (layer.paint['circle-pitch-scale'] === 'map') { | |
gl.uniform1i(program.u_scale_with_map, true); | |
gl.uniform2f(program.u_extrude_scale, | |
painter.transform.pixelsToGLUnits[0] * painter.transform.altitude, | |
painter.transform.pixelsToGLUnits[1] * painter.transform.altitude); | |
} else { | |
gl.uniform1i(program.u_scale_with_map, false); | |
gl.uniform2fv(program.u_extrude_scale, painter.transform.pixelsToGLUnits); | |
} | |
gl.uniform1f(program.u_devicepixelratio, browser.devicePixelRatio); | |
gl.uniformMatrix4fv(program.u_matrix, false, painter.translatePosMatrix( | |
coord.posMatrix, | |
tile, | |
layer.paint['circle-translate'], | |
layer.paint['circle-translate-anchor'] | |
)); | |
bucket.setUniforms(gl, 'circle', program, layer, {zoom: painter.transform.zoom}); | |
for (var k = 0; k < bufferGroups.length; k++) { | |
var group = bufferGroups[k]; | |
group.vaos[layer.id].bind(gl, program, group.layoutVertexBuffer, group.elementBuffer, group.paintVertexBuffers[layer.id]); | |
gl.drawElements(gl.TRIANGLES, group.elementBuffer.length * 3, gl.UNSIGNED_SHORT, 0); | |
} | |
} | |
} | |
},{"../util/browser":93}],18:[function(require,module,exports){ | |
'use strict'; | |
module.exports = drawCollisionDebug; | |
function drawCollisionDebug(painter, source, layer, coords) { | |
var gl = painter.gl; | |
gl.enable(gl.STENCIL_TEST); | |
var program = painter.useProgram('collisionbox'); | |
for (var i = 0; i < coords.length; i++) { | |
var coord = coords[i]; | |
var tile = source.getTile(coord); | |
var bucket = tile.getBucket(layer); | |
if (!bucket) continue; | |
var bufferGroups = bucket.bufferGroups.collisionBox; | |
if (!bufferGroups || !bufferGroups.length) continue; | |
var group = bufferGroups[0]; | |
if (group.layoutVertexBuffer.length === 0) continue; | |
gl.uniformMatrix4fv(program.u_matrix, false, coord.posMatrix); | |
painter.enableTileClippingMask(coord); | |
painter.lineWidth(1); | |
gl.uniform1f(program.u_scale, Math.pow(2, painter.transform.zoom - tile.coord.z)); | |
gl.uniform1f(program.u_zoom, painter.transform.zoom * 10); | |
gl.uniform1f(program.u_maxzoom, (tile.coord.z + 1) * 10); | |
group.vaos[layer.id].bind(gl, program, group.layoutVertexBuffer); | |
gl.drawArrays(gl.LINES, 0, group.layoutVertexBuffer.length); | |
} | |
} | |
},{}],19:[function(require,module,exports){ | |
'use strict'; | |
var textVertices = require('../lib/debugtext'); | |
var browser = require('../util/browser'); | |
var mat4 = require('gl-matrix').mat4; | |
var EXTENT = require('../data/bucket').EXTENT; | |
var Buffer = require('../data/buffer'); | |
var VertexArrayObject = require('./vertex_array_object'); | |
module.exports = drawDebug; | |
function drawDebug(painter, source, coords) { | |
if (painter.isOpaquePass) return; | |
if (!painter.options.debug) return; | |
for (var i = 0; i < coords.length; i++) { | |
drawDebugTile(painter, source, coords[i]); | |
} | |
} | |
function drawDebugTile(painter, source, coord) { | |
var gl = painter.gl; | |
gl.disable(gl.STENCIL_TEST); | |
painter.lineWidth(1 * browser.devicePixelRatio); | |
var posMatrix = coord.posMatrix; | |
var program = painter.useProgram('debug'); | |
gl.uniformMatrix4fv(program.u_matrix, false, posMatrix); | |
gl.uniform4f(program.u_color, 1, 0, 0, 1); | |
painter.debugVAO.bind(gl, program, painter.debugBuffer); | |
gl.drawArrays(gl.LINE_STRIP, 0, painter.debugBuffer.length); | |
var vertices = textVertices(coord.toString(), 50, 200, 5); | |
var debugTextArray = new painter.PosArray(); | |
for (var v = 0; v < vertices.length; v += 2) { | |
debugTextArray.emplaceBack(vertices[v], vertices[v + 1]); | |
} | |
var debugTextBuffer = new Buffer(debugTextArray.serialize(), painter.PosArray.serialize(), Buffer.BufferType.VERTEX); | |
var debugTextVAO = new VertexArrayObject(); | |
debugTextVAO.bind(gl, program, debugTextBuffer); | |
gl.uniform4f(program.u_color, 1, 1, 1, 1); | |
// Draw the halo with multiple 1px lines instead of one wider line because | |
// the gl spec doesn't guarantee support for lines with width > 1. | |
var tileSize = source.getTile(coord).tileSize; | |
var onePixel = EXTENT / (Math.pow(2, painter.transform.zoom - coord.z) * tileSize); | |
var translations = [[-1, -1], [-1, 1], [1, -1], [1, 1]]; | |
for (var i = 0; i < translations.length; i++) { | |
var translation = translations[i]; | |
gl.uniformMatrix4fv(program.u_matrix, false, mat4.translate([], posMatrix, [onePixel * translation[0], onePixel * translation[1], 0])); | |
gl.drawArrays(gl.LINES, 0, debugTextBuffer.length); | |
} | |
gl.uniform4f(program.u_color, 0, 0, 0, 1); | |
gl.uniformMatrix4fv(program.u_matrix, false, posMatrix); | |
gl.drawArrays(gl.LINES, 0, debugTextBuffer.length); | |
} | |
},{"../data/bucket":1,"../data/buffer":6,"../lib/debugtext":13,"../util/browser":93,"./vertex_array_object":28,"gl-matrix":135}],20:[function(require,module,exports){ | |
'use strict'; | |
var pixelsToTileUnits = require('../source/pixels_to_tile_units'); | |
module.exports = draw; | |
function draw(painter, source, layer, coords) { | |
var gl = painter.gl; | |
gl.enable(gl.STENCIL_TEST); | |
var color = layer.paint['fill-color']; | |
var image = layer.paint['fill-pattern']; | |
var opacity = layer.paint['fill-opacity']; | |
var isOutlineColorDefined = layer.getPaintProperty('fill-outline-color'); | |
// Draw fill | |
if (image ? !painter.isOpaquePass : painter.isOpaquePass === (color[3] === 1 && opacity === 1)) { | |
// Once we switch to earcut drawing we can pull most of the WebGL setup | |
// outside of this coords loop. | |
painter.setDepthSublayer(1); | |
for (var i = 0; i < coords.length; i++) { | |
drawFill(painter, source, layer, coords[i]); | |
} | |
} | |
if (!painter.isOpaquePass && layer.paint['fill-antialias']) { | |
painter.lineWidth(2); | |
painter.depthMask(false); | |
if (isOutlineColorDefined || !layer.paint['fill-pattern']) { | |
if (isOutlineColorDefined) { | |
// If we defined a different color for the fill outline, we are | |
// going to ignore the bits in 0x07 and just care about the global | |
// clipping mask. | |
painter.setDepthSublayer(2); | |
} else { | |
// Otherwise, we only want to drawFill the antialiased parts that are | |
// *outside* the current shape. This is important in case the fill | |
// or stroke color is translucent. If we wouldn't clip to outside | |
// the current shape, some pixels from the outline stroke overlapped | |
// the (non-antialiased) fill. | |
painter.setDepthSublayer(0); | |
} | |
} else { | |
// Otherwise, we only want to drawFill the antialiased parts that are | |
// *outside* the current shape. This is important in case the fill | |
// or stroke color is translucent. If we wouldn't clip to outside | |
// the current shape, some pixels from the outline stroke overlapped | |
// the (non-antialiased) fill. | |
painter.setDepthSublayer(0); | |
} | |
for (var j = 0; j < coords.length; j++) { | |
drawStroke(painter, source, layer, coords[j]); | |
} | |
} | |
} | |
function drawFill(painter, source, layer, coord) { | |
var tile = source.getTile(coord); | |
var bucket = tile.getBucket(layer); | |
if (!bucket) return; | |
var bufferGroups = bucket.bufferGroups.fill; | |
if (!bufferGroups) return; | |
var gl = painter.gl; | |
var image = layer.paint['fill-pattern']; | |
var program; | |
if (!image) { | |
var programOptions = bucket.paintAttributes.fill[layer.id]; | |
program = painter.useProgram( | |
'fill', | |
programOptions.defines, | |
programOptions.vertexPragmas, | |
programOptions.fragmentPragmas | |
); | |
bucket.setUniforms(gl, 'fill', program, layer, {zoom: painter.transform.zoom}); | |
} else { | |
// Draw texture fill | |
program = painter.useProgram('pattern'); | |
setPattern(image, layer.paint['fill-opacity'], tile, coord, painter, program); | |
gl.activeTexture(gl.TEXTURE0); | |
painter.spriteAtlas.bind(gl, true); | |
} | |
gl.uniformMatrix4fv(program.u_matrix, false, painter.translatePosMatrix( | |
coord.posMatrix, | |
tile, | |
layer.paint['fill-translate'], | |
layer.paint['fill-translate-anchor'] | |
)); | |
painter.enableTileClippingMask(coord); | |
for (var i = 0; i < bufferGroups.length; i++) { | |
var group = bufferGroups[i]; | |
group.vaos[layer.id].bind(gl, program, group.layoutVertexBuffer, group.elementBuffer, group.paintVertexBuffers[layer.id]); | |
gl.drawElements(gl.TRIANGLES, group.elementBuffer.length, gl.UNSIGNED_SHORT, 0); | |
} | |
} | |
function drawStroke(painter, source, layer, coord) { | |
var tile = source.getTile(coord); | |
var bucket = tile.getBucket(layer); | |
if (!bucket) return; | |
var gl = painter.gl; | |
var bufferGroups = bucket.bufferGroups.fill; | |
var image = layer.paint['fill-pattern']; | |
var opacity = layer.paint['fill-opacity']; | |
var isOutlineColorDefined = layer.getPaintProperty('fill-outline-color'); | |
var program; | |
if (image && !isOutlineColorDefined) { | |
program = painter.useProgram('outlinepattern'); | |
gl.uniform2f(program.u_world, gl.drawingBufferWidth, gl.drawingBufferHeight); | |
} else { | |
var programOptions = bucket.paintAttributes.fill[layer.id]; | |
program = painter.useProgram( | |
'outline', | |
programOptions.defines, | |
programOptions.vertexPragmas, | |
programOptions.fragmentPragmas | |
); | |
gl.uniform2f(program.u_world, gl.drawingBufferWidth, gl.drawingBufferHeight); | |
gl.uniform1f(program.u_opacity, opacity); | |
bucket.setUniforms(gl, 'fill', program, layer, {zoom: painter.transform.zoom}); | |
} | |
gl.uniformMatrix4fv(program.u_matrix, false, painter.translatePosMatrix( | |
coord.posMatrix, | |
tile, | |
layer.paint['fill-translate'], | |
layer.paint['fill-translate-anchor'] | |
)); | |
if (image) { setPattern(image, opacity, tile, coord, painter, program); } | |
painter.enableTileClippingMask(coord); | |
for (var k = 0; k < bufferGroups.length; k++) { | |
var group = bufferGroups[k]; | |
group.secondVaos[layer.id].bind(gl, program, group.layoutVertexBuffer, group.elementBuffer2, group.paintVertexBuffers[layer.id]); | |
gl.drawElements(gl.LINES, group.elementBuffer2.length * 2, gl.UNSIGNED_SHORT, 0); | |
} | |
} | |
function setPattern(image, opacity, tile, coord, painter, program) { | |
var gl = painter.gl; | |
var imagePosA = painter.spriteAtlas.getPosition(image.from, true); | |
var imagePosB = painter.spriteAtlas.getPosition(image.to, true); | |
if (!imagePosA || !imagePosB) return; | |
gl.uniform1i(program.u_image, 0); | |
gl.uniform2fv(program.u_pattern_tl_a, imagePosA.tl); | |
gl.uniform2fv(program.u_pattern_br_a, imagePosA.br); | |
gl.uniform2fv(program.u_pattern_tl_b, imagePosB.tl); | |
gl.uniform2fv(program.u_pattern_br_b, imagePosB.br); | |
gl.uniform1f(program.u_opacity, opacity); | |
gl.uniform1f(program.u_mix, image.t); | |
gl.uniform1f(program.u_tile_units_to_pixels, 1 / pixelsToTileUnits(tile, 1, painter.transform.tileZoom)); | |
gl.uniform2fv(program.u_pattern_size_a, imagePosA.size); | |
gl.uniform2fv(program.u_pattern_size_b, imagePosB.size); | |
gl.uniform1f(program.u_scale_a, image.fromScale); | |
gl.uniform1f(program.u_scale_b, image.toScale); | |
var tileSizeAtNearestZoom = tile.tileSize * Math.pow(2, painter.transform.tileZoom - tile.coord.z); | |
var pixelX = tileSizeAtNearestZoom * (tile.coord.x + coord.w * Math.pow(2, tile.coord.z)); | |
var pixelY = tileSizeAtNearestZoom * tile.coord.y; | |
// split the pixel coord into two pairs of 16 bit numbers. The glsl spec only guarantees 16 bits of precision. | |
gl.uniform2f(program.u_pixel_coord_upper, pixelX >> 16, pixelY >> 16); | |
gl.uniform2f(program.u_pixel_coord_lower, pixelX & 0xFFFF, pixelY & 0xFFFF); | |
gl.activeTexture(gl.TEXTURE0); | |
painter.spriteAtlas.bind(gl, true); | |
} | |
},{"../source/pixels_to_tile_units":32}],21:[function(require,module,exports){ | |
'use strict'; | |
var browser = require('../util/browser'); | |
var mat2 = require('gl-matrix').mat2; | |
var pixelsToTileUnits = require('../source/pixels_to_tile_units'); | |
/** | |
* Draw a line. Under the hood this will read elements from | |
* a tile, dash textures from a lineAtlas, and style properties from a layer. | |
* @param {Object} painter | |
* @param {Object} layer | |
* @param {Object} posMatrix | |
* @param {Tile} tile | |
* @returns {undefined} draws with the painter | |
* @private | |
*/ | |
module.exports = function drawLine(painter, source, layer, coords) { | |
if (painter.isOpaquePass) return; | |
painter.setDepthSublayer(0); | |
painter.depthMask(false); | |
var gl = painter.gl; | |
gl.enable(gl.STENCIL_TEST); | |
// don't draw zero-width lines | |
if (layer.paint['line-width'] <= 0) return; | |
// the distance over which the line edge fades out. | |
// Retina devices need a smaller distance to avoid aliasing. | |
var antialiasing = 1 / browser.devicePixelRatio; | |
var blur = layer.paint['line-blur'] + antialiasing; | |
var color = layer.paint['line-color']; | |
var tr = painter.transform; | |
var antialiasingMatrix = mat2.create(); | |
mat2.scale(antialiasingMatrix, antialiasingMatrix, [1, Math.cos(tr._pitch)]); | |
mat2.rotate(antialiasingMatrix, antialiasingMatrix, painter.transform.angle); | |
// calculate how much longer the real world distance is at the top of the screen | |
// than at the middle of the screen. | |
var topedgelength = Math.sqrt(tr.height * tr.height / 4 * (1 + tr.altitude * tr.altitude)); | |
var x = tr.height / 2 * Math.tan(tr._pitch); | |
var extra = (topedgelength + x) / topedgelength - 1; | |
var dasharray = layer.paint['line-dasharray']; | |
var image = layer.paint['line-pattern']; | |
var program, posA, posB, imagePosA, imagePosB; | |
if (dasharray) { | |
program = painter.useProgram('linesdfpattern'); | |
gl.uniform1f(program.u_linewidth, layer.paint['line-width'] / 2); | |
gl.uniform1f(program.u_gapwidth, layer.paint['line-gap-width'] / 2); | |
gl.uniform1f(program.u_antialiasing, antialiasing / 2); | |
gl.uniform1f(program.u_blur, blur); | |
gl.uniform4fv(program.u_color, color); | |
gl.uniform1f(program.u_opacity, layer.paint['line-opacity']); | |
posA = painter.lineAtlas.getDash(dasharray.from, layer.layout['line-cap'] === 'round'); | |
posB = painter.lineAtlas.getDash(dasharray.to, layer.layout['line-cap'] === 'round'); | |
gl.uniform1i(program.u_image, 0); | |
gl.activeTexture(gl.TEXTURE0); | |
painter.lineAtlas.bind(gl); | |
gl.uniform1f(program.u_tex_y_a, posA.y); | |
gl.uniform1f(program.u_tex_y_b, posB.y); | |
gl.uniform1f(program.u_mix, dasharray.t); | |
gl.uniform1f(program.u_extra, extra); | |
gl.uniform1f(program.u_offset, -layer.paint['line-offset']); | |
gl.uniformMatrix2fv(program.u_antialiasingmatrix, false, antialiasingMatrix); | |
} else if (image) { | |
imagePosA = painter.spriteAtlas.getPosition(image.from, true); | |
imagePosB = painter.spriteAtlas.getPosition(image.to, true); | |
if (!imagePosA || !imagePosB) return; | |
program = painter.useProgram('linepattern'); | |
gl.uniform1i(program.u_image, 0); | |
gl.activeTexture(gl.TEXTURE0); | |
painter.spriteAtlas.bind(gl, true); | |
gl.uniform1f(program.u_linewidth, layer.paint['line-width'] / 2); | |
gl.uniform1f(program.u_gapwidth, layer.paint['line-gap-width'] / 2); | |
gl.uniform1f(program.u_antialiasing, antialiasing / 2); | |
gl.uniform1f(program.u_blur, blur); | |
gl.uniform2fv(program.u_pattern_tl_a, imagePosA.tl); | |
gl.uniform2fv(program.u_pattern_br_a, imagePosA.br); | |
gl.uniform2fv(program.u_pattern_tl_b, imagePosB.tl); | |
gl.uniform2fv(program.u_pattern_br_b, imagePosB.br); | |
gl.uniform1f(program.u_fade, image.t); | |
gl.uniform1f(program.u_opacity, layer.paint['line-opacity']); | |
gl.uniform1f(program.u_extra, extra); | |
gl.uniform1f(program.u_offset, -layer.paint['line-offset']); | |
gl.uniformMatrix2fv(program.u_antialiasingmatrix, false, antialiasingMatrix); | |
} else { | |
program = painter.useProgram('line'); | |
gl.uniform1f(program.u_linewidth, layer.paint['line-width'] / 2); | |
gl.uniform1f(program.u_gapwidth, layer.paint['line-gap-width'] / 2); | |
gl.uniform1f(program.u_antialiasing, antialiasing / 2); | |
gl.uniform1f(program.u_blur, blur); | |
gl.uniform1f(program.u_extra, extra); | |
gl.uniform1f(program.u_offset, -layer.paint['line-offset']); | |
gl.uniformMatrix2fv(program.u_antialiasingmatrix, false, antialiasingMatrix); | |
gl.uniform4fv(program.u_color, color); | |
gl.uniform1f(program.u_opacity, layer.paint['line-opacity']); | |
} | |
for (var k = 0; k < coords.length; k++) { | |
var coord = coords[k]; | |
var tile = source.getTile(coord); | |
var bucket = tile.getBucket(layer); | |
if (!bucket) continue; | |
var bufferGroups = bucket.bufferGroups.line; | |
if (!bufferGroups) continue; | |
painter.enableTileClippingMask(coord); | |
// set uniforms that are different for each tile | |
var posMatrix = painter.translatePosMatrix(coord.posMatrix, tile, layer.paint['line-translate'], layer.paint['line-translate-anchor']); | |
gl.uniformMatrix4fv(program.u_matrix, false, posMatrix); | |
var ratio = 1 / pixelsToTileUnits(tile, 1, painter.transform.zoom); | |
if (dasharray) { | |
var widthA = posA.width * dasharray.fromScale; | |
var widthB = posB.width * dasharray.toScale; | |
var scaleA = [1 / pixelsToTileUnits(tile, widthA, painter.transform.tileZoom), -posA.height / 2]; | |
var scaleB = [1 / pixelsToTileUnits(tile, widthB, painter.transform.tileZoom), -posB.height / 2]; | |
var gamma = painter.lineAtlas.width / (Math.min(widthA, widthB) * 256 * browser.devicePixelRatio) / 2; | |
gl.uniform1f(program.u_ratio, ratio); | |
gl.uniform2fv(program.u_patternscale_a, scaleA); | |
gl.uniform2fv(program.u_patternscale_b, scaleB); | |
gl.uniform1f(program.u_sdfgamma, gamma); | |
} else if (image) { | |
gl.uniform1f(program.u_ratio, ratio); | |
gl.uniform2fv(program.u_pattern_size_a, [ | |
pixelsToTileUnits(tile, imagePosA.size[0] * image.fromScale, painter.transform.tileZoom), | |
imagePosB.size[1] | |
]); | |
gl.uniform2fv(program.u_pattern_size_b, [ | |
pixelsToTileUnits(tile, imagePosB.size[0] * image.toScale, painter.transform.tileZoom), | |
imagePosB.size[1] | |
]); | |
} else { | |
gl.uniform1f(program.u_ratio, ratio); | |
} | |
for (var i = 0; i < bufferGroups.length; i++) { | |
var group = bufferGroups[i]; | |
group.vaos[layer.id].bind(gl, program, group.layoutVertexBuffer, group.elementBuffer); | |
gl.drawElements(gl.TRIANGLES, group.elementBuffer.length * 3, gl.UNSIGNED_SHORT, 0); | |
} | |
} | |
}; | |
},{"../source/pixels_to_tile_units":32,"../util/browser":93,"gl-matrix":135}],22:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var StructArrayType = require('../util/struct_array'); | |
module.exports = drawRaster; | |
function drawRaster(painter, source, layer, coords) { | |
if (painter.isOpaquePass) return; | |
var gl = painter.gl; | |
gl.enable(gl.DEPTH_TEST); | |
painter.depthMask(true); | |
// Change depth function to prevent double drawing in areas where tiles overlap. | |
gl.depthFunc(gl.LESS); | |
var minTileZ = coords.length && coords[0].z; | |
for (var i = 0; i < coords.length; i++) { | |
var coord = coords[i]; | |
// set the lower zoom level to sublayer 0, and higher zoom levels to higher sublayers | |
painter.setDepthSublayer(coord.z - minTileZ); | |
drawRasterTile(painter, source, layer, coord); | |
} | |
gl.depthFunc(gl.LEQUAL); | |
} | |
drawRaster.RasterBoundsArray = new StructArrayType({ | |
members: [ | |
{ name: 'a_pos', type: 'Int16', components: 2 }, | |
{ name: 'a_texture_pos', type: 'Int16', components: 2 } | |
] | |
}); | |
function drawRasterTile(painter, source, layer, coord) { | |
var gl = painter.gl; | |
gl.disable(gl.STENCIL_TEST); | |
var tile = source.getTile(coord); | |
var posMatrix = painter.transform.calculatePosMatrix(coord, source.maxzoom); | |
var program = painter.useProgram('raster'); | |
gl.uniformMatrix4fv(program.u_matrix, false, posMatrix); | |
// color parameters | |
gl.uniform1f(program.u_brightness_low, layer.paint['raster-brightness-min']); | |
gl.uniform1f(program.u_brightness_high, layer.paint['raster-brightness-max']); | |
gl.uniform1f(program.u_saturation_factor, saturationFactor(layer.paint['raster-saturation'])); | |
gl.uniform1f(program.u_contrast_factor, contrastFactor(layer.paint['raster-contrast'])); | |
gl.uniform3fv(program.u_spin_weights, spinWeights(layer.paint['raster-hue-rotate'])); | |
var parentTile = tile.source && tile.source._pyramid.findLoadedParent(coord, 0, {}), | |
opacities = getOpacities(tile, parentTile, layer, painter.transform); | |
var parentScaleBy, parentTL; | |
gl.activeTexture(gl.TEXTURE0); | |
gl.bindTexture(gl.TEXTURE_2D, tile.texture); | |
gl.activeTexture(gl.TEXTURE1); | |
if (parentTile) { | |
gl.bindTexture(gl.TEXTURE_2D, parentTile.texture); | |
parentScaleBy = Math.pow(2, parentTile.coord.z - tile.coord.z); | |
parentTL = [tile.coord.x * parentScaleBy % 1, tile.coord.y * parentScaleBy % 1]; | |
} else { | |
gl.bindTexture(gl.TEXTURE_2D, tile.texture); | |
opacities[1] = 0; | |
} | |
// cross-fade parameters | |
gl.uniform2fv(program.u_tl_parent, parentTL || [0, 0]); | |
gl.uniform1f(program.u_scale_parent, parentScaleBy || 1); | |
gl.uniform1f(program.u_buffer_scale, 1); | |
gl.uniform1f(program.u_opacity0, opacities[0]); | |
gl.uniform1f(program.u_opacity1, opacities[1]); | |
gl.uniform1i(program.u_image0, 0); | |
gl.uniform1i(program.u_image1, 1); | |
var buffer = tile.boundsBuffer || painter.rasterBoundsBuffer; | |
var vao = tile.boundsVAO || painter.rasterBoundsVAO; | |
vao.bind(gl, program, buffer); | |
gl.drawArrays(gl.TRIANGLE_STRIP, 0, buffer.length); | |
} | |
function spinWeights(angle) { | |
angle *= Math.PI / 180; | |
var s = Math.sin(angle); | |
var c = Math.cos(angle); | |
return [ | |
(2 * c + 1) / 3, | |
(-Math.sqrt(3) * s - c + 1) / 3, | |
(Math.sqrt(3) * s - c + 1) / 3 | |
]; | |
} | |
function contrastFactor(contrast) { | |
return contrast > 0 ? | |
1 / (1 - contrast) : | |
1 + contrast; | |
} | |
function saturationFactor(saturation) { | |
return saturation > 0 ? | |
1 - 1 / (1.001 - saturation) : | |
-saturation; | |
} | |
function getOpacities(tile, parentTile, layer, transform) { | |
var opacity = [1, 0]; | |
var fadeDuration = layer.paint['raster-fade-duration']; | |
if (tile.source && fadeDuration > 0) { | |
var now = new Date().getTime(); | |
var sinceTile = (now - tile.timeAdded) / fadeDuration; | |
var sinceParent = parentTile ? (now - parentTile.timeAdded) / fadeDuration : -1; | |
var idealZ = tile.source._pyramid.coveringZoomLevel(transform); | |
var parentFurther = parentTile ? Math.abs(parentTile.coord.z - idealZ) > Math.abs(tile.coord.z - idealZ) : false; | |
if (!parentTile || parentFurther) { | |
// if no parent or parent is older | |
opacity[0] = util.clamp(sinceTile, 0, 1); | |
opacity[1] = 1 - opacity[0]; | |
} else { | |
// parent is younger, zooming out | |
opacity[0] = util.clamp(1 - sinceParent, 0, 1); | |
opacity[1] = 1 - opacity[0]; | |
} | |
} | |
var op = layer.paint['raster-opacity']; | |
opacity[0] *= op; | |
opacity[1] *= op; | |
return opacity; | |
} | |
},{"../util/struct_array":106,"../util/util":108}],23:[function(require,module,exports){ | |
'use strict'; | |
var browser = require('../util/browser'); | |
var drawCollisionDebug = require('./draw_collision_debug'); | |
var pixelsToTileUnits = require('../source/pixels_to_tile_units'); | |
module.exports = drawSymbols; | |
function drawSymbols(painter, source, layer, coords) { | |
if (painter.isOpaquePass) return; | |
var drawAcrossEdges = !(layer.layout['text-allow-overlap'] || layer.layout['icon-allow-overlap'] || | |
layer.layout['text-ignore-placement'] || layer.layout['icon-ignore-placement']); | |
var gl = painter.gl; | |
// Disable the stencil test so that labels aren't clipped to tile boundaries. | |
// | |
// Layers with features that may be drawn overlapping aren't clipped. These | |
// layers are sorted in the y direction, and to draw the correct ordering near | |
// tile edges the icons are included in both tiles and clipped when drawing. | |
if (drawAcrossEdges) { | |
gl.disable(gl.STENCIL_TEST); | |
} else { | |
gl.enable(gl.STENCIL_TEST); | |
} | |
painter.setDepthSublayer(0); | |
painter.depthMask(false); | |
gl.disable(gl.DEPTH_TEST); | |
drawLayerSymbols(painter, source, layer, coords, false, | |
layer.paint['icon-translate'], | |
layer.paint['icon-translate-anchor'], | |
layer.layout['icon-rotation-alignment'], | |
// icon-pitch-alignment is not yet implemented | |
// and we simply inherit the rotation alignment | |
layer.layout['icon-rotation-alignment'], | |
layer.layout['icon-size'], | |
layer.paint['icon-halo-width'], | |
layer.paint['icon-halo-color'], | |
layer.paint['icon-halo-blur'], | |
layer.paint['icon-opacity'], | |
layer.paint['icon-color']); | |
drawLayerSymbols(painter, source, layer, coords, true, | |
layer.paint['text-translate'], | |
layer.paint['text-translate-anchor'], | |
layer.layout['text-rotation-alignment'], | |
layer.layout['text-pitch-alignment'], | |
layer.layout['text-size'], | |
layer.paint['text-halo-width'], | |
layer.paint['text-halo-color'], | |
layer.paint['text-halo-blur'], | |
layer.paint['text-opacity'], | |
layer.paint['text-color']); | |
gl.enable(gl.DEPTH_TEST); | |
drawCollisionDebug(painter, source, layer, coords); | |
} | |
function drawLayerSymbols(painter, source, layer, coords, isText, | |
translate, | |
translateAnchor, | |
rotationAlignment, | |
pitchAlignment, | |
size, | |
haloWidth, | |
haloColor, | |
haloBlur, | |
opacity, | |
color) { | |
for (var j = 0; j < coords.length; j++) { | |
var tile = source.getTile(coords[j]); | |
var bucket = tile.getBucket(layer); | |
if (!bucket) continue; | |
var bothBufferGroups = bucket.bufferGroups; | |
var bufferGroups = isText ? bothBufferGroups.glyph : bothBufferGroups.icon; | |
if (!bufferGroups.length) continue; | |
painter.enableTileClippingMask(coords[j]); | |
drawSymbol(painter, layer, coords[j].posMatrix, tile, bucket, bufferGroups, isText, | |
isText || bucket.sdfIcons, !isText && bucket.iconsNeedLinear, | |
isText ? bucket.adjustedTextSize : bucket.adjustedIconSize, bucket.fontstack, | |
translate, | |
translateAnchor, | |
rotationAlignment, | |
pitchAlignment, | |
size, | |
haloWidth, | |
haloColor, | |
haloBlur, | |
opacity, | |
color); | |
} | |
} | |
function drawSymbol(painter, layer, posMatrix, tile, bucket, bufferGroups, isText, sdf, iconsNeedLinear, adjustedSize, fontstack, | |
translate, | |
translateAnchor, | |
rotationAlignment, | |
pitchAlignment, | |
size, | |
haloWidth, | |
haloColor, | |
haloBlur, | |
opacity, | |
color) { | |
var gl = painter.gl; | |
var tr = painter.transform; | |
var rotateWithMap = rotationAlignment === 'map'; | |
var pitchWithMap = pitchAlignment === 'map'; | |
var defaultSize = isText ? 24 : 1; | |
var fontScale = size / defaultSize; | |
var extrudeScale, s, gammaScale; | |
if (pitchWithMap) { | |
s = pixelsToTileUnits(tile, 1, painter.transform.zoom) * fontScale; | |
gammaScale = 1 / Math.cos(tr._pitch); | |
extrudeScale = [s, s]; | |
} else { | |
s = painter.transform.altitude * fontScale; | |
gammaScale = 1; | |
extrudeScale = [ tr.pixelsToGLUnits[0] * s, tr.pixelsToGLUnits[1] * s]; | |
} | |
if (!isText && !painter.style.sprite.loaded()) | |
return; | |
var program = painter.useProgram(sdf ? 'sdf' : 'icon'); | |
gl.uniformMatrix4fv(program.u_matrix, false, painter.translatePosMatrix(posMatrix, tile, translate, translateAnchor)); | |
gl.uniform1i(program.u_rotate_with_map, rotateWithMap); | |
gl.uniform1i(program.u_pitch_with_map, pitchWithMap); | |
gl.uniform2fv(program.u_extrude_scale, extrudeScale); | |
gl.activeTexture(gl.TEXTURE0); | |
gl.uniform1i(program.u_texture, 0); | |
if (isText) { | |
// use the fonstack used when parsing the tile, not the fontstack | |
// at the current zoom level (layout['text-font']). | |
var glyphAtlas = fontstack && painter.glyphSource.getGlyphAtlas(fontstack); | |
if (!glyphAtlas) return; | |
glyphAtlas.updateTexture(gl); | |
gl.uniform2f(program.u_texsize, glyphAtlas.width / 4, glyphAtlas.height / 4); | |
} else { | |
var mapMoving = painter.options.rotating || painter.options.zooming; | |
var iconScaled = fontScale !== 1 || browser.devicePixelRatio !== painter.spriteAtlas.pixelRatio || iconsNeedLinear; | |
var iconTransformed = pitchWithMap || painter.transform.pitch; | |
painter.spriteAtlas.bind(gl, sdf || mapMoving || iconScaled || iconTransformed); | |
gl.uniform2f(program.u_texsize, painter.spriteAtlas.width / 4, painter.spriteAtlas.height / 4); | |
} | |
// adjust min/max zooms for variable font sizes | |
var zoomAdjust = Math.log(size / adjustedSize) / Math.LN2 || 0; | |
gl.uniform1f(program.u_zoom, (painter.transform.zoom - zoomAdjust) * 10); // current zoom level | |
gl.activeTexture(gl.TEXTURE1); | |
painter.frameHistory.bind(gl); | |
gl.uniform1i(program.u_fadetexture, 1); | |
var group; | |
if (sdf) { | |
var sdfPx = 8; | |
var blurOffset = 1.19; | |
var haloOffset = 6; | |
var gamma = 0.105 * defaultSize / size / browser.devicePixelRatio; | |
if (haloWidth) { | |
// Draw halo underneath the text. | |
gl.uniform1f(program.u_gamma, (haloBlur * blurOffset / fontScale / sdfPx + gamma) * gammaScale); | |
gl.uniform4fv(program.u_color, haloColor); | |
gl.uniform1f(program.u_opacity, opacity); | |
gl.uniform1f(program.u_buffer, (haloOffset - haloWidth / fontScale) / sdfPx); | |
for (var j = 0; j < bufferGroups.length; j++) { | |
group = bufferGroups[j]; | |
group.vaos[layer.id].bind(gl, program, group.layoutVertexBuffer, group.elementBuffer); | |
gl.drawElements(gl.TRIANGLES, group.elementBuffer.length * 3, gl.UNSIGNED_SHORT, 0); | |
} | |
} | |
gl.uniform1f(program.u_gamma, gamma * gammaScale); | |
gl.uniform4fv(program.u_color, color); | |
gl.uniform1f(program.u_opacity, opacity); | |
gl.uniform1f(program.u_buffer, (256 - 64) / 256); | |
gl.uniform1f(program.u_pitch, tr.pitch / 360 * 2 * Math.PI); | |
gl.uniform1f(program.u_bearing, tr.bearing / 360 * 2 * Math.PI); | |
gl.uniform1f(program.u_aspect_ratio, tr.width / tr.height); | |
for (var i = 0; i < bufferGroups.length; i++) { | |
group = bufferGroups[i]; | |
group.vaos[layer.id].bind(gl, program, group.layoutVertexBuffer, group.elementBuffer); | |
gl.drawElements(gl.TRIANGLES, group.elementBuffer.length * 3, gl.UNSIGNED_SHORT, 0); | |
} | |
} else { | |
gl.uniform1f(program.u_opacity, opacity); | |
for (var k = 0; k < bufferGroups.length; k++) { | |
group = bufferGroups[k]; | |
group.vaos[layer.id].bind(gl, program, group.layoutVertexBuffer, group.elementBuffer); | |
gl.drawElements(gl.TRIANGLES, group.elementBuffer.length * 3, gl.UNSIGNED_SHORT, 0); | |
} | |
} | |
} | |
},{"../source/pixels_to_tile_units":32,"../util/browser":93,"./draw_collision_debug":18}],24:[function(require,module,exports){ | |
'use strict'; | |
module.exports = FrameHistory; | |
function FrameHistory() { | |
this.changeTimes = new Float64Array(256); | |
this.changeOpacities = new Uint8Array(256); | |
this.opacities = new Uint8ClampedArray(256); | |
this.array = new Uint8Array(this.opacities.buffer); | |
this.fadeDuration = 300; | |
this.previousZoom = 0; | |
this.firstFrame = true; | |
} | |
FrameHistory.prototype.record = function(zoom) { | |
var now = Date.now(); | |
if (this.firstFrame) { | |
now = 0; | |
this.firstFrame = false; | |
} | |
zoom = Math.floor(zoom * 10); | |
var z; | |
if (zoom < this.previousZoom) { | |
for (z = zoom + 1; z <= this.previousZoom; z++) { | |
this.changeTimes[z] = now; | |
this.changeOpacities[z] = this.opacities[z]; | |
} | |
} else { | |
for (z = zoom; z > this.previousZoom; z--) { | |
this.changeTimes[z] = now; | |
this.changeOpacities[z] = this.opacities[z]; | |
} | |
} | |
for (z = 0; z < 256; z++) { | |
var timeSince = now - this.changeTimes[z]; | |
var opacityChange = timeSince / this.fadeDuration * 255; | |
if (z <= zoom) { | |
this.opacities[z] = this.changeOpacities[z] + opacityChange; | |
} else { | |
this.opacities[z] = this.changeOpacities[z] - opacityChange; | |
} | |
} | |
this.changed = true; | |
this.previousZoom = zoom; | |
}; | |
FrameHistory.prototype.bind = function(gl) { | |
if (!this.texture) { | |
this.texture = gl.createTexture(); | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); | |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, 256, 1, 0, gl.ALPHA, gl.UNSIGNED_BYTE, this.array); | |
} else { | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
if (this.changed) { | |
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 256, 1, gl.ALPHA, gl.UNSIGNED_BYTE, this.array); | |
this.changed = false; | |
} | |
} | |
}; | |
},{}],25:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
module.exports = LineAtlas; | |
/** | |
* A LineAtlas lets us reuse rendered dashed lines | |
* by writing many of them to a texture and then fetching their positions | |
* using .getDash. | |
* | |
* @param {number} width | |
* @param {number} height | |
* @private | |
*/ | |
function LineAtlas(width, height) { | |
this.width = width; | |
this.height = height; | |
this.nextRow = 0; | |
this.bytes = 4; | |
this.data = new Uint8Array(this.width * this.height * this.bytes); | |
this.positions = {}; | |
} | |
LineAtlas.prototype.setSprite = function(sprite) { | |
this.sprite = sprite; | |
}; | |
/** | |
* Get or create a dash line pattern. | |
* | |
* @param {Array<number>} dasharray | |
* @param {boolean} round whether to add circle caps in between dash segments | |
* @returns {Object} position of dash texture in { y, height, width } | |
* @private | |
*/ | |
LineAtlas.prototype.getDash = function(dasharray, round) { | |
var key = dasharray.join(",") + round; | |
if (!this.positions[key]) { | |
this.positions[key] = this.addDash(dasharray, round); | |
} | |
return this.positions[key]; | |
}; | |
LineAtlas.prototype.addDash = function(dasharray, round) { | |
var n = round ? 7 : 0; | |
var height = 2 * n + 1; | |
var offset = 128; | |
if (this.nextRow + height > this.height) { | |
util.warnOnce('LineAtlas out of space'); | |
return null; | |
} | |
var length = 0; | |
for (var i = 0; i < dasharray.length; i++) { | |
length += dasharray[i]; | |
} | |
var stretch = this.width / length; | |
var halfWidth = stretch / 2; | |
// If dasharray has an odd length, both the first and last parts | |
// are dashes and should be joined seamlessly. | |
var oddLength = dasharray.length % 2 === 1; | |
for (var y = -n; y <= n; y++) { | |
var row = this.nextRow + n + y; | |
var index = this.width * row; | |
var left = oddLength ? -dasharray[dasharray.length - 1] : 0; | |
var right = dasharray[0]; | |
var partIndex = 1; | |
for (var x = 0; x < this.width; x++) { | |
while (right < x / stretch) { | |
left = right; | |
right = right + dasharray[partIndex]; | |
if (oddLength && partIndex === dasharray.length - 1) { | |
right += dasharray[0]; | |
} | |
partIndex++; | |
} | |
var distLeft = Math.abs(x - left * stretch); | |
var distRight = Math.abs(x - right * stretch); | |
var dist = Math.min(distLeft, distRight); | |
var inside = (partIndex % 2) === 1; | |
var signedDistance; | |
if (round) { | |
// Add circle caps | |
var distMiddle = n ? y / n * (halfWidth + 1) : 0; | |
if (inside) { | |
var distEdge = halfWidth - Math.abs(distMiddle); | |
signedDistance = Math.sqrt(dist * dist + distEdge * distEdge); | |
} else { | |
signedDistance = halfWidth - Math.sqrt(dist * dist + distMiddle * distMiddle); | |
} | |
} else { | |
signedDistance = (inside ? 1 : -1) * dist; | |
} | |
this.data[3 + (index + x) * 4] = Math.max(0, Math.min(255, signedDistance + offset)); | |
} | |
} | |
var pos = { | |
y: (this.nextRow + n + 0.5) / this.height, | |
height: 2 * n / this.height, | |
width: length | |
}; | |
this.nextRow += height; | |
this.dirty = true; | |
return pos; | |
}; | |
LineAtlas.prototype.bind = function(gl) { | |
if (!this.texture) { | |
this.texture = gl.createTexture(); | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, this.data); | |
} else { | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
if (this.dirty) { | |
this.dirty = false; | |
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, this.width, this.height, gl.RGBA, gl.UNSIGNED_BYTE, this.data); | |
} | |
} | |
}; | |
},{"../util/util":108}],26:[function(require,module,exports){ | |
'use strict'; | |
var browser = require('../util/browser'); | |
var mat4 = require('gl-matrix').mat4; | |
var FrameHistory = require('./frame_history'); | |
var TilePyramid = require('../source/tile_pyramid'); | |
var EXTENT = require('../data/bucket').EXTENT; | |
var pixelsToTileUnits = require('../source/pixels_to_tile_units'); | |
var util = require('../util/util'); | |
var StructArrayType = require('../util/struct_array'); | |
var Buffer = require('../data/buffer'); | |
var VertexArrayObject = require('./vertex_array_object'); | |
var RasterBoundsArray = require('./draw_raster').RasterBoundsArray; | |
var createUniformPragmas = require('./create_uniform_pragmas'); | |
module.exports = Painter; | |
/** | |
* Initialize a new painter object. | |
* | |
* @param {Canvas} gl an experimental-webgl drawing context | |
* @private | |
*/ | |
function Painter(gl, transform) { | |
this.gl = gl; | |
this.transform = transform; | |
this.reusableTextures = {}; | |
this.preFbos = {}; | |
this.frameHistory = new FrameHistory(); | |
this.setup(); | |
// Within each layer there are multiple distinct z-planes that can be drawn to. | |
// This is implemented using the WebGL depth buffer. | |
this.numSublayers = TilePyramid.maxUnderzooming + TilePyramid.maxOverzooming + 1; | |
this.depthEpsilon = 1 / Math.pow(2, 16); | |
this.lineWidthRange = gl.getParameter(gl.ALIASED_LINE_WIDTH_RANGE); | |
} | |
util.extend(Painter.prototype, require('./painter/use_program')); | |
/* | |
* Update the GL viewport, projection matrix, and transforms to compensate | |
* for a new width and height value. | |
*/ | |
Painter.prototype.resize = function(width, height) { | |
var gl = this.gl; | |
this.width = width * browser.devicePixelRatio; | |
this.height = height * browser.devicePixelRatio; | |
gl.viewport(0, 0, this.width, this.height); | |
}; | |
Painter.prototype.setup = function() { | |
var gl = this.gl; | |
gl.verbose = true; | |
// We are blending the new pixels *behind* the existing pixels. That way we can | |
// draw front-to-back and use then stencil buffer to cull opaque pixels early. | |
gl.enable(gl.BLEND); | |
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | |
gl.enable(gl.STENCIL_TEST); | |
gl.enable(gl.DEPTH_TEST); | |
gl.depthFunc(gl.LEQUAL); | |
this._depthMask = false; | |
gl.depthMask(false); | |
var PosArray = this.PosArray = new StructArrayType({ | |
members: [{ name: 'a_pos', type: 'Int16', components: 2 }] | |
}); | |
var tileExtentArray = new PosArray(); | |
tileExtentArray.emplaceBack(0, 0); | |
tileExtentArray.emplaceBack(EXTENT, 0); | |
tileExtentArray.emplaceBack(0, EXTENT); | |
tileExtentArray.emplaceBack(EXTENT, EXTENT); | |
this.tileExtentBuffer = new Buffer(tileExtentArray.serialize(), PosArray.serialize(), Buffer.BufferType.VERTEX); | |
this.tileExtentVAO = new VertexArrayObject(); | |
this.tileExtentPatternVAO = new VertexArrayObject(); | |
var debugArray = new PosArray(); | |
debugArray.emplaceBack(0, 0); | |
debugArray.emplaceBack(EXTENT, 0); | |
debugArray.emplaceBack(EXTENT, EXTENT); | |
debugArray.emplaceBack(0, EXTENT); | |
debugArray.emplaceBack(0, 0); | |
this.debugBuffer = new Buffer(debugArray.serialize(), PosArray.serialize(), Buffer.BufferType.VERTEX); | |
this.debugVAO = new VertexArrayObject(); | |
var rasterBoundsArray = new RasterBoundsArray(); | |
rasterBoundsArray.emplaceBack(0, 0, 0, 0); | |
rasterBoundsArray.emplaceBack(EXTENT, 0, 32767, 0); | |
rasterBoundsArray.emplaceBack(0, EXTENT, 0, 32767); | |
rasterBoundsArray.emplaceBack(EXTENT, EXTENT, 32767, 32767); | |
this.rasterBoundsBuffer = new Buffer(rasterBoundsArray.serialize(), RasterBoundsArray.serialize(), Buffer.BufferType.VERTEX); | |
this.rasterBoundsVAO = new VertexArrayObject(); | |
}; | |
/* | |
* Reset the color buffers of the drawing canvas. | |
*/ | |
Painter.prototype.clearColor = function() { | |
var gl = this.gl; | |
gl.clearColor(0, 0, 0, 0); | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
}; | |
/* | |
* Reset the drawing canvas by clearing the stencil buffer so that we can draw | |
* new tiles at the same location, while retaining previously drawn pixels. | |
*/ | |
Painter.prototype.clearStencil = function() { | |
var gl = this.gl; | |
gl.clearStencil(0x0); | |
gl.stencilMask(0xFF); | |
gl.clear(gl.STENCIL_BUFFER_BIT); | |
}; | |
Painter.prototype.clearDepth = function() { | |
var gl = this.gl; | |
gl.clearDepth(1); | |
this.depthMask(true); | |
gl.clear(gl.DEPTH_BUFFER_BIT); | |
}; | |
Painter.prototype._renderTileClippingMasks = function(coords) { | |
var gl = this.gl; | |
gl.colorMask(false, false, false, false); | |
this.depthMask(false); | |
gl.disable(gl.DEPTH_TEST); | |
gl.enable(gl.STENCIL_TEST); | |
// Only write clipping IDs to the last 5 bits. The first three are used for drawing fills. | |
gl.stencilMask(0xF8); | |
// Tests will always pass, and ref value will be written to stencil buffer. | |
gl.stencilOp(gl.KEEP, gl.KEEP, gl.REPLACE); | |
var idNext = 1; | |
this._tileClippingMaskIDs = {}; | |
for (var i = 0; i < coords.length; i++) { | |
var coord = coords[i]; | |
var id = this._tileClippingMaskIDs[coord.id] = (idNext++) << 3; | |
gl.stencilFunc(gl.ALWAYS, id, 0xF8); | |
var pragmas = createUniformPragmas([ | |
{name: 'u_color', components: 4}, | |
{name: 'u_opacity', components: 1} | |
]); | |
var program = this.useProgram('fill', [], pragmas, pragmas); | |
gl.uniformMatrix4fv(program.u_matrix, false, coord.posMatrix); | |
// Draw the clipping mask | |
this.tileExtentVAO.bind(gl, program, this.tileExtentBuffer); | |
gl.drawArrays(gl.TRIANGLE_STRIP, 0, this.tileExtentBuffer.length); | |
} | |
gl.stencilMask(0x00); | |
gl.colorMask(true, true, true, true); | |
this.depthMask(true); | |
gl.enable(gl.DEPTH_TEST); | |
}; | |
Painter.prototype.enableTileClippingMask = function(coord) { | |
var gl = this.gl; | |
gl.stencilFunc(gl.EQUAL, this._tileClippingMaskIDs[coord.id], 0xF8); | |
}; | |
// Overridden by headless tests. | |
Painter.prototype.prepareBuffers = function() {}; | |
Painter.prototype.bindDefaultFramebuffer = function() { | |
var gl = this.gl; | |
gl.bindFramebuffer(gl.FRAMEBUFFER, null); | |
}; | |
var draw = { | |
symbol: require('./draw_symbol'), | |
circle: require('./draw_circle'), | |
line: require('./draw_line'), | |
fill: require('./draw_fill'), | |
raster: require('./draw_raster'), | |
background: require('./draw_background'), | |
debug: require('./draw_debug') | |
}; | |
Painter.prototype.render = function(style, options) { | |
this.style = style; | |
this.options = options; | |
this.lineAtlas = style.lineAtlas; | |
this.spriteAtlas = style.spriteAtlas; | |
this.spriteAtlas.setSprite(style.sprite); | |
this.glyphSource = style.glyphSource; | |
this.frameHistory.record(this.transform.zoom); | |
this.prepareBuffers(); | |
this.clearColor(); | |
this.clearDepth(); | |
this.showOverdrawInspector(options.showOverdrawInspector); | |
this.depthRange = (style._order.length + 2) * this.numSublayers * this.depthEpsilon; | |
this.renderPass({isOpaquePass: true}); | |
this.renderPass({isOpaquePass: false}); | |
}; | |
Painter.prototype.renderPass = function(options) { | |
var groups = this.style._groups; | |
var isOpaquePass = options.isOpaquePass; | |
this.currentLayer = isOpaquePass ? this.style._order.length : -1; | |
for (var i = 0; i < groups.length; i++) { | |
var group = groups[isOpaquePass ? groups.length - 1 - i : i]; | |
var source = this.style.sources[group.source]; | |
var j; | |
var coords = []; | |
if (source) { | |
coords = source.getVisibleCoordinates(); | |
for (j = 0; j < coords.length; j++) { | |
coords[j].posMatrix = this.transform.calculatePosMatrix(coords[j], source.maxzoom); | |
} | |
this.clearStencil(); | |
if (source.prepare) source.prepare(); | |
if (source.isTileClipped) { | |
this._renderTileClippingMasks(coords); | |
} | |
} | |
if (isOpaquePass) { | |
if (!this._showOverdrawInspector) { | |
this.gl.disable(this.gl.BLEND); | |
} | |
this.isOpaquePass = true; | |
} else { | |
this.gl.enable(this.gl.BLEND); | |
this.isOpaquePass = false; | |
coords.reverse(); | |
} | |
for (j = 0; j < group.length; j++) { | |
var layer = group[isOpaquePass ? group.length - 1 - j : j]; | |
this.currentLayer += isOpaquePass ? -1 : 1; | |
this.renderLayer(this, source, layer, coords); | |
} | |
if (source) { | |
draw.debug(this, source, coords); | |
} | |
} | |
}; | |
Painter.prototype.depthMask = function(mask) { | |
if (mask !== this._depthMask) { | |
this._depthMask = mask; | |
this.gl.depthMask(mask); | |
} | |
}; | |
Painter.prototype.renderLayer = function(painter, source, layer, coords) { | |
if (layer.isHidden(this.transform.zoom)) return; | |
if (layer.type !== 'background' && !coords.length) return; | |
this.id = layer.id; | |
draw[layer.type](painter, source, layer, coords); | |
}; | |
Painter.prototype.setDepthSublayer = function(n) { | |
var farDepth = 1 - ((1 + this.currentLayer) * this.numSublayers + n) * this.depthEpsilon; | |
var nearDepth = farDepth - 1 + this.depthRange; | |
this.gl.depthRange(nearDepth, farDepth); | |
}; | |
Painter.prototype.translatePosMatrix = function(matrix, tile, translate, anchor) { | |
if (!translate[0] && !translate[1]) return matrix; | |
if (anchor === 'viewport') { | |
var sinA = Math.sin(-this.transform.angle); | |
var cosA = Math.cos(-this.transform.angle); | |
translate = [ | |
translate[0] * cosA - translate[1] * sinA, | |
translate[0] * sinA + translate[1] * cosA | |
]; | |
} | |
var translation = [ | |
pixelsToTileUnits(tile, translate[0], this.transform.zoom), | |
pixelsToTileUnits(tile, translate[1], this.transform.zoom), | |
0 | |
]; | |
var translatedMatrix = new Float32Array(16); | |
mat4.translate(translatedMatrix, matrix, translation); | |
return translatedMatrix; | |
}; | |
Painter.prototype.saveTexture = function(texture) { | |
var textures = this.reusableTextures[texture.size]; | |
if (!textures) { | |
this.reusableTextures[texture.size] = [texture]; | |
} else { | |
textures.push(texture); | |
} | |
}; | |
Painter.prototype.getTexture = function(size) { | |
var textures = this.reusableTextures[size]; | |
return textures && textures.length > 0 ? textures.pop() : null; | |
}; | |
Painter.prototype.lineWidth = function(width) { | |
this.gl.lineWidth(util.clamp(width, this.lineWidthRange[0], this.lineWidthRange[1])); | |
}; | |
Painter.prototype.showOverdrawInspector = function(enabled) { | |
if (!enabled && !this._showOverdrawInspector) return; | |
this._showOverdrawInspector = enabled; | |
var gl = this.gl; | |
if (enabled) { | |
gl.blendFunc(gl.CONSTANT_COLOR, gl.ONE); | |
var numOverdrawSteps = 8; | |
var a = 1 / numOverdrawSteps; | |
gl.blendColor(a, a, a, 0); | |
gl.clearColor(0, 0, 0, 1); | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
} else { | |
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | |
} | |
}; | |
},{"../data/bucket":1,"../data/buffer":6,"../source/pixels_to_tile_units":32,"../source/tile_pyramid":37,"../util/browser":93,"../util/struct_array":106,"../util/util":108,"./create_uniform_pragmas":15,"./draw_background":16,"./draw_circle":17,"./draw_debug":19,"./draw_fill":20,"./draw_line":21,"./draw_raster":22,"./draw_symbol":23,"./frame_history":24,"./painter/use_program":27,"./vertex_array_object":28,"gl-matrix":135}],27:[function(require,module,exports){ | |
'use strict'; | |
var assert = require('assert'); | |
var util = require('../../util/util'); | |
var shaders = require('mapbox-gl-shaders'); | |
var utilSource = shaders.util; | |
module.exports._createProgram = function(name, defines, vertexPragmas, fragmentPragmas) { | |
var gl = this.gl; | |
var program = gl.createProgram(); | |
var definition = shaders[name]; | |
var definesSource = '#define MAPBOX_GL_JS;\n'; | |
for (var j = 0; j < defines.length; j++) { | |
definesSource += '#define ' + defines[j] + ';\n'; | |
} | |
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); | |
gl.shaderSource(fragmentShader, applyPragmas(definesSource + definition.fragmentSource, fragmentPragmas)); | |
gl.compileShader(fragmentShader); | |
assert(gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS), gl.getShaderInfoLog(fragmentShader)); | |
gl.attachShader(program, fragmentShader); | |
var vertexShader = gl.createShader(gl.VERTEX_SHADER); | |
gl.shaderSource(vertexShader, applyPragmas(definesSource + utilSource + definition.vertexSource, vertexPragmas)); | |
gl.compileShader(vertexShader); | |
assert(gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS), gl.getShaderInfoLog(vertexShader)); | |
gl.attachShader(program, vertexShader); | |
gl.linkProgram(program); | |
assert(gl.getProgramParameter(program, gl.LINK_STATUS), gl.getProgramInfoLog(program)); | |
var attributes = {}; | |
var numAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES); | |
for (var i = 0; i < numAttributes; i++) { | |
var attribute = gl.getActiveAttrib(program, i); | |
attributes[attribute.name] = gl.getAttribLocation(program, attribute.name); | |
} | |
var uniforms = {}; | |
var numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS); | |
for (var ui = 0; ui < numUniforms; ui++) { | |
var uniform = gl.getActiveUniform(program, ui); | |
uniforms[uniform.name] = gl.getUniformLocation(program, uniform.name); | |
} | |
return util.extend({ | |
program: program, | |
definition: definition, | |
attributes: attributes, | |
numAttributes: numAttributes | |
}, attributes, uniforms); | |
}; | |
module.exports._createProgramCached = function(name, defines, vertexPragmas, fragmentPragmas) { | |
this.cache = this.cache || {}; | |
var key = JSON.stringify({ | |
name: name, | |
defines: defines, | |
vertexPragmas: vertexPragmas, | |
fragmentPragmas: fragmentPragmas | |
}); | |
if (!this.cache[key]) { | |
this.cache[key] = this._createProgram(name, defines, vertexPragmas, fragmentPragmas); | |
} | |
return this.cache[key]; | |
}; | |
module.exports.useProgram = function (nextProgramName, defines, vertexPragmas, fragmentPragmas) { | |
var gl = this.gl; | |
defines = defines || []; | |
if (this._showOverdrawInspector) { | |
defines = defines.concat('OVERDRAW_INSPECTOR'); | |
} | |
var nextProgram = this._createProgramCached(nextProgramName, defines, vertexPragmas, fragmentPragmas); | |
var previousProgram = this.currentProgram; | |
if (previousProgram !== nextProgram) { | |
gl.useProgram(nextProgram.program); | |
this.currentProgram = nextProgram; | |
} | |
return nextProgram; | |
}; | |
function applyPragmas(source, pragmas) { | |
return source.replace(/#pragma mapbox: ([\w]+) ([\w]+) ([\w]+) ([\w]+)/g, function(match, operation, precision, type, name) { | |
return pragmas[operation][name].replace(/{type}/g, type).replace(/{precision}/g, precision); | |
}); | |
} | |
},{"../../util/util":108,"assert":110,"mapbox-gl-shaders":147}],28:[function(require,module,exports){ | |
'use strict'; | |
var assert = require('assert'); | |
module.exports = VertexArrayObject; | |
function VertexArrayObject() { | |
this.boundProgram = null; | |
this.boundVertexBuffer = null; | |
this.boundVertexBuffer2 = null; | |
this.boundElementBuffer = null; | |
this.vao = null; | |
} | |
VertexArrayObject.prototype.bind = function(gl, program, layoutVertexBuffer, elementBuffer, vertexBuffer2) { | |
if (gl.extVertexArrayObject === undefined) { | |
gl.extVertexArrayObject = gl.getExtension("OES_vertex_array_object"); | |
} | |
var isFreshBindRequired = ( | |
!this.vao || | |
this.boundProgram !== program || | |
this.boundVertexBuffer !== layoutVertexBuffer || | |
this.boundVertexBuffer2 !== vertexBuffer2 || | |
this.boundElementBuffer !== elementBuffer | |
); | |
if (!gl.extVertexArrayObject || isFreshBindRequired) { | |
this.freshBind(gl, program, layoutVertexBuffer, elementBuffer, vertexBuffer2); | |
} else { | |
gl.extVertexArrayObject.bindVertexArrayOES(this.vao); | |
} | |
}; | |
VertexArrayObject.prototype.freshBind = function(gl, program, layoutVertexBuffer, elementBuffer, vertexBuffer2) { | |
var numPrevAttributes; | |
var numNextAttributes = program.numAttributes; | |
if (gl.extVertexArrayObject) { | |
if (this.vao) this.destroy(gl); | |
this.vao = gl.extVertexArrayObject.createVertexArrayOES(); | |
gl.extVertexArrayObject.bindVertexArrayOES(this.vao); | |
numPrevAttributes = 0; | |
// store the arguments so that we can verify them when the vao is bound again | |
this.boundProgram = program; | |
this.boundVertexBuffer = layoutVertexBuffer; | |
this.boundVertexBuffer2 = vertexBuffer2; | |
this.boundElementBuffer = elementBuffer; | |
} else { | |
numPrevAttributes = gl.currentNumAttributes || 0; | |
// Disable all attributes from the previous program that aren't used in | |
// the new program. Note: attribute indices are *not* program specific! | |
for (var i = numNextAttributes; i < numPrevAttributes; i++) { | |
// WebGL breaks if you disable attribute 0. | |
// http://stackoverflow.com/questions/20305231 | |
assert(i !== 0); | |
gl.disableVertexAttribArray(i); | |
} | |
} | |
// Enable all attributes for the new program. | |
for (var j = numPrevAttributes; j < numNextAttributes; j++) { | |
gl.enableVertexAttribArray(j); | |
} | |
layoutVertexBuffer.bind(gl); | |
layoutVertexBuffer.setVertexAttribPointers(gl, program); | |
if (vertexBuffer2) { | |
vertexBuffer2.bind(gl); | |
vertexBuffer2.setVertexAttribPointers(gl, program); | |
} | |
if (elementBuffer) { | |
elementBuffer.bind(gl); | |
} | |
gl.currentNumAttributes = numNextAttributes; | |
}; | |
VertexArrayObject.prototype.unbind = function(gl) { | |
var ext = gl.extVertexArrayObject; | |
if (ext) { | |
ext.bindVertexArrayOES(null); | |
} | |
}; | |
VertexArrayObject.prototype.destroy = function(gl) { | |
var ext = gl.extVertexArrayObject; | |
if (ext && this.vao) { | |
ext.deleteVertexArrayOES(this.vao); | |
this.vao = null; | |
} | |
}; | |
},{"assert":110}],29:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var Evented = require('../util/evented'); | |
var TilePyramid = require('./tile_pyramid'); | |
var Source = require('./source'); | |
var urlResolve = require('resolve-url'); | |
var EXTENT = require('../data/bucket').EXTENT; | |
module.exports = GeoJSONSource; | |
/** | |
* A datasource containing GeoJSON. | |
* | |
* @class GeoJSONSource | |
* @param {Object} [options] | |
* @param {Object|string} [options.data] A GeoJSON data object or a URL to one. The latter is preferable in the case of large GeoJSON objects. | |
* @param {number} [options.maxzoom=18] The maximum zoom level at which to preserve detail (1-20). | |
* @param {number} [options.buffer=128] The tile buffer, measured in pixels. The buffer extends each | |
* tile's data just past its visible edges, helping to ensure seamless rendering across tile boundaries. | |
* The default value, 128, is a safe value for label layers, preventing text clipping at boundaries. | |
* You can read more about buffers and clipping in the | |
* [Mapbox Vector Tile Specification](https://www.mapbox.com/vector-tiles/specification/#clipping). | |
* @param {number} [options.tolerance=0.375] The simplification tolerance, measured in pixels. | |
* This value is passed into a modified [Ramer–Douglas–Peucker algorithm](https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm) | |
* to simplify (i.e. reduce the number of points) in curves. Higher values result in greater simplification. | |
* @param {boolean} [options.cluster] If `true`, a collection of point features will be clustered into groups, | |
* according to `options.clusterRadius`. | |
* @param {number} [options.clusterRadius=50] The radius of each cluster when clustering points, measured in pixels. | |
* @param {number} [options.clusterMaxZoom] The maximum zoom level to cluster points in. By default, this value is | |
* one zoom level less than the map's `maxzoom`, so that at the highest zoom level features are not clustered. | |
* @example | |
* var sourceObj = new mapboxgl.GeoJSONSource({ | |
* data: { | |
* "type": "FeatureCollection", | |
* "features": [{ | |
* "type": "Feature", | |
* "geometry": { | |
* "type": "Point", | |
* "coordinates": [ | |
* -76.53063297271729, | |
* 39.18174077994108 | |
* ] | |
* } | |
* }] | |
* } | |
* }); | |
* map.addSource('some id', sourceObj); // add | |
* map.removeSource('some id'); // remove | |
*/ | |
function GeoJSONSource(options) { | |
options = options || {}; | |
this._data = options.data; | |
if (options.maxzoom !== undefined) this.maxzoom = options.maxzoom; | |
var scale = EXTENT / this.tileSize; | |
this.geojsonVtOptions = { | |
buffer: (options.buffer !== undefined ? options.buffer : 128) * scale, | |
tolerance: (options.tolerance !== undefined ? options.tolerance : 0.375) * scale, | |
extent: EXTENT, | |
maxZoom: this.maxzoom | |
}; | |
this.cluster = options.cluster || false; | |
this.superclusterOptions = { | |
maxZoom: Math.min(options.clusterMaxZoom, this.maxzoom - 1) || (this.maxzoom - 1), | |
extent: EXTENT, | |
radius: (options.clusterRadius || 50) * scale, | |
log: false | |
}; | |
this._pyramid = new TilePyramid({ | |
tileSize: this.tileSize, | |
minzoom: this.minzoom, | |
maxzoom: this.maxzoom, | |
reparseOverscaled: true, | |
load: this._loadTile.bind(this), | |
abort: this._abortTile.bind(this), | |
unload: this._unloadTile.bind(this), | |
add: this._addTile.bind(this), | |
remove: this._removeTile.bind(this), | |
redoPlacement: this._redoTilePlacement.bind(this) | |
}); | |
} | |
GeoJSONSource.prototype = util.inherit(Evented, /** @lends GeoJSONSource.prototype */{ | |
minzoom: 0, | |
maxzoom: 18, | |
tileSize: 512, | |
_dirty: true, | |
isTileClipped: true, | |
/** | |
* Sets the GeoJSON data and re-renders the map. | |
* | |
* @param {Object|string} data A GeoJSON data object or a URL to one. The latter is preferable in the case of large GeoJSON files. | |
* @returns {GeoJSONSource} this | |
*/ | |
setData: function(data) { | |
this._data = data; | |
this._dirty = true; | |
this.fire('change'); | |
if (this.map) | |
this.update(this.map.transform); | |
return this; | |
}, | |
onAdd: function(map) { | |
this.map = map; | |
}, | |
loaded: function() { | |
return this._loaded && this._pyramid.loaded(); | |
}, | |
update: function(transform) { | |
if (this._dirty) { | |
this._updateData(); | |
} | |
if (this._loaded) { | |
this._pyramid.update(this.used, transform); | |
} | |
}, | |
reload: function() { | |
if (this._loaded) { | |
this._pyramid.reload(); | |
} | |
}, | |
serialize: function() { | |
return { | |
type: 'geojson', | |
data: this._data | |
}; | |
}, | |
getVisibleCoordinates: Source._getVisibleCoordinates, | |
getTile: Source._getTile, | |
queryRenderedFeatures: Source._queryRenderedVectorFeatures, | |
querySourceFeatures: Source._querySourceFeatures, | |
_updateData: function() { | |
this._dirty = false; | |
var options = { | |
tileSize: this.tileSize, | |
source: this.id, | |
geojsonVtOptions: this.geojsonVtOptions, | |
cluster: this.cluster, | |
superclusterOptions: this.superclusterOptions | |
}; | |
var data = this._data; | |
if (typeof data === 'string') { | |
options.url = typeof window != 'undefined' ? urlResolve(window.location.href, data) : data; | |
} else { | |
options.data = JSON.stringify(data); | |
} | |
this.workerID = this.dispatcher.send('parse geojson', options, function(err) { | |
this._loaded = true; | |
if (err) { | |
this.fire('error', {error: err}); | |
} else { | |
this._pyramid.reload(); | |
this.fire('change'); | |
} | |
}.bind(this)); | |
}, | |
_loadTile: function(tile) { | |
var overscaling = tile.coord.z > this.maxzoom ? Math.pow(2, tile.coord.z - this.maxzoom) : 1; | |
var params = { | |
uid: tile.uid, | |
coord: tile.coord, | |
zoom: tile.coord.z, | |
maxZoom: this.maxzoom, | |
tileSize: this.tileSize, | |
source: this.id, | |
overscaling: overscaling, | |
angle: this.map.transform.angle, | |
pitch: this.map.transform.pitch, | |
showCollisionBoxes: this.map.showCollisionBoxes | |
}; | |
tile.workerID = this.dispatcher.send('load geojson tile', params, function(err, data) { | |
tile.unloadVectorData(this.map.painter); | |
if (tile.aborted) | |
return; | |
if (err) { | |
this.fire('tile.error', {tile: tile}); | |
return; | |
} | |
tile.loadVectorData(data, this.map.style); | |
if (tile.redoWhenDone) { | |
tile.redoWhenDone = false; | |
tile.redoPlacement(this); | |
} | |
this.fire('tile.load', {tile: tile}); | |
}.bind(this), this.workerID); | |
}, | |
_abortTile: function(tile) { | |
tile.aborted = true; | |
}, | |
_addTile: function(tile) { | |
this.fire('tile.add', {tile: tile}); | |
}, | |
_removeTile: function(tile) { | |
this.fire('tile.remove', {tile: tile}); | |
}, | |
_unloadTile: function(tile) { | |
tile.unloadVectorData(this.map.painter); | |
this.dispatcher.send('remove tile', { uid: tile.uid, source: this.id }, function() {}, tile.workerID); | |
}, | |
redoPlacement: Source.redoPlacement, | |
_redoTilePlacement: function(tile) { | |
tile.redoPlacement(this); | |
} | |
}); | |
},{"../data/bucket":1,"../util/evented":100,"../util/util":108,"./source":34,"./tile_pyramid":37,"resolve-url":179}],30:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
var VectorTileFeature = require('vector-tile').VectorTileFeature; | |
var EXTENT = require('../data/bucket').EXTENT; | |
module.exports = GeoJSONWrapper; | |
// conform to vectortile api | |
function GeoJSONWrapper(features) { | |
this.features = features; | |
this.length = features.length; | |
this.extent = EXTENT; | |
} | |
GeoJSONWrapper.prototype.feature = function(i) { | |
return new FeatureWrapper(this.features[i]); | |
}; | |
function FeatureWrapper(feature) { | |
this.type = feature.type; | |
if (feature.type === 1) { | |
this.rawGeometry = []; | |
for (var i = 0; i < feature.geometry.length; i++) { | |
this.rawGeometry.push([feature.geometry[i]]); | |
} | |
} else { | |
this.rawGeometry = feature.geometry; | |
} | |
this.properties = feature.tags; | |
this.extent = EXTENT; | |
} | |
FeatureWrapper.prototype.loadGeometry = function() { | |
var rings = this.rawGeometry; | |
this.geometry = []; | |
for (var i = 0; i < rings.length; i++) { | |
var ring = rings[i], | |
newRing = []; | |
for (var j = 0; j < ring.length; j++) { | |
newRing.push(new Point(ring[j][0], ring[j][1])); | |
} | |
this.geometry.push(newRing); | |
} | |
return this.geometry; | |
}; | |
FeatureWrapper.prototype.bbox = function() { | |
if (!this.geometry) this.loadGeometry(); | |
var rings = this.geometry, | |
x1 = Infinity, | |
x2 = -Infinity, | |
y1 = Infinity, | |
y2 = -Infinity; | |
for (var i = 0; i < rings.length; i++) { | |
var ring = rings[i]; | |
for (var j = 0; j < ring.length; j++) { | |
var coord = ring[j]; | |
x1 = Math.min(x1, coord.x); | |
x2 = Math.max(x2, coord.x); | |
y1 = Math.min(y1, coord.y); | |
y2 = Math.max(y2, coord.y); | |
} | |
} | |
return [x1, y1, x2, y2]; | |
}; | |
FeatureWrapper.prototype.toGeoJSON = VectorTileFeature.prototype.toGeoJSON; | |
},{"../data/bucket":1,"point-geometry":177,"vector-tile":187}],31:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var Tile = require('./tile'); | |
var TileCoord = require('./tile_coord'); | |
var LngLat = require('../geo/lng_lat'); | |
var Point = require('point-geometry'); | |
var Evented = require('../util/evented'); | |
var ajax = require('../util/ajax'); | |
var EXTENT = require('../data/bucket').EXTENT; | |
var RasterBoundsArray = require('../render/draw_raster').RasterBoundsArray; | |
var Buffer = require('../data/buffer'); | |
var VertexArrayObject = require('../render/vertex_array_object'); | |
module.exports = ImageSource; | |
/** | |
* A data source containing an image. | |
* | |
* @class ImageSource | |
* @param {Object} options | |
* @param {string} options.url The URL of an image file. | |
* @param {Array<Array<number>>} options.coordinates Four geographical coordinates, | |
* represented as arrays of longitude and latitude numbers, which define the corners of the image. | |
* The coordinates start at the top left corner of the image and proceed in clockwise order. | |
* They do not have to represent a rectangle. | |
* @example | |
* var sourceObj = new mapboxgl.ImageSource({ | |
* url: 'https://www.mapbox.com/images/foo.png', | |
* coordinates: [ | |
* [-76.54335737228394, 39.18579907229748], | |
* [-76.52803659439087, 39.1838364847587], | |
* [-76.5295386314392, 39.17683392507606], | |
* [-76.54520273208618, 39.17876344106642] | |
* ] | |
* }); | |
* map.addSource('some id', sourceObj); // add | |
* map.removeSource('some id'); // remove | |
*/ | |
function ImageSource(options) { | |
this.url = options.url; | |
this.coordinates = options.coordinates; | |
ajax.getImage(options.url, function(err, image) { | |
// @TODO handle errors via event. | |
if (err) return; | |
this.image = image; | |
this.image.addEventListener('load', function() { | |
this.map._rerender(); | |
}.bind(this)); | |
this._loaded = true; | |
if (this.map) { | |
this.setCoordinates(options.coordinates); | |
} | |
}.bind(this)); | |
} | |
ImageSource.prototype = util.inherit(Evented, /** @lends ImageSource.prototype */ { | |
onAdd: function(map) { | |
this.map = map; | |
if (this.image) { | |
this.setCoordinates(this.coordinates); | |
} | |
}, | |
/** | |
* Sets the image's coordinates and re-renders the map. | |
* | |
* @param {Array<Array<number>>} coordinates Four geographical coordinates, | |
* represented as arrays of longitude and latitude numbers, which define the corners of the image. | |
* The coordinates start at the top left corner of the image and proceed in clockwise order. | |
* They do not have to represent a rectangle. | |
* @returns {ImageSource} this | |
*/ | |
setCoordinates: function(coordinates) { | |
this.coordinates = coordinates; | |
// Calculate which mercator tile is suitable for rendering the image in | |
// and create a buffer with the corner coordinates. These coordinates | |
// may be outside the tile, because raster tiles aren't clipped when rendering. | |
var map = this.map; | |
var cornerZ0Coords = coordinates.map(function(coord) { | |
return map.transform.locationCoordinate(LngLat.convert(coord)).zoomTo(0); | |
}); | |
var centerCoord = this.centerCoord = util.getCoordinatesCenter(cornerZ0Coords); | |
centerCoord.column = Math.round(centerCoord.column); | |
centerCoord.row = Math.round(centerCoord.row); | |
var tileCoords = cornerZ0Coords.map(function(coord) { | |
var zoomedCoord = coord.zoomTo(centerCoord.zoom); | |
return new Point( | |
Math.round((zoomedCoord.column - centerCoord.column) * EXTENT), | |
Math.round((zoomedCoord.row - centerCoord.row) * EXTENT)); | |
}); | |
var maxInt16 = 32767; | |
var array = new RasterBoundsArray(); | |
array.emplaceBack(tileCoords[0].x, tileCoords[0].y, 0, 0); | |
array.emplaceBack(tileCoords[1].x, tileCoords[1].y, maxInt16, 0); | |
array.emplaceBack(tileCoords[3].x, tileCoords[3].y, 0, maxInt16); | |
array.emplaceBack(tileCoords[2].x, tileCoords[2].y, maxInt16, maxInt16); | |
this.tile = new Tile(new TileCoord(centerCoord.zoom, centerCoord.column, centerCoord.row)); | |
this.tile.buckets = {}; | |
this.tile.boundsBuffer = new Buffer(array.serialize(), RasterBoundsArray.serialize(), Buffer.BufferType.VERTEX); | |
this.tile.boundsVAO = new VertexArrayObject(); | |
this.fire('change'); | |
return this; | |
}, | |
loaded: function() { | |
return this.image && this.image.complete; | |
}, | |
update: function() { | |
// noop | |
}, | |
reload: function() { | |
// noop | |
}, | |
prepare: function() { | |
if (!this._loaded || !this.loaded()) return; | |
var painter = this.map.painter; | |
var gl = painter.gl; | |
if (!this.tile.texture) { | |
this.tile.texture = gl.createTexture(); | |
gl.bindTexture(gl.TEXTURE_2D, this.tile.texture); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.image); | |
} else { | |
gl.bindTexture(gl.TEXTURE_2D, this.tile.texture); | |
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, this.image); | |
} | |
}, | |
getVisibleCoordinates: function() { | |
if (this.tile) return [this.tile.coord]; | |
else return []; | |
}, | |
getTile: function() { | |
return this.tile; | |
}, | |
serialize: function() { | |
return { | |
type: 'image', | |
urls: this.url, | |
coordinates: this.coordinates | |
}; | |
} | |
}); | |
},{"../data/bucket":1,"../data/buffer":6,"../geo/lng_lat":10,"../render/draw_raster":22,"../render/vertex_array_object":28,"../util/ajax":92,"../util/evented":100,"../util/util":108,"./tile":35,"./tile_coord":36,"point-geometry":177}],32:[function(require,module,exports){ | |
'use strict'; | |
var Bucket = require('../data/bucket'); | |
/** | |
* Converts a pixel value at a the given zoom level to tile units. | |
* | |
* The shaders mostly calculate everything in tile units so style | |
* properties need to be converted from pixels to tile units using this. | |
* | |
* For example, a translation by 30 pixels at zoom 6.5 will be a | |
* translation by pixelsToTileUnits(30, 6.5) tile units. | |
* | |
* @param {object} tile a {Tile object} will work well, but any object that follows the format {coord: {TileCord object}, tileSize: {number}} will work | |
* @param {number} pixelValue | |
* @param {number} z | |
* @returns {number} value in tile units | |
* @private | |
*/ | |
module.exports = function(tile, pixelValue, z) { | |
return pixelValue * (Bucket.EXTENT / (tile.tileSize * Math.pow(2, z - tile.coord.z))); | |
}; | |
},{"../data/bucket":1}],33:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var ajax = require('../util/ajax'); | |
var Evented = require('../util/evented'); | |
var Source = require('./source'); | |
var normalizeURL = require('../util/mapbox').normalizeTileURL; | |
module.exports = RasterTileSource; | |
function RasterTileSource(options) { | |
util.extend(this, util.pick(options, ['url', 'scheme', 'tileSize'])); | |
Source._loadTileJSON.call(this, options); | |
} | |
RasterTileSource.prototype = util.inherit(Evented, { | |
minzoom: 0, | |
maxzoom: 22, | |
roundZoom: true, | |
scheme: 'xyz', | |
tileSize: 512, | |
_loaded: false, | |
onAdd: function(map) { | |
this.map = map; | |
}, | |
loaded: function() { | |
return this._pyramid && this._pyramid.loaded(); | |
}, | |
update: function(transform) { | |
if (this._pyramid) { | |
this._pyramid.update(this.used, transform, this.map.style.rasterFadeDuration); | |
} | |
}, | |
reload: function() { | |
// noop | |
}, | |
serialize: function() { | |
return { | |
type: 'raster', | |
url: this.url, | |
tileSize: this.tileSize | |
}; | |
}, | |
getVisibleCoordinates: Source._getVisibleCoordinates, | |
getTile: Source._getTile, | |
_loadTile: function(tile) { | |
var url = normalizeURL(tile.coord.url(this.tiles, null, this.scheme), this.url, this.tileSize); | |
tile.request = ajax.getImage(url, done.bind(this)); | |
function done(err, img) { | |
delete tile.request; | |
if (tile.aborted) | |
return; | |
if (err) { | |
tile.state = 'errored'; | |
this.fire('tile.error', {tile: tile, error: err}); | |
return; | |
} | |
var gl = this.map.painter.gl; | |
tile.texture = this.map.painter.getTexture(img.width); | |
if (tile.texture) { | |
gl.bindTexture(gl.TEXTURE_2D, tile.texture); | |
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, img); | |
} else { | |
tile.texture = gl.createTexture(); | |
gl.bindTexture(gl.TEXTURE_2D, tile.texture); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | |
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); | |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img); | |
tile.texture.size = img.width; | |
} | |
gl.generateMipmap(gl.TEXTURE_2D); | |
tile.timeAdded = new Date().getTime(); | |
this.map.animationLoop.set(this.style.rasterFadeDuration); | |
tile.source = this; | |
tile.state = 'loaded'; | |
this.fire('tile.load', {tile: tile}); | |
} | |
}, | |
_abortTile: function(tile) { | |
tile.aborted = true; | |
if (tile.request) { | |
tile.request.abort(); | |
delete tile.request; | |
} | |
}, | |
_addTile: function(tile) { | |
this.fire('tile.add', {tile: tile}); | |
}, | |
_removeTile: function(tile) { | |
this.fire('tile.remove', {tile: tile}); | |
}, | |
_unloadTile: function(tile) { | |
if (tile.texture) this.map.painter.saveTexture(tile.texture); | |
} | |
}); | |
},{"../util/ajax":92,"../util/evented":100,"../util/mapbox":105,"../util/util":108,"./source":34}],34:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var ajax = require('../util/ajax'); | |
var browser = require('../util/browser'); | |
var TilePyramid = require('./tile_pyramid'); | |
var normalizeURL = require('../util/mapbox').normalizeSourceURL; | |
var TileCoord = require('./tile_coord'); | |
exports._loadTileJSON = function(options) { | |
var loaded = function(err, tileJSON) { | |
if (err) { | |
this.fire('error', {error: err}); | |
return; | |
} | |
util.extend(this, util.pick(tileJSON, | |
['tiles', 'minzoom', 'maxzoom', 'attribution'])); | |
if (tileJSON.vector_layers) { | |
this.vectorLayers = tileJSON.vector_layers; | |
this.vectorLayerIds = this.vectorLayers.map(function(layer) { return layer.id; }); | |
} | |
this._pyramid = new TilePyramid({ | |
tileSize: this.tileSize, | |
minzoom: this.minzoom, | |
maxzoom: this.maxzoom, | |
roundZoom: this.roundZoom, | |
reparseOverscaled: this.reparseOverscaled, | |
load: this._loadTile.bind(this), | |
abort: this._abortTile.bind(this), | |
unload: this._unloadTile.bind(this), | |
add: this._addTile.bind(this), | |
remove: this._removeTile.bind(this), | |
redoPlacement: this._redoTilePlacement ? this._redoTilePlacement.bind(this) : undefined | |
}); | |
this.fire('load'); | |
}.bind(this); | |
if (options.url) { | |
ajax.getJSON(normalizeURL(options.url), loaded); | |
} else { | |
browser.frame(loaded.bind(this, null, options)); | |
} | |
}; | |
exports.redoPlacement = function() { | |
if (!this._pyramid) { | |
return; | |
} | |
var ids = this._pyramid.getIds(); | |
for (var i = 0; i < ids.length; i++) { | |
var tile = this._pyramid.getTile(ids[i]); | |
this._redoTilePlacement(tile); | |
} | |
}; | |
exports._getTile = function(coord) { | |
return this._pyramid.getTile(coord.id); | |
}; | |
exports._getVisibleCoordinates = function() { | |
if (!this._pyramid) return []; | |
else return this._pyramid.getRenderableIds().map(TileCoord.fromID); | |
}; | |
function sortTilesIn(a, b) { | |
var coordA = a.coord; | |
var coordB = b.coord; | |
return (coordA.z - coordB.z) || (coordA.y - coordB.y) || (coordA.w - coordB.w) || (coordA.x - coordB.x); | |
} | |
function mergeRenderedFeatureLayers(tiles) { | |
var result = tiles[0] || {}; | |
for (var i = 1; i < tiles.length; i++) { | |
var tile = tiles[i]; | |
for (var layerID in tile) { | |
var tileFeatures = tile[layerID]; | |
var resultFeatures = result[layerID]; | |
if (resultFeatures === undefined) { | |
resultFeatures = result[layerID] = tileFeatures; | |
} else { | |
for (var f = 0; f < tileFeatures.length; f++) { | |
resultFeatures.push(tileFeatures[f]); | |
} | |
} | |
} | |
} | |
return result; | |
} | |
exports._queryRenderedVectorFeatures = function(queryGeometry, params, zoom, bearing) { | |
if (!this._pyramid || !this.map) | |
return []; | |
var tilesIn = this._pyramid.tilesIn(queryGeometry); | |
tilesIn.sort(sortTilesIn); | |
var styleLayers = this.map.style._layers; | |
var renderedFeatureLayers = []; | |
for (var r = 0; r < tilesIn.length; r++) { | |
var tileIn = tilesIn[r]; | |
if (!tileIn.tile.featureIndex) continue; | |
renderedFeatureLayers.push(tileIn.tile.featureIndex.query({ | |
queryGeometry: tileIn.queryGeometry, | |
scale: tileIn.scale, | |
tileSize: tileIn.tile.tileSize, | |
bearing: bearing, | |
params: params | |
}, styleLayers)); | |
} | |
return mergeRenderedFeatureLayers(renderedFeatureLayers); | |
}; | |
exports._querySourceFeatures = function(params) { | |
if (!this._pyramid) { | |
return []; | |
} | |
var pyramid = this._pyramid; | |
var tiles = pyramid.getRenderableIds().map(function(id) { | |
return pyramid.getTile(id); | |
}); | |
var result = []; | |
var dataTiles = {}; | |
for (var i = 0; i < tiles.length; i++) { | |
var tile = tiles[i]; | |
var dataID = new TileCoord(Math.min(tile.sourceMaxZoom, tile.coord.z), tile.coord.x, tile.coord.y, 0).id; | |
if (!dataTiles[dataID]) { | |
dataTiles[dataID] = true; | |
tile.querySourceFeatures(result, params); | |
} | |
} | |
return result; | |
}; | |
/* | |
* Create a tiled data source instance given an options object | |
* | |
* @param {Object} options | |
* @param {string} options.type Either `raster` or `vector`. | |
* @param {string} options.url A tile source URL. This should either be `mapbox://{mapid}` or a full `http[s]` url that points to a TileJSON endpoint. | |
* @param {Array} options.tiles An array of tile sources. If `url` is not specified, `tiles` can be used instead to specify tile sources, as in the TileJSON spec. Other TileJSON keys such as `minzoom` and `maxzoom` can be specified in a source object if `tiles` is used. | |
* @param {string} options.id An optional `id` to assign to the source | |
* @param {number} [options.tileSize=512] Optional tile size (width and height in pixels, assuming tiles are square). This option is only configurable for raster sources | |
* @example | |
* var sourceObj = new mapboxgl.Source.create({ | |
* type: 'vector', | |
* url: 'mapbox://mapbox.mapbox-streets-v6' | |
* }); | |
* map.addSource('some id', sourceObj); // add | |
* map.removeSource('some id'); // remove | |
*/ | |
exports.create = function(source) { | |
// This is not at file scope in order to avoid a circular require. | |
var sources = { | |
vector: require('./vector_tile_source'), | |
raster: require('./raster_tile_source'), | |
geojson: require('./geojson_source'), | |
video: require('./video_source'), | |
image: require('./image_source') | |
}; | |
return exports.is(source) ? source : new sources[source.type](source); | |
}; | |
exports.is = function(source) { | |
// This is not at file scope in order to avoid a circular require. | |
var sources = { | |
vector: require('./vector_tile_source'), | |
raster: require('./raster_tile_source'), | |
geojson: require('./geojson_source'), | |
video: require('./video_source'), | |
image: require('./image_source') | |
}; | |
for (var type in sources) { | |
if (source instanceof sources[type]) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
},{"../util/ajax":92,"../util/browser":93,"../util/mapbox":105,"../util/util":108,"./geojson_source":29,"./image_source":31,"./raster_tile_source":33,"./tile_coord":36,"./tile_pyramid":37,"./vector_tile_source":38,"./video_source":39}],35:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var Bucket = require('../data/bucket'); | |
var FeatureIndex = require('../data/feature_index'); | |
var vt = require('vector-tile'); | |
var Protobuf = require('pbf'); | |
var GeoJSONFeature = require('../util/vectortile_to_geojson'); | |
var featureFilter = require('feature-filter'); | |
var CollisionTile = require('../symbol/collision_tile'); | |
var CollisionBoxArray = require('../symbol/collision_box'); | |
var SymbolInstancesArray = require('../symbol/symbol_instances'); | |
var SymbolQuadsArray = require('../symbol/symbol_quads'); | |
module.exports = Tile; | |
/** | |
* A tile object is the combination of a Coordinate, which defines | |
* its place, as well as a unique ID and data tracking for its content | |
* | |
* @param {Coordinate} coord | |
* @param {number} size | |
* @private | |
*/ | |
function Tile(coord, size, sourceMaxZoom) { | |
this.coord = coord; | |
this.uid = util.uniqueId(); | |
this.uses = 0; | |
this.tileSize = size; | |
this.sourceMaxZoom = sourceMaxZoom; | |
this.buckets = {}; | |
// `this.state` must be one of | |
// | |
// - `loading`: Tile data is in the process of loading. | |
// - `loaded`: Tile data has been loaded. Tile can be rendered. | |
// - `reloading`: Tile data has been loaded and is being updated. Tile can be rendered. | |
// - `unloaded`: Tile data has been deleted. | |
// - `errored`: Tile data was not loaded because of an error. | |
this.state = 'loading'; | |
} | |
Tile.prototype = { | |
/** | |
* Given a data object with a 'buffers' property, load it into | |
* this tile's elementGroups and buffers properties and set loaded | |
* to true. If the data is null, like in the case of an empty | |
* GeoJSON tile, no-op but still set loaded to true. | |
* @param {Object} data | |
* @returns {undefined} | |
* @private | |
*/ | |
loadVectorData: function(data, style) { | |
this.state = 'loaded'; | |
// empty GeoJSON tile | |
if (!data) return; | |
this.collisionBoxArray = new CollisionBoxArray(data.collisionBoxArray); | |
this.collisionTile = new CollisionTile(data.collisionTile, this.collisionBoxArray); | |
this.symbolInstancesArray = new SymbolInstancesArray(data.symbolInstancesArray); | |
this.symbolQuadsArray = new SymbolQuadsArray(data.symbolQuadsArray); | |
this.featureIndex = new FeatureIndex(data.featureIndex, data.rawTileData, this.collisionTile); | |
this.rawTileData = data.rawTileData; | |
this.buckets = unserializeBuckets(data.buckets, style); | |
}, | |
/** | |
* given a data object and a GL painter, destroy and re-create | |
* all of its buffers. | |
* @param {Object} data | |
* @param {Object} painter | |
* @returns {undefined} | |
* @private | |
*/ | |
reloadSymbolData: function(data, painter, style) { | |
if (this.state === 'unloaded') return; | |
this.collisionTile = new CollisionTile(data.collisionTile, this.collisionBoxArray); | |
this.featureIndex.setCollisionTile(this.collisionTile); | |
// Destroy and delete existing symbol buckets | |
for (var id in this.buckets) { | |
var bucket = this.buckets[id]; | |
if (bucket.type === 'symbol') { | |
bucket.destroy(painter.gl); | |
delete this.buckets[id]; | |
} | |
} | |
// Add new symbol buckets | |
util.extend(this.buckets, unserializeBuckets(data.buckets, style)); | |
}, | |
/** | |
* Make sure that this tile doesn't own any data within a given | |
* painter, so that it doesn't consume any memory or maintain | |
* any references to the painter. | |
* @param {Object} painter gl painter object | |
* @returns {undefined} | |
* @private | |
*/ | |
unloadVectorData: function(painter) { | |
for (var id in this.buckets) { | |
var bucket = this.buckets[id]; | |
bucket.destroy(painter.gl); | |
} | |
this.collisionBoxArray = null; | |
this.symbolQuadsArray = null; | |
this.symbolInstancesArray = null; | |
this.collisionTile = null; | |
this.featureIndex = null; | |
this.rawTileData = null; | |
this.buckets = null; | |
this.state = 'unloaded'; | |
}, | |
redoPlacement: function(source) { | |
if (this.state !== 'loaded' || this.state === 'reloading') { | |
this.redoWhenDone = true; | |
return; | |
} | |
this.state = 'reloading'; | |
source.dispatcher.send('redo placement', { | |
uid: this.uid, | |
source: source.id, | |
angle: source.map.transform.angle, | |
pitch: source.map.transform.pitch, | |
showCollisionBoxes: source.map.showCollisionBoxes | |
}, done.bind(this), this.workerID); | |
function done(_, data) { | |
this.reloadSymbolData(data, source.map.painter, source.map.style); | |
source.fire('tile.load', {tile: this}); | |
this.state = 'loaded'; | |
if (this.redoWhenDone) { | |
this.redoPlacement(source); | |
this.redoWhenDone = false; | |
} | |
} | |
}, | |
getBucket: function(layer) { | |
return this.buckets && this.buckets[layer.ref || layer.id]; | |
}, | |
querySourceFeatures: function(result, params) { | |
if (!this.rawTileData) return; | |
if (!this.vtLayers) { | |
this.vtLayers = new vt.VectorTile(new Protobuf(new Uint8Array(this.rawTileData))).layers; | |
} | |
var layer = this.vtLayers._geojsonTileLayer || this.vtLayers[params.sourceLayer]; | |
if (!layer) return; | |
var filter = featureFilter(params.filter); | |
var coord = { z: this.coord.z, x: this.coord.x, y: this.coord.y }; | |
for (var i = 0; i < layer.length; i++) { | |
var feature = layer.feature(i); | |
if (filter(feature)) { | |
var geojsonFeature = new GeoJSONFeature(feature, this.coord.z, this.coord.x, this.coord.y); | |
geojsonFeature.tile = coord; | |
result.push(geojsonFeature); | |
} | |
} | |
}, | |
isRenderable: function() { | |
return this.state === 'loaded' || this.state === 'reloading'; | |
} | |
}; | |
function unserializeBuckets(input, style) { | |
// Guard against the case where the map's style has been set to null while | |
// this bucket has been parsing. | |
if (!style) return; | |
var output = {}; | |
for (var i = 0; i < input.length; i++) { | |
var layer = style.getLayer(input[i].layerId); | |
if (!layer) continue; | |
var bucket = Bucket.create(util.extend({ | |
layer: layer, | |
childLayers: input[i].childLayerIds | |
.map(style.getLayer.bind(style)) | |
.filter(function(layer) { return layer; }) | |
}, input[i])); | |
output[bucket.id] = bucket; | |
} | |
return output; | |
} | |
},{"../data/bucket":1,"../data/feature_index":7,"../symbol/collision_box":61,"../symbol/collision_tile":63,"../symbol/symbol_instances":72,"../symbol/symbol_quads":73,"../util/util":108,"../util/vectortile_to_geojson":109,"feature-filter":124,"pbf":175,"vector-tile":187}],36:[function(require,module,exports){ | |
'use strict'; | |
var assert = require('assert'); | |
var WhooTS = require('whoots-js'); | |
var Coordinate = require('../geo/coordinate'); | |
module.exports = TileCoord; | |
function TileCoord(z, x, y, w) { | |
assert(!isNaN(z) && z >= 0 && z % 1 === 0); | |
assert(!isNaN(x) && x >= 0 && x % 1 === 0); | |
assert(!isNaN(y) && y >= 0 && y % 1 === 0); | |
if (isNaN(w)) w = 0; | |
this.z = +z; | |
this.x = +x; | |
this.y = +y; | |
this.w = +w; | |
// calculate id | |
w *= 2; | |
if (w < 0) w = w * -1 - 1; | |
var dim = 1 << this.z; | |
this.id = ((dim * dim * w + dim * this.y + this.x) * 32) + this.z; | |
// for caching pos matrix calculation when rendering | |
this.posMatrix = null; | |
} | |
TileCoord.prototype.toString = function() { | |
return this.z + "/" + this.x + "/" + this.y; | |
}; | |
TileCoord.prototype.toCoordinate = function(sourceMaxZoom) { | |
var zoom = Math.min(this.z, sourceMaxZoom); | |
var tileScale = Math.pow(2, zoom); | |
var row = this.y; | |
var column = this.x + tileScale * this.w; | |
return new Coordinate(column, row, zoom); | |
}; | |
// Parse a packed integer id into a TileCoord object | |
TileCoord.fromID = function(id) { | |
var z = id % 32, dim = 1 << z; | |
var xy = ((id - z) / 32); | |
var x = xy % dim, y = ((xy - x) / dim) % dim; | |
var w = Math.floor(xy / (dim * dim)); | |
if (w % 2 !== 0) w = w * -1 - 1; | |
w /= 2; | |
return new TileCoord(z, x, y, w); | |
}; | |
function getQuadkey(z, x, y) { | |
var quadkey = '', mask; | |
for (var i = z; i > 0; i--) { | |
mask = 1 << (i - 1); | |
quadkey += ((x & mask ? 1 : 0) + (y & mask ? 2 : 0)); | |
} | |
return quadkey; | |
} | |
// given a list of urls, choose a url template and return a tile URL | |
TileCoord.prototype.url = function(urls, sourceMaxZoom, scheme) { | |
var bbox = WhooTS.getTileBBox(this.x, this.y, this.z); | |
var quadkey = getQuadkey(this.z, this.x, this.y); | |
return urls[(this.x + this.y) % urls.length] | |
.replace('{prefix}', (this.x % 16).toString(16) + (this.y % 16).toString(16)) | |
.replace('{z}', Math.min(this.z, sourceMaxZoom || this.z)) | |
.replace('{x}', this.x) | |
.replace('{y}', scheme === 'tms' ? (Math.pow(2, this.z) - this.y - 1) : this.y) | |
.replace('{quadkey}', quadkey) | |
.replace('{bbox-epsg-3857}', bbox); | |
}; | |
// Return the coordinate of the parent tile | |
TileCoord.prototype.parent = function(sourceMaxZoom) { | |
if (this.z === 0) return null; | |
// the id represents an overscaled tile, return the same coordinates with a lower z | |
if (this.z > sourceMaxZoom) { | |
return new TileCoord(this.z - 1, this.x, this.y, this.w); | |
} | |
return new TileCoord(this.z - 1, Math.floor(this.x / 2), Math.floor(this.y / 2), this.w); | |
}; | |
TileCoord.prototype.wrapped = function() { | |
return new TileCoord(this.z, this.x, this.y, 0); | |
}; | |
// Return the coordinates of the tile's children | |
TileCoord.prototype.children = function(sourceMaxZoom) { | |
if (this.z >= sourceMaxZoom) { | |
// return a single tile coord representing a an overscaled tile | |
return [new TileCoord(this.z + 1, this.x, this.y, this.w)]; | |
} | |
var z = this.z + 1; | |
var x = this.x * 2; | |
var y = this.y * 2; | |
return [ | |
new TileCoord(z, x, y, this.w), | |
new TileCoord(z, x + 1, y, this.w), | |
new TileCoord(z, x, y + 1, this.w), | |
new TileCoord(z, x + 1, y + 1, this.w) | |
]; | |
}; | |
// Taken from polymaps src/Layer.js | |
// https://github.com/simplegeo/polymaps/blob/master/src/Layer.js#L333-L383 | |
function edge(a, b) { | |
if (a.row > b.row) { var t = a; a = b; b = t; } | |
return { | |
x0: a.column, | |
y0: a.row, | |
x1: b.column, | |
y1: b.row, | |
dx: b.column - a.column, | |
dy: b.row - a.row | |
}; | |
} | |
function scanSpans(e0, e1, ymin, ymax, scanLine) { | |
var y0 = Math.max(ymin, Math.floor(e1.y0)); | |
var y1 = Math.min(ymax, Math.ceil(e1.y1)); | |
// sort edges by x-coordinate | |
if ((e0.x0 === e1.x0 && e0.y0 === e1.y0) ? | |
(e0.x0 + e1.dy / e0.dy * e0.dx < e1.x1) : | |
(e0.x1 - e1.dy / e0.dy * e0.dx < e1.x0)) { | |
var t = e0; e0 = e1; e1 = t; | |
} | |
// scan lines! | |
var m0 = e0.dx / e0.dy; | |
var m1 = e1.dx / e1.dy; | |
var d0 = e0.dx > 0; // use y + 1 to compute x0 | |
var d1 = e1.dx < 0; // use y + 1 to compute x1 | |
for (var y = y0; y < y1; y++) { | |
var x0 = m0 * Math.max(0, Math.min(e0.dy, y + d0 - e0.y0)) + e0.x0; | |
var x1 = m1 * Math.max(0, Math.min(e1.dy, y + d1 - e1.y0)) + e1.x0; | |
scanLine(Math.floor(x1), Math.ceil(x0), y); | |
} | |
} | |
function scanTriangle(a, b, c, ymin, ymax, scanLine) { | |
var ab = edge(a, b), | |
bc = edge(b, c), | |
ca = edge(c, a); | |
var t; | |
// sort edges by y-length | |
if (ab.dy > bc.dy) { t = ab; ab = bc; bc = t; } | |
if (ab.dy > ca.dy) { t = ab; ab = ca; ca = t; } | |
if (bc.dy > ca.dy) { t = bc; bc = ca; ca = t; } | |
// scan span! scan span! | |
if (ab.dy) scanSpans(ca, ab, ymin, ymax, scanLine); | |
if (bc.dy) scanSpans(ca, bc, ymin, ymax, scanLine); | |
} | |
TileCoord.cover = function(z, bounds, actualZ) { | |
var tiles = 1 << z; | |
var t = {}; | |
function scanLine(x0, x1, y) { | |
var x, wx, coord; | |
if (y >= 0 && y <= tiles) { | |
for (x = x0; x < x1; x++) { | |
wx = (x % tiles + tiles) % tiles; | |
coord = new TileCoord(actualZ, wx, y, Math.floor(x / tiles)); | |
t[coord.id] = coord; | |
} | |
} | |
} | |
// Divide the screen up in two triangles and scan each of them: | |
// +---/ | |
// | / | | |
// /---+ | |
scanTriangle(bounds[0], bounds[1], bounds[2], 0, tiles, scanLine); | |
scanTriangle(bounds[2], bounds[3], bounds[0], 0, tiles, scanLine); | |
return Object.keys(t).map(function(id) { | |
return t[id]; | |
}); | |
}; | |
},{"../geo/coordinate":9,"assert":110,"whoots-js":195}],37:[function(require,module,exports){ | |
'use strict'; | |
var Tile = require('./tile'); | |
var TileCoord = require('./tile_coord'); | |
var Point = require('point-geometry'); | |
var Cache = require('../util/lru_cache'); | |
var Coordinate = require('../geo/coordinate'); | |
var util = require('../util/util'); | |
var EXTENT = require('../data/bucket').EXTENT; | |
module.exports = TilePyramid; | |
/** | |
* A tile pyramid is a specialized cache and datastructure | |
* that contains tiles. It's used by sources to manage their | |
* data. | |
* | |
* @param {Object} options | |
* @param {number} options.tileSize | |
* @param {number} options.minzoom | |
* @param {number} options.maxzoom | |
* @private | |
*/ | |
function TilePyramid(options) { | |
this.tileSize = options.tileSize; | |
this.minzoom = options.minzoom; | |
this.maxzoom = options.maxzoom; | |
this.roundZoom = options.roundZoom; | |
this.reparseOverscaled = options.reparseOverscaled; | |
this._load = options.load; | |
this._abort = options.abort; | |
this._unload = options.unload; | |
this._add = options.add; | |
this._remove = options.remove; | |
this._redoPlacement = options.redoPlacement; | |
this._tiles = {}; | |
this._cache = new Cache(0, function(tile) { return this._unload(tile); }.bind(this)); | |
this._isIdRenderable = this._isIdRenderable.bind(this); | |
} | |
TilePyramid.maxOverzooming = 10; | |
TilePyramid.maxUnderzooming = 3; | |
TilePyramid.prototype = { | |
/** | |
* Return true if no tile data is pending, tiles will not change unless | |
* an additional API call is received. | |
* @returns {boolean} | |
* @private | |
*/ | |
loaded: function() { | |
for (var t in this._tiles) { | |
var tile = this._tiles[t]; | |
if (tile.state !== 'loaded' && tile.state !== 'errored') | |
return false; | |
} | |
return true; | |
}, | |
/** | |
* Return all tile ids ordered with z-order, and cast to numbers | |
* @returns {Array<number>} ids | |
* @private | |
*/ | |
getIds: function() { | |
return Object.keys(this._tiles).map(Number).sort(compareKeyZoom); | |
}, | |
getRenderableIds: function() { | |
return this.getIds().filter(this._isIdRenderable); | |
}, | |
_isIdRenderable: function(id) { | |
return this._tiles[id].isRenderable() && !this._coveredTiles[id]; | |
}, | |
reload: function() { | |
this._cache.reset(); | |
for (var i in this._tiles) { | |
var tile = this._tiles[i]; | |
// The difference between "loading" tiles and "reloading" tiles is | |
// that "reloading" tiles are "renderable". Therefore, a "loading" | |
// tile cannot become a "reloading" tile without first becoming | |
// a "loaded" tile. | |
if (tile.state !== 'loading') { | |
tile.state = 'reloading'; | |
} | |
this._load(tile); | |
} | |
}, | |
/** | |
* Get a specific tile by id | |
* @param {string|number} id tile id | |
* @returns {Object} tile | |
* @private | |
*/ | |
getTile: function(id) { | |
return this._tiles[id]; | |
}, | |
/** | |
* get the zoom level adjusted for the difference in map and source tilesizes | |
* @param {Object} transform | |
* @returns {number} zoom level | |
* @private | |
*/ | |
getZoom: function(transform) { | |
return transform.zoom + Math.log(transform.tileSize / this.tileSize) / Math.LN2; | |
}, | |
/** | |
* Return a zoom level that will cover all tiles in a given transform | |
* @param {Object} transform | |
* @returns {number} zoom level | |
* @private | |
*/ | |
coveringZoomLevel: function(transform) { | |
return (this.roundZoom ? Math.round : Math.floor)(this.getZoom(transform)); | |
}, | |
/** | |
* Given a transform, return all coordinates that could cover that | |
* transform for a covering zoom level. | |
* @param {Object} transform | |
* @returns {Array<Tile>} tiles | |
* @private | |
*/ | |
coveringTiles: function(transform) { | |
var z = this.coveringZoomLevel(transform); | |
var actualZ = z; | |
if (z < this.minzoom) return []; | |
if (z > this.maxzoom) z = this.maxzoom; | |
var tr = transform, | |
tileCenter = tr.locationCoordinate(tr.center)._zoomTo(z), | |
centerPoint = new Point(tileCenter.column - 0.5, tileCenter.row - 0.5); | |
return TileCoord.cover(z, [ | |
tr.pointCoordinate(new Point(0, 0))._zoomTo(z), | |
tr.pointCoordinate(new Point(tr.width, 0))._zoomTo(z), | |
tr.pointCoordinate(new Point(tr.width, tr.height))._zoomTo(z), | |
tr.pointCoordinate(new Point(0, tr.height))._zoomTo(z) | |
], this.reparseOverscaled ? actualZ : z).sort(function(a, b) { | |
return centerPoint.dist(a) - centerPoint.dist(b); | |
}); | |
}, | |
/** | |
* Recursively find children of the given tile (up to maxCoveringZoom) that are already loaded; | |
* adds found tiles to retain object; returns true if any child is found. | |
* | |
* @param {Coordinate} coord | |
* @param {number} maxCoveringZoom | |
* @param {boolean} retain | |
* @returns {boolean} whether the operation was complete | |
* @private | |
*/ | |
findLoadedChildren: function(coord, maxCoveringZoom, retain) { | |
var found = false; | |
for (var id in this._tiles) { | |
var tile = this._tiles[id]; | |
// only consider renderable tiles on higher zoom levels (up to maxCoveringZoom) | |
if (retain[id] || !tile.isRenderable() || tile.coord.z <= coord.z || tile.coord.z > maxCoveringZoom) continue; | |
// disregard tiles that are not descendants of the given tile coordinate | |
var z2 = Math.pow(2, Math.min(tile.coord.z, this.maxzoom) - Math.min(coord.z, this.maxzoom)); | |
if (Math.floor(tile.coord.x / z2) !== coord.x || | |
Math.floor(tile.coord.y / z2) !== coord.y) | |
continue; | |
// found loaded child | |
retain[id] = true; | |
found = true; | |
// loop through parents; retain the topmost loaded one if found | |
while (tile && tile.coord.z - 1 > coord.z) { | |
var parentId = tile.coord.parent(this.maxzoom).id; | |
tile = this._tiles[parentId]; | |
if (tile && tile.isRenderable()) { | |
delete retain[id]; | |
retain[parentId] = true; | |
} | |
} | |
} | |
return found; | |
}, | |
/** | |
* Find a loaded parent of the given tile (up to minCoveringZoom); | |
* adds the found tile to retain object and returns the tile if found | |
* | |
* @param {Coordinate} coord | |
* @param {number} minCoveringZoom | |
* @param {boolean} retain | |
* @returns {Tile} tile object | |
* @private | |
*/ | |
findLoadedParent: function(coord, minCoveringZoom, retain) { | |
for (var z = coord.z - 1; z >= minCoveringZoom; z--) { | |
coord = coord.parent(this.maxzoom); | |
var tile = this._tiles[coord.id]; | |
if (tile && tile.isRenderable()) { | |
retain[coord.id] = true; | |
return tile; | |
} | |
if (this._cache.has(coord.id)) { | |
this.addTile(coord); | |
retain[coord.id] = true; | |
return this._tiles[coord.id]; | |
} | |
} | |
}, | |
/** | |
* Resizes the tile cache based on the current viewport's size. | |
* | |
* Larger viewports use more tiles and need larger caches. Larger viewports | |
* are more likely to be found on devices with more memory and on pages where | |
* the map is more important. | |
* | |
* @private | |
*/ | |
updateCacheSize: function(transform) { | |
var widthInTiles = Math.ceil(transform.width / transform.tileSize) + 1; | |
var heightInTiles = Math.ceil(transform.height / transform.tileSize) + 1; | |
var approxTilesInView = widthInTiles * heightInTiles; | |
var commonZoomRange = 5; | |
this._cache.setMaxSize(Math.floor(approxTilesInView * commonZoomRange)); | |
}, | |
/** | |
* Removes tiles that are outside the viewport and adds new tiles that | |
* are inside the viewport. | |
* @private | |
*/ | |
update: function(used, transform, fadeDuration) { | |
var i; | |
var coord; | |
var tile; | |
this.updateCacheSize(transform); | |
// Determine the overzooming/underzooming amounts. | |
var zoom = (this.roundZoom ? Math.round : Math.floor)(this.getZoom(transform)); | |
var minCoveringZoom = Math.max(zoom - TilePyramid.maxOverzooming, this.minzoom); | |
var maxCoveringZoom = Math.max(zoom + TilePyramid.maxUnderzooming, this.minzoom); | |
// Retain is a list of tiles that we shouldn't delete, even if they are not | |
// the most ideal tile for the current viewport. This may include tiles like | |
// parent or child tiles that are *already* loaded. | |
var retain = {}; | |
var now = new Date().getTime(); | |
// Covered is a list of retained tiles who's areas are full covered by other, | |
// better, retained tiles. They are not drawn separately. | |
this._coveredTiles = {}; | |
var required = used ? this.coveringTiles(transform) : []; | |
for (i = 0; i < required.length; i++) { | |
coord = required[i]; | |
tile = this.addTile(coord); | |
retain[coord.id] = true; | |
if (tile.isRenderable()) | |
continue; | |
// The tile we require is not yet loaded. | |
// Retain child or parent tiles that cover the same area. | |
if (!this.findLoadedChildren(coord, maxCoveringZoom, retain)) { | |
this.findLoadedParent(coord, minCoveringZoom, retain); | |
} | |
} | |
var parentsForFading = {}; | |
var ids = Object.keys(retain); | |
for (var k = 0; k < ids.length; k++) { | |
var id = ids[k]; | |
coord = TileCoord.fromID(id); | |
tile = this._tiles[id]; | |
if (tile && tile.timeAdded > now - (fadeDuration || 0)) { | |
// This tile is still fading in. Find tiles to cross-fade with it. | |
if (this.findLoadedChildren(coord, maxCoveringZoom, retain)) { | |
retain[id] = true; | |
} | |
this.findLoadedParent(coord, minCoveringZoom, parentsForFading); | |
} | |
} | |
var fadedParent; | |
for (fadedParent in parentsForFading) { | |
if (!retain[fadedParent]) { | |
// If a tile is only needed for fading, mark it as covered so that it isn't rendered on it's own. | |
this._coveredTiles[fadedParent] = true; | |
} | |
} | |
for (fadedParent in parentsForFading) { | |
retain[fadedParent] = true; | |
} | |
// Remove the tiles we don't need anymore. | |
var remove = util.keysDifference(this._tiles, retain); | |
for (i = 0; i < remove.length; i++) { | |
this.removeTile(+remove[i]); | |
} | |
this.transform = transform; | |
}, | |
/** | |
* Add a tile, given its coordinate, to the pyramid. | |
* @param {Coordinate} coord | |
* @returns {Coordinate} the coordinate. | |
* @private | |
*/ | |
addTile: function(coord) { | |
var tile = this._tiles[coord.id]; | |
if (tile) | |
return tile; | |
var wrapped = coord.wrapped(); | |
tile = this._tiles[wrapped.id]; | |
if (!tile) { | |
tile = this._cache.get(wrapped.id); | |
if (tile && this._redoPlacement) { | |
this._redoPlacement(tile); | |
} | |
} | |
if (!tile) { | |
var zoom = coord.z; | |
var overscaling = zoom > this.maxzoom ? Math.pow(2, zoom - this.maxzoom) : 1; | |
tile = new Tile(wrapped, this.tileSize * overscaling, this.maxzoom); | |
this._load(tile); | |
} | |
tile.uses++; | |
this._tiles[coord.id] = tile; | |
this._add(tile, coord); | |
return tile; | |
}, | |
/** | |
* Remove a tile, given its id, from the pyramid | |
* @param {string|number} id tile id | |
* @returns {undefined} nothing | |
* @private | |
*/ | |
removeTile: function(id) { | |
var tile = this._tiles[id]; | |
if (!tile) | |
return; | |
tile.uses--; | |
delete this._tiles[id]; | |
this._remove(tile); | |
if (tile.uses > 0) | |
return; | |
if (tile.isRenderable()) { | |
this._cache.add(tile.coord.wrapped().id, tile); | |
} else { | |
this._abort(tile); | |
this._unload(tile); | |
} | |
}, | |
/** | |
* Remove all tiles from this pyramid | |
* @private | |
*/ | |
clearTiles: function() { | |
for (var id in this._tiles) | |
this.removeTile(id); | |
this._cache.reset(); | |
}, | |
/** | |
* Search through our current tiles and attempt to find the tiles that | |
* cover the given bounds. | |
* @param {Array<Coordinate>} queryGeometry coordinates of the corners of bounding rectangle | |
* @returns {Array<Object>} result items have {tile, minX, maxX, minY, maxY}, where min/max bounding values are the given bounds transformed in into the coordinate space of this tile. | |
* @private | |
*/ | |
tilesIn: function(queryGeometry) { | |
var tileResults = {}; | |
var ids = this.getIds(); | |
var minX = Infinity; | |
var minY = Infinity; | |
var maxX = -Infinity; | |
var maxY = -Infinity; | |
var z = queryGeometry[0].zoom; | |
for (var k = 0; k < queryGeometry.length; k++) { | |
var p = queryGeometry[k]; | |
minX = Math.min(minX, p.column); | |
minY = Math.min(minY, p.row); | |
maxX = Math.max(maxX, p.column); | |
maxY = Math.max(maxY, p.row); | |
} | |
for (var i = 0; i < ids.length; i++) { | |
var tile = this._tiles[ids[i]]; | |
var coord = TileCoord.fromID(ids[i]); | |
var tileSpaceBounds = [ | |
coordinateToTilePoint(coord, tile.sourceMaxZoom, new Coordinate(minX, minY, z)), | |
coordinateToTilePoint(coord, tile.sourceMaxZoom, new Coordinate(maxX, maxY, z)) | |
]; | |
if (tileSpaceBounds[0].x < EXTENT && tileSpaceBounds[0].y < EXTENT && | |
tileSpaceBounds[1].x >= 0 && tileSpaceBounds[1].y >= 0) { | |
var tileSpaceQueryGeometry = []; | |
for (var j = 0; j < queryGeometry.length; j++) { | |
tileSpaceQueryGeometry.push(coordinateToTilePoint(coord, tile.sourceMaxZoom, queryGeometry[j])); | |
} | |
var tileResult = tileResults[tile.coord.id]; | |
if (tileResult === undefined) { | |
tileResult = tileResults[tile.coord.id] = { | |
tile: tile, | |
coord: coord, | |
queryGeometry: [], | |
scale: Math.pow(2, this.transform.zoom - tile.coord.z) | |
}; | |
} | |
// Wrapped tiles share one tileResult object but can have multiple queryGeometry parts | |
tileResult.queryGeometry.push(tileSpaceQueryGeometry); | |
} | |
} | |
var results = []; | |
for (var t in tileResults) { | |
results.push(tileResults[t]); | |
} | |
return results; | |
} | |
}; | |
/** | |
* Convert a coordinate to a point in a tile's coordinate space. | |
* @param {Coordinate} tileCoord | |
* @param {Coordinate} coord | |
* @returns {Object} position | |
* @private | |
*/ | |
function coordinateToTilePoint(tileCoord, sourceMaxZoom, coord) { | |
var zoomedCoord = coord.zoomTo(Math.min(tileCoord.z, sourceMaxZoom)); | |
return { | |
x: (zoomedCoord.column - (tileCoord.x + tileCoord.w * Math.pow(2, tileCoord.z))) * EXTENT, | |
y: (zoomedCoord.row - tileCoord.y) * EXTENT | |
}; | |
} | |
function compareKeyZoom(a, b) { | |
return (a % 32) - (b % 32); | |
} | |
},{"../data/bucket":1,"../geo/coordinate":9,"../util/lru_cache":104,"../util/util":108,"./tile":35,"./tile_coord":36,"point-geometry":177}],38:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var Evented = require('../util/evented'); | |
var Source = require('./source'); | |
var normalizeURL = require('../util/mapbox').normalizeTileURL; | |
module.exports = VectorTileSource; | |
function VectorTileSource(options) { | |
util.extend(this, util.pick(options, ['url', 'scheme', 'tileSize'])); | |
this._options = util.extend({ type: 'vector' }, options); | |
if (this.tileSize !== 512) { | |
throw new Error('vector tile sources must have a tileSize of 512'); | |
} | |
Source._loadTileJSON.call(this, options); | |
} | |
VectorTileSource.prototype = util.inherit(Evented, { | |
minzoom: 0, | |
maxzoom: 22, | |
scheme: 'xyz', | |
tileSize: 512, | |
reparseOverscaled: true, | |
_loaded: false, | |
isTileClipped: true, | |
onAdd: function(map) { | |
this.map = map; | |
}, | |
loaded: function() { | |
return this._pyramid && this._pyramid.loaded(); | |
}, | |
update: function(transform) { | |
if (this._pyramid) { | |
this._pyramid.update(this.used, transform); | |
} | |
}, | |
reload: function() { | |
if (this._pyramid) { | |
this._pyramid.reload(); | |
} | |
}, | |
serialize: function() { | |
return util.extend({}, this._options); | |
}, | |
getVisibleCoordinates: Source._getVisibleCoordinates, | |
getTile: Source._getTile, | |
queryRenderedFeatures: Source._queryRenderedVectorFeatures, | |
querySourceFeatures: Source._querySourceFeatures, | |
_loadTile: function(tile) { | |
var overscaling = tile.coord.z > this.maxzoom ? Math.pow(2, tile.coord.z - this.maxzoom) : 1; | |
var params = { | |
url: normalizeURL(tile.coord.url(this.tiles, this.maxzoom, this.scheme), this.url), | |
uid: tile.uid, | |
coord: tile.coord, | |
zoom: tile.coord.z, | |
tileSize: this.tileSize * overscaling, | |
source: this.id, | |
overscaling: overscaling, | |
angle: this.map.transform.angle, | |
pitch: this.map.transform.pitch, | |
showCollisionBoxes: this.map.showCollisionBoxes | |
}; | |
if (tile.workerID) { | |
params.rawTileData = tile.rawTileData; | |
this.dispatcher.send('reload tile', params, this._tileLoaded.bind(this, tile), tile.workerID); | |
} else { | |
tile.workerID = this.dispatcher.send('load tile', params, this._tileLoaded.bind(this, tile)); | |
} | |
}, | |
_tileLoaded: function(tile, err, data) { | |
if (tile.aborted) | |
return; | |
if (err) { | |
tile.state = 'errored'; | |
this.fire('tile.error', {tile: tile, error: err}); | |
return; | |
} | |
tile.loadVectorData(data, this.map.style); | |
if (tile.redoWhenDone) { | |
tile.redoWhenDone = false; | |
tile.redoPlacement(this); | |
} | |
this.fire('tile.load', {tile: tile}); | |
this.fire('tile.stats', data.bucketStats); | |
}, | |
_abortTile: function(tile) { | |
tile.aborted = true; | |
this.dispatcher.send('abort tile', { uid: tile.uid, source: this.id }, null, tile.workerID); | |
}, | |
_addTile: function(tile) { | |
this.fire('tile.add', {tile: tile}); | |
}, | |
_removeTile: function(tile) { | |
this.fire('tile.remove', {tile: tile}); | |
}, | |
_unloadTile: function(tile) { | |
tile.unloadVectorData(this.map.painter); | |
this.dispatcher.send('remove tile', { uid: tile.uid, source: this.id }, null, tile.workerID); | |
}, | |
redoPlacement: Source.redoPlacement, | |
_redoTilePlacement: function(tile) { | |
tile.redoPlacement(this); | |
} | |
}); | |
},{"../util/evented":100,"../util/mapbox":105,"../util/util":108,"./source":34}],39:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var Tile = require('./tile'); | |
var TileCoord = require('./tile_coord'); | |
var LngLat = require('../geo/lng_lat'); | |
var Point = require('point-geometry'); | |
var Evented = require('../util/evented'); | |
var ajax = require('../util/ajax'); | |
var EXTENT = require('../data/bucket').EXTENT; | |
var RasterBoundsArray = require('../render/draw_raster').RasterBoundsArray; | |
var Buffer = require('../data/buffer'); | |
var VertexArrayObject = require('../render/vertex_array_object'); | |
module.exports = VideoSource; | |
/** | |
* A data source containing video. | |
* | |
* @class VideoSource | |
* @param {Object} options | |
* @param {Array<string>} options.urls An array of URLs to video files. | |
* @param {Array<Array<number>>} options.coordinates Four geographical coordinates, | |
* represented as arrays of longitude and latitude numbers, which define the corners of the video. | |
* The coordinates start at the top left corner of the video and proceed in clockwise order. | |
* They do not have to represent a rectangle. | |
* @example | |
* var sourceObj = new mapboxgl.VideoSource({ | |
* url: [ | |
* 'https://www.mapbox.com/videos/baltimore-smoke.mp4', | |
* 'https://www.mapbox.com/videos/baltimore-smoke.webm' | |
* ], | |
* coordinates: [ | |
* [-76.54335737228394, 39.18579907229748], | |
* [-76.52803659439087, 39.1838364847587], | |
* [-76.5295386314392, 39.17683392507606], | |
* [-76.54520273208618, 39.17876344106642] | |
* ] | |
* }); | |
* map.addSource('some id', sourceObj); // add | |
* map.removeSource('some id'); // remove | |
*/ | |
function VideoSource(options) { | |
this.urls = options.urls; | |
this.coordinates = options.coordinates; | |
ajax.getVideo(options.urls, function(err, video) { | |
// @TODO handle errors via event. | |
if (err) return; | |
this.video = video; | |
this.video.loop = true; | |
var loopID; | |
// start repainting when video starts playing | |
this.video.addEventListener('playing', function() { | |
loopID = this.map.style.animationLoop.set(Infinity); | |
this.map._rerender(); | |
}.bind(this)); | |
// stop repainting when video stops | |
this.video.addEventListener('pause', function() { | |
this.map.style.animationLoop.cancel(loopID); | |
}.bind(this)); | |
this._loaded = true; | |
if (this.map) { | |
this.video.play(); | |
this.setCoordinates(options.coordinates); | |
} | |
}.bind(this)); | |
} | |
VideoSource.prototype = util.inherit(Evented, /** @lends VideoSource.prototype */{ | |
roundZoom: true, | |
/** | |
* Returns the HTML `video` element. | |
* | |
* @returns {HTMLVideoElement} The HTML `video` element. | |
*/ | |
getVideo: function() { | |
return this.video; | |
}, | |
onAdd: function(map) { | |
this.map = map; | |
if (this.video) { | |
this.video.play(); | |
this.setCoordinates(this.coordinates); | |
} | |
}, | |
/** | |
* Sets the video's coordinates and re-renders the map. | |
* | |
* @param {Array<Array<number>>} coordinates Four geographical coordinates, | |
* represented as arrays of longitude and latitude numbers, which define the corners of the video. | |
* The coordinates start at the top left corner of the video and proceed in clockwise order. | |
* They do not have to represent a rectangle. | |
* @returns {VideoSource} this | |
*/ | |
setCoordinates: function(coordinates) { | |
this.coordinates = coordinates; | |
// Calculate which mercator tile is suitable for rendering the video in | |
// and create a buffer with the corner coordinates. These coordinates | |
// may be outside the tile, because raster tiles aren't clipped when rendering. | |
var map = this.map; | |
var cornerZ0Coords = coordinates.map(function(coord) { | |
return map.transform.locationCoordinate(LngLat.convert(coord)).zoomTo(0); | |
}); | |
var centerCoord = this.centerCoord = util.getCoordinatesCenter(cornerZ0Coords); | |
centerCoord.column = Math.round(centerCoord.column); | |
centerCoord.row = Math.round(centerCoord.row); | |
var tileCoords = cornerZ0Coords.map(function(coord) { | |
var zoomedCoord = coord.zoomTo(centerCoord.zoom); | |
return new Point( | |
Math.round((zoomedCoord.column - centerCoord.column) * EXTENT), | |
Math.round((zoomedCoord.row - centerCoord.row) * EXTENT)); | |
}); | |
var maxInt16 = 32767; | |
var array = new RasterBoundsArray(); | |
array.emplaceBack(tileCoords[0].x, tileCoords[0].y, 0, 0); | |
array.emplaceBack(tileCoords[1].x, tileCoords[1].y, maxInt16, 0); | |
array.emplaceBack(tileCoords[3].x, tileCoords[3].y, 0, maxInt16); | |
array.emplaceBack(tileCoords[2].x, tileCoords[2].y, maxInt16, maxInt16); | |
this.tile = new Tile(new TileCoord(centerCoord.zoom, centerCoord.column, centerCoord.row)); | |
this.tile.buckets = {}; | |
this.tile.boundsBuffer = new Buffer(array.serialize(), RasterBoundsArray.serialize(), Buffer.BufferType.VERTEX); | |
this.tile.boundsVAO = new VertexArrayObject(); | |
this.fire('change'); | |
return this; | |
}, | |
loaded: function() { | |
return this.video && this.video.readyState >= 2; | |
}, | |
update: function() { | |
// noop | |
}, | |
reload: function() { | |
// noop | |
}, | |
prepare: function() { | |
if (!this._loaded) return; | |
if (this.video.readyState < 2) return; // not enough data for current position | |
var gl = this.map.painter.gl; | |
if (!this.tile.texture) { | |
this.tile.texture = gl.createTexture(); | |
gl.bindTexture(gl.TEXTURE_2D, this.tile.texture); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.video); | |
} else { | |
gl.bindTexture(gl.TEXTURE_2D, this.tile.texture); | |
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, this.video); | |
} | |
this._currentTime = this.video.currentTime; | |
}, | |
getVisibleCoordinates: function() { | |
if (this.tile) return [this.tile.coord]; | |
else return []; | |
}, | |
getTile: function() { | |
return this.tile; | |
}, | |
serialize: function() { | |
return { | |
type: 'video', | |
urls: this.urls, | |
coordinates: this.coordinates | |
}; | |
} | |
}); | |
},{"../data/bucket":1,"../data/buffer":6,"../geo/lng_lat":10,"../render/draw_raster":22,"../render/vertex_array_object":28,"../util/ajax":92,"../util/evented":100,"../util/util":108,"./tile":35,"./tile_coord":36,"point-geometry":177}],40:[function(require,module,exports){ | |
'use strict'; | |
var Actor = require('../util/actor'); | |
var WorkerTile = require('./worker_tile'); | |
var StyleLayer = require('../style/style_layer'); | |
var util = require('../util/util'); | |
var ajax = require('../util/ajax'); | |
var vt = require('vector-tile'); | |
var Protobuf = require('pbf'); | |
var supercluster = require('supercluster'); | |
var geojsonvt = require('geojson-vt'); | |
var rewind = require('geojson-rewind'); | |
var GeoJSONWrapper = require('./geojson_wrapper'); | |
var vtpbf = require('vt-pbf'); | |
module.exports = function(self) { | |
return new Worker(self); | |
}; | |
function Worker(self) { | |
this.self = self; | |
this.actor = new Actor(self, this); | |
this.loading = {}; | |
this.loaded = {}; | |
this.geoJSONIndexes = {}; | |
} | |
util.extend(Worker.prototype, { | |
'set layers': function(layers) { | |
this.layers = {}; | |
var that = this; | |
// Filter layers and create an id -> layer map | |
var childLayerIndicies = []; | |
for (var i = 0; i < layers.length; i++) { | |
var layer = layers[i]; | |
if (layer.type === 'fill' || layer.type === 'line' || layer.type === 'circle' || layer.type === 'symbol') { | |
if (layer.ref) { | |
childLayerIndicies.push(i); | |
} else { | |
setLayer(layer); | |
} | |
} | |
} | |
// Create an instance of StyleLayer per layer | |
for (var j = 0; j < childLayerIndicies.length; j++) { | |
setLayer(layers[childLayerIndicies[j]]); | |
} | |
function setLayer(serializedLayer) { | |
var styleLayer = StyleLayer.create( | |
serializedLayer, | |
serializedLayer.ref && that.layers[serializedLayer.ref] | |
); | |
styleLayer.updatePaintTransitions({}, {transition: false}); | |
that.layers[styleLayer.id] = styleLayer; | |
} | |
this.layerFamilies = createLayerFamilies(this.layers); | |
}, | |
'update layers': function(layers) { | |
var that = this; | |
var id; | |
var layer; | |
// Update ref parents | |
for (id in layers) { | |
layer = layers[id]; | |
if (layer.ref) updateLayer(layer); | |
} | |
// Update ref children | |
for (id in layers) { | |
layer = layers[id]; | |
if (!layer.ref) updateLayer(layer); | |
} | |
function updateLayer(layer) { | |
var refLayer = that.layers[layer.ref]; | |
if (that.layers[layer.id]) { | |
that.layers[layer.id].set(layer, refLayer); | |
} else { | |
that.layers[layer.id] = StyleLayer.create(layer, refLayer); | |
} | |
that.layers[layer.id].updatePaintTransitions({}, {transition: false}); | |
} | |
this.layerFamilies = createLayerFamilies(this.layers); | |
}, | |
'load tile': function(params, callback) { | |
var source = params.source, | |
uid = params.uid; | |
if (!this.loading[source]) | |
this.loading[source] = {}; | |
var tile = this.loading[source][uid] = new WorkerTile(params); | |
tile.xhr = ajax.getArrayBuffer(params.url, done.bind(this)); | |
function done(err, data) { | |
delete this.loading[source][uid]; | |
if (err) return callback(err); | |
tile.data = new vt.VectorTile(new Protobuf(new Uint8Array(data))); | |
tile.parse(tile.data, this.layerFamilies, this.actor, data, callback); | |
this.loaded[source] = this.loaded[source] || {}; | |
this.loaded[source][uid] = tile; | |
} | |
}, | |
'reload tile': function(params, callback) { | |
var loaded = this.loaded[params.source], | |
uid = params.uid; | |
if (loaded && loaded[uid]) { | |
var tile = loaded[uid]; | |
tile.parse(tile.data, this.layerFamilies, this.actor, params.rawTileData, callback); | |
} | |
}, | |
'abort tile': function(params) { | |
var loading = this.loading[params.source], | |
uid = params.uid; | |
if (loading && loading[uid]) { | |
loading[uid].xhr.abort(); | |
delete loading[uid]; | |
} | |
}, | |
'remove tile': function(params) { | |
var loaded = this.loaded[params.source], | |
uid = params.uid; | |
if (loaded && loaded[uid]) { | |
delete loaded[uid]; | |
} | |
}, | |
'redo placement': function(params, callback) { | |
var loaded = this.loaded[params.source], | |
loading = this.loading[params.source], | |
uid = params.uid; | |
if (loaded && loaded[uid]) { | |
var tile = loaded[uid]; | |
var result = tile.redoPlacement(params.angle, params.pitch, params.showCollisionBoxes); | |
if (result.result) { | |
callback(null, result.result, result.transferables); | |
} | |
} else if (loading && loading[uid]) { | |
loading[uid].angle = params.angle; | |
} | |
}, | |
'parse geojson': function(params, callback) { | |
var indexData = function(err, data) { | |
rewind(data, true); | |
if (err) return callback(err); | |
if (typeof data != 'object') { | |
return callback(new Error("Input data is not a valid GeoJSON object.")); | |
} | |
try { | |
this.geoJSONIndexes[params.source] = params.cluster ? | |
supercluster(params.superclusterOptions).load(data.features) : | |
geojsonvt(data, params.geojsonVtOptions); | |
} catch (err) { | |
return callback(err); | |
} | |
callback(null); | |
}.bind(this); | |
// Not, because of same origin issues, urls must either include an | |
// explicit origin or absolute path. | |
// ie: /foo/bar.json or http://example.com/bar.json | |
// but not ../foo/bar.json | |
if (params.url) { | |
ajax.getJSON(params.url, indexData); | |
} else if (typeof params.data === 'string') { | |
indexData(null, JSON.parse(params.data)); | |
} else { | |
return callback(new Error("Input data is not a valid GeoJSON object.")); | |
} | |
}, | |
'load geojson tile': function(params, callback) { | |
var source = params.source, | |
coord = params.coord; | |
if (!this.geoJSONIndexes[source]) return callback(null, null); // we couldn't load the file | |
var geoJSONTile = this.geoJSONIndexes[source].getTile(Math.min(coord.z, params.maxZoom), coord.x, coord.y); | |
var tile = geoJSONTile ? new WorkerTile(params) : undefined; | |
this.loaded[source] = this.loaded[source] || {}; | |
this.loaded[source][params.uid] = tile; | |
if (geoJSONTile) { | |
var geojsonWrapper = new GeoJSONWrapper(geoJSONTile.features); | |
geojsonWrapper.name = '_geojsonTileLayer'; | |
var pbf = vtpbf({ layers: { '_geojsonTileLayer': geojsonWrapper }}); | |
if (pbf.byteOffset !== 0 || pbf.byteLength !== pbf.buffer.byteLength) { | |
// Compatibility with node Buffer (https://github.com/mapbox/pbf/issues/35) | |
pbf = new Uint8Array(pbf); | |
} | |
tile.parse(geojsonWrapper, this.layerFamilies, this.actor, pbf.buffer, callback); | |
} else { | |
return callback(null, null); // nothing in the given tile | |
} | |
} | |
}); | |
function createLayerFamilies(layers) { | |
var families = {}; | |
for (var layerId in layers) { | |
var layer = layers[layerId]; | |
var parentLayerId = layer.ref || layer.id; | |
var parentLayer = layers[parentLayerId]; | |
if (parentLayer.layout && parentLayer.layout.visibility === 'none') continue; | |
families[parentLayerId] = families[parentLayerId] || []; | |
if (layerId === parentLayerId) { | |
families[parentLayerId].unshift(layer); | |
} else { | |
families[parentLayerId].push(layer); | |
} | |
} | |
return families; | |
} | |
},{"../style/style_layer":48,"../util/actor":91,"../util/ajax":92,"../util/util":108,"./geojson_wrapper":30,"./worker_tile":41,"geojson-rewind":125,"geojson-vt":130,"pbf":175,"supercluster":181,"vector-tile":187,"vt-pbf":191}],41:[function(require,module,exports){ | |
'use strict'; | |
var FeatureIndex = require('../data/feature_index'); | |
var CollisionTile = require('../symbol/collision_tile'); | |
var Bucket = require('../data/bucket'); | |
var CollisionBoxArray = require('../symbol/collision_box'); | |
var DictionaryCoder = require('../util/dictionary_coder'); | |
var util = require('../util/util'); | |
var SymbolInstancesArray = require('../symbol/symbol_instances'); | |
var SymbolQuadsArray = require('../symbol/symbol_quads'); | |
module.exports = WorkerTile; | |
function WorkerTile(params) { | |
this.coord = params.coord; | |
this.uid = params.uid; | |
this.zoom = params.zoom; | |
this.tileSize = params.tileSize; | |
this.source = params.source; | |
this.overscaling = params.overscaling; | |
this.angle = params.angle; | |
this.pitch = params.pitch; | |
this.showCollisionBoxes = params.showCollisionBoxes; | |
} | |
WorkerTile.prototype.parse = function(data, layerFamilies, actor, rawTileData, callback) { | |
this.status = 'parsing'; | |
this.data = data; | |
this.collisionBoxArray = new CollisionBoxArray(); | |
this.symbolInstancesArray = new SymbolInstancesArray(); | |
this.symbolQuadsArray = new SymbolQuadsArray(); | |
var collisionTile = new CollisionTile(this.angle, this.pitch, this.collisionBoxArray); | |
var featureIndex = new FeatureIndex(this.coord, this.overscaling, collisionTile, data.layers); | |
var sourceLayerCoder = new DictionaryCoder(data.layers ? Object.keys(data.layers).sort() : ['_geojsonTileLayer']); | |
var stats = { _total: 0 }; | |
var tile = this; | |
var bucketsById = {}; | |
var bucketsBySourceLayer = {}; | |
var i; | |
var layer; | |
var sourceLayerId; | |
var bucket; | |
// Map non-ref layers to buckets. | |
var bucketIndex = 0; | |
for (var layerId in layerFamilies) { | |
layer = layerFamilies[layerId][0]; | |
if (layer.source !== this.source) continue; | |
if (layer.ref) continue; | |
if (layer.minzoom && this.zoom < layer.minzoom) continue; | |
if (layer.maxzoom && this.zoom >= layer.maxzoom) continue; | |
if (layer.layout && layer.layout.visibility === 'none') continue; | |
if (data.layers && !data.layers[layer.sourceLayer]) continue; | |
bucket = Bucket.create({ | |
layer: layer, | |
index: bucketIndex++, | |
childLayers: layerFamilies[layerId], | |
zoom: this.zoom, | |
overscaling: this.overscaling, | |
showCollisionBoxes: this.showCollisionBoxes, | |
collisionBoxArray: this.collisionBoxArray, | |
symbolQuadsArray: this.symbolQuadsArray, | |
symbolInstancesArray: this.symbolInstancesArray, | |
sourceLayerIndex: sourceLayerCoder.encode(layer.sourceLayer || '_geojsonTileLayer') | |
}); | |
bucket.createFilter(); | |
bucketsById[layer.id] = bucket; | |
if (data.layers) { // vectortile | |
sourceLayerId = layer.sourceLayer; | |
bucketsBySourceLayer[sourceLayerId] = bucketsBySourceLayer[sourceLayerId] || {}; | |
bucketsBySourceLayer[sourceLayerId][layer.id] = bucket; | |
} | |
} | |
// read each layer, and sort its features into buckets | |
if (data.layers) { // vectortile | |
for (sourceLayerId in bucketsBySourceLayer) { | |
if (layer.version === 1) { | |
util.warnOnce( | |
'Vector tile source "' + this.source + '" layer "' + | |
sourceLayerId + '" does not use vector tile spec v2 ' + | |
'and therefore may have some rendering errors.' | |
); | |
} | |
layer = data.layers[sourceLayerId]; | |
if (layer) { | |
sortLayerIntoBuckets(layer, bucketsBySourceLayer[sourceLayerId]); | |
} | |
} | |
} else { // geojson | |
sortLayerIntoBuckets(data, bucketsById); | |
} | |
function sortLayerIntoBuckets(layer, buckets) { | |
for (var i = 0; i < layer.length; i++) { | |
var feature = layer.feature(i); | |
feature.index = i; | |
for (var id in buckets) { | |
if (buckets[id].filter(feature)) | |
buckets[id].features.push(feature); | |
} | |
} | |
} | |
var buckets = [], | |
symbolBuckets = this.symbolBuckets = [], | |
otherBuckets = []; | |
featureIndex.bucketLayerIDs = {}; | |
for (var id in bucketsById) { | |
bucket = bucketsById[id]; | |
if (bucket.features.length === 0) continue; | |
featureIndex.bucketLayerIDs[bucket.index] = bucket.childLayers.map(getLayerId); | |
buckets.push(bucket); | |
if (bucket.type === 'symbol') | |
symbolBuckets.push(bucket); | |
else | |
otherBuckets.push(bucket); | |
} | |
var icons = {}; | |
var stacks = {}; | |
var deps = 0; | |
if (symbolBuckets.length > 0) { | |
// Get dependencies for symbol buckets | |
for (i = symbolBuckets.length - 1; i >= 0; i--) { | |
symbolBuckets[i].updateIcons(icons); | |
symbolBuckets[i].updateFont(stacks); | |
} | |
for (var fontName in stacks) { | |
stacks[fontName] = Object.keys(stacks[fontName]).map(Number); | |
} | |
icons = Object.keys(icons); | |
actor.send('get glyphs', {uid: this.uid, stacks: stacks}, function(err, newStacks) { | |
stacks = newStacks; | |
gotDependency(err); | |
}); | |
if (icons.length) { | |
actor.send('get icons', {icons: icons}, function(err, newIcons) { | |
icons = newIcons; | |
gotDependency(err); | |
}); | |
} else { | |
gotDependency(); | |
} | |
} | |
// immediately parse non-symbol buckets (they have no dependencies) | |
for (i = otherBuckets.length - 1; i >= 0; i--) { | |
parseBucket(this, otherBuckets[i]); | |
} | |
if (symbolBuckets.length === 0) | |
return done(); | |
function gotDependency(err) { | |
if (err) return callback(err); | |
deps++; | |
if (deps === 2) { | |
// all symbol bucket dependencies fetched; parse them in proper order | |
for (var i = symbolBuckets.length - 1; i >= 0; i--) { | |
parseBucket(tile, symbolBuckets[i]); | |
} | |
done(); | |
} | |
} | |
function parseBucket(tile, bucket) { | |
var now = Date.now(); | |
bucket.populateArrays(collisionTile, stacks, icons); | |
var time = Date.now() - now; | |
if (bucket.type !== 'symbol') { | |
for (var i = 0; i < bucket.features.length; i++) { | |
var feature = bucket.features[i]; | |
featureIndex.insert(feature, feature.index, bucket.sourceLayerIndex, bucket.index); | |
} | |
} | |
bucket.features = null; | |
stats._total += time; | |
stats[bucket.id] = (stats[bucket.id] || 0) + time; | |
} | |
function done() { | |
tile.status = 'done'; | |
if (tile.redoPlacementAfterDone) { | |
tile.redoPlacement(tile.angle, tile.pitch, null); | |
tile.redoPlacementAfterDone = false; | |
} | |
var featureIndex_ = featureIndex.serialize(); | |
var collisionTile_ = collisionTile.serialize(); | |
var collisionBoxArray = tile.collisionBoxArray.serialize(); | |
var symbolInstancesArray = tile.symbolInstancesArray.serialize(); | |
var symbolQuadsArray = tile.symbolQuadsArray.serialize(); | |
var transferables = [rawTileData].concat(featureIndex_.transferables).concat(collisionTile_.transferables); | |
var nonEmptyBuckets = buckets.filter(isBucketNonEmpty); | |
callback(null, { | |
buckets: nonEmptyBuckets.map(serializeBucket), | |
bucketStats: stats, | |
featureIndex: featureIndex_.data, | |
collisionTile: collisionTile_.data, | |
collisionBoxArray: collisionBoxArray, | |
symbolInstancesArray: symbolInstancesArray, | |
symbolQuadsArray: symbolQuadsArray, | |
rawTileData: rawTileData | |
}, getTransferables(nonEmptyBuckets).concat(transferables)); | |
} | |
}; | |
WorkerTile.prototype.redoPlacement = function(angle, pitch, showCollisionBoxes) { | |
if (this.status !== 'done') { | |
this.redoPlacementAfterDone = true; | |
this.angle = angle; | |
return {}; | |
} | |
var collisionTile = new CollisionTile(angle, pitch, this.collisionBoxArray); | |
var buckets = this.symbolBuckets; | |
for (var i = buckets.length - 1; i >= 0; i--) { | |
buckets[i].placeFeatures(collisionTile, showCollisionBoxes); | |
} | |
var collisionTile_ = collisionTile.serialize(); | |
var nonEmptyBuckets = buckets.filter(isBucketNonEmpty); | |
return { | |
result: { | |
buckets: nonEmptyBuckets.map(serializeBucket), | |
collisionTile: collisionTile_.data | |
}, | |
transferables: getTransferables(nonEmptyBuckets).concat(collisionTile_.transferables) | |
}; | |
}; | |
function isBucketNonEmpty(bucket) { | |
return !bucket.isEmpty(); | |
} | |
function serializeBucket(bucket) { | |
return bucket.serialize(); | |
} | |
function getTransferables(buckets) { | |
var transferables = []; | |
for (var i in buckets) { | |
buckets[i].getTransferables(transferables); | |
} | |
return transferables; | |
} | |
function getLayerId(layer) { | |
return layer.id; | |
} | |
},{"../data/bucket":1,"../data/feature_index":7,"../symbol/collision_box":61,"../symbol/collision_tile":63,"../symbol/symbol_instances":72,"../symbol/symbol_quads":73,"../util/dictionary_coder":99,"../util/util":108}],42:[function(require,module,exports){ | |
'use strict'; | |
module.exports = AnimationLoop; | |
function AnimationLoop() { | |
this.n = 0; | |
this.times = []; | |
} | |
// Are all animations done? | |
AnimationLoop.prototype.stopped = function() { | |
this.times = this.times.filter(function(t) { | |
return t.time >= (new Date()).getTime(); | |
}); | |
return !this.times.length; | |
}; | |
// Add a new animation that will run t milliseconds | |
// Returns an id that can be used to cancel it layer | |
AnimationLoop.prototype.set = function(t) { | |
this.times.push({ id: this.n, time: t + (new Date()).getTime() }); | |
return this.n++; | |
}; | |
// Cancel an animation | |
AnimationLoop.prototype.cancel = function(n) { | |
this.times = this.times.filter(function(t) { | |
return t.id !== n; | |
}); | |
}; | |
},{}],43:[function(require,module,exports){ | |
'use strict'; | |
var Evented = require('../util/evented'); | |
var ajax = require('../util/ajax'); | |
var browser = require('../util/browser'); | |
var normalizeURL = require('../util/mapbox').normalizeSpriteURL; | |
module.exports = ImageSprite; | |
function ImageSprite(base) { | |
this.base = base; | |
this.retina = browser.devicePixelRatio > 1; | |
var format = this.retina ? '@2x' : ''; | |
ajax.getJSON(normalizeURL(base, format, '.json'), function(err, data) { | |
if (err) { | |
this.fire('error', {error: err}); | |
return; | |
} | |
this.data = data; | |
if (this.img) this.fire('load'); | |
}.bind(this)); | |
ajax.getImage(normalizeURL(base, format, '.png'), function(err, img) { | |
if (err) { | |
this.fire('error', {error: err}); | |
return; | |
} | |
// premultiply the sprite | |
var data = img.getData(); | |
var newdata = img.data = new Uint8Array(data.length); | |
for (var i = 0; i < data.length; i += 4) { | |
var alpha = data[i + 3] / 255; | |
newdata[i + 0] = data[i + 0] * alpha; | |
newdata[i + 1] = data[i + 1] * alpha; | |
newdata[i + 2] = data[i + 2] * alpha; | |
newdata[i + 3] = data[i + 3]; | |
} | |
this.img = img; | |
if (this.data) this.fire('load'); | |
}.bind(this)); | |
} | |
ImageSprite.prototype = Object.create(Evented); | |
ImageSprite.prototype.toJSON = function() { | |
return this.base; | |
}; | |
ImageSprite.prototype.loaded = function() { | |
return !!(this.data && this.img); | |
}; | |
ImageSprite.prototype.resize = function(/*gl*/) { | |
if (browser.devicePixelRatio > 1 !== this.retina) { | |
var newSprite = new ImageSprite(this.base); | |
newSprite.on('load', function() { | |
this.img = newSprite.img; | |
this.data = newSprite.data; | |
this.retina = newSprite.retina; | |
}.bind(this)); | |
} | |
}; | |
function SpritePosition() {} | |
SpritePosition.prototype = { x: 0, y: 0, width: 0, height: 0, pixelRatio: 1, sdf: false }; | |
ImageSprite.prototype.getSpritePosition = function(name) { | |
if (!this.loaded()) return new SpritePosition(); | |
var pos = this.data && this.data[name]; | |
if (pos && this.img) return pos; | |
return new SpritePosition(); | |
}; | |
},{"../util/ajax":92,"../util/browser":93,"../util/evented":100,"../util/mapbox":105}],44:[function(require,module,exports){ | |
'use strict'; | |
var parseColorString = require('csscolorparser').parseCSSColor; | |
var util = require('../util/util'); | |
var StyleFunction = require('./style_function'); | |
var cache = {}; | |
module.exports = function parseColor(input) { | |
if (StyleFunction.isFunctionDefinition(input)) { | |
return util.extend({}, input, { | |
stops: input.stops.map(function(stop) { | |
return [stop[0], parseColor(stop[1])]; | |
}) | |
}); | |
} else if (typeof input === 'string') { | |
if (!cache[input]) { | |
var rgba = parseColorString(input); | |
if (!rgba) { throw new Error('Invalid color ' + input); } | |
// GL expects all components to be in the range [0, 1] and to be | |
// multipled by the alpha value. | |
cache[input] = [ | |
rgba[0] / 255 * rgba[3], | |
rgba[1] / 255 * rgba[3], | |
rgba[2] / 255 * rgba[3], | |
rgba[3] | |
]; | |
} | |
return cache[input]; | |
} else { | |
throw new Error('Invalid color ' + input); | |
} | |
}; | |
},{"../util/util":108,"./style_function":47,"csscolorparser":122}],45:[function(require,module,exports){ | |
'use strict'; | |
var Evented = require('../util/evented'); | |
var StyleLayer = require('./style_layer'); | |
var ImageSprite = require('./image_sprite'); | |
var GlyphSource = require('../symbol/glyph_source'); | |
var SpriteAtlas = require('../symbol/sprite_atlas'); | |
var LineAtlas = require('../render/line_atlas'); | |
var util = require('../util/util'); | |
var ajax = require('../util/ajax'); | |
var normalizeURL = require('../util/mapbox').normalizeStyleURL; | |
var browser = require('../util/browser'); | |
var Dispatcher = require('../util/dispatcher'); | |
var AnimationLoop = require('./animation_loop'); | |
var validateStyle = require('./validate_style'); | |
var Source = require('../source/source'); | |
var styleSpec = require('./style_spec'); | |
var StyleFunction = require('./style_function'); | |
module.exports = Style; | |
function Style(stylesheet, animationLoop, workerCount) { | |
this.animationLoop = animationLoop || new AnimationLoop(); | |
this.dispatcher = new Dispatcher(workerCount || 1, this); | |
this.spriteAtlas = new SpriteAtlas(1024, 1024); | |
this.lineAtlas = new LineAtlas(256, 512); | |
this._layers = {}; | |
this._order = []; | |
this._groups = []; | |
this.sources = {}; | |
this.zoomHistory = {}; | |
util.bindAll([ | |
'_forwardSourceEvent', | |
'_forwardTileEvent', | |
'_forwardLayerEvent', | |
'_redoPlacement' | |
], this); | |
this._resetUpdates(); | |
var loaded = function(err, stylesheet) { | |
if (err) { | |
this.fire('error', {error: err}); | |
return; | |
} | |
if (validateStyle.emitErrors(this, validateStyle(stylesheet))) return; | |
this._loaded = true; | |
this.stylesheet = stylesheet; | |
this.updateClasses(); | |
var sources = stylesheet.sources; | |
for (var id in sources) { | |
this.addSource(id, sources[id]); | |
} | |
if (stylesheet.sprite) { | |
this.sprite = new ImageSprite(stylesheet.sprite); | |
this.sprite.on('load', this.fire.bind(this, 'change')); | |
} | |
this.glyphSource = new GlyphSource(stylesheet.glyphs); | |
this._resolve(); | |
this.fire('load'); | |
}.bind(this); | |
if (typeof stylesheet === 'string') { | |
ajax.getJSON(normalizeURL(stylesheet), loaded); | |
} else { | |
browser.frame(loaded.bind(this, null, stylesheet)); | |
} | |
this.on('source.load', function(event) { | |
var source = event.source; | |
if (source && source.vectorLayerIds) { | |
for (var layerId in this._layers) { | |
var layer = this._layers[layerId]; | |
if (layer.source === source.id) { | |
this._validateLayer(layer); | |
} | |
} | |
} | |
}); | |
} | |
Style.prototype = util.inherit(Evented, { | |
_loaded: false, | |
_validateLayer: function(layer) { | |
var source = this.sources[layer.source]; | |
if (!layer.sourceLayer) return; | |
if (!source) return; | |
if (!source.vectorLayerIds) return; | |
if (source.vectorLayerIds.indexOf(layer.sourceLayer) === -1) { | |
this.fire('error', { | |
error: new Error( | |
'Source layer "' + layer.sourceLayer + '" ' + | |
'does not exist on source "' + source.id + '" ' + | |
'as specified by style layer "' + layer.id + '"' | |
) | |
}); | |
} | |
}, | |
loaded: function() { | |
if (!this._loaded) | |
return false; | |
if (Object.keys(this._updates.sources).length) | |
return false; | |
for (var id in this.sources) | |
if (!this.sources[id].loaded()) | |
return false; | |
if (this.sprite && !this.sprite.loaded()) | |
return false; | |
return true; | |
}, | |
_resolve: function() { | |
var layer, layerJSON; | |
this._layers = {}; | |
this._order = this.stylesheet.layers.map(function(layer) { | |
return layer.id; | |
}); | |
// resolve all layers WITHOUT a ref | |
for (var i = 0; i < this.stylesheet.layers.length; i++) { | |
layerJSON = this.stylesheet.layers[i]; | |
if (layerJSON.ref) continue; | |
layer = StyleLayer.create(layerJSON); | |
this._layers[layer.id] = layer; | |
layer.on('error', this._forwardLayerEvent); | |
} | |
// resolve all layers WITH a ref | |
for (var j = 0; j < this.stylesheet.layers.length; j++) { | |
layerJSON = this.stylesheet.layers[j]; | |
if (!layerJSON.ref) continue; | |
var refLayer = this.getLayer(layerJSON.ref); | |
layer = StyleLayer.create(layerJSON, refLayer); | |
this._layers[layer.id] = layer; | |
layer.on('error', this._forwardLayerEvent); | |
} | |
this._groupLayers(); | |
this._updateWorkerLayers(); | |
}, | |
_groupLayers: function() { | |
var group; | |
this._groups = []; | |
// Split into groups of consecutive top-level layers with the same source. | |
for (var i = 0; i < this._order.length; ++i) { | |
var layer = this._layers[this._order[i]]; | |
if (!group || layer.source !== group.source) { | |
group = []; | |
group.source = layer.source; | |
this._groups.push(group); | |
} | |
group.push(layer); | |
} | |
}, | |
_updateWorkerLayers: function(ids) { | |
this.dispatcher.broadcast(ids ? 'update layers' : 'set layers', this._serializeLayers(ids)); | |
}, | |
_serializeLayers: function(ids) { | |
ids = ids || this._order; | |
var serialized = []; | |
var options = {includeRefProperties: true}; | |
for (var i = 0; i < ids.length; i++) { | |
serialized.push(this._layers[ids[i]].serialize(options)); | |
} | |
return serialized; | |
}, | |
_applyClasses: function(classes, options) { | |
if (!this._loaded) return; | |
classes = classes || []; | |
options = options || {transition: true}; | |
var transition = this.stylesheet.transition || {}; | |
var layers = this._updates.allPaintProps ? this._layers : this._updates.paintProps; | |
for (var id in layers) { | |
var layer = this._layers[id]; | |
var props = this._updates.paintProps[id]; | |
if (this._updates.allPaintProps || props.all) { | |
layer.updatePaintTransitions(classes, options, transition, this.animationLoop); | |
} else { | |
for (var paintName in props) { | |
this._layers[id].updatePaintTransition(paintName, classes, options, transition, this.animationLoop); | |
} | |
} | |
} | |
}, | |
_recalculate: function(z) { | |
for (var sourceId in this.sources) | |
this.sources[sourceId].used = false; | |
this._updateZoomHistory(z); | |
this.rasterFadeDuration = 300; | |
for (var layerId in this._layers) { | |
var layer = this._layers[layerId]; | |
layer.recalculate(z, this.zoomHistory); | |
if (!layer.isHidden(z) && layer.source) { | |
this.sources[layer.source].used = true; | |
} | |
} | |
var maxZoomTransitionDuration = 300; | |
if (Math.floor(this.z) !== Math.floor(z)) { | |
this.animationLoop.set(maxZoomTransitionDuration); | |
} | |
this.z = z; | |
this.fire('zoom'); | |
}, | |
_updateZoomHistory: function(z) { | |
var zh = this.zoomHistory; | |
if (zh.lastIntegerZoom === undefined) { | |
// first time | |
zh.lastIntegerZoom = Math.floor(z); | |
zh.lastIntegerZoomTime = 0; | |
zh.lastZoom = z; | |
} | |
// check whether an integer zoom level as passed since the last frame | |
// and if yes, record it with the time. Used for transitioning patterns. | |
if (Math.floor(zh.lastZoom) < Math.floor(z)) { | |
zh.lastIntegerZoom = Math.floor(z); | |
zh.lastIntegerZoomTime = Date.now(); | |
} else if (Math.floor(zh.lastZoom) > Math.floor(z)) { | |
zh.lastIntegerZoom = Math.floor(z + 1); | |
zh.lastIntegerZoomTime = Date.now(); | |
} | |
zh.lastZoom = z; | |
}, | |
_checkLoaded: function () { | |
if (!this._loaded) { | |
throw new Error('Style is not done loading'); | |
} | |
}, | |
/** | |
* Apply queued style updates in a batch | |
* @private | |
*/ | |
update: function(classes, options) { | |
if (!this._updates.changed) return this; | |
if (this._updates.allLayers) { | |
this._groupLayers(); | |
this._updateWorkerLayers(); | |
} else { | |
var updatedIds = Object.keys(this._updates.layers); | |
if (updatedIds.length) { | |
this._updateWorkerLayers(updatedIds); | |
} | |
} | |
var updatedSourceIds = Object.keys(this._updates.sources); | |
var i; | |
for (i = 0; i < updatedSourceIds.length; i++) { | |
this._reloadSource(updatedSourceIds[i]); | |
} | |
for (i = 0; i < this._updates.events.length; i++) { | |
var args = this._updates.events[i]; | |
this.fire(args[0], args[1]); | |
} | |
this._applyClasses(classes, options); | |
if (this._updates.changed) { | |
this.fire('change'); | |
} | |
this._resetUpdates(); | |
return this; | |
}, | |
_resetUpdates: function() { | |
this._updates = { | |
events: [], | |
layers: {}, | |
sources: {}, | |
paintProps: {} | |
}; | |
}, | |
addSource: function(id, source) { | |
this._checkLoaded(); | |
if (this.sources[id] !== undefined) { | |
throw new Error('There is already a source with this ID'); | |
} | |
if (!Source.is(source) && this._handleErrors(validateStyle.source, 'sources.' + id, source)) return this; | |
source = Source.create(source); | |
this.sources[id] = source; | |
source.id = id; | |
source.style = this; | |
source.dispatcher = this.dispatcher; | |
source | |
.on('load', this._forwardSourceEvent) | |
.on('error', this._forwardSourceEvent) | |
.on('change', this._forwardSourceEvent) | |
.on('tile.add', this._forwardTileEvent) | |
.on('tile.load', this._forwardTileEvent) | |
.on('tile.error', this._forwardTileEvent) | |
.on('tile.remove', this._forwardTileEvent) | |
.on('tile.stats', this._forwardTileEvent); | |
this._updates.events.push(['source.add', {source: source}]); | |
this._updates.changed = true; | |
return this; | |
}, | |
/** | |
* Remove a source from this stylesheet, given its id. | |
* @param {string} id id of the source to remove | |
* @returns {Style} this style | |
* @throws {Error} if no source is found with the given ID | |
* @private | |
*/ | |
removeSource: function(id) { | |
this._checkLoaded(); | |
if (this.sources[id] === undefined) { | |
throw new Error('There is no source with this ID'); | |
} | |
var source = this.sources[id]; | |
delete this.sources[id]; | |
source | |
.off('load', this._forwardSourceEvent) | |
.off('error', this._forwardSourceEvent) | |
.off('change', this._forwardSourceEvent) | |
.off('tile.add', this._forwardTileEvent) | |
.off('tile.load', this._forwardTileEvent) | |
.off('tile.error', this._forwardTileEvent) | |
.off('tile.remove', this._forwardTileEvent) | |
.off('tile.stats', this._forwardTileEvent); | |
this._updates.events.push(['source.remove', {source: source}]); | |
this._updates.changed = true; | |
return this; | |
}, | |
/** | |
* Get a source by id. | |
* @param {string} id id of the desired source | |
* @returns {Object} source | |
* @private | |
*/ | |
getSource: function(id) { | |
return this.sources[id]; | |
}, | |
/** | |
* Add a layer to the map style. The layer will be inserted before the layer with | |
* ID `before`, or appended if `before` is omitted. | |
* @param {StyleLayer|Object} layer | |
* @param {string=} before ID of an existing layer to insert before | |
* @fires layer.add | |
* @returns {Style} `this` | |
* @private | |
*/ | |
addLayer: function(layer, before) { | |
this._checkLoaded(); | |
if (!(layer instanceof StyleLayer)) { | |
// this layer is not in the style.layers array, so we pass an impossible array index | |
if (this._handleErrors(validateStyle.layer, | |
'layers.' + layer.id, layer, false, {arrayIndex: -1})) return this; | |
var refLayer = layer.ref && this.getLayer(layer.ref); | |
layer = StyleLayer.create(layer, refLayer); | |
} | |
this._validateLayer(layer); | |
layer.on('error', this._forwardLayerEvent); | |
this._layers[layer.id] = layer; | |
this._order.splice(before ? this._order.indexOf(before) : Infinity, 0, layer.id); | |
this._updates.allLayers = true; | |
if (layer.source) { | |
this._updates.sources[layer.source] = true; | |
} | |
this._updates.events.push(['layer.add', {layer: layer}]); | |
return this.updateClasses(layer.id); | |
}, | |
/** | |
* Remove a layer from this stylesheet, given its id. | |
* @param {string} id id of the layer to remove | |
* @returns {Style} this style | |
* @throws {Error} if no layer is found with the given ID | |
* @private | |
*/ | |
removeLayer: function(id) { | |
this._checkLoaded(); | |
var layer = this._layers[id]; | |
if (layer === undefined) { | |
throw new Error('There is no layer with this ID'); | |
} | |
for (var i in this._layers) { | |
if (this._layers[i].ref === id) { | |
this.removeLayer(i); | |
} | |
} | |
layer.off('error', this._forwardLayerEvent); | |
delete this._layers[id]; | |
delete this._updates.layers[id]; | |
delete this._updates.paintProps[id]; | |
this._order.splice(this._order.indexOf(id), 1); | |
this._updates.allLayers = true; | |
this._updates.events.push(['layer.remove', {layer: layer}]); | |
this._updates.changed = true; | |
return this; | |
}, | |
/** | |
* Return the style layer object with the given `id`. | |
* | |
* @param {string} id - id of the desired layer | |
* @returns {?Object} a layer, if one with the given `id` exists | |
* @private | |
*/ | |
getLayer: function(id) { | |
return this._layers[id]; | |
}, | |
/** | |
* If a layer has a `ref` property that makes it derive some values | |
* from another layer, return that referent layer. Otherwise, | |
* returns the layer itself. | |
* @param {string} id the layer's id | |
* @returns {Layer} the referent layer or the layer itself | |
* @private | |
*/ | |
getReferentLayer: function(id) { | |
var layer = this.getLayer(id); | |
if (layer.ref) { | |
layer = this.getLayer(layer.ref); | |
} | |
return layer; | |
}, | |
setLayerZoomRange: function(layerId, minzoom, maxzoom) { | |
this._checkLoaded(); | |
var layer = this.getReferentLayer(layerId); | |
if (layer.minzoom === minzoom && layer.maxzoom === maxzoom) return this; | |
if (minzoom != null) { | |
layer.minzoom = minzoom; | |
} | |
if (maxzoom != null) { | |
layer.maxzoom = maxzoom; | |
} | |
return this._updateLayer(layer); | |
}, | |
setFilter: function(layerId, filter) { | |
this._checkLoaded(); | |
var layer = this.getReferentLayer(layerId); | |
if (filter !== null && this._handleErrors(validateStyle.filter, 'layers.' + layer.id + '.filter', filter)) return this; | |
if (util.deepEqual(layer.filter, filter)) return this; | |
layer.filter = util.clone(filter); | |
return this._updateLayer(layer); | |
}, | |
/** | |
* Get a layer's filter object | |
* @param {string} layer the layer to inspect | |
* @returns {*} the layer's filter, if any | |
* @private | |
*/ | |
getFilter: function(layer) { | |
return this.getReferentLayer(layer).filter; | |
}, | |
setLayoutProperty: function(layerId, name, value) { | |
this._checkLoaded(); | |
var layer = this.getReferentLayer(layerId); | |
if (util.deepEqual(layer.getLayoutProperty(name), value)) return this; | |
layer.setLayoutProperty(name, value); | |
return this._updateLayer(layer); | |
}, | |
/** | |
* Get a layout property's value from a given layer | |
* @param {string} layer the layer to inspect | |
* @param {string} name the name of the layout property | |
* @returns {*} the property value | |
* @private | |
*/ | |
getLayoutProperty: function(layer, name) { | |
return this.getReferentLayer(layer).getLayoutProperty(name); | |
}, | |
setPaintProperty: function(layerId, name, value, klass) { | |
this._checkLoaded(); | |
var layer = this.getLayer(layerId); | |
if (util.deepEqual(layer.getPaintProperty(name, klass), value)) return this; | |
var wasFeatureConstant = layer.isPaintValueFeatureConstant(name); | |
layer.setPaintProperty(name, value, klass); | |
var isFeatureConstant = !( | |
value && | |
StyleFunction.isFunctionDefinition(value) && | |
value.property !== '$zoom' && | |
value.property !== undefined | |
); | |
if (!isFeatureConstant || !wasFeatureConstant) { | |
this._updates.layers[layerId] = true; | |
if (layer.source) { | |
this._updates.sources[layer.source] = true; | |
} | |
} | |
return this.updateClasses(layerId, name); | |
}, | |
getPaintProperty: function(layer, name, klass) { | |
return this.getLayer(layer).getPaintProperty(name, klass); | |
}, | |
updateClasses: function (layerId, paintName) { | |
this._updates.changed = true; | |
if (!layerId) { | |
this._updates.allPaintProps = true; | |
} else { | |
var props = this._updates.paintProps; | |
if (!props[layerId]) props[layerId] = {}; | |
props[layerId][paintName || 'all'] = true; | |
} | |
return this; | |
}, | |
serialize: function() { | |
return util.filterObject({ | |
version: this.stylesheet.version, | |
name: this.stylesheet.name, | |
metadata: this.stylesheet.metadata, | |
center: this.stylesheet.center, | |
zoom: this.stylesheet.zoom, | |
bearing: this.stylesheet.bearing, | |
pitch: this.stylesheet.pitch, | |
sprite: this.stylesheet.sprite, | |
glyphs: this.stylesheet.glyphs, | |
transition: this.stylesheet.transition, | |
sources: util.mapObject(this.sources, function(source) { | |
return source.serialize(); | |
}), | |
layers: this._order.map(function(id) { | |
return this._layers[id].serialize(); | |
}, this) | |
}, function(value) { return value !== undefined; }); | |
}, | |
_updateLayer: function (layer) { | |
this._updates.layers[layer.id] = true; | |
if (layer.source) { | |
this._updates.sources[layer.source] = true; | |
} | |
this._updates.changed = true; | |
return this; | |
}, | |
_flattenRenderedFeatures: function(sourceResults) { | |
var features = []; | |
for (var l = this._order.length - 1; l >= 0; l--) { | |
var layerID = this._order[l]; | |
for (var s = 0; s < sourceResults.length; s++) { | |
var layerFeatures = sourceResults[s][layerID]; | |
if (layerFeatures) { | |
for (var f = 0; f < layerFeatures.length; f++) { | |
features.push(layerFeatures[f]); | |
} | |
} | |
} | |
} | |
return features; | |
}, | |
queryRenderedFeatures: function(queryGeometry, params, zoom, bearing) { | |
if (params && params.filter) { | |
this._handleErrors(validateStyle.filter, 'queryRenderedFeatures.filter', params.filter, true); | |
} | |
var sourceResults = []; | |
for (var id in this.sources) { | |
var source = this.sources[id]; | |
if (source.queryRenderedFeatures) { | |
sourceResults.push(source.queryRenderedFeatures(queryGeometry, params, zoom, bearing)); | |
} | |
} | |
return this._flattenRenderedFeatures(sourceResults); | |
}, | |
querySourceFeatures: function(sourceID, params) { | |
if (params && params.filter) { | |
this._handleErrors(validateStyle.filter, 'querySourceFeatures.filter', params.filter, true); | |
} | |
var source = this.getSource(sourceID); | |
return source && source.querySourceFeatures ? source.querySourceFeatures(params) : []; | |
}, | |
_handleErrors: function(validate, key, value, throws, props) { | |
var action = throws ? validateStyle.throwErrors : validateStyle.emitErrors; | |
var result = validate.call(validateStyle, util.extend({ | |
key: key, | |
style: this.serialize(), | |
value: value, | |
styleSpec: styleSpec | |
}, props)); | |
return action.call(validateStyle, this, result); | |
}, | |
_remove: function() { | |
this.dispatcher.remove(); | |
}, | |
_reloadSource: function(id) { | |
this.sources[id].reload(); | |
}, | |
_updateSources: function(transform) { | |
for (var id in this.sources) { | |
this.sources[id].update(transform); | |
} | |
}, | |
_redoPlacement: function() { | |
for (var id in this.sources) { | |
if (this.sources[id].redoPlacement) this.sources[id].redoPlacement(); | |
} | |
}, | |
_forwardSourceEvent: function(e) { | |
this.fire('source.' + e.type, util.extend({source: e.target}, e)); | |
}, | |
_forwardTileEvent: function(e) { | |
this.fire(e.type, util.extend({source: e.target}, e)); | |
}, | |
_forwardLayerEvent: function(e) { | |
this.fire('layer.' + e.type, util.extend({layer: {id: e.target.id}}, e)); | |
}, | |
// Callbacks from web workers | |
'get sprite json': function(params, callback) { | |
var sprite = this.sprite; | |
if (sprite.loaded()) { | |
callback(null, { sprite: sprite.data, retina: sprite.retina }); | |
} else { | |
sprite.on('load', function() { | |
callback(null, { sprite: sprite.data, retina: sprite.retina }); | |
}); | |
} | |
}, | |
'get icons': function(params, callback) { | |
var sprite = this.sprite; | |
var spriteAtlas = this.spriteAtlas; | |
if (sprite.loaded()) { | |
spriteAtlas.setSprite(sprite); | |
spriteAtlas.addIcons(params.icons, callback); | |
} else { | |
sprite.on('load', function() { | |
spriteAtlas.setSprite(sprite); | |
spriteAtlas.addIcons(params.icons, callback); | |
}); | |
} | |
}, | |
'get glyphs': function(params, callback) { | |
var stacks = params.stacks, | |
remaining = Object.keys(stacks).length, | |
allGlyphs = {}; | |
for (var fontName in stacks) { | |
this.glyphSource.getSimpleGlyphs(fontName, stacks[fontName], params.uid, done); | |
} | |
function done(err, glyphs, fontName) { | |
if (err) console.error(err); | |
allGlyphs[fontName] = glyphs; | |
remaining--; | |
if (remaining === 0) | |
callback(null, allGlyphs); | |
} | |
} | |
}); | |
},{"../render/line_atlas":25,"../source/source":34,"../symbol/glyph_source":66,"../symbol/sprite_atlas":71,"../util/ajax":92,"../util/browser":93,"../util/dispatcher":95,"../util/evented":100,"../util/mapbox":105,"../util/util":108,"./animation_loop":42,"./image_sprite":43,"./style_function":47,"./style_layer":48,"./style_spec":55,"./validate_style":57}],46:[function(require,module,exports){ | |
'use strict'; | |
var MapboxGLFunction = require('./style_function'); | |
var parseColor = require('./parse_color'); | |
var util = require('../util/util'); | |
module.exports = StyleDeclaration; | |
function StyleDeclaration(reference, value) { | |
this.value = util.clone(value); | |
this.isFunction = MapboxGLFunction.isFunctionDefinition(value); | |
// immutable representation of value. used for comparison | |
this.json = JSON.stringify(this.value); | |
var parsedValue = reference.type === 'color' ? parseColor(this.value) : value; | |
this.calculate = MapboxGLFunction[reference.function || 'piecewise-constant'](parsedValue); | |
this.isFeatureConstant = this.calculate.isFeatureConstant; | |
this.isZoomConstant = this.calculate.isZoomConstant; | |
if (reference.function === 'piecewise-constant' && reference.transition) { | |
this.calculate = transitioned(this.calculate); | |
} | |
if (!this.isFeatureConstant && !this.isZoomConstant) { | |
this.stopZoomLevels = []; | |
var interpolationAmountStops = []; | |
var stops = this.value.stops; | |
for (var i = 0; i < this.value.stops.length; i++) { | |
var zoom = stops[i][0].zoom; | |
if (this.stopZoomLevels.indexOf(zoom) < 0) { | |
this.stopZoomLevels.push(zoom); | |
interpolationAmountStops.push([zoom, interpolationAmountStops.length]); | |
} | |
} | |
this.calculateInterpolationT = MapboxGLFunction.interpolated({ | |
stops: interpolationAmountStops, | |
base: value.base | |
}); | |
} | |
} | |
// This function is used to smoothly transition between discrete values, such | |
// as images and dasharrays. | |
function transitioned(calculate) { | |
return function(globalProperties, featureProperties) { | |
var z = globalProperties.zoom; | |
var zh = globalProperties.zoomHistory; | |
var duration = globalProperties.duration; | |
var fraction = z % 1; | |
var t = Math.min((Date.now() - zh.lastIntegerZoomTime) / duration, 1); | |
var fromScale = 1; | |
var toScale = 1; | |
var mix, from, to; | |
if (z > zh.lastIntegerZoom) { | |
mix = fraction + (1 - fraction) * t; | |
fromScale *= 2; | |
from = calculate({zoom: z - 1}, featureProperties); | |
to = calculate({zoom: z}, featureProperties); | |
} else { | |
mix = 1 - (1 - t) * fraction; | |
to = calculate({zoom: z}, featureProperties); | |
from = calculate({zoom: z + 1}, featureProperties); | |
fromScale /= 2; | |
} | |
if (from === undefined || to === undefined) { | |
return undefined; | |
} else { | |
return { | |
from: from, | |
fromScale: fromScale, | |
to: to, | |
toScale: toScale, | |
t: mix | |
}; | |
} | |
}; | |
} | |
},{"../util/util":108,"./parse_color":44,"./style_function":47}],47:[function(require,module,exports){ | |
'use strict'; | |
var MapboxGLFunction = require('mapbox-gl-function'); | |
exports.interpolated = function(parameters) { | |
var inner = MapboxGLFunction.interpolated(parameters); | |
var outer = function(globalProperties, featureProperties) { | |
return inner(globalProperties && globalProperties.zoom, featureProperties || {}); | |
}; | |
outer.isFeatureConstant = inner.isFeatureConstant; | |
outer.isZoomConstant = inner.isZoomConstant; | |
return outer; | |
}; | |
exports['piecewise-constant'] = function(parameters) { | |
var inner = MapboxGLFunction['piecewise-constant'](parameters); | |
var outer = function(globalProperties, featureProperties) { | |
return inner(globalProperties && globalProperties.zoom, featureProperties || {}); | |
}; | |
outer.isFeatureConstant = inner.isFeatureConstant; | |
outer.isZoomConstant = inner.isZoomConstant; | |
return outer; | |
}; | |
exports.isFunctionDefinition = MapboxGLFunction.isFunctionDefinition; | |
},{"mapbox-gl-function":146}],48:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var StyleTransition = require('./style_transition'); | |
var StyleDeclaration = require('./style_declaration'); | |
var styleSpec = require('./style_spec'); | |
var validateStyle = require('./validate_style'); | |
var parseColor = require('./parse_color'); | |
var Evented = require('../util/evented'); | |
module.exports = StyleLayer; | |
var TRANSITION_SUFFIX = '-transition'; | |
StyleLayer.create = function(layer, refLayer) { | |
var Classes = { | |
background: require('./style_layer/background_style_layer'), | |
circle: require('./style_layer/circle_style_layer'), | |
fill: require('./style_layer/fill_style_layer'), | |
line: require('./style_layer/line_style_layer'), | |
raster: require('./style_layer/raster_style_layer'), | |
symbol: require('./style_layer/symbol_style_layer') | |
}; | |
return new Classes[(refLayer || layer).type](layer, refLayer); | |
}; | |
function StyleLayer(layer, refLayer) { | |
this.set(layer, refLayer); | |
} | |
StyleLayer.prototype = util.inherit(Evented, { | |
set: function(layer, refLayer) { | |
this.id = layer.id; | |
this.ref = layer.ref; | |
this.metadata = layer.metadata; | |
this.type = (refLayer || layer).type; | |
this.source = (refLayer || layer).source; | |
this.sourceLayer = (refLayer || layer)['source-layer']; | |
this.minzoom = (refLayer || layer).minzoom; | |
this.maxzoom = (refLayer || layer).maxzoom; | |
this.filter = (refLayer || layer).filter; | |
this.paint = {}; | |
this.layout = {}; | |
this._paintSpecifications = styleSpec['paint_' + this.type]; | |
this._layoutSpecifications = styleSpec['layout_' + this.type]; | |
this._paintTransitions = {}; // {[propertyName]: StyleTransition} | |
this._paintTransitionOptions = {}; // {[className]: {[propertyName]: { duration:Number, delay:Number }}} | |
this._paintDeclarations = {}; // {[className]: {[propertyName]: StyleDeclaration}} | |
this._layoutDeclarations = {}; // {[propertyName]: StyleDeclaration} | |
this._layoutFunctions = {}; // {[propertyName]: Boolean} | |
var paintName, layoutName; | |
// Resolve paint declarations | |
for (var key in layer) { | |
var match = key.match(/^paint(?:\.(.*))?$/); | |
if (match) { | |
var klass = match[1] || ''; | |
for (paintName in layer[key]) { | |
this.setPaintProperty(paintName, layer[key][paintName], klass); | |
} | |
} | |
} | |
// Resolve layout declarations | |
if (this.ref) { | |
this._layoutDeclarations = refLayer._layoutDeclarations; | |
} else { | |
for (layoutName in layer.layout) { | |
this.setLayoutProperty(layoutName, layer.layout[layoutName]); | |
} | |
} | |
// set initial layout/paint values | |
for (paintName in this._paintSpecifications) { | |
this.paint[paintName] = this.getPaintValue(paintName); | |
} | |
for (layoutName in this._layoutSpecifications) { | |
this._updateLayoutValue(layoutName); | |
} | |
}, | |
setLayoutProperty: function(name, value) { | |
if (value == null) { | |
delete this._layoutDeclarations[name]; | |
} else { | |
var key = 'layers.' + this.id + '.layout.' + name; | |
if (this._handleErrors(validateStyle.layoutProperty, key, name, value)) return; | |
this._layoutDeclarations[name] = new StyleDeclaration(this._layoutSpecifications[name], value); | |
} | |
this._updateLayoutValue(name); | |
}, | |
getLayoutProperty: function(name) { | |
return ( | |
this._layoutDeclarations[name] && | |
this._layoutDeclarations[name].value | |
); | |
}, | |
getLayoutValue: function(name, globalProperties, featureProperties) { | |
var specification = this._layoutSpecifications[name]; | |
var declaration = this._layoutDeclarations[name]; | |
if (declaration) { | |
return declaration.calculate(globalProperties, featureProperties); | |
} else { | |
return specification.default; | |
} | |
}, | |
setPaintProperty: function(name, value, klass) { | |
var validateStyleKey = 'layers.' + this.id + (klass ? '["paint.' + klass + '"].' : '.paint.') + name; | |
if (util.endsWith(name, TRANSITION_SUFFIX)) { | |
if (!this._paintTransitionOptions[klass || '']) { | |
this._paintTransitionOptions[klass || ''] = {}; | |
} | |
if (value === null || value === undefined) { | |
delete this._paintTransitionOptions[klass || ''][name]; | |
} else { | |
if (this._handleErrors(validateStyle.paintProperty, validateStyleKey, name, value)) return; | |
this._paintTransitionOptions[klass || ''][name] = value; | |
} | |
} else { | |
if (!this._paintDeclarations[klass || '']) { | |
this._paintDeclarations[klass || ''] = {}; | |
} | |
if (value === null || value === undefined) { | |
delete this._paintDeclarations[klass || ''][name]; | |
} else { | |
if (this._handleErrors(validateStyle.paintProperty, validateStyleKey, name, value)) return; | |
this._paintDeclarations[klass || ''][name] = new StyleDeclaration(this._paintSpecifications[name], value); | |
} | |
} | |
}, | |
getPaintProperty: function(name, klass) { | |
klass = klass || ''; | |
if (util.endsWith(name, TRANSITION_SUFFIX)) { | |
return ( | |
this._paintTransitionOptions[klass] && | |
this._paintTransitionOptions[klass][name] | |
); | |
} else { | |
return ( | |
this._paintDeclarations[klass] && | |
this._paintDeclarations[klass][name] && | |
this._paintDeclarations[klass][name].value | |
); | |
} | |
}, | |
getPaintValue: function(name, globalProperties, featureProperties) { | |
var specification = this._paintSpecifications[name]; | |
var transition = this._paintTransitions[name]; | |
if (transition) { | |
return transition.calculate(globalProperties, featureProperties); | |
} else if (specification.type === 'color' && specification.default) { | |
return parseColor(specification.default); | |
} else { | |
return specification.default; | |
} | |
}, | |
getPaintValueStopZoomLevels: function(name) { | |
var transition = this._paintTransitions[name]; | |
if (transition) { | |
return transition.declaration.stopZoomLevels; | |
} else { | |
return []; | |
} | |
}, | |
getPaintInterpolationT: function(name, zoom) { | |
var transition = this._paintTransitions[name]; | |
return transition.declaration.calculateInterpolationT({ zoom: zoom }); | |
}, | |
isPaintValueFeatureConstant: function(name) { | |
var transition = this._paintTransitions[name]; | |
if (transition) { | |
return transition.declaration.isFeatureConstant; | |
} else { | |
return true; | |
} | |
}, | |
isLayoutValueFeatureConstant: function(name) { | |
var declaration = this._layoutDeclarations[name]; | |
if (declaration) { | |
return declaration.isFeatureConstant; | |
} else { | |
return true; | |
} | |
}, | |
isPaintValueZoomConstant: function(name) { | |
var transition = this._paintTransitions[name]; | |
if (transition) { | |
return transition.declaration.isZoomConstant; | |
} else { | |
return true; | |
} | |
}, | |
isHidden: function(zoom) { | |
if (this.minzoom && zoom < this.minzoom) return true; | |
if (this.maxzoom && zoom >= this.maxzoom) return true; | |
if (this.layout['visibility'] === 'none') return true; | |
if (this.paint[this.type + '-opacity'] === 0) return true; | |
return false; | |
}, | |
updatePaintTransitions: function(classes, options, globalOptions, animationLoop) { | |
var declarations = util.extend({}, this._paintDeclarations['']); | |
for (var i = 0; i < classes.length; i++) { | |
util.extend(declarations, this._paintDeclarations[classes[i]]); | |
} | |
var name; | |
for (name in declarations) { // apply new declarations | |
this._applyPaintDeclaration(name, declarations[name], options, globalOptions, animationLoop); | |
} | |
for (name in this._paintTransitions) { | |
if (!(name in declarations)) // apply removed declarations | |
this._applyPaintDeclaration(name, null, options, globalOptions, animationLoop); | |
} | |
}, | |
updatePaintTransition: function(name, classes, options, globalOptions, animationLoop) { | |
var declaration = this._paintDeclarations[''][name]; | |
for (var i = 0; i < classes.length; i++) { | |
var classPaintDeclarations = this._paintDeclarations[classes[i]]; | |
if (classPaintDeclarations && classPaintDeclarations[name]) { | |
declaration = classPaintDeclarations[name]; | |
} | |
} | |
this._applyPaintDeclaration(name, declaration, options, globalOptions, animationLoop); | |
}, | |
// update all zoom-dependent layout/paint values | |
recalculate: function(zoom, zoomHistory) { | |
for (var paintName in this._paintTransitions) { | |
this.paint[paintName] = this.getPaintValue(paintName, {zoom: zoom, zoomHistory: zoomHistory}); | |
} | |
for (var layoutName in this._layoutFunctions) { | |
this.layout[layoutName] = this.getLayoutValue(layoutName, {zoom: zoom, zoomHistory: zoomHistory}); | |
} | |
}, | |
serialize: function(options) { | |
var output = { | |
'id': this.id, | |
'ref': this.ref, | |
'metadata': this.metadata, | |
'minzoom': this.minzoom, | |
'maxzoom': this.maxzoom | |
}; | |
for (var klass in this._paintDeclarations) { | |
var key = klass === '' ? 'paint' : 'paint.' + klass; | |
output[key] = util.mapObject(this._paintDeclarations[klass], getDeclarationValue); | |
} | |
if (!this.ref || (options && options.includeRefProperties)) { | |
util.extend(output, { | |
'type': this.type, | |
'source': this.source, | |
'source-layer': this.sourceLayer, | |
'filter': this.filter, | |
'layout': util.mapObject(this._layoutDeclarations, getDeclarationValue) | |
}); | |
} | |
return util.filterObject(output, function(value, key) { | |
return value !== undefined && !(key === 'layout' && !Object.keys(value).length); | |
}); | |
}, | |
// set paint transition based on a given paint declaration | |
_applyPaintDeclaration: function (name, declaration, options, globalOptions, animationLoop) { | |
var oldTransition = options.transition ? this._paintTransitions[name] : undefined; | |
var spec = this._paintSpecifications[name]; | |
if (declaration === null || declaration === undefined) { | |
declaration = new StyleDeclaration(spec, spec.default); | |
} | |
if (oldTransition && oldTransition.declaration.json === declaration.json) return; | |
var transitionOptions = util.extend({ | |
duration: 300, | |
delay: 0 | |
}, globalOptions, this.getPaintProperty(name + TRANSITION_SUFFIX)); | |
var newTransition = this._paintTransitions[name] = | |
new StyleTransition(spec, declaration, oldTransition, transitionOptions); | |
if (!newTransition.instant()) { | |
newTransition.loopID = animationLoop.set(newTransition.endTime - Date.now()); | |
} | |
if (oldTransition) { | |
animationLoop.cancel(oldTransition.loopID); | |
} | |
}, | |
// update layout value if it's constant, or mark it as zoom-dependent | |
_updateLayoutValue: function(name) { | |
var declaration = this._layoutDeclarations[name]; | |
if (declaration && declaration.isFunction) { | |
this._layoutFunctions[name] = true; | |
} else { | |
delete this._layoutFunctions[name]; | |
this.layout[name] = this.getLayoutValue(name); | |
} | |
}, | |
_handleErrors: function(validate, key, name, value) { | |
return validateStyle.emitErrors(this, validate.call(validateStyle, { | |
key: key, | |
layerType: this.type, | |
objectKey: name, | |
value: value, | |
styleSpec: styleSpec, | |
// Workaround for https://github.com/mapbox/mapbox-gl-js/issues/2407 | |
style: {glyphs: true, sprite: true} | |
})); | |
} | |
}); | |
function getDeclarationValue(declaration) { | |
return declaration.value; | |
} | |
},{"../util/evented":100,"../util/util":108,"./parse_color":44,"./style_declaration":46,"./style_layer/background_style_layer":49,"./style_layer/circle_style_layer":50,"./style_layer/fill_style_layer":51,"./style_layer/line_style_layer":52,"./style_layer/raster_style_layer":53,"./style_layer/symbol_style_layer":54,"./style_spec":55,"./style_transition":56,"./validate_style":57}],49:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../../util/util'); | |
var StyleLayer = require('../style_layer'); | |
function BackgroundStyleLayer() { | |
StyleLayer.apply(this, arguments); | |
} | |
module.exports = BackgroundStyleLayer; | |
BackgroundStyleLayer.prototype = util.inherit(StyleLayer, {}); | |
},{"../../util/util":108,"../style_layer":48}],50:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../../util/util'); | |
var StyleLayer = require('../style_layer'); | |
function CircleStyleLayer() { | |
StyleLayer.apply(this, arguments); | |
} | |
module.exports = CircleStyleLayer; | |
CircleStyleLayer.prototype = util.inherit(StyleLayer, {}); | |
},{"../../util/util":108,"../style_layer":48}],51:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../../util/util'); | |
var StyleLayer = require('../style_layer'); | |
function FillStyleLayer() { | |
StyleLayer.apply(this, arguments); | |
} | |
FillStyleLayer.prototype = util.inherit(StyleLayer, { | |
getPaintValue: function(name, globalProperties, featureProperties) { | |
if (name === 'fill-outline-color' && this.getPaintProperty('fill-outline-color') === undefined) { | |
return StyleLayer.prototype.getPaintValue.call(this, 'fill-color', globalProperties, featureProperties); | |
} else { | |
return StyleLayer.prototype.getPaintValue.call(this, name, globalProperties, featureProperties); | |
} | |
}, | |
getPaintValueStopZoomLevels: function(name) { | |
if (name === 'fill-outline-color' && this.getPaintProperty('fill-outline-color') === undefined) { | |
return StyleLayer.prototype.getPaintValueStopZoomLevels.call(this, 'fill-color'); | |
} else { | |
return StyleLayer.prototype.getPaintValueStopZoomLevels.call(this, arguments); | |
} | |
}, | |
getPaintInterpolationT: function(name, zoom) { | |
if (name === 'fill-outline-color' && this.getPaintProperty('fill-outline-color') === undefined) { | |
return StyleLayer.prototype.getPaintInterpolationT.call(this, 'fill-color', zoom); | |
} else { | |
return StyleLayer.prototype.getPaintInterpolationT.call(this, name, zoom); | |
} | |
}, | |
isPaintValueFeatureConstant: function(name) { | |
if (name === 'fill-outline-color' && this.getPaintProperty('fill-outline-color') === undefined) { | |
return StyleLayer.prototype.isPaintValueFeatureConstant.call(this, 'fill-color'); | |
} else { | |
return StyleLayer.prototype.isPaintValueFeatureConstant.call(this, name); | |
} | |
}, | |
isPaintValueZoomConstant: function(name) { | |
if (name === 'fill-outline-color' && this.getPaintProperty('fill-outline-color') === undefined) { | |
return StyleLayer.prototype.isPaintValueZoomConstant.call(this, 'fill-color'); | |
} else { | |
return StyleLayer.prototype.isPaintValueZoomConstant.call(this, name); | |
} | |
} | |
}); | |
module.exports = FillStyleLayer; | |
},{"../../util/util":108,"../style_layer":48}],52:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../../util/util'); | |
var StyleLayer = require('../style_layer'); | |
function LineStyleLayer() { | |
StyleLayer.apply(this, arguments); | |
} | |
module.exports = LineStyleLayer; | |
LineStyleLayer.prototype = util.inherit(StyleLayer, { | |
getPaintValue: function(name, globalProperties, featureProperties) { | |
var value = StyleLayer.prototype.getPaintValue.apply(this, arguments); | |
// If the line is dashed, scale the dash lengths by the line | |
// width at the previous round zoom level. | |
if (value && name === 'line-dasharray') { | |
var flooredZoom = Math.floor(globalProperties.zoom); | |
if (this._flooredZoom !== flooredZoom) { | |
this._flooredZoom = flooredZoom; | |
this._flooredLineWidth = this.getPaintValue('line-width', globalProperties, featureProperties); | |
} | |
value.fromScale *= this._flooredLineWidth; | |
value.toScale *= this._flooredLineWidth; | |
} | |
return value; | |
} | |
}); | |
},{"../../util/util":108,"../style_layer":48}],53:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../../util/util'); | |
var StyleLayer = require('../style_layer'); | |
function RasterStyleLayer() { | |
StyleLayer.apply(this, arguments); | |
} | |
module.exports = RasterStyleLayer; | |
RasterStyleLayer.prototype = util.inherit(StyleLayer, {}); | |
},{"../../util/util":108,"../style_layer":48}],54:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../../util/util'); | |
var StyleLayer = require('../style_layer'); | |
function SymbolStyleLayer() { | |
StyleLayer.apply(this, arguments); | |
} | |
module.exports = SymbolStyleLayer; | |
SymbolStyleLayer.prototype = util.inherit(StyleLayer, { | |
isHidden: function() { | |
if (StyleLayer.prototype.isHidden.apply(this, arguments)) return true; | |
var isTextHidden = this.paint['text-opacity'] === 0 || !this.layout['text-field']; | |
var isIconHidden = this.paint['icon-opacity'] === 0 || !this.layout['icon-image']; | |
if (isTextHidden && isIconHidden) return true; | |
return false; | |
}, | |
getLayoutValue: function(name, globalProperties, featureProperties) { | |
if (name === 'text-rotation-alignment' && | |
this.getLayoutValue('symbol-placement', globalProperties, featureProperties) === 'line' && | |
!this.getLayoutProperty('text-rotation-alignment')) { | |
return 'map'; | |
} else if (name === 'icon-rotation-alignment' && | |
this.getLayoutValue('symbol-placement', globalProperties, featureProperties) === 'line' && | |
!this.getLayoutProperty('icon-rotation-alignment')) { | |
return 'map'; | |
// If unspecified `text-pitch-alignment` inherits `text-rotation-alignment` | |
} else if (name === 'text-pitch-alignment' && !this.getLayoutProperty('text-pitch-alignment')) { | |
return this.getLayoutValue('text-rotation-alignment'); | |
} else { | |
return StyleLayer.prototype.getLayoutValue.apply(this, arguments); | |
} | |
} | |
}); | |
},{"../../util/util":108,"../style_layer":48}],55:[function(require,module,exports){ | |
'use strict'; | |
module.exports = require('mapbox-gl-style-spec/reference/latest'); | |
},{"mapbox-gl-style-spec/reference/latest":169}],56:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var interpolate = require('../util/interpolate'); | |
module.exports = StyleTransition; | |
/* | |
* Represents a transition between two declarations | |
*/ | |
function StyleTransition(reference, declaration, oldTransition, value) { | |
this.declaration = declaration; | |
this.startTime = this.endTime = (new Date()).getTime(); | |
if (reference.function === 'piecewise-constant' && reference.transition) { | |
this.interp = interpZoomTransitioned; | |
} else { | |
this.interp = interpolate[reference.type]; | |
} | |
this.oldTransition = oldTransition; | |
this.duration = value.duration || 0; | |
this.delay = value.delay || 0; | |
if (!this.instant()) { | |
this.endTime = this.startTime + this.duration + this.delay; | |
this.ease = util.easeCubicInOut; | |
} | |
if (oldTransition && oldTransition.endTime <= this.startTime) { | |
// Old transition is done running, so we can | |
// delete its reference to its old transition. | |
delete oldTransition.oldTransition; | |
} | |
} | |
StyleTransition.prototype.instant = function() { | |
return !this.oldTransition || !this.interp || (this.duration === 0 && this.delay === 0); | |
}; | |
/* | |
* Return the value of the transitioning property at zoom level `z` and optional time `t` | |
*/ | |
StyleTransition.prototype.calculate = function(globalProperties, featureProperties) { | |
var value = this.declaration.calculate( | |
util.extend({}, globalProperties, {duration: this.duration}), | |
featureProperties | |
); | |
if (this.instant()) return value; | |
var t = globalProperties.time || Date.now(); | |
if (t < this.endTime) { | |
var oldValue = this.oldTransition.calculate( | |
util.extend({}, globalProperties, {time: this.startTime}), | |
featureProperties | |
); | |
var eased = this.ease((t - this.startTime - this.delay) / this.duration); | |
value = this.interp(oldValue, value, eased); | |
} | |
return value; | |
}; | |
// This function is used to smoothly transition between discrete values, such | |
// as images and dasharrays. | |
function interpZoomTransitioned(from, to, t) { | |
if ((from && from.to) === undefined || (to && to.to) === undefined) { | |
return undefined; | |
} else { | |
return { | |
from: from.to, | |
fromScale: from.toScale, | |
to: to.to, | |
toScale: to.toScale, | |
t: t | |
}; | |
} | |
} | |
},{"../util/interpolate":102,"../util/util":108}],57:[function(require,module,exports){ | |
'use strict'; | |
module.exports = require('mapbox-gl-style-spec/lib/validate_style.min'); | |
module.exports.emitErrors = function throwErrors(emitter, errors) { | |
if (errors && errors.length) { | |
for (var i = 0; i < errors.length; i++) { | |
emitter.fire('error', { error: new Error(errors[i].message) }); | |
} | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
module.exports.throwErrors = function throwErrors(emitter, errors) { | |
if (errors) { | |
for (var i = 0; i < errors.length; i++) { | |
throw new Error(errors[i].message); | |
} | |
} | |
}; | |
},{"mapbox-gl-style-spec/lib/validate_style.min":168}],58:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
module.exports = Anchor; | |
function Anchor(x, y, angle, segment) { | |
this.x = x; | |
this.y = y; | |
this.angle = angle; | |
if (segment !== undefined) { | |
this.segment = segment; | |
} | |
} | |
Anchor.prototype = Object.create(Point.prototype); | |
Anchor.prototype.clone = function() { | |
return new Anchor(this.x, this.y, this.angle, this.segment); | |
}; | |
},{"point-geometry":177}],59:[function(require,module,exports){ | |
'use strict'; | |
module.exports = checkMaxAngle; | |
/** | |
* Labels placed around really sharp angles aren't readable. Check if any | |
* part of the potential label has a combined angle that is too big. | |
* | |
* @param {Array<Point>} line | |
* @param {Anchor} anchor The point on the line around which the label is anchored. | |
* @param {number} labelLength The length of the label in geometry units. | |
* @param {number} windowSize The check fails if the combined angles within a part of the line that is `windowSize` long is too big. | |
* @param {number} maxAngle The maximum combined angle that any window along the label is allowed to have. | |
* | |
* @returns {boolean} whether the label should be placed | |
* @private | |
*/ | |
function checkMaxAngle(line, anchor, labelLength, windowSize, maxAngle) { | |
// horizontal labels always pass | |
if (anchor.segment === undefined) return true; | |
var p = anchor; | |
var index = anchor.segment + 1; | |
var anchorDistance = 0; | |
// move backwards along the line to the first segment the label appears on | |
while (anchorDistance > -labelLength / 2) { | |
index--; | |
// there isn't enough room for the label after the beginning of the line | |
if (index < 0) return false; | |
anchorDistance -= line[index].dist(p); | |
p = line[index]; | |
} | |
anchorDistance += line[index].dist(line[index + 1]); | |
index++; | |
// store recent corners and their total angle difference | |
var recentCorners = []; | |
var recentAngleDelta = 0; | |
// move forwards by the length of the label and check angles along the way | |
while (anchorDistance < labelLength / 2) { | |
var prev = line[index - 1]; | |
var current = line[index]; | |
var next = line[index + 1]; | |
// there isn't enough room for the label before the end of the line | |
if (!next) return false; | |
var angleDelta = prev.angleTo(current) - current.angleTo(next); | |
// restrict angle to -pi..pi range | |
angleDelta = Math.abs(((angleDelta + 3 * Math.PI) % (Math.PI * 2)) - Math.PI); | |
recentCorners.push({ | |
distance: anchorDistance, | |
angleDelta: angleDelta | |
}); | |
recentAngleDelta += angleDelta; | |
// remove corners that are far enough away from the list of recent anchors | |
while (anchorDistance - recentCorners[0].distance > windowSize) { | |
recentAngleDelta -= recentCorners.shift().angleDelta; | |
} | |
// the sum of angles within the window area exceeds the maximum allowed value. check fails. | |
if (recentAngleDelta > maxAngle) return false; | |
index++; | |
anchorDistance += current.dist(next); | |
} | |
// no part of the line had an angle greater than the maximum allowed. check passes. | |
return true; | |
} | |
},{}],60:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
module.exports = clipLine; | |
/** | |
* Returns the part of a multiline that intersects with the provided rectangular box. | |
* | |
* @param {Array<Array<Point>>} lines | |
* @param {number} x1 the left edge of the box | |
* @param {number} y1 the top edge of the box | |
* @param {number} x2 the right edge of the box | |
* @param {number} y2 the bottom edge of the box | |
* @returns {Array<Array<Point>>} lines | |
* @private | |
*/ | |
function clipLine(lines, x1, y1, x2, y2) { | |
var clippedLines = []; | |
for (var l = 0; l < lines.length; l++) { | |
var line = lines[l]; | |
var clippedLine; | |
for (var i = 0; i < line.length - 1; i++) { | |
var p0 = line[i]; | |
var p1 = line[i + 1]; | |
if (p0.x < x1 && p1.x < x1) { | |
continue; | |
} else if (p0.x < x1) { | |
p0 = new Point(x1, p0.y + (p1.y - p0.y) * ((x1 - p0.x) / (p1.x - p0.x)))._round(); | |
} else if (p1.x < x1) { | |
p1 = new Point(x1, p0.y + (p1.y - p0.y) * ((x1 - p0.x) / (p1.x - p0.x)))._round(); | |
} | |
if (p0.y < y1 && p1.y < y1) { | |
continue; | |
} else if (p0.y < y1) { | |
p0 = new Point(p0.x + (p1.x - p0.x) * ((y1 - p0.y) / (p1.y - p0.y)), y1)._round(); | |
} else if (p1.y < y1) { | |
p1 = new Point(p0.x + (p1.x - p0.x) * ((y1 - p0.y) / (p1.y - p0.y)), y1)._round(); | |
} | |
if (p0.x >= x2 && p1.x >= x2) { | |
continue; | |
} else if (p0.x >= x2) { | |
p0 = new Point(x2, p0.y + (p1.y - p0.y) * ((x2 - p0.x) / (p1.x - p0.x)))._round(); | |
} else if (p1.x >= x2) { | |
p1 = new Point(x2, p0.y + (p1.y - p0.y) * ((x2 - p0.x) / (p1.x - p0.x)))._round(); | |
} | |
if (p0.y >= y2 && p1.y >= y2) { | |
continue; | |
} else if (p0.y >= y2) { | |
p0 = new Point(p0.x + (p1.x - p0.x) * ((y2 - p0.y) / (p1.y - p0.y)), y2)._round(); | |
} else if (p1.y >= y2) { | |
p1 = new Point(p0.x + (p1.x - p0.x) * ((y2 - p0.y) / (p1.y - p0.y)), y2)._round(); | |
} | |
if (!clippedLine || !p0.equals(clippedLine[clippedLine.length - 1])) { | |
clippedLine = [p0]; | |
clippedLines.push(clippedLine); | |
} | |
clippedLine.push(p1); | |
} | |
} | |
return clippedLines; | |
} | |
},{"point-geometry":177}],61:[function(require,module,exports){ | |
'use strict'; | |
var StructArrayType = require('../util/struct_array'); | |
var util = require('../util/util'); | |
var Point = require('point-geometry'); | |
/** | |
* A collision box represents an area of the map that that is covered by a | |
* label. CollisionFeature uses one or more of these collision boxes to | |
* represent all the area covered by a single label. They are used to | |
* prevent collisions between labels. | |
* | |
* A collision box actually represents a 3d volume. The first two dimensions, | |
* x and y, are specified with `anchor` along with `x1`, `y1`, `x2`, `y2`. | |
* The third dimension, zoom, is limited by `maxScale` which determines | |
* how far in the z dimensions the box extends. | |
* | |
* As you zoom in on a map, all points on the map get further and further apart | |
* but labels stay roughly the same size. Labels cover less real world area on | |
* the map at higher zoom levels than they do at lower zoom levels. This is why | |
* areas are are represented with an anchor point and offsets from that point | |
* instead of just using four absolute points. | |
* | |
* Line labels are represented by a set of these boxes spaced out along a line. | |
* When you zoom in, line labels cover less real world distance along the line | |
* than they used to. Collision boxes near the edges that used to cover label | |
* no longer do. If a box doesn't cover the label anymore it should be ignored | |
* when doing collision checks. `maxScale` is how much you can scale the map | |
* before the label isn't within the box anymore. | |
* For example | |
* lower zoom: | |
* https://cloud.githubusercontent.com/assets/1421652/8060094/4d975f76-0e91-11e5-84b1-4edeb30a5875.png | |
* slightly higher zoom: | |
* https://cloud.githubusercontent.com/assets/1421652/8060061/26ae1c38-0e91-11e5-8c5a-9f380bf29f0a.png | |
* In the zoomed in image the two grey boxes on either side don't cover the | |
* label anymore. Their maxScale is smaller than the current scale. | |
* | |
* | |
* @class CollisionBoxArray | |
* @private | |
*/ | |
var CollisionBoxArray = module.exports = new StructArrayType({ | |
members: [ | |
// the box is centered around the anchor point | |
{ type: 'Int16', name: 'anchorPointX' }, | |
{ type: 'Int16', name: 'anchorPointY' }, | |
// distances to the edges from the anchor | |
{ type: 'Int16', name: 'x1' }, | |
{ type: 'Int16', name: 'y1' }, | |
{ type: 'Int16', name: 'x2' }, | |
{ type: 'Int16', name: 'y2' }, | |
// the box is only valid for scales < maxScale. | |
// The box does not block other boxes at scales >= maxScale; | |
{ type: 'Float32', name: 'maxScale' }, | |
// the index of the feature in the original vectortile | |
{ type: 'Uint32', name: 'featureIndex' }, | |
// the source layer the feature appears in | |
{ type: 'Uint16', name: 'sourceLayerIndex' }, | |
// the bucket the feature appears in | |
{ type: 'Uint16', name: 'bucketIndex' }, | |
// rotated and scaled bbox used for indexing | |
{ type: 'Int16', name: 'bbox0' }, | |
{ type: 'Int16', name: 'bbox1' }, | |
{ type: 'Int16', name: 'bbox2' }, | |
{ type: 'Int16', name: 'bbox3' }, | |
{ type: 'Float32', name: 'placementScale' } | |
]}); | |
util.extendAll(CollisionBoxArray.prototype.StructType.prototype, { | |
get anchorPoint() { | |
return new Point(this.anchorPointX, this.anchorPointY); | |
} | |
}); | |
},{"../util/struct_array":106,"../util/util":108,"point-geometry":177}],62:[function(require,module,exports){ | |
'use strict'; | |
module.exports = CollisionFeature; | |
/** | |
* A CollisionFeature represents the area of the tile covered by a single label. | |
* It is used with CollisionTile to check if the label overlaps with any | |
* previous labels. A CollisionFeature is mostly just a set of CollisionBox | |
* objects. | |
* | |
* @class CollisionFeature | |
* @param {Array<Point>} line The geometry the label is placed on. | |
* @param {Anchor} anchor The point along the line around which the label is anchored. | |
* @param {VectorTileFeature} feature The VectorTileFeature that this CollisionFeature was created for. | |
* @param {Array<string>} layerIDs The IDs of the layers that this CollisionFeature is a part of. | |
* @param {Object} shaped The text or icon shaping results. | |
* @param {number} boxScale A magic number used to convert from glyph metrics units to geometry units. | |
* @param {number} padding The amount of padding to add around the label edges. | |
* @param {boolean} alignLine Whether the label is aligned with the line or the viewport. | |
* | |
* @private | |
*/ | |
function CollisionFeature(collisionBoxArray, line, anchor, featureIndex, sourceLayerIndex, bucketIndex, shaped, boxScale, padding, alignLine, straight) { | |
var y1 = shaped.top * boxScale - padding; | |
var y2 = shaped.bottom * boxScale + padding; | |
var x1 = shaped.left * boxScale - padding; | |
var x2 = shaped.right * boxScale + padding; | |
this.boxStartIndex = collisionBoxArray.length; | |
if (alignLine) { | |
var height = y2 - y1; | |
var length = x2 - x1; | |
if (height > 0) { | |
// set minimum box height to avoid very many small labels | |
height = Math.max(10 * boxScale, height); | |
if (straight) { | |
// used for icon labels that are aligned with the line, but don't curve along it | |
var vector = line[anchor.segment + 1].sub(line[anchor.segment])._unit()._mult(length); | |
var straightLine = [anchor.sub(vector), anchor.add(vector)]; | |
this._addLineCollisionBoxes(collisionBoxArray, straightLine, anchor, 0, length, height, featureIndex, sourceLayerIndex, bucketIndex); | |
} else { | |
// used for text labels that curve along a line | |
this._addLineCollisionBoxes(collisionBoxArray, line, anchor, anchor.segment, length, height, featureIndex, sourceLayerIndex, bucketIndex); | |
} | |
} | |
} else { | |
collisionBoxArray.emplaceBack(anchor.x, anchor.y, x1, y1, x2, y2, Infinity, featureIndex, sourceLayerIndex, bucketIndex, | |
0, 0, 0, 0, 0); | |
} | |
this.boxEndIndex = collisionBoxArray.length; | |
} | |
/** | |
* Create a set of CollisionBox objects for a line. | |
* | |
* @param {Array<Point>} line | |
* @param {Anchor} anchor | |
* @param {number} labelLength The length of the label in geometry units. | |
* @param {Anchor} anchor The point along the line around which the label is anchored. | |
* @param {VectorTileFeature} feature The VectorTileFeature that this CollisionFeature was created for. | |
* @param {number} boxSize The size of the collision boxes that will be created. | |
* | |
* @private | |
*/ | |
CollisionFeature.prototype._addLineCollisionBoxes = function(collisionBoxArray, line, anchor, segment, labelLength, boxSize, featureIndex, sourceLayerIndex, bucketIndex) { | |
var step = boxSize / 2; | |
var nBoxes = Math.floor(labelLength / step); | |
// offset the center of the first box by half a box so that the edge of the | |
// box is at the edge of the label. | |
var firstBoxOffset = -boxSize / 2; | |
var bboxes = this.boxes; | |
var p = anchor; | |
var index = segment + 1; | |
var anchorDistance = firstBoxOffset; | |
// move backwards along the line to the first segment the label appears on | |
do { | |
index--; | |
// there isn't enough room for the label after the beginning of the line | |
// checkMaxAngle should have already caught this | |
if (index < 0) return bboxes; | |
anchorDistance -= line[index].dist(p); | |
p = line[index]; | |
} while (anchorDistance > -labelLength / 2); | |
var segmentLength = line[index].dist(line[index + 1]); | |
for (var i = 0; i < nBoxes; i++) { | |
// the distance the box will be from the anchor | |
var boxDistanceToAnchor = -labelLength / 2 + i * step; | |
// the box is not on the current segment. Move to the next segment. | |
while (anchorDistance + segmentLength < boxDistanceToAnchor) { | |
anchorDistance += segmentLength; | |
index++; | |
// There isn't enough room before the end of the line. | |
if (index + 1 >= line.length) return bboxes; | |
segmentLength = line[index].dist(line[index + 1]); | |
} | |
// the distance the box will be from the beginning of the segment | |
var segmentBoxDistance = boxDistanceToAnchor - anchorDistance; | |
var p0 = line[index]; | |
var p1 = line[index + 1]; | |
var boxAnchorPoint = p1.sub(p0)._unit()._mult(segmentBoxDistance)._add(p0)._round(); | |
var distanceToInnerEdge = Math.max(Math.abs(boxDistanceToAnchor - firstBoxOffset) - step / 2, 0); | |
var maxScale = labelLength / 2 / distanceToInnerEdge; | |
collisionBoxArray.emplaceBack(boxAnchorPoint.x, boxAnchorPoint.y, | |
-boxSize / 2, -boxSize / 2, boxSize / 2, boxSize / 2, maxScale, | |
featureIndex, sourceLayerIndex, bucketIndex, | |
0, 0, 0, 0, 0); | |
} | |
return bboxes; | |
}; | |
},{}],63:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
var EXTENT = require('../data/bucket').EXTENT; | |
var Grid = require('grid-index'); | |
module.exports = CollisionTile; | |
/** | |
* A collision tile used to prevent symbols from overlapping. It keep tracks of | |
* where previous symbols have been placed and is used to check if a new | |
* symbol overlaps with any previously added symbols. | |
* | |
* @class CollisionTile | |
* @param {number} angle | |
* @param {number} pitch | |
* @private | |
*/ | |
function CollisionTile(angle, pitch, collisionBoxArray) { | |
if (typeof angle === 'object') { | |
var serialized = angle; | |
collisionBoxArray = pitch; | |
angle = serialized.angle; | |
pitch = serialized.pitch; | |
this.grid = new Grid(serialized.grid); | |
this.ignoredGrid = new Grid(serialized.ignoredGrid); | |
} else { | |
this.grid = new Grid(EXTENT, 12, 6); | |
this.ignoredGrid = new Grid(EXTENT, 12, 0); | |
} | |
this.angle = angle; | |
this.pitch = pitch; | |
var sin = Math.sin(angle), | |
cos = Math.cos(angle); | |
this.rotationMatrix = [cos, -sin, sin, cos]; | |
this.reverseRotationMatrix = [cos, sin, -sin, cos]; | |
// Stretch boxes in y direction to account for the map tilt. | |
this.yStretch = 1 / Math.cos(pitch / 180 * Math.PI); | |
// The amount the map is squished depends on the y position. | |
// Sort of account for this by making all boxes a bit bigger. | |
this.yStretch = Math.pow(this.yStretch, 1.3); | |
this.collisionBoxArray = collisionBoxArray; | |
if (collisionBoxArray.length === 0) { | |
// the first collisionBoxArray is passed to a CollisionTile | |
// tempCollisionBox | |
collisionBoxArray.emplaceBack(); | |
var maxInt16 = 32767; | |
//left | |
collisionBoxArray.emplaceBack(0, 0, 0, -maxInt16, 0, maxInt16, maxInt16, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0); | |
// right | |
collisionBoxArray.emplaceBack(EXTENT, 0, 0, -maxInt16, 0, maxInt16, maxInt16, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0); | |
// top | |
collisionBoxArray.emplaceBack(0, 0, -maxInt16, 0, maxInt16, 0, maxInt16, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0); | |
// bottom | |
collisionBoxArray.emplaceBack(0, EXTENT, -maxInt16, 0, maxInt16, 0, maxInt16, | |
0, 0, 0, 0, 0, 0, 0, 0, | |
0); | |
} | |
this.tempCollisionBox = collisionBoxArray.get(0); | |
this.edges = [ | |
collisionBoxArray.get(1), | |
collisionBoxArray.get(2), | |
collisionBoxArray.get(3), | |
collisionBoxArray.get(4) | |
]; | |
} | |
CollisionTile.prototype.serialize = function() { | |
var data = { | |
angle: this.angle, | |
pitch: this.pitch, | |
grid: this.grid.toArrayBuffer(), | |
ignoredGrid: this.ignoredGrid.toArrayBuffer() | |
}; | |
return { | |
data: data, | |
transferables: [data.grid, data.ignoredGrid] | |
}; | |
}; | |
CollisionTile.prototype.minScale = 0.25; | |
CollisionTile.prototype.maxScale = 2; | |
/** | |
* Find the scale at which the collisionFeature can be shown without | |
* overlapping with other features. | |
* | |
* @param {CollisionFeature} collisionFeature | |
* @returns {number} placementScale | |
* @private | |
*/ | |
CollisionTile.prototype.placeCollisionFeature = function(collisionFeature, allowOverlap, avoidEdges) { | |
var collisionBoxArray = this.collisionBoxArray; | |
var minPlacementScale = this.minScale; | |
var rotationMatrix = this.rotationMatrix; | |
var yStretch = this.yStretch; | |
for (var b = collisionFeature.boxStartIndex; b < collisionFeature.boxEndIndex; b++) { | |
var box = collisionBoxArray.get(b); | |
var anchorPoint = box.anchorPoint._matMult(rotationMatrix); | |
var x = anchorPoint.x; | |
var y = anchorPoint.y; | |
var x1 = x + box.x1; | |
var y1 = y + box.y1 * yStretch; | |
var x2 = x + box.x2; | |
var y2 = y + box.y2 * yStretch; | |
box.bbox0 = x1; | |
box.bbox1 = y1; | |
box.bbox2 = x2; | |
box.bbox3 = y2; | |
if (!allowOverlap) { | |
var blockingBoxes = this.grid.query(x1, y1, x2, y2); | |
for (var i = 0; i < blockingBoxes.length; i++) { | |
var blocking = collisionBoxArray.get(blockingBoxes[i]); | |
var blockingAnchorPoint = blocking.anchorPoint._matMult(rotationMatrix); | |
minPlacementScale = this.getPlacementScale(minPlacementScale, anchorPoint, box, blockingAnchorPoint, blocking); | |
if (minPlacementScale >= this.maxScale) { | |
return minPlacementScale; | |
} | |
} | |
} | |
if (avoidEdges) { | |
var rotatedCollisionBox; | |
if (this.angle) { | |
var reverseRotationMatrix = this.reverseRotationMatrix; | |
var tl = new Point(box.x1, box.y1).matMult(reverseRotationMatrix); | |
var tr = new Point(box.x2, box.y1).matMult(reverseRotationMatrix); | |
var bl = new Point(box.x1, box.y2).matMult(reverseRotationMatrix); | |
var br = new Point(box.x2, box.y2).matMult(reverseRotationMatrix); | |
rotatedCollisionBox = this.tempCollisionBox; | |
rotatedCollisionBox.anchorPointX = box.anchorPoint.x; | |
rotatedCollisionBox.anchorPointY = box.anchorPoint.y; | |
rotatedCollisionBox.x1 = Math.min(tl.x, tr.x, bl.x, br.x); | |
rotatedCollisionBox.y1 = Math.min(tl.y, tr.x, bl.x, br.x); | |
rotatedCollisionBox.x2 = Math.max(tl.x, tr.x, bl.x, br.x); | |
rotatedCollisionBox.y2 = Math.max(tl.y, tr.x, bl.x, br.x); | |
rotatedCollisionBox.maxScale = box.maxScale; | |
} else { | |
rotatedCollisionBox = box; | |
} | |
for (var k = 0; k < this.edges.length; k++) { | |
var edgeBox = this.edges[k]; | |
minPlacementScale = this.getPlacementScale(minPlacementScale, box.anchorPoint, rotatedCollisionBox, edgeBox.anchorPoint, edgeBox); | |
if (minPlacementScale >= this.maxScale) { | |
return minPlacementScale; | |
} | |
} | |
} | |
} | |
return minPlacementScale; | |
}; | |
CollisionTile.prototype.queryRenderedSymbols = function(minX, minY, maxX, maxY, scale) { | |
var sourceLayerFeatures = {}; | |
var result = []; | |
var collisionBoxArray = this.collisionBoxArray; | |
var rotationMatrix = this.rotationMatrix; | |
var anchorPoint = new Point(minX, minY)._matMult(rotationMatrix); | |
var queryBox = this.tempCollisionBox; | |
queryBox.anchorX = anchorPoint.x; | |
queryBox.anchorY = anchorPoint.y; | |
queryBox.x1 = 0; | |
queryBox.y1 = 0; | |
queryBox.x2 = maxX - minX; | |
queryBox.y2 = maxY - minY; | |
queryBox.maxScale = scale; | |
// maxScale is stored using a Float32. Convert `scale` to the stored Float32 value. | |
scale = queryBox.maxScale; | |
var searchBox = [ | |
anchorPoint.x + queryBox.x1 / scale, | |
anchorPoint.y + queryBox.y1 / scale * this.yStretch, | |
anchorPoint.x + queryBox.x2 / scale, | |
anchorPoint.y + queryBox.y2 / scale * this.yStretch | |
]; | |
var blockingBoxKeys = this.grid.query(searchBox[0], searchBox[1], searchBox[2], searchBox[3]); | |
var blockingBoxKeys2 = this.ignoredGrid.query(searchBox[0], searchBox[1], searchBox[2], searchBox[3]); | |
for (var k = 0; k < blockingBoxKeys2.length; k++) { | |
blockingBoxKeys.push(blockingBoxKeys2[k]); | |
} | |
for (var i = 0; i < blockingBoxKeys.length; i++) { | |
var blocking = collisionBoxArray.get(blockingBoxKeys[i]); | |
var sourceLayer = blocking.sourceLayerIndex; | |
var featureIndex = blocking.featureIndex; | |
if (sourceLayerFeatures[sourceLayer] === undefined) { | |
sourceLayerFeatures[sourceLayer] = {}; | |
} | |
if (!sourceLayerFeatures[sourceLayer][featureIndex]) { | |
var blockingAnchorPoint = blocking.anchorPoint.matMult(rotationMatrix); | |
var minPlacementScale = this.getPlacementScale(this.minScale, anchorPoint, queryBox, blockingAnchorPoint, blocking); | |
if (minPlacementScale >= scale) { | |
sourceLayerFeatures[sourceLayer][featureIndex] = true; | |
result.push(blockingBoxKeys[i]); | |
} | |
} | |
} | |
return result; | |
}; | |
CollisionTile.prototype.getPlacementScale = function(minPlacementScale, anchorPoint, box, blockingAnchorPoint, blocking) { | |
// Find the lowest scale at which the two boxes can fit side by side without overlapping. | |
// Original algorithm: | |
var anchorDiffX = anchorPoint.x - blockingAnchorPoint.x; | |
var anchorDiffY = anchorPoint.y - blockingAnchorPoint.y; | |
var s1 = (blocking.x1 - box.x2) / anchorDiffX; // scale at which new box is to the left of old box | |
var s2 = (blocking.x2 - box.x1) / anchorDiffX; // scale at which new box is to the right of old box | |
var s3 = (blocking.y1 - box.y2) * this.yStretch / anchorDiffY; // scale at which new box is to the top of old box | |
var s4 = (blocking.y2 - box.y1) * this.yStretch / anchorDiffY; // scale at which new box is to the bottom of old box | |
if (isNaN(s1) || isNaN(s2)) s1 = s2 = 1; | |
if (isNaN(s3) || isNaN(s4)) s3 = s4 = 1; | |
var collisionFreeScale = Math.min(Math.max(s1, s2), Math.max(s3, s4)); | |
var blockingMaxScale = blocking.maxScale; | |
var boxMaxScale = box.maxScale; | |
if (collisionFreeScale > blockingMaxScale) { | |
// After a box's maxScale the label has shrunk enough that the box is no longer needed to cover it, | |
// so unblock the new box at the scale that the old box disappears. | |
collisionFreeScale = blockingMaxScale; | |
} | |
if (collisionFreeScale > boxMaxScale) { | |
// If the box can only be shown after it is visible, then the box can never be shown. | |
// But the label can be shown after this box is not visible. | |
collisionFreeScale = boxMaxScale; | |
} | |
if (collisionFreeScale > minPlacementScale && | |
collisionFreeScale >= blocking.placementScale) { | |
// If this collision occurs at a lower scale than previously found collisions | |
// and the collision occurs while the other label is visible | |
// this this is the lowest scale at which the label won't collide with anything | |
minPlacementScale = collisionFreeScale; | |
} | |
return minPlacementScale; | |
}; | |
/** | |
* Remember this collisionFeature and what scale it was placed at to block | |
* later features from overlapping with it. | |
* | |
* @param {CollisionFeature} collisionFeature | |
* @param {number} minPlacementScale | |
* @private | |
*/ | |
CollisionTile.prototype.insertCollisionFeature = function(collisionFeature, minPlacementScale, ignorePlacement) { | |
var grid = ignorePlacement ? this.ignoredGrid : this.grid; | |
var collisionBoxArray = this.collisionBoxArray; | |
for (var k = collisionFeature.boxStartIndex; k < collisionFeature.boxEndIndex; k++) { | |
var box = collisionBoxArray.get(k); | |
box.placementScale = minPlacementScale; | |
if (minPlacementScale < this.maxScale) { | |
grid.insert(k, box.bbox0, box.bbox1, box.bbox2, box.bbox3); | |
} | |
} | |
}; | |
},{"../data/bucket":1,"grid-index":145,"point-geometry":177}],64:[function(require,module,exports){ | |
'use strict'; | |
var interpolate = require('../util/interpolate'); | |
var Anchor = require('../symbol/anchor'); | |
var checkMaxAngle = require('./check_max_angle'); | |
module.exports = getAnchors; | |
function getAnchors(line, spacing, maxAngle, shapedText, shapedIcon, glyphSize, boxScale, overscaling, tileExtent) { | |
// Resample a line to get anchor points for labels and check that each | |
// potential label passes text-max-angle check and has enough froom to fit | |
// on the line. | |
var angleWindowSize = shapedText ? | |
3 / 5 * glyphSize * boxScale : | |
0; | |
var labelLength = Math.max( | |
shapedText ? shapedText.right - shapedText.left : 0, | |
shapedIcon ? shapedIcon.right - shapedIcon.left : 0); | |
// Is the line continued from outside the tile boundary? | |
var isLineContinued = line[0].x === 0 || line[0].x === tileExtent || line[0].y === 0 || line[0].y === tileExtent; | |
// Is the label long, relative to the spacing? | |
// If so, adjust the spacing so there is always a minimum space of `spacing / 4` between label edges. | |
if (spacing - labelLength * boxScale < spacing / 4) { | |
spacing = labelLength * boxScale + spacing / 4; | |
} | |
// Offset the first anchor by: | |
// Either half the label length plus a fixed extra offset if the line is not continued | |
// Or half the spacing if the line is continued. | |
// For non-continued lines, add a bit of fixed extra offset to avoid collisions at T intersections. | |
var fixedExtraOffset = glyphSize * 2; | |
var offset = !isLineContinued ? | |
((labelLength / 2 + fixedExtraOffset) * boxScale * overscaling) % spacing : | |
(spacing / 2 * overscaling) % spacing; | |
return resample(line, offset, spacing, angleWindowSize, maxAngle, labelLength * boxScale, isLineContinued, false, tileExtent); | |
} | |
function resample(line, offset, spacing, angleWindowSize, maxAngle, labelLength, isLineContinued, placeAtMiddle, tileExtent) { | |
var halfLabelLength = labelLength / 2; | |
var lineLength = 0; | |
for (var k = 0; k < line.length - 1; k++) { | |
lineLength += line[k].dist(line[k + 1]); | |
} | |
var distance = 0, | |
markedDistance = offset - spacing; | |
var anchors = []; | |
for (var i = 0; i < line.length - 1; i++) { | |
var a = line[i], | |
b = line[i + 1]; | |
var segmentDist = a.dist(b), | |
angle = b.angleTo(a); | |
while (markedDistance + spacing < distance + segmentDist) { | |
markedDistance += spacing; | |
var t = (markedDistance - distance) / segmentDist, | |
x = interpolate(a.x, b.x, t), | |
y = interpolate(a.y, b.y, t); | |
// Check that the point is within the tile boundaries and that | |
// the label would fit before the beginning and end of the line | |
// if placed at this point. | |
if (x >= 0 && x < tileExtent && y >= 0 && y < tileExtent && | |
markedDistance - halfLabelLength >= 0 && | |
markedDistance + halfLabelLength <= lineLength) { | |
var anchor = new Anchor(x, y, angle, i)._round(); | |
if (!angleWindowSize || checkMaxAngle(line, anchor, labelLength, angleWindowSize, maxAngle)) { | |
anchors.push(anchor); | |
} | |
} | |
} | |
distance += segmentDist; | |
} | |
if (!placeAtMiddle && !anchors.length && !isLineContinued) { | |
// The first attempt at finding anchors at which labels can be placed failed. | |
// Try again, but this time just try placing one anchor at the middle of the line. | |
// This has the most effect for short lines in overscaled tiles, since the | |
// initial offset used in overscaled tiles is calculated to align labels with positions in | |
// parent tiles instead of placing the label as close to the beginning as possible. | |
anchors = resample(line, distance / 2, spacing, angleWindowSize, maxAngle, labelLength, isLineContinued, true, tileExtent); | |
} | |
return anchors; | |
} | |
},{"../symbol/anchor":58,"../util/interpolate":102,"./check_max_angle":59}],65:[function(require,module,exports){ | |
'use strict'; | |
var ShelfPack = require('shelf-pack'); | |
var util = require('../util/util'); | |
module.exports = GlyphAtlas; | |
function GlyphAtlas(width, height) { | |
this.width = width; | |
this.height = height; | |
this.bin = new ShelfPack(width, height); | |
this.index = {}; | |
this.ids = {}; | |
this.data = new Uint8Array(width * height); | |
} | |
GlyphAtlas.prototype.getGlyphs = function() { | |
var glyphs = {}, | |
split, | |
name, | |
id; | |
for (var key in this.ids) { | |
split = key.split('#'); | |
name = split[0]; | |
id = split[1]; | |
if (!glyphs[name]) glyphs[name] = []; | |
glyphs[name].push(id); | |
} | |
return glyphs; | |
}; | |
GlyphAtlas.prototype.getRects = function() { | |
var rects = {}, | |
split, | |
name, | |
id; | |
for (var key in this.ids) { | |
split = key.split('#'); | |
name = split[0]; | |
id = split[1]; | |
if (!rects[name]) rects[name] = {}; | |
rects[name][id] = this.index[key]; | |
} | |
return rects; | |
}; | |
GlyphAtlas.prototype.addGlyph = function(id, name, glyph, buffer) { | |
if (!glyph) return null; | |
var key = name + "#" + glyph.id; | |
// The glyph is already in this texture. | |
if (this.index[key]) { | |
if (this.ids[key].indexOf(id) < 0) { | |
this.ids[key].push(id); | |
} | |
return this.index[key]; | |
} | |
// The glyph bitmap has zero width. | |
if (!glyph.bitmap) { | |
return null; | |
} | |
var bufferedWidth = glyph.width + buffer * 2; | |
var bufferedHeight = glyph.height + buffer * 2; | |
// Add a 1px border around every image. | |
var padding = 1; | |
var packWidth = bufferedWidth + 2 * padding; | |
var packHeight = bufferedHeight + 2 * padding; | |
// Increase to next number divisible by 4, but at least 1. | |
// This is so we can scale down the texture coordinates and pack them | |
// into 2 bytes rather than 4 bytes. | |
packWidth += (4 - packWidth % 4); | |
packHeight += (4 - packHeight % 4); | |
var rect = this.bin.packOne(packWidth, packHeight); | |
if (!rect) { | |
this.resize(); | |
rect = this.bin.packOne(packWidth, packHeight); | |
} | |
if (!rect) { | |
util.warnOnce('glyph bitmap overflow'); | |
return null; | |
} | |
this.index[key] = rect; | |
this.ids[key] = [id]; | |
var target = this.data; | |
var source = glyph.bitmap; | |
for (var y = 0; y < bufferedHeight; y++) { | |
var y1 = this.width * (rect.y + y + padding) + rect.x + padding; | |
var y2 = bufferedWidth * y; | |
for (var x = 0; x < bufferedWidth; x++) { | |
target[y1 + x] = source[y2 + x]; | |
} | |
} | |
this.dirty = true; | |
return rect; | |
}; | |
GlyphAtlas.prototype.resize = function() { | |
var origw = this.width, | |
origh = this.height; | |
// For now, don't grow the atlas beyond 1024x1024 because of how | |
// texture coords pack into unsigned byte in symbol bucket. | |
if (origw > 512 || origh > 512) return; | |
if (this.texture) { | |
if (this.gl) { | |
this.gl.deleteTexture(this.texture); | |
} | |
this.texture = null; | |
} | |
this.width *= 2; | |
this.height *= 2; | |
this.bin.resize(this.width, this.height); | |
var buf = new ArrayBuffer(this.width * this.height), | |
src, dst; | |
for (var i = 0; i < origh; i++) { | |
src = new Uint8Array(this.data.buffer, origh * i, origw); | |
dst = new Uint8Array(buf, origh * i * 2, origw); | |
dst.set(src); | |
} | |
this.data = new Uint8Array(buf); | |
}; | |
GlyphAtlas.prototype.bind = function(gl) { | |
this.gl = gl; | |
if (!this.texture) { | |
this.texture = gl.createTexture(); | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | |
gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, this.width, this.height, 0, gl.ALPHA, gl.UNSIGNED_BYTE, null); | |
} else { | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
} | |
}; | |
GlyphAtlas.prototype.updateTexture = function(gl) { | |
this.bind(gl); | |
if (this.dirty) { | |
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, this.width, this.height, gl.ALPHA, gl.UNSIGNED_BYTE, this.data); | |
this.dirty = false; | |
} | |
}; | |
},{"../util/util":108,"shelf-pack":180}],66:[function(require,module,exports){ | |
'use strict'; | |
var normalizeURL = require('../util/mapbox').normalizeGlyphsURL; | |
var getArrayBuffer = require('../util/ajax').getArrayBuffer; | |
var Glyphs = require('../util/glyphs'); | |
var GlyphAtlas = require('../symbol/glyph_atlas'); | |
var Protobuf = require('pbf'); | |
module.exports = GlyphSource; | |
/** | |
* A glyph source has a URL from which to load new glyphs and manages | |
* GlyphAtlases in which to store glyphs used by the requested fontstacks | |
* and ranges. | |
* | |
* @param {string} url glyph template url | |
* @private | |
*/ | |
function GlyphSource(url) { | |
this.url = url && normalizeURL(url); | |
this.atlases = {}; | |
this.stacks = {}; | |
this.loading = {}; | |
} | |
GlyphSource.prototype.getSimpleGlyphs = function(fontstack, glyphIDs, uid, callback) { | |
if (this.stacks[fontstack] === undefined) { | |
this.stacks[fontstack] = {}; | |
} | |
if (this.atlases[fontstack] === undefined) { | |
this.atlases[fontstack] = new GlyphAtlas(128, 128); | |
} | |
var glyphs = {}; | |
var stack = this.stacks[fontstack]; | |
var atlas = this.atlases[fontstack]; | |
// the number of pixels the sdf bitmaps are padded by | |
var buffer = 3; | |
var missing = {}; | |
var remaining = 0; | |
var range; | |
for (var i = 0; i < glyphIDs.length; i++) { | |
var glyphID = glyphIDs[i]; | |
range = Math.floor(glyphID / 256); | |
if (stack[range]) { | |
var glyph = stack[range].glyphs[glyphID]; | |
var rect = atlas.addGlyph(uid, fontstack, glyph, buffer); | |
if (glyph) glyphs[glyphID] = new SimpleGlyph(glyph, rect, buffer); | |
} else { | |
if (missing[range] === undefined) { | |
missing[range] = []; | |
remaining++; | |
} | |
missing[range].push(glyphID); | |
} | |
} | |
if (!remaining) callback(undefined, glyphs, fontstack); | |
var onRangeLoaded = function(err, range, data) { | |
if (!err) { | |
var stack = this.stacks[fontstack][range] = data.stacks[0]; | |
for (var i = 0; i < missing[range].length; i++) { | |
var glyphID = missing[range][i]; | |
var glyph = stack.glyphs[glyphID]; | |
var rect = atlas.addGlyph(uid, fontstack, glyph, buffer); | |
if (glyph) glyphs[glyphID] = new SimpleGlyph(glyph, rect, buffer); | |
} | |
} | |
remaining--; | |
if (!remaining) callback(undefined, glyphs, fontstack); | |
}.bind(this); | |
for (var r in missing) { | |
this.loadRange(fontstack, r, onRangeLoaded); | |
} | |
}; | |
// A simplified representation of the glyph containing only the properties needed for shaping. | |
function SimpleGlyph(glyph, rect, buffer) { | |
var padding = 1; | |
this.advance = glyph.advance; | |
this.left = glyph.left - buffer - padding; | |
this.top = glyph.top + buffer + padding; | |
this.rect = rect; | |
} | |
GlyphSource.prototype.loadRange = function(fontstack, range, callback) { | |
if (range * 256 > 65535) return callback('glyphs > 65535 not supported'); | |
if (this.loading[fontstack] === undefined) { | |
this.loading[fontstack] = {}; | |
} | |
var loading = this.loading[fontstack]; | |
if (loading[range]) { | |
loading[range].push(callback); | |
} else { | |
loading[range] = [callback]; | |
var rangeName = (range * 256) + '-' + (range * 256 + 255); | |
var url = glyphUrl(fontstack, rangeName, this.url); | |
getArrayBuffer(url, function(err, data) { | |
var glyphs = !err && new Glyphs(new Protobuf(new Uint8Array(data))); | |
for (var i = 0; i < loading[range].length; i++) { | |
loading[range][i](err, range, glyphs); | |
} | |
delete loading[range]; | |
}); | |
} | |
}; | |
GlyphSource.prototype.getGlyphAtlas = function(fontstack) { | |
return this.atlases[fontstack]; | |
}; | |
/** | |
* Use CNAME sharding to load a specific glyph range over a randomized | |
* but consistent subdomain. | |
* @param {string} fontstack comma-joined fonts | |
* @param {string} range comma-joined range | |
* @param {url} url templated url | |
* @param {string} [subdomains=abc] subdomains as a string where each letter is one. | |
* @returns {string} a url to load that section of glyphs | |
* @private | |
*/ | |
function glyphUrl(fontstack, range, url, subdomains) { | |
subdomains = subdomains || 'abc'; | |
return url | |
.replace('{s}', subdomains[fontstack.length % subdomains.length]) | |
.replace('{fontstack}', fontstack) | |
.replace('{range}', range); | |
} | |
},{"../symbol/glyph_atlas":65,"../util/ajax":92,"../util/glyphs":101,"../util/mapbox":105,"pbf":175}],67:[function(require,module,exports){ | |
'use strict'; | |
module.exports = function (features, textFeatures, geometries) { | |
var leftIndex = {}, | |
rightIndex = {}, | |
mergedFeatures = [], | |
mergedGeom = [], | |
mergedTexts = [], | |
mergedIndex = 0, | |
k; | |
function add(k) { | |
mergedFeatures.push(features[k]); | |
mergedGeom.push(geometries[k]); | |
mergedTexts.push(textFeatures[k]); | |
mergedIndex++; | |
} | |
function mergeFromRight(leftKey, rightKey, geom) { | |
var i = rightIndex[leftKey]; | |
delete rightIndex[leftKey]; | |
rightIndex[rightKey] = i; | |
mergedGeom[i][0].pop(); | |
mergedGeom[i][0] = mergedGeom[i][0].concat(geom[0]); | |
return i; | |
} | |
function mergeFromLeft(leftKey, rightKey, geom) { | |
var i = leftIndex[rightKey]; | |
delete leftIndex[rightKey]; | |
leftIndex[leftKey] = i; | |
mergedGeom[i][0].shift(); | |
mergedGeom[i][0] = geom[0].concat(mergedGeom[i][0]); | |
return i; | |
} | |
function getKey(text, geom, onRight) { | |
var point = onRight ? geom[0][geom[0].length - 1] : geom[0][0]; | |
return text + ':' + point.x + ':' + point.y; | |
} | |
for (k = 0; k < features.length; k++) { | |
var geom = geometries[k], | |
text = textFeatures[k]; | |
if (!text) { | |
add(k); | |
continue; | |
} | |
var leftKey = getKey(text, geom), | |
rightKey = getKey(text, geom, true); | |
if ((leftKey in rightIndex) && (rightKey in leftIndex) && (rightIndex[leftKey] !== leftIndex[rightKey])) { | |
// found lines with the same text adjacent to both ends of the current line, merge all three | |
var j = mergeFromLeft(leftKey, rightKey, geom); | |
var i = mergeFromRight(leftKey, rightKey, mergedGeom[j]); | |
delete leftIndex[leftKey]; | |
delete rightIndex[rightKey]; | |
rightIndex[getKey(text, mergedGeom[i], true)] = i; | |
mergedGeom[j] = null; | |
} else if (leftKey in rightIndex) { | |
// found mergeable line adjacent to the start of the current line, merge | |
mergeFromRight(leftKey, rightKey, geom); | |
} else if (rightKey in leftIndex) { | |
// found mergeable line adjacent to the end of the current line, merge | |
mergeFromLeft(leftKey, rightKey, geom); | |
} else { | |
// no adjacent lines, add as a new item | |
add(k); | |
leftIndex[leftKey] = mergedIndex - 1; | |
rightIndex[rightKey] = mergedIndex - 1; | |
} | |
} | |
return { | |
features: mergedFeatures, | |
textFeatures: mergedTexts, | |
geometries: mergedGeom | |
}; | |
}; | |
},{}],68:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
module.exports = { | |
getIconQuads: getIconQuads, | |
getGlyphQuads: getGlyphQuads, | |
SymbolQuad: SymbolQuad | |
}; | |
var minScale = 0.5; // underscale by 1 zoom level | |
/** | |
* A textured quad for rendering a single icon or glyph. | |
* | |
* The zoom range the glyph can be shown is defined by minScale and maxScale. | |
* | |
* @param {Point} anchorPoint the point the symbol is anchored around | |
* @param {Point} tl The offset of the top left corner from the anchor. | |
* @param {Point} tr The offset of the top right corner from the anchor. | |
* @param {Point} bl The offset of the bottom left corner from the anchor. | |
* @param {Point} br The offset of the bottom right corner from the anchor. | |
* @param {Object} tex The texture coordinates. | |
* @param {number} anchorAngle The angle of the label at it's center, not the angle of this quad. | |
* @param {number} glyphAngle The angle of the glyph to be positioned in the quad. | |
* @param {number} minScale The minimum scale, relative to the tile's intended scale, that the glyph can be shown at. | |
* @param {number} maxScale The maximum scale, relative to the tile's intended scale, that the glyph can be shown at. | |
* | |
* @class SymbolQuad | |
* @private | |
*/ | |
function SymbolQuad(anchorPoint, tl, tr, bl, br, tex, anchorAngle, glyphAngle, minScale, maxScale) { | |
this.anchorPoint = anchorPoint; | |
this.tl = tl; | |
this.tr = tr; | |
this.bl = bl; | |
this.br = br; | |
this.tex = tex; | |
this.anchorAngle = anchorAngle; | |
this.glyphAngle = glyphAngle; | |
this.minScale = minScale; | |
this.maxScale = maxScale; | |
} | |
/** | |
* Create the quads used for rendering an icon. | |
* | |
* @param {Anchor} anchor | |
* @param {PositionedIcon} shapedIcon | |
* @param {number} boxScale A magic number for converting glyph metric units to geometry units. | |
* @param {Array<Array<Point>>} line | |
* @param {StyleLayer} layer | |
* @param {boolean} alongLine Whether the icon should be placed along the line. | |
* @param {Shaping} shapedText Shaping for corresponding text | |
* @returns {Array<SymbolQuad>} | |
* @private | |
*/ | |
function getIconQuads(anchor, shapedIcon, boxScale, line, layer, alongLine, shapedText, globalProperties, featureProperties) { | |
var rect = shapedIcon.image.rect; | |
var layout = layer.layout; | |
var border = 1; | |
var left = shapedIcon.left - border; | |
var right = left + rect.w / shapedIcon.image.pixelRatio; | |
var top = shapedIcon.top - border; | |
var bottom = top + rect.h / shapedIcon.image.pixelRatio; | |
var tl, tr, br, bl; | |
// text-fit mode | |
if (layout['icon-text-fit'] !== 'none' && shapedText) { | |
var iconWidth = (right - left), | |
iconHeight = (bottom - top), | |
size = layout['text-size'] / 24, | |
textLeft = shapedText.left * size, | |
textRight = shapedText.right * size, | |
textTop = shapedText.top * size, | |
textBottom = shapedText.bottom * size, | |
textWidth = textRight - textLeft, | |
textHeight = textBottom - textTop, | |
padT = layout['icon-text-fit-padding'][0], | |
padR = layout['icon-text-fit-padding'][1], | |
padB = layout['icon-text-fit-padding'][2], | |
padL = layout['icon-text-fit-padding'][3], | |
offsetY = layout['icon-text-fit'] === 'width' ? (textHeight - iconHeight) * 0.5 : 0, | |
offsetX = layout['icon-text-fit'] === 'height' ? (textWidth - iconWidth) * 0.5 : 0, | |
width = layout['icon-text-fit'] === 'width' || layout['icon-text-fit'] === 'both' ? textWidth : iconWidth, | |
height = layout['icon-text-fit'] === 'height' || layout['icon-text-fit'] === 'both' ? textHeight : iconHeight; | |
tl = new Point(textLeft + offsetX - padL, textTop + offsetY - padT); | |
tr = new Point(textLeft + offsetX + padR + width, textTop + offsetY - padT); | |
br = new Point(textLeft + offsetX + padR + width, textTop + offsetY + padB + height); | |
bl = new Point(textLeft + offsetX - padL, textTop + offsetY + padB + height); | |
// Normal icon size mode | |
} else { | |
tl = new Point(left, top); | |
tr = new Point(right, top); | |
br = new Point(right, bottom); | |
bl = new Point(left, bottom); | |
} | |
var angle = layer.getLayoutValue('icon-rotate', globalProperties, featureProperties) * Math.PI / 180; | |
if (alongLine) { | |
var prev = line[anchor.segment]; | |
if (anchor.y === prev.y && anchor.x === prev.x && anchor.segment + 1 < line.length) { | |
var next = line[anchor.segment + 1]; | |
angle += Math.atan2(anchor.y - next.y, anchor.x - next.x) + Math.PI; | |
} else { | |
angle += Math.atan2(anchor.y - prev.y, anchor.x - prev.x); | |
} | |
} | |
if (angle) { | |
var sin = Math.sin(angle), | |
cos = Math.cos(angle), | |
matrix = [cos, -sin, sin, cos]; | |
tl = tl.matMult(matrix); | |
tr = tr.matMult(matrix); | |
bl = bl.matMult(matrix); | |
br = br.matMult(matrix); | |
} | |
return [new SymbolQuad(new Point(anchor.x, anchor.y), tl, tr, bl, br, shapedIcon.image.rect, 0, 0, minScale, Infinity)]; | |
} | |
/** | |
* Create the quads used for rendering a text label. | |
* | |
* @param {Anchor} anchor | |
* @param {Shaping} shaping | |
* @param {number} boxScale A magic number for converting from glyph metric units to geometry units. | |
* @param {Array<Array<Point>>} line | |
* @param {StyleLayer} layer | |
* @param {boolean} alongLine Whether the label should be placed along the line. | |
* @returns {Array<SymbolQuad>} | |
* @private | |
*/ | |
function getGlyphQuads(anchor, shaping, boxScale, line, layer, alongLine) { | |
var textRotate = layer.layout['text-rotate'] * Math.PI / 180; | |
var keepUpright = layer.layout['text-keep-upright']; | |
var positionedGlyphs = shaping.positionedGlyphs; | |
var quads = []; | |
for (var k = 0; k < positionedGlyphs.length; k++) { | |
var positionedGlyph = positionedGlyphs[k]; | |
var glyph = positionedGlyph.glyph; | |
var rect = glyph.rect; | |
if (!rect) continue; | |
var centerX = (positionedGlyph.x + glyph.advance / 2) * boxScale; | |
var glyphInstances; | |
var labelMinScale = minScale; | |
if (alongLine) { | |
glyphInstances = []; | |
labelMinScale = getSegmentGlyphs(glyphInstances, anchor, centerX, line, anchor.segment, true); | |
if (keepUpright) { | |
labelMinScale = Math.min(labelMinScale, getSegmentGlyphs(glyphInstances, anchor, centerX, line, anchor.segment, false)); | |
} | |
} else { | |
glyphInstances = [{ | |
anchorPoint: new Point(anchor.x, anchor.y), | |
offset: 0, | |
angle: 0, | |
maxScale: Infinity, | |
minScale: minScale | |
}]; | |
} | |
var x1 = positionedGlyph.x + glyph.left, | |
y1 = positionedGlyph.y - glyph.top, | |
x2 = x1 + rect.w, | |
y2 = y1 + rect.h, | |
otl = new Point(x1, y1), | |
otr = new Point(x2, y1), | |
obl = new Point(x1, y2), | |
obr = new Point(x2, y2); | |
for (var i = 0; i < glyphInstances.length; i++) { | |
var instance = glyphInstances[i], | |
tl = otl, | |
tr = otr, | |
bl = obl, | |
br = obr; | |
if (textRotate) { | |
var sin = Math.sin(textRotate), | |
cos = Math.cos(textRotate), | |
matrix = [cos, -sin, sin, cos]; | |
tl = tl.matMult(matrix); | |
tr = tr.matMult(matrix); | |
bl = bl.matMult(matrix); | |
br = br.matMult(matrix); | |
} | |
// Prevent label from extending past the end of the line | |
var glyphMinScale = Math.max(instance.minScale, labelMinScale); | |
var anchorAngle = (anchor.angle + instance.offset + 2 * Math.PI) % (2 * Math.PI); | |
var glyphAngle = (instance.angle + instance.offset + 2 * Math.PI) % (2 * Math.PI); | |
quads.push(new SymbolQuad(instance.anchorPoint, tl, tr, bl, br, rect, anchorAngle, glyphAngle, glyphMinScale, instance.maxScale)); | |
} | |
} | |
return quads; | |
} | |
/** | |
* We can only render glyph quads that slide along a straight line. To draw | |
* curved lines we need an instance of a glyph for each segment it appears on. | |
* This creates all the instances of a glyph that are necessary to render a label. | |
* | |
* We need a | |
* @param {Array<Object>} glyphInstances An empty array that glyphInstances are added to. | |
* @param {Anchor} anchor | |
* @param {number} offset The glyph's offset from the center of the label. | |
* @param {Array<Point>} line | |
* @param {number} segment The index of the segment of the line on which the anchor exists. | |
* @param {boolean} forward If true get the glyphs that come later on the line, otherwise get the glyphs that come earlier. | |
* | |
* @returns {Array<Object>} glyphInstances | |
* @private | |
*/ | |
function getSegmentGlyphs(glyphs, anchor, offset, line, segment, forward) { | |
var upsideDown = !forward; | |
if (offset < 0) forward = !forward; | |
if (forward) segment++; | |
var newAnchorPoint = new Point(anchor.x, anchor.y); | |
var end = line[segment]; | |
var prevScale = Infinity; | |
offset = Math.abs(offset); | |
var placementScale = minScale; | |
while (true) { | |
var distance = newAnchorPoint.dist(end); | |
var scale = offset / distance; | |
// Get the angle of the line segment | |
var angle = Math.atan2(end.y - newAnchorPoint.y, end.x - newAnchorPoint.x); | |
if (!forward) angle += Math.PI; | |
glyphs.push({ | |
anchorPoint: newAnchorPoint, | |
offset: upsideDown ? Math.PI : 0, | |
minScale: scale, | |
maxScale: prevScale, | |
angle: (angle + 2 * Math.PI) % (2 * Math.PI) | |
}); | |
if (scale <= placementScale) break; | |
newAnchorPoint = end; | |
// skip duplicate nodes | |
while (newAnchorPoint.equals(end)) { | |
segment += forward ? 1 : -1; | |
end = line[segment]; | |
if (!end) { | |
return scale; | |
} | |
} | |
var unit = end.sub(newAnchorPoint)._unit(); | |
newAnchorPoint = newAnchorPoint.sub(unit._mult(distance)); | |
prevScale = scale; | |
} | |
return placementScale; | |
} | |
},{"point-geometry":177}],69:[function(require,module,exports){ | |
'use strict'; | |
var resolveTokens = require('../util/token'); | |
module.exports = resolveText; | |
/** | |
* For an array of features determine what glyphs need to be loaded | |
* and apply any text preprocessing. The remaining users of text should | |
* use the `textFeatures` key returned by this function rather than accessing | |
* feature text directly. | |
* @private | |
*/ | |
function resolveText(features, layoutProperties, codepoints) { | |
var textFeatures = []; | |
for (var i = 0, fl = features.length; i < fl; i++) { | |
var text = resolveTokens(features[i].properties, layoutProperties['text-field']); | |
if (!text) { | |
textFeatures[i] = null; | |
continue; | |
} | |
text = text.toString(); | |
var transform = layoutProperties['text-transform']; | |
if (transform === 'uppercase') { | |
text = text.toLocaleUpperCase(); | |
} else if (transform === 'lowercase') { | |
text = text.toLocaleLowerCase(); | |
} | |
for (var j = 0; j < text.length; j++) { | |
codepoints[text.charCodeAt(j)] = true; | |
} | |
// Track indexes of features with text. | |
textFeatures[i] = text; | |
} | |
return textFeatures; | |
} | |
},{"../util/token":107}],70:[function(require,module,exports){ | |
'use strict'; | |
module.exports = { | |
shapeText: shapeText, | |
shapeIcon: shapeIcon | |
}; | |
// The position of a glyph relative to the text's anchor point. | |
function PositionedGlyph(codePoint, x, y, glyph) { | |
this.codePoint = codePoint; | |
this.x = x; | |
this.y = y; | |
this.glyph = glyph; | |
} | |
// A collection of positioned glyphs and some metadata | |
function Shaping(positionedGlyphs, text, top, bottom, left, right) { | |
this.positionedGlyphs = positionedGlyphs; | |
this.text = text; | |
this.top = top; | |
this.bottom = bottom; | |
this.left = left; | |
this.right = right; | |
} | |
function shapeText(text, glyphs, maxWidth, lineHeight, horizontalAlign, verticalAlign, justify, spacing, translate) { | |
var positionedGlyphs = []; | |
var shaping = new Shaping(positionedGlyphs, text, translate[1], translate[1], translate[0], translate[0]); | |
// the y offset *should* be part of the font metadata | |
var yOffset = -17; | |
var x = 0; | |
var y = yOffset; | |
for (var i = 0; i < text.length; i++) { | |
var codePoint = text.charCodeAt(i); | |
var glyph = glyphs[codePoint]; | |
if (!glyph) continue; | |
positionedGlyphs.push(new PositionedGlyph(codePoint, x, y, glyph)); | |
x += glyph.advance + spacing; | |
} | |
if (!positionedGlyphs.length) return false; | |
linewrap(shaping, glyphs, lineHeight, maxWidth, horizontalAlign, verticalAlign, justify, translate); | |
return shaping; | |
} | |
var invisible = { | |
0x20: true, // space | |
0x200b: true // zero-width space | |
}; | |
var breakable = { | |
0x20: true, // space | |
0x26: true, // ampersand | |
0x2b: true, // plus sign | |
0x2d: true, // hyphen-minus | |
0x2f: true, // solidus | |
0xad: true, // soft hyphen | |
0xb7: true, // middle dot | |
0x200b: true, // zero-width space | |
0x2010: true, // hyphen | |
0x2013: true // en dash | |
}; | |
function linewrap(shaping, glyphs, lineHeight, maxWidth, horizontalAlign, verticalAlign, justify, translate) { | |
var lastSafeBreak = null; | |
var lengthBeforeCurrentLine = 0; | |
var lineStartIndex = 0; | |
var line = 0; | |
var maxLineLength = 0; | |
var positionedGlyphs = shaping.positionedGlyphs; | |
if (maxWidth) { | |
for (var i = 0; i < positionedGlyphs.length; i++) { | |
var positionedGlyph = positionedGlyphs[i]; | |
positionedGlyph.x -= lengthBeforeCurrentLine; | |
positionedGlyph.y += lineHeight * line; | |
if (positionedGlyph.x > maxWidth && lastSafeBreak !== null) { | |
var lineLength = positionedGlyphs[lastSafeBreak + 1].x; | |
maxLineLength = Math.max(lineLength, maxLineLength); | |
for (var k = lastSafeBreak + 1; k <= i; k++) { | |
positionedGlyphs[k].y += lineHeight; | |
positionedGlyphs[k].x -= lineLength; | |
} | |
if (justify) { | |
// Collapse invisible characters. | |
var lineEnd = lastSafeBreak; | |
if (invisible[positionedGlyphs[lastSafeBreak].codePoint]) { | |
lineEnd--; | |
} | |
justifyLine(positionedGlyphs, glyphs, lineStartIndex, lineEnd, justify); | |
} | |
lineStartIndex = lastSafeBreak + 1; | |
lastSafeBreak = null; | |
lengthBeforeCurrentLine += lineLength; | |
line++; | |
} | |
if (breakable[positionedGlyph.codePoint]) { | |
lastSafeBreak = i; | |
} | |
} | |
} | |
var lastPositionedGlyph = positionedGlyphs[positionedGlyphs.length - 1]; | |
var lastLineLength = lastPositionedGlyph.x + glyphs[lastPositionedGlyph.codePoint].advance; | |
maxLineLength = Math.max(maxLineLength, lastLineLength); | |
var height = (line + 1) * lineHeight; | |
justifyLine(positionedGlyphs, glyphs, lineStartIndex, positionedGlyphs.length - 1, justify); | |
align(positionedGlyphs, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, line, translate); | |
// Calculate the bounding box | |
shaping.top += -verticalAlign * height; | |
shaping.bottom = shaping.top + height; | |
shaping.left += -horizontalAlign * maxLineLength; | |
shaping.right = shaping.left + maxLineLength; | |
} | |
function justifyLine(positionedGlyphs, glyphs, start, end, justify) { | |
var lastAdvance = glyphs[positionedGlyphs[end].codePoint].advance; | |
var lineIndent = (positionedGlyphs[end].x + lastAdvance) * justify; | |
for (var j = start; j <= end; j++) { | |
positionedGlyphs[j].x -= lineIndent; | |
} | |
} | |
function align(positionedGlyphs, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, line, translate) { | |
var shiftX = (justify - horizontalAlign) * maxLineLength + translate[0]; | |
var shiftY = (-verticalAlign * (line + 1) + 0.5) * lineHeight + translate[1]; | |
for (var j = 0; j < positionedGlyphs.length; j++) { | |
positionedGlyphs[j].x += shiftX; | |
positionedGlyphs[j].y += shiftY; | |
} | |
} | |
function shapeIcon(image, layout) { | |
if (!image || !image.rect) return null; | |
var dx = layout['icon-offset'][0]; | |
var dy = layout['icon-offset'][1]; | |
var x1 = dx - image.width / 2; | |
var x2 = x1 + image.width; | |
var y1 = dy - image.height / 2; | |
var y2 = y1 + image.height; | |
return new PositionedIcon(image, y1, y2, x1, x2); | |
} | |
function PositionedIcon(image, top, bottom, left, right) { | |
this.image = image; | |
this.top = top; | |
this.bottom = bottom; | |
this.left = left; | |
this.right = right; | |
} | |
},{}],71:[function(require,module,exports){ | |
'use strict'; | |
var ShelfPack = require('shelf-pack'); | |
var browser = require('../util/browser'); | |
var util = require('../util/util'); | |
module.exports = SpriteAtlas; | |
function SpriteAtlas(width, height) { | |
this.width = width; | |
this.height = height; | |
this.bin = new ShelfPack(width, height); | |
this.images = {}; | |
this.data = false; | |
this.texture = 0; // WebGL ID | |
this.filter = 0; // WebGL ID | |
this.pixelRatio = 1; | |
this.dirty = true; | |
} | |
function copyBitmap(src, srcStride, srcX, srcY, dst, dstStride, dstX, dstY, width, height, wrap) { | |
var srcI = srcY * srcStride + srcX; | |
var dstI = dstY * dstStride + dstX; | |
var x, y; | |
if (wrap) { | |
// add 1 pixel wrapped padding on each side of the image | |
dstI -= dstStride; | |
for (y = -1; y <= height; y++, srcI = ((y + height) % height + srcY) * srcStride + srcX, dstI += dstStride) { | |
for (x = -1; x <= width; x++) { | |
dst[dstI + x] = src[srcI + ((x + width) % width)]; | |
} | |
} | |
} else { | |
for (y = 0; y < height; y++, srcI += srcStride, dstI += dstStride) { | |
for (x = 0; x < width; x++) { | |
dst[dstI + x] = src[srcI + x]; | |
} | |
} | |
} | |
} | |
SpriteAtlas.prototype.allocateImage = function(pixelWidth, pixelHeight) { | |
pixelWidth = pixelWidth / this.pixelRatio; | |
pixelHeight = pixelHeight / this.pixelRatio; | |
// Increase to next number divisible by 4, but at least 1. | |
// This is so we can scale down the texture coordinates and pack them | |
// into 2 bytes rather than 4 bytes. | |
// Pad icons to prevent them from polluting neighbours during linear interpolation | |
var padding = 2; | |
var packWidth = pixelWidth + padding + (4 - (pixelWidth + padding) % 4); | |
var packHeight = pixelHeight + padding + (4 - (pixelHeight + padding) % 4);// + 4; | |
var rect = this.bin.packOne(packWidth, packHeight); | |
if (!rect) { | |
util.warnOnce('SpriteAtlas out of space.'); | |
return null; | |
} | |
return rect; | |
}; | |
SpriteAtlas.prototype.getImage = function(name, wrap) { | |
if (this.images[name]) { | |
return this.images[name]; | |
} | |
if (!this.sprite) { | |
return null; | |
} | |
var pos = this.sprite.getSpritePosition(name); | |
if (!pos.width || !pos.height) { | |
return null; | |
} | |
var rect = this.allocateImage(pos.width, pos.height); | |
if (!rect) { | |
return null; | |
} | |
var image = new AtlasImage(rect, pos.width / pos.pixelRatio, pos.height / pos.pixelRatio, pos.sdf, pos.pixelRatio / this.pixelRatio); | |
this.images[name] = image; | |
this.copy(rect, pos, wrap); | |
return image; | |
}; | |
// Return position of a repeating fill pattern. | |
SpriteAtlas.prototype.getPosition = function(name, repeating) { | |
var image = this.getImage(name, repeating); | |
var rect = image && image.rect; | |
if (!rect) { | |
return null; | |
} | |
var width = image.width * image.pixelRatio; | |
var height = image.height * image.pixelRatio; | |
var padding = 1; | |
return { | |
size: [image.width, image.height], | |
tl: [(rect.x + padding) / this.width, (rect.y + padding) / this.height], | |
br: [(rect.x + padding + width) / this.width, (rect.y + padding + height) / this.height] | |
}; | |
}; | |
SpriteAtlas.prototype.allocate = function() { | |
if (!this.data) { | |
var w = Math.floor(this.width * this.pixelRatio); | |
var h = Math.floor(this.height * this.pixelRatio); | |
this.data = new Uint32Array(w * h); | |
for (var i = 0; i < this.data.length; i++) { | |
this.data[i] = 0; | |
} | |
} | |
}; | |
SpriteAtlas.prototype.copy = function(dst, src, wrap) { | |
if (!this.sprite.img.data) return; | |
var srcImg = new Uint32Array(this.sprite.img.data.buffer); | |
this.allocate(); | |
var dstImg = this.data; | |
var padding = 1; | |
copyBitmap( | |
/* source buffer */ srcImg, | |
/* source stride */ this.sprite.img.width, | |
/* source x */ src.x, | |
/* source y */ src.y, | |
/* dest buffer */ dstImg, | |
/* dest stride */ this.width * this.pixelRatio, | |
/* dest x */ (dst.x + padding) * this.pixelRatio, | |
/* dest y */ (dst.y + padding) * this.pixelRatio, | |
/* icon dimension */ src.width, | |
/* icon dimension */ src.height, | |
/* wrap */ wrap | |
); | |
this.dirty = true; | |
}; | |
SpriteAtlas.prototype.setSprite = function(sprite) { | |
if (sprite) { | |
this.pixelRatio = browser.devicePixelRatio > 1 ? 2 : 1; | |
if (this.canvas) { | |
this.canvas.width = this.width * this.pixelRatio; | |
this.canvas.height = this.height * this.pixelRatio; | |
} | |
} | |
this.sprite = sprite; | |
}; | |
SpriteAtlas.prototype.addIcons = function(icons, callback) { | |
for (var i = 0; i < icons.length; i++) { | |
this.getImage(icons[i]); | |
} | |
callback(null, this.images); | |
}; | |
SpriteAtlas.prototype.bind = function(gl, linear) { | |
var first = false; | |
if (!this.texture) { | |
this.texture = gl.createTexture(); | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | |
first = true; | |
} else { | |
gl.bindTexture(gl.TEXTURE_2D, this.texture); | |
} | |
var filterVal = linear ? gl.LINEAR : gl.NEAREST; | |
if (filterVal !== this.filter) { | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filterVal); | |
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filterVal); | |
this.filter = filterVal; | |
} | |
if (this.dirty) { | |
this.allocate(); | |
if (first) { | |
gl.texImage2D( | |
gl.TEXTURE_2D, // enum target | |
0, // ind level | |
gl.RGBA, // ind internalformat | |
this.width * this.pixelRatio, // GLsizei width | |
this.height * this.pixelRatio, // GLsizei height | |
0, // ind border | |
gl.RGBA, // enum format | |
gl.UNSIGNED_BYTE, // enum type | |
new Uint8Array(this.data.buffer) // Object data | |
); | |
} else { | |
gl.texSubImage2D( | |
gl.TEXTURE_2D, // enum target | |
0, // int level | |
0, // int xoffset | |
0, // int yoffset | |
this.width * this.pixelRatio, // long width | |
this.height * this.pixelRatio, // long height | |
gl.RGBA, // enum format | |
gl.UNSIGNED_BYTE, // enum type | |
new Uint8Array(this.data.buffer) // Object pixels | |
); | |
} | |
this.dirty = false; | |
} | |
}; | |
function AtlasImage(rect, width, height, sdf, pixelRatio) { | |
this.rect = rect; | |
this.width = width; | |
this.height = height; | |
this.sdf = sdf; | |
this.pixelRatio = pixelRatio; | |
} | |
},{"../util/browser":93,"../util/util":108,"shelf-pack":180}],72:[function(require,module,exports){ | |
'use strict'; | |
var StructArrayType = require('../util/struct_array'); | |
var util = require('../util/util'); | |
var Point = require('point-geometry'); | |
/* | |
* | |
* A StructArray implementation of symbolInstances from data/bucket/symbol_bucket.js | |
* this will allow symbolInstances to be transferred between the worker and main threads | |
* | |
* @class SymbolInstanceArray | |
* @private | |
*/ | |
var SymbolInstancesArray = module.exports = new StructArrayType({ | |
members: [ | |
{ type: 'Uint16', name: 'textBoxStartIndex' }, | |
{ type: 'Uint16', name: 'textBoxEndIndex' }, | |
{ type: 'Uint16', name: 'iconBoxStartIndex' }, | |
{ type: 'Uint16', name: 'iconBoxEndIndex' }, | |
{ type: 'Uint16', name: 'glyphQuadStartIndex' }, | |
{ type: 'Uint16', name: 'glyphQuadEndIndex' }, | |
{ type: 'Uint16', name: 'iconQuadStartIndex' }, | |
{ type: 'Uint16', name: 'iconQuadEndIndex' }, | |
// each symbolInstance is centered around the anchor point | |
{ type: 'Int16', name: 'anchorPointX' }, | |
{ type: 'Int16', name: 'anchorPointY' }, | |
// index -- not sure if we need this -@mollymerp | |
{ type: 'Int8', name: 'index' } | |
] | |
}); | |
util.extendAll(SymbolInstancesArray.prototype.StructType.prototype, { | |
get anchorPoint() { | |
return new Point(this.anchorPointX, this.anchorPointY); | |
} | |
}); | |
},{"../util/struct_array":106,"../util/util":108,"point-geometry":177}],73:[function(require,module,exports){ | |
'use strict'; | |
var StructArrayType = require('../util/struct_array'); | |
var util = require('../util/util'); | |
var Point = require('point-geometry'); | |
var SymbolQuad = require('./quads').SymbolQuad; | |
// notes from ansis on slack: | |
// it would be best if they are added to a buffer in advance so that they are only created once. There would be a separate buffer with all the individual collision boxes and then SymbolInstance would store the beginning and end indexes of a feature's collisionboxes. CollisionFeature wouldn't really exist as a standalone thing, it would just be a range of boxes in the big collision box buffer | |
/* | |
* | |
* A StructArray implementation of glyphQuad from symbol/quads | |
* this will allow glyph quads to be transferred between the worker and main threads along with the rest of | |
* the symbolInstances | |
* | |
* @class SymbolQuadsArray | |
* @private | |
*/ | |
var SymbolQuadsArray = module.exports = new StructArrayType({ | |
members: [ | |
// the quad is centered around the anchor point | |
{ type: 'Int16', name: 'anchorPointX' }, | |
{ type: 'Int16', name: 'anchorPointY' }, | |
// the offsets of the tl (top-left), tr, bl, br corners from the anchor point | |
// do these need to be floats? | |
{ type: 'Float32', name: 'tlX' }, | |
{ type: 'Float32', name: 'tlY' }, | |
{ type: 'Float32', name: 'trX' }, | |
{ type: 'Float32', name: 'trY' }, | |
{ type: 'Float32', name: 'blX' }, | |
{ type: 'Float32', name: 'blY' }, | |
{ type: 'Float32', name: 'brX' }, | |
{ type: 'Float32', name: 'brY' }, | |
// texture coordinates (height, width, x, and y) | |
{ type: 'Int16', name: 'texH' }, | |
{ type: 'Int16', name: 'texW' }, | |
{ type: 'Int16', name: 'texX' }, | |
{ type: 'Int16', name: 'texY' }, | |
// the angle of the label at it's center, not the angle of this quad. | |
{ type: 'Float32', name: 'anchorAngle' }, | |
// the angle of this quad. | |
{ type: 'Float32', name: 'glyphAngle' }, | |
// quad is only valid for scales < maxScale && scale > minScale. | |
{ type: 'Float32', name: 'maxScale' }, | |
{ type: 'Float32', name: 'minScale' } | |
] | |
}); | |
util.extendAll(SymbolQuadsArray.prototype.StructType.prototype, { | |
get anchorPoint() { | |
return new Point(this.anchorPointX, this.anchorPointY); | |
}, | |
get SymbolQuad() { | |
return new SymbolQuad(this.anchorPoint, | |
new Point(this.tlX, this.tlY), | |
new Point(this.trX, this.trY), | |
new Point(this.blX, this.blY), | |
new Point(this.brX, this.brY), | |
{ x: this.texX, y: this.texY, h: this.texH, w: this.texW, height: this.texH, width: this.texW }, | |
this.anchorAngle, | |
this.glyphAngle, | |
this.minScale, | |
this.maxScale); | |
} | |
}); | |
},{"../util/struct_array":106,"../util/util":108,"./quads":68,"point-geometry":177}],74:[function(require,module,exports){ | |
'use strict'; | |
var DOM = require('../util/dom'); | |
var Point = require('point-geometry'); | |
var handlers = { | |
scrollZoom: require('./handler/scroll_zoom'), | |
boxZoom: require('./handler/box_zoom'), | |
dragRotate: require('./handler/drag_rotate'), | |
dragPan: require('./handler/drag_pan'), | |
keyboard: require('./handler/keyboard'), | |
doubleClickZoom: require('./handler/dblclick_zoom'), | |
touchZoomRotate: require('./handler/touch_zoom_rotate') | |
}; | |
module.exports = function bindHandlers(map, options) { | |
var el = map.getCanvasContainer(); | |
var contextMenuEvent = null; | |
var startPos = null; | |
var tapped = null; | |
for (var name in handlers) { | |
map[name] = new handlers[name](map, options); | |
if (options.interactive && options[name]) { | |
map[name].enable(); | |
} | |
} | |
el.addEventListener('mouseout', onMouseOut, false); | |
el.addEventListener('mousedown', onMouseDown, false); | |
el.addEventListener('mouseup', onMouseUp, false); | |
el.addEventListener('mousemove', onMouseMove, false); | |
el.addEventListener('touchstart', onTouchStart, false); | |
el.addEventListener('touchend', onTouchEnd, false); | |
el.addEventListener('touchmove', onTouchMove, false); | |
el.addEventListener('touchcancel', onTouchCancel, false); | |
el.addEventListener('click', onClick, false); | |
el.addEventListener('dblclick', onDblClick, false); | |
el.addEventListener('contextmenu', onContextMenu, false); | |
function onMouseOut(e) { | |
fireMouseEvent('mouseout', e); | |
} | |
function onMouseDown(e) { | |
map.stop(); | |
startPos = DOM.mousePos(el, e); | |
fireMouseEvent('mousedown', e); | |
} | |
function onMouseUp(e) { | |
var rotating = map.dragRotate && map.dragRotate.isActive(); | |
if (contextMenuEvent && !rotating) { | |
fireMouseEvent('contextmenu', contextMenuEvent); | |
} | |
contextMenuEvent = null; | |
fireMouseEvent('mouseup', e); | |
} | |
function onMouseMove(e) { | |
if (map.dragPan && map.dragPan.isActive()) return; | |
if (map.dragRotate && map.dragRotate.isActive()) return; | |
var target = e.toElement || e.target; | |
while (target && target !== el) target = target.parentNode; | |
if (target !== el) return; | |
fireMouseEvent('mousemove', e); | |
} | |
function onTouchStart(e) { | |
map.stop(); | |
fireTouchEvent('touchstart', e); | |
if (!e.touches || e.touches.length > 1) return; | |
if (!tapped) { | |
tapped = setTimeout(onTouchTimeout, 300); | |
} else { | |
clearTimeout(tapped); | |
tapped = null; | |
fireMouseEvent('dblclick', e); | |
} | |
} | |
function onTouchMove(e) { | |
fireTouchEvent('touchmove', e); | |
} | |
function onTouchEnd(e) { | |
fireTouchEvent('touchend', e); | |
} | |
function onTouchCancel(e) { | |
fireTouchEvent('touchcancel', e); | |
} | |
function onTouchTimeout() { | |
tapped = null; | |
} | |
function onClick(e) { | |
var pos = DOM.mousePos(el, e); | |
if (pos.equals(startPos)) { | |
fireMouseEvent('click', e); | |
} | |
} | |
function onDblClick(e) { | |
fireMouseEvent('dblclick', e); | |
e.preventDefault(); | |
} | |
function onContextMenu(e) { | |
contextMenuEvent = e; | |
e.preventDefault(); | |
} | |
function fireMouseEvent(type, e) { | |
var pos = DOM.mousePos(el, e); | |
return map.fire(type, { | |
lngLat: map.unproject(pos), | |
point: pos, | |
originalEvent: e | |
}); | |
} | |
function fireTouchEvent(type, e) { | |
var touches = DOM.touchPos(el, e); | |
var singular = touches.reduce(function(prev, curr, i, arr) { | |
return prev.add(curr.div(arr.length)); | |
}, new Point(0, 0)); | |
return map.fire(type, { | |
lngLat: map.unproject(singular), | |
point: singular, | |
lngLats: touches.map(function(t) { return map.unproject(t); }, this), | |
points: touches, | |
originalEvent: e | |
}); | |
} | |
}; | |
/** | |
* @typedef {Object} MapMouseEvent | |
* @property {string} type The event type. | |
* @property {Map} target The `Map` object that fired the event. | |
* @property {MouseEvent} originalEvent | |
* @property {Point} point The pixel coordinates of the mouse event target, relative to the map | |
* and measured from the top left corner. | |
* @property {LngLat} lngLat The geographic location on the map of the mouse event target. | |
*/ | |
/** | |
* @typedef {Object} MapTouchEvent | |
* @property {string} type The event type. | |
* @property {Map} target The `Map` object that fired the event. | |
* @property {TouchEvent} originalEvent | |
* @property {Point} point The pixel coordinates of the center of the touch event points, relative to the map | |
* and measured from the top left corner. | |
* @property {LngLat} lngLat The geographic location on the map of the center of the touch event points. | |
* @property {Array<Point>} points The array of pixel coordinates corresponding to | |
* a [touch event's `touches`](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/touches) | |
* property. | |
* @property {Array<LngLat>} lngLats The geographical locations on the map corresponding to | |
* a [touch event's `touches`](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/touches) | |
* property. | |
*/ | |
},{"../util/dom":96,"./handler/box_zoom":80,"./handler/dblclick_zoom":81,"./handler/drag_pan":82,"./handler/drag_rotate":83,"./handler/keyboard":84,"./handler/scroll_zoom":85,"./handler/touch_zoom_rotate":86,"point-geometry":177}],75:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util/util'); | |
var interpolate = require('../util/interpolate'); | |
var browser = require('../util/browser'); | |
var LngLat = require('../geo/lng_lat'); | |
var LngLatBounds = require('../geo/lng_lat_bounds'); | |
var Point = require('point-geometry'); | |
/** | |
* Options common to {@link Map#jumpTo}, {@link Map#easeTo}, and {@link Map#flyTo}, | |
* controlling the destination's location, zoom level, bearing, and pitch. | |
* All properties are optional. Unspecified | |
* options will default to the map's current value for that property. | |
* | |
* @typedef {Object} CameraOptions | |
* @property {LngLatLike} center The destination's center. | |
* @property {number} zoom The destination's zoom level. | |
* @property {number} bearing The destination's bearing (rotation), measured in degrees counter-clockwise from north. | |
* @property {number} pitch The destination's pitch (tilt), measured in degrees. | |
* @property {LngLatLike} around If a `zoom` is specified, `around` determines the zoom center (defaults to the center of the map). | |
*/ | |
/** | |
* Options common to map movement methods that involve animation, such as {@link Map#panBy} and | |
* {@link Map#easeTo}, controlling the duration and easing function of the animation. All properties | |
* are optional. | |
* | |
* @typedef {Object} AnimationOptions | |
* @property {number} duration The animation's duration, measured in milliseconds. | |
* @property {Function} easing The animation's easing function. | |
* @property {PointLike} offset `x` and `y` coordinates representing the animation's origin of movement relative to the map's center. | |
* @property {boolean} animate If `false`, no animation will occur. | |
*/ | |
var Camera = module.exports = function() {}; | |
util.extend(Camera.prototype, /** @lends Map.prototype */{ | |
/** | |
* Returns the map's geographical centerpoint. | |
* | |
* @returns {LngLat} The map's geographical centerpoint. | |
*/ | |
getCenter: function() { return this.transform.center; }, | |
/** | |
* Sets the map's geographical centerpoint. Equivalent to `jumpTo({center: center})`. | |
* | |
* @param {LngLatLike} center The centerpoint to set. | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
* @example | |
* map.setCenter([-74, 38]); | |
*/ | |
setCenter: function(center, eventData) { | |
this.jumpTo({center: center}, eventData); | |
return this; | |
}, | |
/** | |
* Pans the map by the specified offest. | |
* | |
* @param {Array<number>} offset `x` and `y` coordinates by which to pan the map. | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
panBy: function(offset, options, eventData) { | |
this.panTo(this.transform.center, | |
util.extend({offset: Point.convert(offset).mult(-1)}, options), eventData); | |
return this; | |
}, | |
/** | |
* Pans the map to the specified location, with an animated transition. | |
* | |
* @param {LngLatLike} lnglat The location to pan the map to. | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
panTo: function(lnglat, options, eventData) { | |
return this.easeTo(util.extend({ | |
center: lnglat | |
}, options), eventData); | |
}, | |
/** | |
* Returns the map's current zoom level. | |
* | |
* @returns {number} The map's current zoom level. | |
*/ | |
getZoom: function() { return this.transform.zoom; }, | |
/** | |
* Sets the map's zoom level. Equivalent to `jumpTo({zoom: zoom})`. | |
* | |
* @param {number} zoom The zoom level to set (0-20). | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires zoomstart | |
* @fires move | |
* @fires zoom | |
* @fires moveend | |
* @fires zoomend | |
* @returns {Map} `this` | |
* @example | |
* // zoom the map to 5 | |
* map.setZoom(5); | |
*/ | |
setZoom: function(zoom, eventData) { | |
this.jumpTo({zoom: zoom}, eventData); | |
return this; | |
}, | |
/** | |
* Zooms the map to the specified zoom level, with an animated transition. | |
* | |
* @param {number} zoom The zoom level to transition to. | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires zoomstart | |
* @fires move | |
* @fires zoom | |
* @fires moveend | |
* @fires zoomend | |
* @returns {Map} `this` | |
*/ | |
zoomTo: function(zoom, options, eventData) { | |
return this.easeTo(util.extend({ | |
zoom: zoom | |
}, options), eventData); | |
}, | |
/** | |
* Increases the map's zoom level by 1. | |
* | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires zoomstart | |
* @fires move | |
* @fires zoom | |
* @fires moveend | |
* @fires zoomend | |
* @returns {Map} `this` | |
*/ | |
zoomIn: function(options, eventData) { | |
this.zoomTo(this.getZoom() + 1, options, eventData); | |
return this; | |
}, | |
/** | |
* Decreases the map's zoom level by 1. | |
* | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires zoomstart | |
* @fires move | |
* @fires zoom | |
* @fires moveend | |
* @fires zoomend | |
* @returns {Map} `this` | |
*/ | |
zoomOut: function(options, eventData) { | |
this.zoomTo(this.getZoom() - 1, options, eventData); | |
return this; | |
}, | |
/** | |
* Returns the map's current bearing (rotation). | |
* | |
* @returns {number} The map's current bearing, measured in degrees counter-clockwise from north. | |
*/ | |
getBearing: function() { return this.transform.bearing; }, | |
/** | |
* Sets the maps' bearing (rotation). Equivalent to `jumpTo({bearing: bearing})`. | |
* | |
* @param {number} bearing The bearing to set, measured in degrees counter-clockwise from north. | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
* @example | |
* // rotate the map to 90 degrees | |
* map.setBearing(90); | |
*/ | |
setBearing: function(bearing, eventData) { | |
this.jumpTo({bearing: bearing}, eventData); | |
return this; | |
}, | |
/** | |
* Rotates the map to the specified bearing, with an animated transition. | |
* | |
* @param {number} bearing The bearing to rotate the map to, measured in degrees counter-clockwise from north. | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
rotateTo: function(bearing, options, eventData) { | |
return this.easeTo(util.extend({ | |
bearing: bearing | |
}, options), eventData); | |
}, | |
/** | |
* Rotates the map to a bearing of 0 (due north), with an animated transition. | |
* | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
resetNorth: function(options, eventData) { | |
this.rotateTo(0, util.extend({duration: 1000}, options), eventData); | |
return this; | |
}, | |
/** | |
* Snaps the map's bearing to 0 (due north), if the current bearing is close enough to it (i.e. within the `bearingSnap` threshold). | |
* | |
* @param {AnimationOptions} [options] | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
snapToNorth: function(options, eventData) { | |
if (Math.abs(this.getBearing()) < this._bearingSnap) { | |
return this.resetNorth(options, eventData); | |
} | |
return this; | |
}, | |
/** | |
* Returns the map's current pitch (tilt). | |
* | |
* @returns {number} The map's current pitch, measured in degrees away from the plane of the screen. | |
*/ | |
getPitch: function() { return this.transform.pitch; }, | |
/** | |
* Sets the map's pitch (tilt). Equivalent to `jumpTo({pitch: pitch})`. | |
* | |
* @param {number} pitch The pitch to set, measured in degrees away from the plane of the screen (0-60). | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
setPitch: function(pitch, eventData) { | |
this.jumpTo({pitch: pitch}, eventData); | |
return this; | |
}, | |
/** | |
* Pans and zooms the map to contain its visible area within the specified geographical bounds. | |
* | |
* @param {LngLatBoundsLike} bounds The bounds to fit the visible area into. | |
* @param {Object} [options] | |
* @param {boolean} [options.linear=false] If `true`, the map transitions using | |
* {@link Map#easeTo}. If `false`, the map transitions using {@link Map#flyTo}. See | |
* {@link Map#flyTo} for information about the options specific to that animated transition. | |
* @param {Function} [options.easing] An easing function for the animated transition. | |
* @param {number} [options.padding=0] The amount of padding, in pixels, to allow around the specified bounds. | |
* @param {PointLike} [options.offset=[0, 0]] The center of the given bounds relative to the map's center, measured in pixels. | |
* @param {number} [options.maxZoom] The maximum zoom level to allow when the map view transitions to the specified bounds. | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
fitBounds: function(bounds, options, eventData) { | |
options = util.extend({ | |
padding: 0, | |
offset: [0, 0], | |
maxZoom: Infinity | |
}, options); | |
bounds = LngLatBounds.convert(bounds); | |
var offset = Point.convert(options.offset), | |
tr = this.transform, | |
nw = tr.project(bounds.getNorthWest()), | |
se = tr.project(bounds.getSouthEast()), | |
size = se.sub(nw), | |
scaleX = (tr.width - options.padding * 2 - Math.abs(offset.x) * 2) / size.x, | |
scaleY = (tr.height - options.padding * 2 - Math.abs(offset.y) * 2) / size.y; | |
options.center = tr.unproject(nw.add(se).div(2)); | |
options.zoom = Math.min(tr.scaleZoom(tr.scale * Math.min(scaleX, scaleY)), options.maxZoom); | |
options.bearing = 0; | |
return options.linear ? | |
this.easeTo(options, eventData) : | |
this.flyTo(options, eventData); | |
}, | |
/** | |
* Changes any combination of center, zoom, bearing, and pitch, without | |
* an animated transition. The map will retain its current values for any | |
* details not specified in `options`. | |
* | |
* @param {CameraOptions} options | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires zoomstart | |
* @fires move | |
* @fires zoom | |
* @fires rotate | |
* @fires pitch | |
* @fires zoomend | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
jumpTo: function(options, eventData) { | |
this.stop(); | |
var tr = this.transform, | |
zoomChanged = false, | |
bearingChanged = false, | |
pitchChanged = false; | |
if ('zoom' in options && tr.zoom !== +options.zoom) { | |
zoomChanged = true; | |
tr.zoom = +options.zoom; | |
} | |
if ('center' in options) { | |
tr.center = LngLat.convert(options.center); | |
} | |
if ('bearing' in options && tr.bearing !== +options.bearing) { | |
bearingChanged = true; | |
tr.bearing = +options.bearing; | |
} | |
if ('pitch' in options && tr.pitch !== +options.pitch) { | |
pitchChanged = true; | |
tr.pitch = +options.pitch; | |
} | |
this.fire('movestart', eventData) | |
.fire('move', eventData); | |
if (zoomChanged) { | |
this.fire('zoomstart', eventData) | |
.fire('zoom', eventData) | |
.fire('zoomend', eventData); | |
} | |
if (bearingChanged) { | |
this.fire('rotate', eventData); | |
} | |
if (pitchChanged) { | |
this.fire('pitch', eventData); | |
} | |
return this.fire('moveend', eventData); | |
}, | |
/** | |
* Changes any combination of center, zoom, bearing, and pitch, with an animated transition | |
* between old and new values. The map will retain its current values for any | |
* details not specified in `options`. | |
* | |
* @param {CameraOptions|AnimationOptions} options Options describing the destination and animation of the transition. | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires zoomstart | |
* @fires move | |
* @fires zoom | |
* @fires rotate | |
* @fires pitch | |
* @fires zoomend | |
* @fires moveend | |
* @returns {Map} `this` | |
*/ | |
easeTo: function(options, eventData) { | |
this.stop(); | |
options = util.extend({ | |
offset: [0, 0], | |
duration: 500, | |
easing: util.ease | |
}, options); | |
var tr = this.transform, | |
offset = Point.convert(options.offset), | |
startZoom = this.getZoom(), | |
startBearing = this.getBearing(), | |
startPitch = this.getPitch(), | |
zoom = 'zoom' in options ? +options.zoom : startZoom, | |
bearing = 'bearing' in options ? this._normalizeBearing(options.bearing, startBearing) : startBearing, | |
pitch = 'pitch' in options ? +options.pitch : startPitch, | |
toLngLat, | |
toPoint; | |
if ('center' in options) { | |
toLngLat = LngLat.convert(options.center); | |
toPoint = tr.centerPoint.add(offset); | |
} else if ('around' in options) { | |
toLngLat = LngLat.convert(options.around); | |
toPoint = tr.locationPoint(toLngLat); | |
} else { | |
toPoint = tr.centerPoint.add(offset); | |
toLngLat = tr.pointLocation(toPoint); | |
} | |
var fromPoint = tr.locationPoint(toLngLat); | |
if (options.animate === false) options.duration = 0; | |
this.zooming = (zoom !== startZoom); | |
this.rotating = (startBearing !== bearing); | |
this.pitching = (pitch !== startPitch); | |
if (!options.noMoveStart) { | |
this.fire('movestart', eventData); | |
} | |
if (this.zooming) { | |
this.fire('zoomstart', eventData); | |
} | |
clearTimeout(this._onEaseEnd); | |
this._ease(function (k) { | |
if (this.zooming) { | |
tr.zoom = interpolate(startZoom, zoom, k); | |
} | |
if (this.rotating) { | |
tr.bearing = interpolate(startBearing, bearing, k); | |
} | |
if (this.pitching) { | |
tr.pitch = interpolate(startPitch, pitch, k); | |
} | |
tr.setLocationAtPoint(toLngLat, fromPoint.add(toPoint.sub(fromPoint)._mult(k))); | |
this.fire('move', eventData); | |
if (this.zooming) { | |
this.fire('zoom', eventData); | |
} | |
if (this.rotating) { | |
this.fire('rotate', eventData); | |
} | |
if (this.pitching) { | |
this.fire('pitch', eventData); | |
} | |
}, function() { | |
if (options.delayEndEvents) { | |
this._onEaseEnd = setTimeout(this._easeToEnd.bind(this, eventData), options.delayEndEvents); | |
} else { | |
this._easeToEnd(eventData); | |
} | |
}.bind(this), options); | |
return this; | |
}, | |
_easeToEnd: function(eventData) { | |
if (this.zooming) { | |
this.fire('zoomend', eventData); | |
} | |
this.fire('moveend', eventData); | |
this.zooming = false; | |
this.rotating = false; | |
this.pitching = false; | |
}, | |
/** | |
* Changes any combination of center, zoom, bearing, and pitch, animating the transition along a curve that | |
* evokes flight. The animation seamlessly incorporates zooming and panning to help | |
* the user maintain her bearings even after traversing a great distance. | |
* | |
* @param {Object} options Options describing the destination and animation of the transition. | |
* Accepts [CameraOptions](#CameraOptions), [AnimationOptions](#AnimationOptions), | |
* and the following additional options. | |
* @param {number} [options.curve=1.42] The zooming "curve" that will occur along the | |
* flight path. A high value maximizes zooming for an exaggerated animation, while a low | |
* value minimizes zooming for an effect closer to {@link Map#easeTo}. 1.42 is the average | |
* value selected by participants in the user study discussed in | |
* [van Wijk (2003)](https://www.win.tue.nl/~vanwijk/zoompan.pdf). A value of | |
* `Math.pow(6, 0.25)` would be equivalent to the root mean squared average velocity. A | |
* value of 1 would produce a circular motion. | |
* @param {number} [options.minZoom] The zero-based zoom level at the peak of the flight path. If | |
* `options.curve` is specified, this option is ignored. | |
* @param {number} [options.speed=1.2] The average speed of the animation defined in relation to | |
* `options.curve`. A speed of 1.2 means that the map appears to move along the flight path | |
* by 1.2 times `options.curve` screenfuls every second. A _screenful_ is the map's visible span. | |
* It does not correspond to a fixed physical distance, but varies by zoom level. | |
* @param {number} [options.screenSpeed] The average speed of the animation measured in screenfuls | |
* per second, assuming a linear timing curve. If `options.speed` is specified, this option is ignored. | |
* @param {Function} [options.easing] An easing function for the animated transition. | |
* @param {Object} [eventData] Data to propagate to any event listeners. | |
* @fires movestart | |
* @fires zoomstart | |
* @fires move | |
* @fires zoom | |
* @fires rotate | |
* @fires pitch | |
* @fires zoomend | |
* @fires moveend | |
* @returns {Map} `this` | |
* @example | |
* // fly with default options to null island | |
* map.flyTo({center: [0, 0], zoom: 9}); | |
* // using flyTo options | |
* map.flyTo({ | |
* center: [0, 0], | |
* zoom: 9, | |
* speed: 0.2, | |
* curve: 1, | |
* easing: function(t) { | |
* return t; | |
* } | |
* }); | |
*/ | |
flyTo: function(options, eventData) { | |
// This method implements an “optimal path” animation, as detailed in: | |
// | |
// Van Wijk, Jarke J.; Nuij, Wim A. A. “Smooth and efficient zooming and panning.” INFOVIS | |
// ’03. pp. 15–22. <https://www.win.tue.nl/~vanwijk/zoompan.pdf#page=5>. | |
// | |
// Where applicable, local variable documentation begins with the associated variable or | |
// function in van Wijk (2003). | |
this.stop(); | |
options = util.extend({ | |
offset: [0, 0], | |
speed: 1.2, | |
curve: 1.42, | |
easing: util.ease | |
}, options); | |
var tr = this.transform, | |
offset = Point.convert(options.offset), | |
startZoom = this.getZoom(), | |
startBearing = this.getBearing(), | |
startPitch = this.getPitch(); | |
var center = 'center' in options ? LngLat.convert(options.center) : this.getCenter(); | |
var zoom = 'zoom' in options ? +options.zoom : startZoom; | |
var bearing = 'bearing' in options ? this._normalizeBearing(options.bearing, startBearing) : startBearing; | |
var pitch = 'pitch' in options ? +options.pitch : startPitch; | |
// If a path crossing the antimeridian would be shorter, extend the final coordinate so that | |
// interpolating between the two endpoints will cross it. | |
if (Math.abs(tr.center.lng) + Math.abs(center.lng) > 180) { | |
if (tr.center.lng > 0 && center.lng < 0) { | |
center.lng += 360; | |
} else if (tr.center.lng < 0 && center.lng > 0) { | |
center.lng -= 360; | |
} | |
} | |
var scale = tr.zoomScale(zoom - startZoom), | |
from = tr.point, | |
to = 'center' in options ? tr.project(center).sub(offset.div(scale)) : from; | |
var startWorldSize = tr.worldSize, | |
rho = options.curve, | |
// w₀: Initial visible span, measured in pixels at the initial scale. | |
w0 = Math.max(tr.width, tr.height), | |
// w₁: Final visible span, measured in pixels with respect to the initial scale. | |
w1 = w0 / scale, | |
// Length of the flight path as projected onto the ground plane, measured in pixels from | |
// the world image origin at the initial scale. | |
u1 = to.sub(from).mag(); | |
if ('minZoom' in options) { | |
var minZoom = util.clamp(Math.min(options.minZoom, startZoom, zoom), tr.minZoom, tr.maxZoom); | |
// w<sub>m</sub>: Maximum visible span, measured in pixels with respect to the initial | |
// scale. | |
var wMax = w0 / tr.zoomScale(minZoom - startZoom); | |
rho = Math.sqrt(wMax / u1 * 2); | |
} | |
// ρ² | |
var rho2 = rho * rho; | |
/** | |
* rᵢ: Returns the zoom-out factor at one end of the animation. | |
* | |
* @param i 0 for the ascent or 1 for the descent. | |
* @private | |
*/ | |
function r(i) { | |
var b = (w1 * w1 - w0 * w0 + (i ? -1 : 1) * rho2 * rho2 * u1 * u1) / (2 * (i ? w1 : w0) * rho2 * u1); | |
return Math.log(Math.sqrt(b * b + 1) - b); | |
} | |
function sinh(n) { return (Math.exp(n) - Math.exp(-n)) / 2; } | |
function cosh(n) { return (Math.exp(n) + Math.exp(-n)) / 2; } | |
function tanh(n) { return sinh(n) / cosh(n); } | |
// r₀: Zoom-out factor during ascent. | |
var r0 = r(0), | |
/** | |
* w(s): Returns the visible span on the ground, measured in pixels with respect to the | |
* initial scale. | |
* | |
* Assumes an angular field of view of 2 arctan ½ ≈ 53°. | |
* @private | |
*/ | |
w = function (s) { return (cosh(r0) / cosh(r0 + rho * s)); }, | |
/** | |
* u(s): Returns the distance along the flight path as projected onto the ground plane, | |
* measured in pixels from the world image origin at the initial scale. | |
* @private | |
*/ | |
u = function (s) { return w0 * ((cosh(r0) * tanh(r0 + rho * s) - sinh(r0)) / rho2) / u1; }, | |
// S: Total length of the flight path, measured in ρ-screenfuls. | |
S = (r(1) - r0) / rho; | |
// When u₀ = u₁, the optimal path doesn’t require both ascent and descent. | |
if (Math.abs(u1) < 0.000001) { | |
// Perform a more or less instantaneous transition if the path is too short. | |
if (Math.abs(w0 - w1) < 0.000001) return this.easeTo(options); | |
var k = w1 < w0 ? -1 : 1; | |
S = Math.abs(Math.log(w1 / w0)) / rho; | |
u = function() { return 0; }; | |
w = function(s) { return Math.exp(k * rho * s); }; | |
} | |
if ('duration' in options) { | |
options.duration = +options.duration; | |
} else { | |
var V = 'screenSpeed' in options ? +options.screenSpeed / rho : +options.speed; | |
options.duration = 1000 * S / V; | |
} | |
this.zooming = true; | |
if (startBearing !== bearing) this.rotating = true; | |
if (startPitch !== pitch) this.pitching = true; | |
this.fire('movestart', eventData); | |
this.fire('zoomstart', eventData); | |
this._ease(function (k) { | |
// s: The distance traveled along the flight path, measured in ρ-screenfuls. | |
var s = k * S, | |
us = u(s); | |
tr.zoom = startZoom + tr.scaleZoom(1 / w(s)); | |
tr.center = tr.unproject(from.add(to.sub(from).mult(us)), startWorldSize); | |
if (this.rotating) { | |
tr.bearing = interpolate(startBearing, bearing, k); | |
} | |
if (this.pitching) { | |
tr.pitch = interpolate(startPitch, pitch, k); | |
} | |
this.fire('move', eventData); | |
this.fire('zoom', eventData); | |
if (this.rotating) { | |
this.fire('rotate', eventData); | |
} | |
if (this.pitching) { | |
this.fire('pitch', eventData); | |
} | |
}, function() { | |
this.fire('zoomend', eventData); | |
this.fire('moveend', eventData); | |
this.zooming = false; | |
this.rotating = false; | |
this.pitching = false; | |
}, options); | |
return this; | |
}, | |
isEasing: function() { | |
return !!this._abortFn; | |
}, | |
/** | |
* Stops any animated transition underway. | |
* | |
* @returns {Map} `this` | |
*/ | |
stop: function() { | |
if (this._abortFn) { | |
this._abortFn(); | |
this._finishEase(); | |
} | |
return this; | |
}, | |
_ease: function(frame, finish, options) { | |
this._finishFn = finish; | |
this._abortFn = browser.timed(function (t) { | |
frame.call(this, options.easing(t)); | |
if (t === 1) { | |
this._finishEase(); | |
} | |
}, options.animate === false ? 0 : options.duration, this); | |
}, | |
_finishEase: function() { | |
delete this._abortFn; | |
// The finish function might emit events which trigger new eases, which | |
// set a new _finishFn. Ensure we don't delete it unintentionally. | |
var finish = this._finishFn; | |
delete this._finishFn; | |
finish.call(this); | |
}, | |
// convert bearing so that it's numerically close to the current one so that it interpolates properly | |
_normalizeBearing: function(bearing, currentBearing) { | |
bearing = util.wrap(bearing, -180, 180); | |
var diff = Math.abs(bearing - currentBearing); | |
if (Math.abs(bearing - 360 - currentBearing) < diff) bearing -= 360; | |
if (Math.abs(bearing + 360 - currentBearing) < diff) bearing += 360; | |
return bearing; | |
}, | |
_updateEasing: function(duration, zoom, bezier) { | |
var easing; | |
if (this.ease) { | |
var ease = this.ease, | |
t = (Date.now() - ease.start) / ease.duration, | |
speed = ease.easing(t + 0.01) - ease.easing(t), | |
// Quick hack to make new bezier that is continuous with last | |
x = 0.27 / Math.sqrt(speed * speed + 0.0001) * 0.01, | |
y = Math.sqrt(0.27 * 0.27 - x * x); | |
easing = util.bezier(x, y, 0.25, 1); | |
} else { | |
easing = bezier ? util.bezier.apply(util, bezier) : util.ease; | |
} | |
// store information on current easing | |
this.ease = { | |
start: (new Date()).getTime(), | |
to: Math.pow(2, zoom), | |
duration: duration, | |
easing: easing | |
}; | |
return easing; | |
} | |
}); | |
/** | |
* Fired whenever the map's pitch (tilt) changes. | |
* | |
* @event pitch | |
* @memberof Map | |
* @instance | |
* @property {MapEventData} data | |
*/ | |
},{"../geo/lng_lat":10,"../geo/lng_lat_bounds":11,"../util/browser":93,"../util/interpolate":102,"../util/util":108,"point-geometry":177}],76:[function(require,module,exports){ | |
'use strict'; | |
var Control = require('./control'); | |
var DOM = require('../../util/dom'); | |
var util = require('../../util/util'); | |
module.exports = Attribution; | |
/** | |
* An `Attribution` control presents the map's [attribution information](https://www.mapbox.com/help/attribution/). | |
* Extends [`Control`](#Control). | |
* | |
* @class Attribution | |
* @param {Object} [options] | |
* @param {string} [options.position='bottom-right'] A string indicating the control's position on the map. Options are `'top-right'`, `'top-left'`, `'bottom-right'`, and `'bottom-left'`. | |
* @example | |
* var map = new mapboxgl.Map({attributionControl: false}) | |
* .addControl(new mapboxgl.Attribution({position: 'top-left'})); | |
*/ | |
function Attribution(options) { | |
util.setOptions(this, options); | |
} | |
Attribution.createAttributionString = function(sources) { | |
var attributions = []; | |
for (var id in sources) { | |
var source = sources[id]; | |
if (source.attribution && attributions.indexOf(source.attribution) < 0) { | |
attributions.push(source.attribution); | |
} | |
} | |
// remove any entries that are substrings of another entry. | |
// first sort by length so that substrings come first | |
attributions.sort(function (a, b) { return a.length - b.length; }); | |
attributions = attributions.filter(function (attrib, i) { | |
for (var j = i + 1; j < attributions.length; j++) { | |
if (attributions[j].indexOf(attrib) >= 0) { return false; } | |
} | |
return true; | |
}); | |
return attributions.join(' | '); | |
}; | |
Attribution.prototype = util.inherit(Control, { | |
options: { | |
position: 'bottom-right' | |
}, | |
onAdd: function(map) { | |
var className = 'mapboxgl-ctrl-attrib', | |
container = this._container = DOM.create('div', className, map.getContainer()); | |
this._update(); | |
map.on('source.load', this._update.bind(this)); | |
map.on('source.change', this._update.bind(this)); | |
map.on('source.remove', this._update.bind(this)); | |
map.on('moveend', this._updateEditLink.bind(this)); | |
return container; | |
}, | |
_update: function() { | |
if (this._map.style) { | |
this._container.innerHTML = Attribution.createAttributionString(this._map.style.sources); | |
} | |
this._editLink = this._container.getElementsByClassName('mapbox-improve-map')[0]; | |
this._updateEditLink(); | |
}, | |
_updateEditLink: function() { | |
if (this._editLink) { | |
var center = this._map.getCenter(); | |
this._editLink.href = 'https://www.mapbox.com/map-feedback/#/' + | |
center.lng + '/' + center.lat + '/' + Math.round(this._map.getZoom() + 1); | |
} | |
} | |
}); | |
},{"../../util/dom":96,"../../util/util":108,"./control":77}],77:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../../util/util'); | |
var Evented = require('../../util/evented'); | |
module.exports = Control; | |
/** | |
* The base class for map-related interface elements. | |
* | |
* The `Control` class mixes in [`Evented`](#Evented) methods. | |
* | |
* @class Control | |
*/ | |
function Control() {} | |
Control.prototype = { | |
/** | |
* Adds the control to a map. | |
* | |
* @param {Map} map The Mapbox GL JS map to add the control to. | |
* @returns {Control} `this` | |
*/ | |
addTo: function(map) { | |
this._map = map; | |
var container = this._container = this.onAdd(map); | |
if (this.options && this.options.position) { | |
var pos = this.options.position; | |
var corner = map._controlCorners[pos]; | |
container.className += ' mapboxgl-ctrl'; | |
if (pos.indexOf('bottom') !== -1) { | |
corner.insertBefore(container, corner.firstChild); | |
} else { | |
corner.appendChild(container); | |
} | |
} | |
return this; | |
}, | |
/** | |
* Removes the control from the map it has been added to. | |
* | |
* @returns {Control} `this` | |
*/ | |
remove: function() { | |
this._container.parentNode.removeChild(this._container); | |
if (this.onRemove) this.onRemove(this._map); | |
this._map = null; | |
return this; | |
} | |
}; | |
util.extend(Control.prototype, Evented); | |
},{"../../util/evented":100,"../../util/util":108}],78:[function(require,module,exports){ | |
'use strict'; | |
var Control = require('./control'); | |
var browser = require('../../util/browser'); | |
var DOM = require('../../util/dom'); | |
var util = require('../../util/util'); | |
module.exports = Geolocate; | |
var geoOptions = { enableHighAccuracy: false, timeout: 6000 /* 6sec */ }; | |
/** | |
* A `Geolocate` control provides a button that uses the browser's geolocation | |
* API to locate the user on the map. Extends [`Control`](#Control). | |
* | |
* @class Geolocate | |
* @param {Object} [options] | |
* @param {string} [options.position='top-right'] A string indicating the control's position on the map. Options are `'top-right'`, `'top-left'`, `'bottom-right'`, and `'bottom-left'`. | |
* @example | |
* map.addControl(new mapboxgl.Geolocate({position: 'top-left'})); // position is optional | |
*/ | |
function Geolocate(options) { | |
util.setOptions(this, options); | |
} | |
Geolocate.prototype = util.inherit(Control, { | |
options: { | |
position: 'top-right' | |
}, | |
onAdd: function(map) { | |
var className = 'mapboxgl-ctrl'; | |
var container = this._container = DOM.create('div', className + '-group', map.getContainer()); | |
if (!browser.supportsGeolocation) return container; | |
this._container.addEventListener('contextmenu', this._onContextMenu.bind(this)); | |
this._geolocateButton = DOM.create('button', (className + '-icon ' + className + '-geolocate'), this._container); | |
this._geolocateButton.addEventListener('click', this._onClickGeolocate.bind(this)); | |
return container; | |
}, | |
_onContextMenu: function(e) { | |
e.preventDefault(); | |
}, | |
_onClickGeolocate: function() { | |
navigator.geolocation.getCurrentPosition(this._success.bind(this), this._error.bind(this), geoOptions); | |
// This timeout ensures that we still call finish() even if | |
// the user declines to share their location in Firefox | |
this._timeoutId = setTimeout(this._finish.bind(this), 10000 /* 10sec */); | |
}, | |
_success: function(position) { | |
this._map.jumpTo({ | |
center: [position.coords.longitude, position.coords.latitude], | |
zoom: 17, | |
bearing: 0, | |
pitch: 0 | |
}); | |
this.fire('geolocate', position); | |
this._finish(); | |
}, | |
_error: function(error) { | |
this.fire('error', error); | |
this._finish(); | |
}, | |
_finish: function() { | |
if (this._timeoutId) { clearTimeout(this._timeoutId); } | |
this._timeoutId = undefined; | |
} | |
}); | |
/** | |
* geolocate event. | |
* | |
* @event geolocate | |
* @memberof Geolocate | |
* @instance | |
* @property {Position} data The returned [Position](https://developer.mozilla.org/en-US/docs/Web/API/Position) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition). | |
* | |
*/ | |
/** | |
* error event. | |
* | |
* @event error | |
* @memberof Geolocate | |
* @instance | |
* @property {PositionError} data The returned [PositionError](https://developer.mozilla.org/en-US/docs/Web/API/PositionError) object from the callback in [Geolocation.getCurrentPosition()](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition). | |
* | |
*/ | |
},{"../../util/browser":93,"../../util/dom":96,"../../util/util":108,"./control":77}],79:[function(require,module,exports){ | |
'use strict'; | |
var Control = require('./control'); | |
var DOM = require('../../util/dom'); | |
var util = require('../../util/util'); | |
module.exports = Navigation; | |
/** | |
* A `Navigation` control contains zoom buttons and a compass. | |
* Extends [`Control`](#Control). | |
* | |
* @class Navigation | |
* @param {Object} [options] | |
* @param {string} [options.position='top-right'] A string indicating the control's position on the map. Options are `'top-right'`, `'top-left'`, `'bottom-right'`, and `'bottom-left'`. | |
* @example | |
* var nav = new mapboxgl.Navigation({position: 'top-left'}); // position is optional | |
* map.addControl(nav); | |
*/ | |
function Navigation(options) { | |
util.setOptions(this, options); | |
} | |
Navigation.prototype = util.inherit(Control, { | |
options: { | |
position: 'top-right' | |
}, | |
onAdd: function(map) { | |
var className = 'mapboxgl-ctrl'; | |
var container = this._container = DOM.create('div', className + '-group', map.getContainer()); | |
this._container.addEventListener('contextmenu', this._onContextMenu.bind(this)); | |
this._zoomInButton = this._createButton(className + '-icon ' + className + '-zoom-in', map.zoomIn.bind(map)); | |
this._zoomOutButton = this._createButton(className + '-icon ' + className + '-zoom-out', map.zoomOut.bind(map)); | |
this._compass = this._createButton(className + '-icon ' + className + '-compass', map.resetNorth.bind(map)); | |
this._compassArrow = DOM.create('div', 'arrow', this._compass); | |
this._compass.addEventListener('mousedown', this._onCompassDown.bind(this)); | |
this._onCompassMove = this._onCompassMove.bind(this); | |
this._onCompassUp = this._onCompassUp.bind(this); | |
map.on('rotate', this._rotateCompassArrow.bind(this)); | |
this._rotateCompassArrow(); | |
this._el = map.getCanvasContainer(); | |
return container; | |
}, | |
_onContextMenu: function(e) { | |
e.preventDefault(); | |
}, | |
_onCompassDown: function(e) { | |
if (e.button !== 0) return; | |
DOM.disableDrag(); | |
document.addEventListener('mousemove', this._onCompassMove); | |
document.addEventListener('mouseup', this._onCompassUp); | |
this._el.dispatchEvent(copyMouseEvent(e)); | |
e.stopPropagation(); | |
}, | |
_onCompassMove: function(e) { | |
if (e.button !== 0) return; | |
this._el.dispatchEvent(copyMouseEvent(e)); | |
e.stopPropagation(); | |
}, | |
_onCompassUp: function(e) { | |
if (e.button !== 0) return; | |
document.removeEventListener('mousemove', this._onCompassMove); | |
document.removeEventListener('mouseup', this._onCompassUp); | |
DOM.enableDrag(); | |
this._el.dispatchEvent(copyMouseEvent(e)); | |
e.stopPropagation(); | |
}, | |
_createButton: function(className, fn) { | |
var a = DOM.create('button', className, this._container); | |
a.addEventListener('click', function() { fn(); }); | |
return a; | |
}, | |
_rotateCompassArrow: function() { | |
var rotate = 'rotate(' + (this._map.transform.angle * (180 / Math.PI)) + 'deg)'; | |
this._compassArrow.style.transform = rotate; | |
} | |
}); | |
function copyMouseEvent(e) { | |
return new MouseEvent(e.type, { | |
button: 2, // right click | |
buttons: 2, // right click | |
bubbles: true, | |
cancelable: true, | |
detail: e.detail, | |
view: e.view, | |
screenX: e.screenX, | |
screenY: e.screenY, | |
clientX: e.clientX, | |
clientY: e.clientY, | |
movementX: e.movementX, | |
movementY: e.movementY, | |
ctrlKey: e.ctrlKey, | |
shiftKey: e.shiftKey, | |
altKey: e.altKey, | |
metaKey: e.metaKey | |
}); | |
} | |
},{"../../util/dom":96,"../../util/util":108,"./control":77}],80:[function(require,module,exports){ | |
'use strict'; | |
var DOM = require('../../util/dom'), | |
LngLatBounds = require('../../geo/lng_lat_bounds'), | |
util = require('../../util/util'); | |
module.exports = BoxZoomHandler; | |
/** | |
* The `BoxZoomHandler` allows the user to zoom the map to fit within a bounding box. | |
* The bounding box is defined by clicking and holding `shift` while dragging the cursor. | |
* | |
* @class BoxZoomHandler | |
* @param {Map} map The Mapbox GL JS map to add the handler to. | |
*/ | |
function BoxZoomHandler(map) { | |
this._map = map; | |
this._el = map.getCanvasContainer(); | |
this._container = map.getContainer(); | |
util.bindHandlers(this); | |
} | |
BoxZoomHandler.prototype = { | |
_enabled: false, | |
_active: false, | |
/** | |
* Returns a Boolean indicating whether the "box zoom" interaction is enabled. | |
* | |
* @returns {boolean} `true` if the "box zoom" interaction is enabled. | |
*/ | |
isEnabled: function () { | |
return this._enabled; | |
}, | |
/** | |
* Returns a Boolean indicating whether the "box zoom" interaction is active, i.e. currently being used. | |
* | |
* @returns {boolean} `true` if the "box zoom" interaction is active. | |
*/ | |
isActive: function () { | |
return this._active; | |
}, | |
/** | |
* Enables the "box zoom" interaction. | |
* | |
* @example | |
* map.boxZoom.enable(); | |
*/ | |
enable: function () { | |
if (this.isEnabled()) return; | |
this._el.addEventListener('mousedown', this._onMouseDown, false); | |
this._enabled = true; | |
}, | |
/** | |
* Disables the "box zoom" interaction. | |
* | |
* @example | |
* map.boxZoom.disable(); | |
*/ | |
disable: function () { | |
if (!this.isEnabled()) return; | |
this._el.removeEventListener('mousedown', this._onMouseDown); | |
this._enabled = false; | |
}, | |
_onMouseDown: function (e) { | |
if (!(e.shiftKey && e.button === 0)) return; | |
document.addEventListener('mousemove', this._onMouseMove, false); | |
document.addEventListener('keydown', this._onKeyDown, false); | |
document.addEventListener('mouseup', this._onMouseUp, false); | |
DOM.disableDrag(); | |
this._startPos = DOM.mousePos(this._el, e); | |
this._active = true; | |
}, | |
_onMouseMove: function (e) { | |
var p0 = this._startPos, | |
p1 = DOM.mousePos(this._el, e); | |
if (!this._box) { | |
this._box = DOM.create('div', 'mapboxgl-boxzoom', this._container); | |
this._container.classList.add('mapboxgl-crosshair'); | |
this._fireEvent('boxzoomstart', e); | |
} | |
var minX = Math.min(p0.x, p1.x), | |
maxX = Math.max(p0.x, p1.x), | |
minY = Math.min(p0.y, p1.y), | |
maxY = Math.max(p0.y, p1.y); | |
DOM.setTransform(this._box, 'translate(' + minX + 'px,' + minY + 'px)'); | |
this._box.style.width = (maxX - minX) + 'px'; | |
this._box.style.height = (maxY - minY) + 'px'; | |
}, | |
_onMouseUp: function (e) { | |
if (e.button !== 0) return; | |
var p0 = this._startPos, | |
p1 = DOM.mousePos(this._el, e), | |
bounds = new LngLatBounds(this._map.unproject(p0), this._map.unproject(p1)); | |
this._finish(); | |
if (p0.x === p1.x && p0.y === p1.y) { | |
this._fireEvent('boxzoomcancel', e); | |
} else { | |
this._map | |
.fitBounds(bounds, {linear: true}) | |
.fire('boxzoomend', { originalEvent: e, boxZoomBounds: bounds }); | |
} | |
}, | |
_onKeyDown: function (e) { | |
if (e.keyCode === 27) { | |
this._finish(); | |
this._fireEvent('boxzoomcancel', e); | |
} | |
}, | |
_finish: function () { | |
this._active = false; | |
document.removeEventListener('mousemove', this._onMouseMove, false); | |
document.removeEventListener('keydown', this._onKeyDown, false); | |
document.removeEventListener('mouseup', this._onMouseUp, false); | |
this._container.classList.remove('mapboxgl-crosshair'); | |
if (this._box) { | |
this._box.parentNode.removeChild(this._box); | |
this._box = null; | |
} | |
DOM.enableDrag(); | |
}, | |
_fireEvent: function (type, e) { | |
return this._map.fire(type, { originalEvent: e }); | |
} | |
}; | |
/** | |
* @typedef {Object} MapBoxZoomEvent | |
* @property {MouseEvent} originalEvent | |
* @property {LngLatBounds} boxZoomBounds The bounding box of the "box zoom" interaction. | |
* This property is only provided for `boxzoomend` events. | |
*/ | |
/** | |
* Fired when a "box zoom" interaction starts. See [`BoxZoomHandler`](#BoxZoomHandler). | |
* | |
* @event boxzoomstart | |
* @memberof Map | |
* @instance | |
* @property {MapBoxZoomEvent} data | |
*/ | |
/** | |
* Fired when a "box zoom" interaction ends. See [`BoxZoomHandler`](#BoxZoomHandler). | |
* | |
* @event boxzoomend | |
* @memberof Map | |
* @instance | |
* @type {Object} | |
* @property {MapBoxZoomEvent} data | |
*/ | |
/** | |
* Fired when the user cancels a "box zoom" interaction, or when the bounding box does not meet the minimum size threshold. | |
* See [`BoxZoomHandler`](#BoxZoomHandler). | |
* | |
* @event boxzoomcancel | |
* @memberof Map | |
* @instance | |
* @property {MapBoxZoomEvent} data | |
*/ | |
},{"../../geo/lng_lat_bounds":11,"../../util/dom":96,"../../util/util":108}],81:[function(require,module,exports){ | |
'use strict'; | |
module.exports = DoubleClickZoomHandler; | |
/** | |
* The `DoubleClickZoomHandler` allows the user to zoom the map at a point by | |
* double clicking. | |
* | |
* @class DoubleClickZoomHandler | |
* @param {Map} map The Mapbox GL JS map to add the handler to. | |
*/ | |
function DoubleClickZoomHandler(map) { | |
this._map = map; | |
this._onDblClick = this._onDblClick.bind(this); | |
} | |
DoubleClickZoomHandler.prototype = { | |
_enabled: false, | |
/** | |
* Returns a Boolean indicating whether the "double click to zoom" interaction is enabled. | |
* | |
* @returns {boolean} `true` if the "double click to zoom" interaction is enabled. | |
*/ | |
isEnabled: function () { | |
return this._enabled; | |
}, | |
/** | |
* Enables the "double click to zoom" interaction. | |
* | |
* @example | |
* map.doubleClickZoom.enable(); | |
*/ | |
enable: function () { | |
if (this.isEnabled()) return; | |
this._map.on('dblclick', this._onDblClick); | |
this._enabled = true; | |
}, | |
/** | |
* Disables the "double click to zoom" interaction. | |
* | |
* @example | |
* map.doubleClickZoom.disable(); | |
*/ | |
disable: function () { | |
if (!this.isEnabled()) return; | |
this._map.off('dblclick', this._onDblClick); | |
this._enabled = false; | |
}, | |
_onDblClick: function (e) { | |
this._map.zoomTo( | |
this._map.getZoom() + (e.originalEvent.shiftKey ? -1 : 1), | |
{around: e.lngLat}, | |
e | |
); | |
} | |
}; | |
},{}],82:[function(require,module,exports){ | |
'use strict'; | |
var DOM = require('../../util/dom'), | |
util = require('../../util/util'); | |
module.exports = DragPanHandler; | |
var inertiaLinearity = 0.3, | |
inertiaEasing = util.bezier(0, 0, inertiaLinearity, 1), | |
inertiaMaxSpeed = 1400, // px/s | |
inertiaDeceleration = 2500; // px/s^2 | |
/** | |
* The `DragPanHandler` allows the user to pan the map by clicking and dragging | |
* the cursor. | |
* | |
* @class DragPanHandler | |
* @param {Map} map The Mapbox GL JS map to add the handler to. | |
*/ | |
function DragPanHandler(map) { | |
this._map = map; | |
this._el = map.getCanvasContainer(); | |
util.bindHandlers(this); | |
} | |
DragPanHandler.prototype = { | |
_enabled: false, | |
_active: false, | |
/** | |
* Returns a Boolean indicating whether the "drag to pan" interaction is enabled. | |
* | |
* @returns {boolean} `true` if the "drag to pan" interaction is enabled. | |
*/ | |
isEnabled: function () { | |
return this._enabled; | |
}, | |
/** | |
* Returns a Boolean indicating whether the "drag to pan" interaction is active, i.e. currently being used. | |
* | |
* @returns {boolean} `true` if the "drag to pan" interaction is active. | |
*/ | |
isActive: function () { | |
return this._active; | |
}, | |
/** | |
* Enables the "drag to pan" interaction. | |
* | |
* @example | |
* map.dragPan.enable(); | |
*/ | |
enable: function () { | |
if (this.isEnabled()) return; | |
this._el.addEventListener('mousedown', this._onDown); | |
this._el.addEventListener('touchstart', this._onDown); | |
this._enabled = true; | |
}, | |
/** | |
* Disables the "drag to pan" interaction. | |
* | |
* @example | |
* map.dragPan.disable(); | |
*/ | |
disable: function () { | |
if (!this.isEnabled()) return; | |
this._el.removeEventListener('mousedown', this._onDown); | |
this._el.removeEventListener('touchstart', this._onDown); | |
this._enabled = false; | |
}, | |
_onDown: function (e) { | |
if (this._ignoreEvent(e)) return; | |
if (this.isActive()) return; | |
if (e.touches) { | |
document.addEventListener('touchmove', this._onMove); | |
document.addEventListener('touchend', this._onTouchEnd); | |
} else { | |
document.addEventListener('mousemove', this._onMove); | |
document.addEventListener('mouseup', this._onMouseUp); | |
} | |
this._active = false; | |
this._startPos = this._pos = DOM.mousePos(this._el, e); | |
this._inertia = [[Date.now(), this._pos]]; | |
}, | |
_onMove: function (e) { | |
if (this._ignoreEvent(e)) return; | |
if (!this.isActive()) { | |
this._active = true; | |
this._fireEvent('dragstart', e); | |
this._fireEvent('movestart', e); | |
} | |
var pos = DOM.mousePos(this._el, e), | |
map = this._map; | |
map.stop(); | |
this._drainInertiaBuffer(); | |
this._inertia.push([Date.now(), pos]); | |
map.transform.setLocationAtPoint(map.transform.pointLocation(this._pos), pos); | |
this._fireEvent('drag', e); | |
this._fireEvent('move', e); | |
this._pos = pos; | |
e.preventDefault(); | |
}, | |
_onUp: function (e) { | |
if (!this.isActive()) return; | |
this._active = false; | |
this._fireEvent('dragend', e); | |
this._drainInertiaBuffer(); | |
var finish = function() { | |
this._fireEvent('moveend', e); | |
}.bind(this); | |
var inertia = this._inertia; | |
if (inertia.length < 2) { | |
finish(); | |
return; | |
} | |
var last = inertia[inertia.length - 1], | |
first = inertia[0], | |
flingOffset = last[1].sub(first[1]), | |
flingDuration = (last[0] - first[0]) / 1000; | |
if (flingDuration === 0 || last[1].equals(first[1])) { | |
finish(); | |
return; | |
} | |
// calculate px/s velocity & adjust for increased initial animation speed when easing out | |
var velocity = flingOffset.mult(inertiaLinearity / flingDuration), | |
speed = velocity.mag(); // px/s | |
if (speed > inertiaMaxSpeed) { | |
speed = inertiaMaxSpeed; | |
velocity._unit()._mult(speed); | |
} | |
var duration = speed / (inertiaDeceleration * inertiaLinearity), | |
offset = velocity.mult(-duration / 2); | |
this._map.panBy(offset, { | |
duration: duration * 1000, | |
easing: inertiaEasing, | |
noMoveStart: true | |
}, { originalEvent: e }); | |
}, | |
_onMouseUp: function (e) { | |
if (this._ignoreEvent(e)) return; | |
this._onUp(e); | |
document.removeEventListener('mousemove', this._onMove); | |
document.removeEventListener('mouseup', this._onMouseUp); | |
}, | |
_onTouchEnd: function (e) { | |
if (this._ignoreEvent(e)) return; | |
this._onUp(e); | |
document.removeEventListener('touchmove', this._onMove); | |
document.removeEventListener('touchend', this._onTouchEnd); | |
}, | |
_fireEvent: function (type, e) { | |
return this._map.fire(type, { originalEvent: e }); | |
}, | |
_ignoreEvent: function (e) { | |
var map = this._map; | |
if (map.boxZoom && map.boxZoom.isActive()) return true; | |
if (map.dragRotate && map.dragRotate.isActive()) return true; | |
if (e.touches) { | |
return (e.touches.length > 1); | |
} else { | |
if (e.ctrlKey) return true; | |
var buttons = 1, // left button | |
button = 0; // left button | |
return (e.type === 'mousemove' ? e.buttons & buttons === 0 : e.button !== button); | |
} | |
}, | |
_drainInertiaBuffer: function () { | |
var inertia = this._inertia, | |
now = Date.now(), | |
cutoff = 160; // msec | |
while (inertia.length > 0 && now - inertia[0][0] > cutoff) inertia.shift(); | |
} | |
}; | |
/** | |
* Fired when a "drag to pan" interaction starts. See [`DragPanHandler`](#DragPanHandler). | |
* | |
* @event dragstart | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired repeatedly during a "drag to pan" interaction. See [`DragPanHandler`](#DragPanHandler). | |
* | |
* @event drag | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired when a "drag to pan" interaction ends. See [`DragPanHandler`](#DragPanHandler). | |
* | |
* @event dragend | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
},{"../../util/dom":96,"../../util/util":108}],83:[function(require,module,exports){ | |
'use strict'; | |
var DOM = require('../../util/dom'), | |
Point = require('point-geometry'), | |
util = require('../../util/util'); | |
module.exports = DragRotateHandler; | |
var inertiaLinearity = 0.25, | |
inertiaEasing = util.bezier(0, 0, inertiaLinearity, 1), | |
inertiaMaxSpeed = 180, // deg/s | |
inertiaDeceleration = 720; // deg/s^2 | |
/** | |
* The `DragRotateHandler` allows the user to rotate the map by clicking and | |
* dragging the cursor while holding the right mouse button or `ctrl` key. | |
* | |
* @class DragRotateHandler | |
* @param {Map} map The Mapbox GL JS map to add the handler to. | |
* @param {Object} [options] | |
* @param {number} [options.bearingSnap] The threshold, measured in degrees, that determines when the map's | |
* bearing (rotation) will snap to north. | |
*/ | |
function DragRotateHandler(map, options) { | |
this._map = map; | |
this._el = map.getCanvasContainer(); | |
this._bearingSnap = options.bearingSnap; | |
util.bindHandlers(this); | |
} | |
DragRotateHandler.prototype = { | |
_enabled: false, | |
_active: false, | |
/** | |
* Returns a Boolean indicating whether the "drag to rotate" interaction is enabled. | |
* | |
* @returns {boolean} `true` if the "drag to rotate" interaction is enabled. | |
*/ | |
isEnabled: function () { | |
return this._enabled; | |
}, | |
/** | |
* Returns a Boolean indicating whether the "drag to rotate" interaction is active, i.e. currently being used. | |
* | |
* @returns {boolean} `true` if the "drag to rotate" interaction is active. | |
*/ | |
isActive: function () { | |
return this._active; | |
}, | |
/** | |
* Enables the "drag to rotate" interaction. | |
* | |
* @example | |
* map.dragRotate.enable(); | |
*/ | |
enable: function () { | |
if (this.isEnabled()) return; | |
this._el.addEventListener('mousedown', this._onDown); | |
this._enabled = true; | |
}, | |
/** | |
* Disables the "drag to rotate" interaction. | |
* | |
* @example | |
* map.dragRotate.disable(); | |
*/ | |
disable: function () { | |
if (!this.isEnabled()) return; | |
this._el.removeEventListener('mousedown', this._onDown); | |
this._enabled = false; | |
}, | |
_onDown: function (e) { | |
if (this._ignoreEvent(e)) return; | |
if (this.isActive()) return; | |
document.addEventListener('mousemove', this._onMove); | |
document.addEventListener('mouseup', this._onUp); | |
this._active = false; | |
this._inertia = [[Date.now(), this._map.getBearing()]]; | |
this._startPos = this._pos = DOM.mousePos(this._el, e); | |
this._center = this._map.transform.centerPoint; // Center of rotation | |
// If the first click was too close to the center, move the center of rotation by 200 pixels | |
// in the direction of the click. | |
var startToCenter = this._startPos.sub(this._center), | |
startToCenterDist = startToCenter.mag(); | |
if (startToCenterDist < 200) { | |
this._center = this._startPos.add(new Point(-200, 0)._rotate(startToCenter.angle())); | |
} | |
e.preventDefault(); | |
}, | |
_onMove: function (e) { | |
if (this._ignoreEvent(e)) return; | |
if (!this.isActive()) { | |
this._active = true; | |
this._fireEvent('rotatestart', e); | |
this._fireEvent('movestart', e); | |
} | |
var map = this._map; | |
map.stop(); | |
var p1 = this._pos, | |
p2 = DOM.mousePos(this._el, e), | |
center = this._center, | |
bearingDiff = p1.sub(center).angleWith(p2.sub(center)) / Math.PI * 180, | |
bearing = map.getBearing() - bearingDiff, | |
inertia = this._inertia, | |
last = inertia[inertia.length - 1]; | |
this._drainInertiaBuffer(); | |
inertia.push([Date.now(), map._normalizeBearing(bearing, last[1])]); | |
map.transform.bearing = bearing; | |
this._fireEvent('rotate', e); | |
this._fireEvent('move', e); | |
this._pos = p2; | |
}, | |
_onUp: function (e) { | |
if (this._ignoreEvent(e)) return; | |
document.removeEventListener('mousemove', this._onMove); | |
document.removeEventListener('mouseup', this._onUp); | |
if (!this.isActive()) return; | |
this._active = false; | |
this._fireEvent('rotateend', e); | |
this._drainInertiaBuffer(); | |
var map = this._map, | |
mapBearing = map.getBearing(), | |
inertia = this._inertia; | |
var finish = function() { | |
if (Math.abs(mapBearing) < this._bearingSnap) { | |
map.resetNorth({noMoveStart: true}, { originalEvent: e }); | |
} else { | |
this._fireEvent('moveend', e); | |
} | |
}.bind(this); | |
if (inertia.length < 2) { | |
finish(); | |
return; | |
} | |
var first = inertia[0], | |
last = inertia[inertia.length - 1], | |
previous = inertia[inertia.length - 2], | |
bearing = map._normalizeBearing(mapBearing, previous[1]), | |
flingDiff = last[1] - first[1], | |
sign = flingDiff < 0 ? -1 : 1, | |
flingDuration = (last[0] - first[0]) / 1000; | |
if (flingDiff === 0 || flingDuration === 0) { | |
finish(); | |
return; | |
} | |
var speed = Math.abs(flingDiff * (inertiaLinearity / flingDuration)); // deg/s | |
if (speed > inertiaMaxSpeed) { | |
speed = inertiaMaxSpeed; | |
} | |
var duration = speed / (inertiaDeceleration * inertiaLinearity), | |
offset = sign * speed * (duration / 2); | |
bearing += offset; | |
if (Math.abs(map._normalizeBearing(bearing, 0)) < this._bearingSnap) { | |
bearing = map._normalizeBearing(0, bearing); | |
} | |
map.rotateTo(bearing, { | |
duration: duration * 1000, | |
easing: inertiaEasing, | |
noMoveStart: true | |
}, { originalEvent: e }); | |
}, | |
_fireEvent: function (type, e) { | |
return this._map.fire(type, { originalEvent: e }); | |
}, | |
_ignoreEvent: function (e) { | |
var map = this._map; | |
if (map.boxZoom && map.boxZoom.isActive()) return true; | |
if (map.dragPan && map.dragPan.isActive()) return true; | |
if (e.touches) { | |
return (e.touches.length > 1); | |
} else { | |
var buttons = (e.ctrlKey ? 1 : 2), // ? ctrl+left button : right button | |
button = (e.ctrlKey ? 0 : 2); // ? ctrl+left button : right button | |
return (e.type === 'mousemove' ? e.buttons & buttons === 0 : e.button !== button); | |
} | |
}, | |
_drainInertiaBuffer: function () { | |
var inertia = this._inertia, | |
now = Date.now(), | |
cutoff = 160; //msec | |
while (inertia.length > 0 && now - inertia[0][0] > cutoff) | |
inertia.shift(); | |
} | |
}; | |
/** | |
* Fired when a "drag to rotate" interaction starts. See [`DragRotateHandler`](#DragRotateHandler). | |
* | |
* @event rotatestart | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired repeatedly during a "drag to rotate" interaction. See [`DragRotateHandler`](#DragRotateHandler). | |
* | |
* @event rotate | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired when a "drag to rotate" interaction ends. See [`DragRotateHandler`](#DragRotateHandler). | |
* | |
* @event rotateend | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
},{"../../util/dom":96,"../../util/util":108,"point-geometry":177}],84:[function(require,module,exports){ | |
'use strict'; | |
module.exports = KeyboardHandler; | |
var panDelta = 80, | |
rotateDelta = 2, | |
pitchDelta = 5; | |
/** | |
* The `KeyboardHandler` allows the user to zoom, rotate, and pan the map using | |
* the following keyboard shortcuts: | |
* | |
* - `=` / `+`: Increase the zoom level by 1. | |
* - `Shift-=` / `Shift-+`: Increase the zoom level by 2. | |
* - `-`: Decrease the zoom level by 1. | |
* - `Shift--`: Decrease the zoom level by 2. | |
* - Arrow keys: Pan by 80 pixels. | |
* - `Shift+⇢`: Increase the rotation by 2 degrees. | |
* - `Shift+⇠`: Decrease the rotation by 2 degrees. | |
* - `Shift+⇡`: Increase the pitch by 5 degrees. | |
* - `Shift+⇣`: Decrease the pitch by 5 degrees. | |
* | |
* @class KeyboardHandler | |
* @param {Map} map The Mapbox GL JS map to add the handler to. | |
*/ | |
function KeyboardHandler(map) { | |
this._map = map; | |
this._el = map.getCanvasContainer(); | |
this._onKeyDown = this._onKeyDown.bind(this); | |
} | |
KeyboardHandler.prototype = { | |
_enabled: false, | |
/** | |
* Returns a Boolean indicating whether keyboard interaction is enabled. | |
* | |
* @returns {boolean} `true` if keyboard interaction is enabled. | |
*/ | |
isEnabled: function () { | |
return this._enabled; | |
}, | |
/** | |
* Enables keyboard interaction. | |
* | |
* @example | |
* map.keyboard.enable(); | |
*/ | |
enable: function () { | |
if (this.isEnabled()) return; | |
this._el.addEventListener('keydown', this._onKeyDown, false); | |
this._enabled = true; | |
}, | |
/** | |
* Disables keyboard interaction. | |
* | |
* @example | |
* map.keyboard.disable(); | |
*/ | |
disable: function () { | |
if (!this.isEnabled()) return; | |
this._el.removeEventListener('keydown', this._onKeyDown); | |
this._enabled = false; | |
}, | |
_onKeyDown: function (e) { | |
if (e.altKey || e.ctrlKey || e.metaKey) return; | |
var map = this._map, | |
eventData = { originalEvent: e }; | |
if (map.isEasing()) return; | |
switch (e.keyCode) { | |
case 61: | |
case 107: | |
case 171: | |
case 187: | |
map.zoomTo(Math.round(map.getZoom()) + (e.shiftKey ? 2 : 1), eventData); | |
break; | |
case 189: | |
case 109: | |
case 173: | |
map.zoomTo(Math.round(map.getZoom()) - (e.shiftKey ? 2 : 1), eventData); | |
break; | |
case 37: | |
if (e.shiftKey) { | |
map.easeTo({ bearing: map.getBearing() - rotateDelta }, eventData); | |
} else { | |
e.preventDefault(); | |
map.panBy([-panDelta, 0], eventData); | |
} | |
break; | |
case 39: | |
if (e.shiftKey) { | |
map.easeTo({ bearing: map.getBearing() + rotateDelta }, eventData); | |
} else { | |
e.preventDefault(); | |
map.panBy([panDelta, 0], eventData); | |
} | |
break; | |
case 38: | |
if (e.shiftKey) { | |
map.easeTo({ pitch: map.getPitch() + pitchDelta }, eventData); | |
} else { | |
e.preventDefault(); | |
map.panBy([0, -panDelta], eventData); | |
} | |
break; | |
case 40: | |
if (e.shiftKey) { | |
map.easeTo({ pitch: Math.max(map.getPitch() - pitchDelta, 0) }, eventData); | |
} else { | |
e.preventDefault(); | |
map.panBy([0, panDelta], eventData); | |
} | |
break; | |
} | |
} | |
}; | |
},{}],85:[function(require,module,exports){ | |
'use strict'; | |
var DOM = require('../../util/dom'), | |
browser = require('../../util/browser'), | |
util = require('../../util/util'); | |
module.exports = ScrollZoomHandler; | |
var ua = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase() : '', | |
firefox = ua.indexOf('firefox') !== -1, | |
safari = ua.indexOf('safari') !== -1 && ua.indexOf('chrom') === -1; | |
/** | |
* The `ScrollZoomHandler` allows the user to zoom the map by scrolling. | |
* | |
* @class ScrollZoomHandler | |
* @param {Map} map The Mapbox GL JS map to add the handler to. | |
*/ | |
function ScrollZoomHandler(map) { | |
this._map = map; | |
this._el = map.getCanvasContainer(); | |
util.bindHandlers(this); | |
} | |
ScrollZoomHandler.prototype = { | |
_enabled: false, | |
/** | |
* Returns a Boolean indicating whether the "scroll to zoom" interaction is enabled. | |
* | |
* @returns {boolean} `true` if the "scroll to zoom" interaction is enabled. | |
*/ | |
isEnabled: function () { | |
return this._enabled; | |
}, | |
/** | |
* Enables the "scroll to zoom" interaction. | |
* | |
* @example | |
* map.scrollZoom.enable(); | |
*/ | |
enable: function () { | |
if (this.isEnabled()) return; | |
this._el.addEventListener('wheel', this._onWheel, false); | |
this._el.addEventListener('mousewheel', this._onWheel, false); | |
this._enabled = true; | |
}, | |
/** | |
* Disables the "scroll to zoom" interaction. | |
* | |
* @example | |
* map.scrollZoom.disable(); | |
*/ | |
disable: function () { | |
if (!this.isEnabled()) return; | |
this._el.removeEventListener('wheel', this._onWheel); | |
this._el.removeEventListener('mousewheel', this._onWheel); | |
this._enabled = false; | |
}, | |
_onWheel: function (e) { | |
var value; | |
if (e.type === 'wheel') { | |
value = e.deltaY; | |
// Firefox doubles the values on retina screens... | |
if (firefox && e.deltaMode === window.WheelEvent.DOM_DELTA_PIXEL) value /= browser.devicePixelRatio; | |
if (e.deltaMode === window.WheelEvent.DOM_DELTA_LINE) value *= 40; | |
} else if (e.type === 'mousewheel') { | |
value = -e.wheelDeltaY; | |
if (safari) value = value / 3; | |
} | |
var now = browser.now(), | |
timeDelta = now - (this._time || 0); | |
this._pos = DOM.mousePos(this._el, e); | |
this._time = now; | |
if (value !== 0 && (value % 4.000244140625) === 0) { | |
// This one is definitely a mouse wheel event. | |
this._type = 'wheel'; | |
// Normalize this value to match trackpad. | |
value = Math.floor(value / 4); | |
} else if (value !== 0 && Math.abs(value) < 4) { | |
// This one is definitely a trackpad event because it is so small. | |
this._type = 'trackpad'; | |
} else if (timeDelta > 400) { | |
// This is likely a new scroll action. | |
this._type = null; | |
this._lastValue = value; | |
// Start a timeout in case this was a singular event, and dely it by up to 40ms. | |
this._timeout = setTimeout(this._onTimeout, 40); | |
} else if (!this._type) { | |
// This is a repeating event, but we don't know the type of event just yet. | |
// If the delta per time is small, we assume it's a fast trackpad; otherwise we switch into wheel mode. | |
this._type = (Math.abs(timeDelta * value) < 200) ? 'trackpad' : 'wheel'; | |
// Make sure our delayed event isn't fired again, because we accumulate | |
// the previous event (which was less than 40ms ago) into this event. | |
if (this._timeout) { | |
clearTimeout(this._timeout); | |
this._timeout = null; | |
value += this._lastValue; | |
} | |
} | |
// Slow down zoom if shift key is held for more precise zooming | |
if (e.shiftKey && value) value = value / 4; | |
// Only fire the callback if we actually know what type of scrolling device the user uses. | |
if (this._type) this._zoom(-value, e); | |
e.preventDefault(); | |
}, | |
_onTimeout: function () { | |
this._type = 'wheel'; | |
this._zoom(-this._lastValue); | |
}, | |
_zoom: function (delta, e) { | |
if (delta === 0) return; | |
var map = this._map; | |
// Scale by sigmoid of scroll wheel delta. | |
var scale = 2 / (1 + Math.exp(-Math.abs(delta / 100))); | |
if (delta < 0 && scale !== 0) scale = 1 / scale; | |
var fromScale = map.ease ? map.ease.to : map.transform.scale, | |
targetZoom = map.transform.scaleZoom(fromScale * scale); | |
map.zoomTo(targetZoom, { | |
duration: 0, | |
around: map.unproject(this._pos), | |
delayEndEvents: 200 | |
}, { originalEvent: e }); | |
} | |
}; | |
/** | |
* Fired just before the map begins a transition from one zoom level to another, | |
* as the result of either user interaction or methods such as [Map#flyTo](#Map#flyTo). | |
* | |
* @event zoomstart | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired repeatedly during an animated transition from one zoom level to another, | |
* as the result of either user interaction or methods such as [Map#flyTo](#Map#flyTo). | |
* | |
* @event zoom | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired just after the map completes a transition from one zoom level to another, | |
* as the result of either user interaction or methods such as [Map#flyTo](#Map#flyTo). | |
* | |
* @event zoomend | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
},{"../../util/browser":93,"../../util/dom":96,"../../util/util":108}],86:[function(require,module,exports){ | |
'use strict'; | |
var DOM = require('../../util/dom'), | |
util = require('../../util/util'); | |
module.exports = TouchZoomRotateHandler; | |
var inertiaLinearity = 0.15, | |
inertiaEasing = util.bezier(0, 0, inertiaLinearity, 1), | |
inertiaDeceleration = 12, // scale / s^2 | |
inertiaMaxSpeed = 2.5, // scale / s | |
significantScaleThreshold = 0.15, | |
significantRotateThreshold = 4; | |
/** | |
* The `TouchZoomRotateHandler` allows the user to zoom and rotate the map by | |
* pinching on a touchscreen. | |
* | |
* @class TouchZoomRotateHandler | |
* @param {Map} map The Mapbox GL JS map to add the handler to. | |
*/ | |
function TouchZoomRotateHandler(map) { | |
this._map = map; | |
this._el = map.getCanvasContainer(); | |
util.bindHandlers(this); | |
} | |
TouchZoomRotateHandler.prototype = { | |
_enabled: false, | |
/** | |
* Returns a Boolean indicating whether the "pinch to rotate and zoom" interaction is enabled. | |
* | |
* @returns {boolean} `true` if the "pinch to rotate and zoom" interaction is enabled. | |
*/ | |
isEnabled: function () { | |
return this._enabled; | |
}, | |
/** | |
* Enables the "pinch to rotate and zoom" interaction. | |
* | |
* @example | |
* map.touchZoomRotate.enable(); | |
*/ | |
enable: function () { | |
if (this.isEnabled()) return; | |
this._el.addEventListener('touchstart', this._onStart, false); | |
this._enabled = true; | |
}, | |
/** | |
* Disables the "pinch to rotate and zoom" interaction. | |
* | |
* @example | |
* map.touchZoomRotate.disable(); | |
*/ | |
disable: function () { | |
if (!this.isEnabled()) return; | |
this._el.removeEventListener('touchstart', this._onStart); | |
this._enabled = false; | |
}, | |
/** | |
* Disables the "pinch to rotate" interaction, leaving the "pinch to zoom" | |
* interaction enabled. | |
* | |
* @example | |
* map.touchZoomRotate.disableRotation(); | |
*/ | |
disableRotation: function() { | |
this._rotationDisabled = true; | |
}, | |
/** | |
* Enables the "pinch to rotate" interaction. | |
* | |
* @example | |
* map.touchZoomRotate.enable(); | |
* map.touchZoomRotate.enableRotation(); | |
*/ | |
enableRotation: function() { | |
this._rotationDisabled = false; | |
}, | |
_onStart: function (e) { | |
if (e.touches.length !== 2) return; | |
var p0 = DOM.mousePos(this._el, e.touches[0]), | |
p1 = DOM.mousePos(this._el, e.touches[1]); | |
this._startVec = p0.sub(p1); | |
this._startScale = this._map.transform.scale; | |
this._startBearing = this._map.transform.bearing; | |
this._gestureIntent = undefined; | |
this._inertia = []; | |
document.addEventListener('touchmove', this._onMove, false); | |
document.addEventListener('touchend', this._onEnd, false); | |
}, | |
_onMove: function (e) { | |
if (e.touches.length !== 2) return; | |
var p0 = DOM.mousePos(this._el, e.touches[0]), | |
p1 = DOM.mousePos(this._el, e.touches[1]), | |
p = p0.add(p1).div(2), | |
vec = p0.sub(p1), | |
scale = vec.mag() / this._startVec.mag(), | |
bearing = this._rotationDisabled ? 0 : vec.angleWith(this._startVec) * 180 / Math.PI, | |
map = this._map; | |
// Determine 'intent' by whichever threshold is surpassed first, | |
// then keep that state for the duration of this gesture. | |
if (!this._gestureIntent) { | |
var scalingSignificantly = (Math.abs(1 - scale) > significantScaleThreshold), | |
rotatingSignificantly = (Math.abs(bearing) > significantRotateThreshold); | |
if (rotatingSignificantly) { | |
this._gestureIntent = 'rotate'; | |
} else if (scalingSignificantly) { | |
this._gestureIntent = 'zoom'; | |
} | |
if (this._gestureIntent) { | |
this._startVec = vec; | |
this._startScale = map.transform.scale; | |
this._startBearing = map.transform.bearing; | |
} | |
} else { | |
var param = { duration: 0, around: map.unproject(p) }; | |
if (this._gestureIntent === 'rotate') { | |
param.bearing = this._startBearing + bearing; | |
} | |
if (this._gestureIntent === 'zoom' || this._gestureIntent === 'rotate') { | |
param.zoom = map.transform.scaleZoom(this._startScale * scale); | |
} | |
map.stop(); | |
this._drainInertiaBuffer(); | |
this._inertia.push([Date.now(), scale, p]); | |
map.easeTo(param, { originalEvent: e }); | |
} | |
e.preventDefault(); | |
}, | |
_onEnd: function (e) { | |
document.removeEventListener('touchmove', this._onMove); | |
document.removeEventListener('touchend', this._onEnd); | |
this._drainInertiaBuffer(); | |
var inertia = this._inertia, | |
map = this._map; | |
if (inertia.length < 2) { | |
map.snapToNorth({}, { originalEvent: e }); | |
return; | |
} | |
var last = inertia[inertia.length - 1], | |
first = inertia[0], | |
lastScale = map.transform.scaleZoom(this._startScale * last[1]), | |
firstScale = map.transform.scaleZoom(this._startScale * first[1]), | |
scaleOffset = lastScale - firstScale, | |
scaleDuration = (last[0] - first[0]) / 1000, | |
p = last[2]; | |
if (scaleDuration === 0 || lastScale === firstScale) { | |
map.snapToNorth({}, { originalEvent: e }); | |
return; | |
} | |
// calculate scale/s speed and adjust for increased initial animation speed when easing | |
var speed = scaleOffset * inertiaLinearity / scaleDuration; // scale/s | |
if (Math.abs(speed) > inertiaMaxSpeed) { | |
if (speed > 0) { | |
speed = inertiaMaxSpeed; | |
} else { | |
speed = -inertiaMaxSpeed; | |
} | |
} | |
var duration = Math.abs(speed / (inertiaDeceleration * inertiaLinearity)) * 1000, | |
targetScale = lastScale + speed * duration / 2000; | |
if (targetScale < 0) { | |
targetScale = 0; | |
} | |
map.easeTo({ | |
zoom: targetScale, | |
duration: duration, | |
easing: inertiaEasing, | |
around: map.unproject(p) | |
}, { originalEvent: e }); | |
}, | |
_drainInertiaBuffer: function() { | |
var inertia = this._inertia, | |
now = Date.now(), | |
cutoff = 160; // msec | |
while (inertia.length > 2 && now - inertia[0][0] > cutoff) inertia.shift(); | |
} | |
}; | |
},{"../../util/dom":96,"../../util/util":108}],87:[function(require,module,exports){ | |
'use strict'; | |
/* | |
* Adds the map's position to its page's location hash. | |
* Passed as an option to the map object. | |
* | |
* @class mapboxgl.Hash | |
* @returns {Hash} `this` | |
*/ | |
module.exports = Hash; | |
var util = require('../util/util'); | |
function Hash() { | |
util.bindAll([ | |
'_onHashChange', | |
'_updateHash' | |
], this); | |
} | |
Hash.prototype = { | |
/* | |
* Map element to listen for coordinate changes | |
* | |
* @param {Object} map | |
* @returns {Hash} `this` | |
*/ | |
addTo: function(map) { | |
this._map = map; | |
window.addEventListener('hashchange', this._onHashChange, false); | |
this._map.on('moveend', this._updateHash); | |
return this; | |
}, | |
/* | |
* Removes hash | |
* | |
* @returns {Popup} `this` | |
*/ | |
remove: function() { | |
window.removeEventListener('hashchange', this._onHashChange, false); | |
this._map.off('moveend', this._updateHash); | |
delete this._map; | |
return this; | |
}, | |
_onHashChange: function() { | |
var loc = location.hash.replace('#', '').split('/'); | |
if (loc.length >= 3) { | |
this._map.jumpTo({ | |
center: [+loc[2], +loc[1]], | |
zoom: +loc[0], | |
bearing: +(loc[3] || 0) | |
}); | |
return true; | |
} | |
return false; | |
}, | |
_updateHash: function() { | |
var center = this._map.getCenter(), | |
zoom = this._map.getZoom(), | |
bearing = this._map.getBearing(), | |
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)), | |
hash = '#' + (Math.round(zoom * 100) / 100) + | |
'/' + center.lat.toFixed(precision) + | |
'/' + center.lng.toFixed(precision) + | |
(bearing ? '/' + (Math.round(bearing * 10) / 10) : ''); | |
window.history.replaceState('', '', hash); | |
} | |
}; | |
},{"../util/util":108}],88:[function(require,module,exports){ | |
'use strict'; | |
var Canvas = require('../util/canvas'); | |
var util = require('../util/util'); | |
var browser = require('../util/browser'); | |
var window = require('../util/browser').window; | |
var Evented = require('../util/evented'); | |
var DOM = require('../util/dom'); | |
var Style = require('../style/style'); | |
var AnimationLoop = require('../style/animation_loop'); | |
var Painter = require('../render/painter'); | |
var Transform = require('../geo/transform'); | |
var Hash = require('./hash'); | |
var bindHandlers = require('./bind_handlers'); | |
var Camera = require('./camera'); | |
var LngLat = require('../geo/lng_lat'); | |
var LngLatBounds = require('../geo/lng_lat_bounds'); | |
var Point = require('point-geometry'); | |
var Attribution = require('./control/attribution'); | |
var defaultMinZoom = 0; | |
var defaultMaxZoom = 20; | |
var defaultOptions = { | |
center: [0, 0], | |
zoom: 0, | |
bearing: 0, | |
pitch: 0, | |
minZoom: defaultMinZoom, | |
maxZoom: defaultMaxZoom, | |
interactive: true, | |
scrollZoom: true, | |
boxZoom: true, | |
dragRotate: true, | |
dragPan: true, | |
keyboard: true, | |
doubleClickZoom: true, | |
touchZoomRotate: true, | |
bearingSnap: 7, | |
hash: false, | |
attributionControl: true, | |
failIfMajorPerformanceCaveat: false, | |
preserveDrawingBuffer: false, | |
trackResize: true, | |
workerCount: Math.max(browser.hardwareConcurrency - 1, 1) | |
}; | |
/** | |
* The `Map` object represents the map on your page. It exposes methods | |
* and properties that enable you to programmatically change the map, | |
* and fires events as users interact with it. | |
* | |
* You create a `Map` by specifying a `container` and other options. | |
* Then Mapbox GL JS initializes the map on the page and returns your `Map` | |
* object. | |
* | |
* The `Map` class mixes in [`Evented`](#Evented) methods. | |
* | |
* @class Map | |
* @param {Object} options | |
* @param {HTMLElement|string} options.container The HTML element in which Mapbox GL JS will render the map, or the element's string `id`. | |
* @param {number} [options.minZoom=0] The minimum zoom level of the map (1-20). | |
* @param {number} [options.maxZoom=20] The maximum zoom level of the map (1-20). | |
* @param {Object|string} [options.style] The map's Mapbox style. This must be an a JSON object conforming to | |
* the schema described in the [Mapbox Style Specification](https://mapbox.com/mapbox-gl-style-spec/), or a URL to | |
* such JSON. | |
* | |
* To load a style from the Mapbox API, you can use a URL of the form `mapbox://styles/:owner/:style`, | |
* where `:owner` is your Mapbox account name and `:style` is the style ID. Or you can use one of the following | |
* [the predefined Mapbox styles](https://www.mapbox.com/maps/): | |
* | |
* * `mapbox://styles/mapbox/streets-v9` | |
* * `mapbox://styles/mapbox/outdoors-v9` | |
* * `mapbox://styles/mapbox/light-v9` | |
* * `mapbox://styles/mapbox/dark-v9` | |
* * `mapbox://styles/mapbox/satellite-v9` | |
* * `mapbox://styles/mapbox/satellite-streets-v9` | |
* | |
* @param {boolean} [options.hash=false] If `true`, the map's position (zoom, center latitude, center longitude, and bearing) will be synced with the hash fragment of the page's URL. | |
* For example, `http://path/to/my/page.html#2.59/39.26/53.07/-24.1`. | |
* @param {boolean} [options.interactive=true] If `false`, no mouse, touch, or keyboard listeners will be attached to the map, so it will not respond to interaction. | |
* @param {number} [options.bearingSnap=7] The threshold, measured in degrees, that determines when the map's | |
* bearing (rotation) will snap to north. For example, with a `bearingSnap` of 7, if the user rotates | |
* the map within 7 degrees of north, the map will automatically snap to exact north. | |
* @param {Array<string>} [options.classes] Mapbox style class names with which to initialize the map. | |
* Keep in mind that these classes are used for controlling a style layer's paint properties, so are *not* reflected | |
* in an HTML element's `class` attribute. To learn more about Mapbox style classes, read about | |
* [Layers](https://www.mapbox.com/mapbox-gl-style-spec/#layers) in the style specification. | |
* @param {boolean} [options.attributionControl=true] If `true`, an [Attribution](#Attribution) control will be added to the map. | |
* @param {boolean} [options.failIfMajorPerformanceCaveat=false] If `true`, map creation will fail if the performance of Mapbox | |
* GL JS would be dramatically worse than expected (i.e. a software renderer would be used). | |
* @param {boolean} [options.preserveDrawingBuffer=false] If `true`, the map's canvas can be exported to a PNG using `map.getCanvas().toDataURL()`. This is `false` by default as a performance optimization. | |
* @param {LngLatBoundsLike} [options.maxBounds] If set, the map will be constrained to the given bounds. | |
* @param {boolean} [options.scrollZoom=true] If `true`, the "scroll to zoom" interaction is enabled (see [`ScrollZoomHandler`](#ScrollZoomHandler)). | |
* @param {boolean} [options.boxZoom=true] If `true`, the "box zoom" interaction is enabled (see [`BoxZoomHandler`](#BoxZoomHandler)). | |
* @param {boolean} [options.dragRotate=true] If `true`, the "drag to rotate" interaction is enabled (see [`DragRotateHandler`](#DragRotateHandler)). | |
* @param {boolean} [options.dragPan=true] If `true`, the "drag to pan" interaction is enabled (see [`DragPanHandler`](#DragPanHandler)). | |
* @param {boolean} [options.keyboard=true] If `true`, keyboard shortcuts are enabled (see [`KeyboardHandler`](#KeyboardHandler)). | |
* @param {boolean} [options.doubleClickZoom=true] If `true`, the "double click to zoom" interaction is enabled (see [`DoubleClickZoomHandler`](#DoubleClickZoomHandler)). | |
* @param {boolean} [options.touchZoomRotate=true] If `true`, the "pinch to rotate and zoom" interaction is enabled (see [`TouchZoomRotateHandler`](#TouchZoomRotateHandler)). | |
* @param {boolean} [options.trackResize=true] If `true`, the map will automatically resize when the browser window resizes. | |
* @param {LngLatLike} [options.center=[0, 0]] The inital geographical centerpoint of the map. If `center` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `[0, 0]`. | |
* @param {number} [options.zoom=0] The initial zoom level of the map. If `zoom` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `0`. | |
* @param {number} [options.bearing=0] The initial bearing (rotation) of the map, measured in degrees counter-clockwise from north. If `bearing` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `0`. | |
* @param {number} [options.pitch=0] The initial pitch (tilt) of the map, measured in degrees away from the plane of the screen (0-60). If `pitch` is not specified in the constructor options, Mapbox GL JS will look for it in the map's style object. If it is not specified in the style, either, it will default to `0`. | |
* @param {number} [options.workerCount=navigator.hardwareConcurrency - 1] The number of WebWorkers that Mapbox GL JS should use to process vector tile data. | |
* @example | |
* var map = new mapboxgl.Map({ | |
* container: 'map', | |
* center: [-122.420679, 37.772537], | |
* zoom: 13, | |
* style: style_object, | |
* hash: true | |
* }); | |
*/ | |
var Map = module.exports = function(options) { | |
options = util.extend({}, defaultOptions, options); | |
if (options.workerCount < 1) { | |
throw new Error('workerCount must an integer greater than or equal to 1.'); | |
} | |
this._interactive = options.interactive; | |
this._failIfMajorPerformanceCaveat = options.failIfMajorPerformanceCaveat; | |
this._preserveDrawingBuffer = options.preserveDrawingBuffer; | |
this._trackResize = options.trackResize; | |
this._workerCount = options.workerCount; | |
this._bearingSnap = options.bearingSnap; | |
if (typeof options.container === 'string') { | |
this._container = document.getElementById(options.container); | |
} else { | |
this._container = options.container; | |
} | |
this.animationLoop = new AnimationLoop(); | |
this.transform = new Transform(options.minZoom, options.maxZoom); | |
if (options.maxBounds) { | |
this.setMaxBounds(options.maxBounds); | |
} | |
util.bindAll([ | |
'_forwardStyleEvent', | |
'_forwardSourceEvent', | |
'_forwardLayerEvent', | |
'_forwardTileEvent', | |
'_onStyleLoad', | |
'_onStyleChange', | |
'_onSourceAdd', | |
'_onSourceRemove', | |
'_onSourceUpdate', | |
'_onWindowOnline', | |
'_onWindowResize', | |
'onError', | |
'_update', | |
'_render' | |
], this); | |
this._setupContainer(); | |
this._setupPainter(); | |
this.on('move', this._update.bind(this, false)); | |
this.on('zoom', this._update.bind(this, true)); | |
this.on('moveend', function() { | |
this.animationLoop.set(300); // text fading | |
this._rerender(); | |
}.bind(this)); | |
if (typeof window !== 'undefined') { | |
window.addEventListener('online', this._onWindowOnline, false); | |
window.addEventListener('resize', this._onWindowResize, false); | |
} | |
bindHandlers(this, options); | |
this._hash = options.hash && (new Hash()).addTo(this); | |
// don't set position from options if set through hash | |
if (!this._hash || !this._hash._onHashChange()) { | |
this.jumpTo({ | |
center: options.center, | |
zoom: options.zoom, | |
bearing: options.bearing, | |
pitch: options.pitch | |
}); | |
} | |
this.stacks = {}; | |
this._classes = []; | |
this.resize(); | |
if (options.classes) this.setClasses(options.classes); | |
if (options.style) this.setStyle(options.style); | |
if (options.attributionControl) this.addControl(new Attribution(options.attributionControl)); | |
this.on('error', this.onError); | |
this.on('style.error', this.onError); | |
this.on('source.error', this.onError); | |
this.on('tile.error', this.onError); | |
this.on('layer.error', this.onError); | |
}; | |
util.extend(Map.prototype, Evented); | |
util.extend(Map.prototype, Camera.prototype); | |
util.extend(Map.prototype, /** @lends Map.prototype */{ | |
/** | |
* Adds a [`Control`](#Control) to the map, calling `control.addTo(this)`. | |
* | |
* @param {Control} control The [`Control`](#Control) to add. | |
* @returns {Map} `this` | |
*/ | |
addControl: function(control) { | |
control.addTo(this); | |
return this; | |
}, | |
/** | |
* Adds a Mapbox style class to the map. | |
* | |
* Keep in mind that these classes are used for controlling a style layer's paint properties, so are *not* reflected | |
* in an HTML element's `class` attribute. To learn more about Mapbox style classes, read about | |
* [Layers](https://www.mapbox.com/mapbox-gl-style-spec/#layers) in the style specification. | |
* | |
* @param {string} klass The style class to add. | |
* @param {StyleOptions} [options] | |
* @fires change | |
* @returns {Map} `this` | |
*/ | |
addClass: function(klass, options) { | |
if (this._classes.indexOf(klass) >= 0 || klass === '') return this; | |
this._classes.push(klass); | |
this._classOptions = options; | |
if (this.style) this.style.updateClasses(); | |
return this._update(true); | |
}, | |
/** | |
* Removes a Mapbox style class from the map. | |
* | |
* @param {string} klass The style class to remove. | |
* @param {StyleOptions} [options] | |
* @fires change | |
* @returns {Map} `this` | |
*/ | |
removeClass: function(klass, options) { | |
var i = this._classes.indexOf(klass); | |
if (i < 0 || klass === '') return this; | |
this._classes.splice(i, 1); | |
this._classOptions = options; | |
if (this.style) this.style.updateClasses(); | |
return this._update(true); | |
}, | |
/** | |
* Replaces the map's existing Mapbox style classes with a new array of classes. | |
* | |
* @param {Array<string>} klasses The style classes to set. | |
* @param {StyleOptions} [options] | |
* @fires change | |
* @returns {Map} `this` | |
*/ | |
setClasses: function(klasses, options) { | |
var uniqueClasses = {}; | |
for (var i = 0; i < klasses.length; i++) { | |
if (klasses[i] !== '') uniqueClasses[klasses[i]] = true; | |
} | |
this._classes = Object.keys(uniqueClasses); | |
this._classOptions = options; | |
if (this.style) this.style.updateClasses(); | |
return this._update(true); | |
}, | |
/** | |
* Returns a Boolean indicating whether the map has the | |
* specified Mapbox style class. | |
* | |
* @param {string} klass The style class to test. | |
* @returns {boolean} `true` if the map has the specified style class. | |
*/ | |
hasClass: function(klass) { | |
return this._classes.indexOf(klass) >= 0; | |
}, | |
/** | |
* Returns the map's Mapbox style classes. | |
* | |
* @returns {Array<string>} The map's style classes. | |
*/ | |
getClasses: function() { | |
return this._classes; | |
}, | |
/** | |
* Resizes the map according to the dimensions of its | |
* `container` element. | |
* | |
* This method must be called after the map's `container` is resized by another script, | |
* or when the map is shown after being initially hidden with CSS. | |
* | |
* @returns {Map} `this` | |
*/ | |
resize: function() { | |
var width = 0, height = 0; | |
if (this._container) { | |
width = this._container.offsetWidth || 400; | |
height = this._container.offsetHeight || 300; | |
} | |
this._canvas.resize(width, height); | |
this.transform.resize(width, height); | |
this.painter.resize(width, height); | |
return this | |
.fire('movestart') | |
.fire('move') | |
.fire('resize') | |
.fire('moveend'); | |
}, | |
/** | |
* Returns the map's geographical bounds. | |
* | |
* @returns {LngLatBounds} The map's geographical bounds. | |
*/ | |
getBounds: function() { | |
var bounds = new LngLatBounds( | |
this.transform.pointLocation(new Point(0, 0)), | |
this.transform.pointLocation(this.transform.size)); | |
if (this.transform.angle || this.transform.pitch) { | |
bounds.extend(this.transform.pointLocation(new Point(this.transform.size.x, 0))); | |
bounds.extend(this.transform.pointLocation(new Point(0, this.transform.size.y))); | |
} | |
return bounds; | |
}, | |
/** | |
* Sets or clears the map's geographical bounds. | |
* | |
* Pan and zoom operations are constrained within these bounds. | |
* If a pan or zoom is performed that would | |
* display regions outside these bounds, the map will | |
* instead display a position and zoom level | |
* as close as possible to the operation's request while still | |
* remaining within the bounds. | |
* | |
* @param {LngLatBoundsLike | null | undefined} lnglatbounds The maximum bounds to set. If `null` or `undefined` is provided, the function removes the map's maximum bounds. | |
* @returns {Map} `this` | |
*/ | |
setMaxBounds: function (lnglatbounds) { | |
if (lnglatbounds) { | |
var b = LngLatBounds.convert(lnglatbounds); | |
this.transform.lngRange = [b.getWest(), b.getEast()]; | |
this.transform.latRange = [b.getSouth(), b.getNorth()]; | |
this.transform._constrain(); | |
this._update(); | |
} else if (lnglatbounds === null || lnglatbounds === undefined) { | |
this.transform.lngRange = []; | |
this.transform.latRange = []; | |
this._update(); | |
} | |
return this; | |
}, | |
/** | |
* Sets or clears the map's minimum zoom level. | |
* If the map's current zoom level is lower than the new minimum, | |
* the map will zoom to the new minimum. | |
* | |
* @param {?number} minZoom The minimum zoom level to set (0-20). | |
* If `null` or `undefined` is provided, the function removes the current minimum zoom (i.e. sets it to 0). | |
* @returns {Map} `this` | |
*/ | |
setMinZoom: function(minZoom) { | |
minZoom = minZoom === null || minZoom === undefined ? defaultMinZoom : minZoom; | |
if (minZoom >= defaultMinZoom && minZoom <= this.transform.maxZoom) { | |
this.transform.minZoom = minZoom; | |
this._update(); | |
if (this.getZoom() < minZoom) this.setZoom(minZoom); | |
return this; | |
} else throw new Error('minZoom must be between ' + defaultMinZoom + ' and the current maxZoom, inclusive'); | |
}, | |
/** | |
* Sets or clears the map's maximum zoom level. | |
* If the map's current zoom level is higher than the new maximum, | |
* the map will zoom to the new maximum. | |
* | |
* @param {?number} maxZoom The maximum zoom level to set (0-20). | |
* If `null` or `undefined` is provided, the function removes the current maximum zoom (sets it to 20). | |
* @returns {Map} `this` | |
*/ | |
setMaxZoom: function(maxZoom) { | |
maxZoom = maxZoom === null || maxZoom === undefined ? defaultMaxZoom : maxZoom; | |
if (maxZoom >= this.transform.minZoom && maxZoom <= defaultMaxZoom) { | |
this.transform.maxZoom = maxZoom; | |
this._update(); | |
if (this.getZoom() > maxZoom) this.setZoom(maxZoom); | |
return this; | |
} else throw new Error('maxZoom must be between the current minZoom and ' + defaultMaxZoom + ', inclusive'); | |
}, | |
/** | |
* Returns a [`Point`](#Point) representing pixel coordinates, relative to the map's `container`, | |
* that correspond to the specified geographical location. | |
* | |
* @param {LngLatLike} lnglat The geographical location to project. | |
* @returns {Point} The [`Point`](#Point) corresponding to `lnglat`, relative to the map's `container`. | |
*/ | |
project: function(lnglat) { | |
return this.transform.locationPoint(LngLat.convert(lnglat)); | |
}, | |
/** | |
* Returns a [`LngLat`](#LngLat) representing geographical coordinates that correspond | |
* to the specified pixel coordinates. | |
* | |
* @param {PointLike} point The pixel coordinates to unproject. | |
* @returns {LngLat} The [`LngLat`](#LngLat) corresponding to `point`. | |
*/ | |
unproject: function(point) { | |
return this.transform.pointLocation(Point.convert(point)); | |
}, | |
/** | |
* Returns an array of [GeoJSON](http://geojson.org/) | |
* [Feature objects](http://geojson.org/geojson-spec.html#feature-objects) | |
* representing visible features that satisfy the query parameters. | |
* | |
* @param {PointLike|Array<PointLike>} [region] - The geometry of the query region: | |
* either a single point or southwest and northeast points describing a bounding box. | |
* Omitting this parameter (i.e. calling [`Map#queryRenderedFeatures`](#Map#queryRenderedFeatures) with zero arguments, | |
* or with only a `parameters` argument) is equivalent to passing a bounding box encompassing the entire | |
* map viewport. | |
* @param {Object} [parameters] | |
* @param {Array<string>} [parameters.layers] An array of style layer IDs for the query to inspect. | |
* Only features within these layers will be returned. If this parameter is undefined, all layers will be checked. | |
* @param {Array} [parameters.filter] A [filter](https://www.mapbox.com/mapbox-gl-style-spec/#types-filter) | |
* to limit query results. | |
* | |
* @returns {Array<Object>} An array of [GeoJSON](http://geojson.org/) | |
* [feature objects](http://geojson.org/geojson-spec.html#feature-objects). | |
* | |
* The `properties` value of each returned feature object contains the properties of its source feature. For GeoJSON sources, only | |
* string and numeric property values are supported (i.e. `null`, `Array`, and `Object` values are not supported). | |
* | |
* Each feature includes a top-level `layer` property whose value is an object representing the style layer to | |
* which the feature belongs. Layout and paint properties in this object contain values which are fully evaluated | |
* for the given zoom level and feature. | |
* | |
* Only visible features are returned. The topmost rendered feature appears first in the returned array, and | |
* subsequent features are sorted by descending z-order. Features that are rendered multiple times (due to wrapping | |
* across the antimeridian at low zoom levels) are returned only once (though subject to the following caveat). | |
* | |
* Because features come from tiled vector data or GeoJSON data that is converted to tiles internally, feature | |
* geometries are clipped at tile boundaries and, as a result, features may appear multiple times in query | |
* results when they span multiple tiles. For example, suppose | |
* there is a highway running through the bounding rectangle of a query. The results of the query will be those | |
* parts of the highway that lie within the map tiles covering the bounding rectangle, even if the highway extends | |
* into other tiles, and the portion of the highway within each map tile will be returned as a separate feature. | |
* | |
* @example | |
* // Find all features at a point | |
* var features = map.queryRenderedFeatures( | |
* [20, 35], | |
* { layers: ['my-layer-name'] } | |
* ); | |
* | |
* @example | |
* // Find all features within a static bounding box | |
* var features = map.queryRenderedFeatures( | |
* [[10, 20], [30, 50]], | |
* { layers: ['my-layer-name'] } | |
* ); | |
* | |
* @example | |
* // Find all features within a bounding box around a point | |
* var width = 10; | |
* var height = 20; | |
* var features = map.queryRenderedFeatures([ | |
* [point.x - width / 2, point.y - height / 2], | |
* [point.x + width / 2, point.y + height / 2] | |
* ], { layers: ['my-layer-name'] }); | |
* | |
* @example | |
* // Query all rendered features from a single layer | |
* var features = map.queryRenderedFeatures({ layers: ['my-layer-name'] }); | |
*/ | |
queryRenderedFeatures: function(pointOrBox, params) { | |
if (!(pointOrBox instanceof Point || Array.isArray(pointOrBox))) { | |
params = pointOrBox; | |
pointOrBox = undefined; | |
} | |
var queryGeometry = this._makeQueryGeometry(pointOrBox); | |
return this.style.queryRenderedFeatures(queryGeometry, params, this.transform.zoom, this.transform.angle); | |
}, | |
_makeQueryGeometry: function(pointOrBox) { | |
if (pointOrBox === undefined) { | |
// bounds was omitted: use full viewport | |
pointOrBox = [ | |
Point.convert([0, 0]), | |
Point.convert([this.transform.width, this.transform.height]) | |
]; | |
} | |
var queryGeometry; | |
var isPoint = pointOrBox instanceof Point || typeof pointOrBox[0] === 'number'; | |
if (isPoint) { | |
var point = Point.convert(pointOrBox); | |
queryGeometry = [point]; | |
} else { | |
var box = [Point.convert(pointOrBox[0]), Point.convert(pointOrBox[1])]; | |
queryGeometry = [ | |
box[0], | |
new Point(box[1].x, box[0].y), | |
box[1], | |
new Point(box[0].x, box[1].y), | |
box[0] | |
]; | |
} | |
queryGeometry = queryGeometry.map(function(p) { | |
return this.transform.pointCoordinate(p); | |
}.bind(this)); | |
return queryGeometry; | |
}, | |
/** | |
* Returns an array of [GeoJSON](http://geojson.org/) | |
* [Feature objects](http://geojson.org/geojson-spec.html#feature-objects) | |
* representing features within the specified vector tile or GeoJSON source that satisfy the query parameters. | |
* | |
* @param {string} sourceID The ID of the vector tile or GeoJSON source to query. | |
* @param {Object} parameters | |
* @param {string} [parameters.sourceLayer] The name of the vector tile layer to query. *For vector tile | |
* sources, this parameter is required.* For GeoJSON sources, it is ignored. | |
* @param {Array} [parameters.filter] A [filter](https://www.mapbox.com/mapbox-gl-style-spec/#types-filter) | |
* to limit query results. | |
* | |
* @returns {Array<Object>} An array of [GeoJSON](http://geojson.org/) | |
* [Feature objects](http://geojson.org/geojson-spec.html#feature-objects). | |
* | |
* In contrast to [`Map#queryRenderedFeatures`](#Map#queryRenderedFeatures), this function | |
* returns all features matching the query parameters, | |
* whether or not they are rendered by the current style (i.e. visible). The domain of the query includes all currently-loaded | |
* vector tiles and GeoJSON source tiles: this function does not check tiles outside the currently | |
* visible viewport. | |
* | |
* Because features come from tiled vector data or GeoJSON data that is converted to tiles internally, feature | |
* geometries are clipped at tile boundaries and, as a result, features may appear multiple times in query | |
* results when they span multiple tiles. For example, suppose | |
* there is a highway running through the bounding rectangle of a query. The results of the query will be those | |
* parts of the highway that lie within the map tiles covering the bounding rectangle, even if the highway extends | |
* into other tiles, and the portion of the highway within each map tile will be returned as a separate feature. | |
*/ | |
querySourceFeatures: function(sourceID, params) { | |
return this.style.querySourceFeatures(sourceID, params); | |
}, | |
/** | |
* Replaces the map's Mapbox style object with a new value. | |
* | |
* @param {Object|string} style A JSON object conforming to the schema described in the | |
* [Mapbox Style Specification](https://mapbox.com/mapbox-gl-style-spec/), or a URL to such JSON. | |
* @returns {Map} `this` | |
*/ | |
setStyle: function(style) { | |
if (this.style) { | |
this.style | |
.off('load', this._onStyleLoad) | |
.off('error', this._forwardStyleEvent) | |
.off('change', this._onStyleChange) | |
.off('source.add', this._onSourceAdd) | |
.off('source.remove', this._onSourceRemove) | |
.off('source.load', this._onSourceUpdate) | |
.off('source.error', this._forwardSourceEvent) | |
.off('source.change', this._onSourceUpdate) | |
.off('layer.add', this._forwardLayerEvent) | |
.off('layer.remove', this._forwardLayerEvent) | |
.off('layer.error', this._forwardLayerEvent) | |
.off('tile.add', this._forwardTileEvent) | |
.off('tile.remove', this._forwardTileEvent) | |
.off('tile.load', this._update) | |
.off('tile.error', this._forwardTileEvent) | |
.off('tile.stats', this._forwardTileEvent) | |
._remove(); | |
this.off('rotate', this.style._redoPlacement); | |
this.off('pitch', this.style._redoPlacement); | |
} | |
if (!style) { | |
this.style = null; | |
return this; | |
} else if (style instanceof Style) { | |
this.style = style; | |
} else { | |
this.style = new Style(style, this.animationLoop, this._workerCount); | |
} | |
this.style | |
.on('load', this._onStyleLoad) | |
.on('error', this._forwardStyleEvent) | |
.on('change', this._onStyleChange) | |
.on('source.add', this._onSourceAdd) | |
.on('source.remove', this._onSourceRemove) | |
.on('source.load', this._onSourceUpdate) | |
.on('source.error', this._forwardSourceEvent) | |
.on('source.change', this._onSourceUpdate) | |
.on('layer.add', this._forwardLayerEvent) | |
.on('layer.remove', this._forwardLayerEvent) | |
.on('layer.error', this._forwardLayerEvent) | |
.on('tile.add', this._forwardTileEvent) | |
.on('tile.remove', this._forwardTileEvent) | |
.on('tile.load', this._update) | |
.on('tile.error', this._forwardTileEvent) | |
.on('tile.stats', this._forwardTileEvent); | |
this.on('rotate', this.style._redoPlacement); | |
this.on('pitch', this.style._redoPlacement); | |
return this; | |
}, | |
/** | |
* Returns the map's Mapbox style object, which can be used to recreate the map's style. | |
* | |
* @returns {Object} The map's style object. | |
*/ | |
getStyle: function() { | |
if (this.style) { | |
return this.style.serialize(); | |
} | |
}, | |
/** | |
* Adds a source to the map's style. | |
* | |
* @param {string} id The ID of the source to add. Must not conflict with existing sources. | |
* @param {Object} source The source object, conforming to the | |
* Mapbox Style Specification's [source defintion](https://www.mapbox.com/mapbox-gl-style-spec/#sources). | |
* @fires source.add | |
* @returns {Map} `this` | |
*/ | |
addSource: function(id, source) { | |
this.style.addSource(id, source); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Removes a source from the map's style. | |
* | |
* @param {string} id The ID of the source to remove. | |
* @fires source.remove | |
* @returns {Map} `this` | |
*/ | |
removeSource: function(id) { | |
this.style.removeSource(id); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Returns the source with the specified ID in the map's style. | |
* | |
* @param {string} id The ID of the source to get. | |
* @returns {?Object} The style source with the specified ID, or `undefined` | |
* if the ID corresponds to no existing sources. | |
*/ | |
getSource: function(id) { | |
return this.style.getSource(id); | |
}, | |
/** | |
* Adds a [Mapbox style layer](https://www.mapbox.com/mapbox-gl-style-spec/#layers) | |
* to the map's style. | |
* | |
* A layer defines styling for data from a specified source. | |
* | |
* @param {Object} layer The style layer to add, conforming to the Mapbox Style Specification's | |
* [layer definition](https://www.mapbox.com/mapbox-gl-style-spec/#layers). | |
* @param {string} [before] The ID of an existing layer to insert the new layer before. | |
* If this argument is omitted, the layer will be inserted before every existing layer. | |
* @fires layer.add | |
* @returns {Map} `this` | |
*/ | |
addLayer: function(layer, before) { | |
this.style.addLayer(layer, before); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Removes a layer from the map's style. | |
* | |
* Also removes any layers which refer to the specified layer via a | |
* [`ref` property](https://www.mapbox.com/mapbox-gl-style-spec/#layer-ref). | |
* | |
* @param {string} id The ID of the layer to remove. | |
* @throws {Error} if no layer with the specified `id` exists. | |
* @fires layer.remove | |
* @returns {Map} `this` | |
*/ | |
removeLayer: function(id) { | |
this.style.removeLayer(id); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Returns the layer with the specified ID in the map's style. | |
* | |
* @param {string} id The ID of the layer to get. | |
* @returns {?Object} The layer with the specified ID, or `undefined` | |
* if the ID corresponds to no existing layers. | |
*/ | |
getLayer: function(id) { | |
return this.style.getLayer(id); | |
}, | |
/** | |
* Sets the filter for the specified style layer. | |
* | |
* @param {string} layer The ID of the layer to which the filter will be applied. | |
* @param {Array} filter The filter, conforming to the Mapbox Style Specification's | |
* [filter definition](https://www.mapbox.com/mapbox-gl-style-spec/#types-filter). | |
* @returns {Map} `this` | |
* @example | |
* map.setFilter('my-layer', ['==', 'name', 'USA']); | |
*/ | |
setFilter: function(layer, filter) { | |
this.style.setFilter(layer, filter); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Sets the zoom extent for the specified style layer. | |
* | |
* @param {string} layerId The ID of the layer to which the zoom extent will be applied. | |
* @param {number} minzoom The minimum zoom to set (0-20). | |
* @param {number} maxzoom The maximum zoom to set (0-20). | |
* @returns {Map} `this` | |
* @example | |
* map.setLayerZoomRange('my-layer', 2, 5); | |
*/ | |
setLayerZoomRange: function(layerId, minzoom, maxzoom) { | |
this.style.setLayerZoomRange(layerId, minzoom, maxzoom); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Returns the filter applied to the specified style layer. | |
* | |
* @param {string} layer The ID of the style layer whose filter to get. | |
* @returns {Array} The layer's filter. | |
*/ | |
getFilter: function(layer) { | |
return this.style.getFilter(layer); | |
}, | |
/** | |
* Sets the value of a paint property in the specified style layer. | |
* | |
* @param {string} layer The ID of the layer to set the paint property in. | |
* @param {string} name The name of the paint property to set. | |
* @param {*} value The value of the paint propery to set. | |
* Must be of a type appropriate for the property, as defined in the [Mapbox Style Specification](https://www.mapbox.com/mapbox-gl-style-spec/). | |
* @param {string=} klass A style class specifier for the paint property. | |
* @returns {Map} `this` | |
* @example | |
* map.setPaintProperty('my-layer', 'fill-color', '#faafee'); | |
*/ | |
setPaintProperty: function(layer, name, value, klass) { | |
this.style.setPaintProperty(layer, name, value, klass); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Returns the value of a paint property in the specified style layer. | |
* | |
* @param {string} layer The ID of the layer to get the paint property from. | |
* @param {string} name The name of a paint property to get. | |
* @param {string=} klass A class specifier for the paint property. | |
* @returns {*} The value of the specified paint property. | |
*/ | |
getPaintProperty: function(layer, name, klass) { | |
return this.style.getPaintProperty(layer, name, klass); | |
}, | |
/** | |
* Sets the value of a layout property in the specified style layer. | |
* | |
* @param {string} layer The ID of the layer to set the layout property in. | |
* @param {string} name The name of the layout property to set. | |
* @param {*} value The value of the layout propery. Must be of a type appropriate for the property, as defined in the [Mapbox Style Specification](https://www.mapbox.com/mapbox-gl-style-spec/). | |
* @returns {Map} `this` | |
* @example | |
* map.setLayoutProperty('my-layer', 'visibility', 'none'); | |
*/ | |
setLayoutProperty: function(layer, name, value) { | |
this.style.setLayoutProperty(layer, name, value); | |
this._update(true); | |
return this; | |
}, | |
/** | |
* Returns the value of a layout property in the specified style layer. | |
* | |
* @param {string} layer The ID of the layer to get the layout property from. | |
* @param {string} name The name of the layout property to get. | |
* @returns {*} The value of the specified layout property. | |
*/ | |
getLayoutProperty: function(layer, name) { | |
return this.style.getLayoutProperty(layer, name); | |
}, | |
/** | |
* Returns the map's containing HTML element. | |
* | |
* @returns {HTMLElement} The map's container. | |
*/ | |
getContainer: function() { | |
return this._container; | |
}, | |
/** | |
* Returns the HTML element containing the map's `<canvas>` element. | |
* | |
* If you want to add non-GL overlays to the map, you should append them to this element. | |
* | |
* This is the element to which event bindings for map interactivity (such as panning and zooming) are | |
* attached. It will receive bubbled events from child elements such as the `<canvas>`, but not from | |
* map controls. | |
* | |
* @returns {HTMLElement} The container of the map's `<canvas>`. | |
*/ | |
getCanvasContainer: function() { | |
return this._canvasContainer; | |
}, | |
/** | |
* Returns the map's `<canvas>` element. | |
* | |
* @returns {HTMLCanvasElement} The map's `<canvas>` element. | |
*/ | |
getCanvas: function() { | |
return this._canvas.getElement(); | |
}, | |
_setupContainer: function() { | |
var container = this._container; | |
container.classList.add('mapboxgl-map'); | |
var canvasContainer = this._canvasContainer = DOM.create('div', 'mapboxgl-canvas-container', container); | |
if (this._interactive) { | |
canvasContainer.classList.add('mapboxgl-interactive'); | |
} | |
this._canvas = new Canvas(this, canvasContainer); | |
var controlContainer = this._controlContainer = DOM.create('div', 'mapboxgl-control-container', container); | |
var corners = this._controlCorners = {}; | |
['top-left', 'top-right', 'bottom-left', 'bottom-right'].forEach(function (pos) { | |
corners[pos] = DOM.create('div', 'mapboxgl-ctrl-' + pos, controlContainer); | |
}); | |
}, | |
_setupPainter: function() { | |
var gl = this._canvas.getWebGLContext({ | |
failIfMajorPerformanceCaveat: this._failIfMajorPerformanceCaveat, | |
preserveDrawingBuffer: this._preserveDrawingBuffer | |
}); | |
if (!gl) { | |
this.fire('error', { error: new Error('Failed to initialize WebGL') }); | |
return; | |
} | |
this.painter = new Painter(gl, this.transform); | |
}, | |
/** | |
* Fired when the WebGL context is lost. | |
* | |
* @event webglcontextlost | |
* @memberof Map | |
* @instance | |
* @type {Object} | |
* @property {WebGLContextEvent} originalEvent The original DOM event. | |
*/ | |
_contextLost: function(event) { | |
event.preventDefault(); | |
if (this._frameId) { | |
browser.cancelFrame(this._frameId); | |
} | |
this.fire('webglcontextlost', {originalEvent: event}); | |
}, | |
/** | |
* Fired when the WebGL context is restored. | |
* | |
* @event webglcontextrestored | |
* @memberof Map | |
* @instance | |
* @type {Object} | |
* @property {WebGLContextEvent} originalEvent The original DOM event. | |
*/ | |
_contextRestored: function(event) { | |
this._setupPainter(); | |
this.resize(); | |
this._update(); | |
this.fire('webglcontextrestored', {originalEvent: event}); | |
}, | |
/** | |
* Returns a Boolean indicating whether the map is fully loaded. | |
* | |
* Returns `false` if the style is not yet fully loaded, | |
* or if there has been a change to the sources or style that | |
* has not yet fully loaded. | |
* | |
* @returns {boolean} A Boolean indicating whether the map is fully loaded. | |
*/ | |
loaded: function() { | |
if (this._styleDirty || this._sourcesDirty) | |
return false; | |
if (!this.style || !this.style.loaded()) | |
return false; | |
return true; | |
}, | |
/** | |
* Update this map's style and sources, and re-render the map. | |
* | |
* @param {boolean} updateStyle mark the map's style for reprocessing as | |
* well as its sources | |
* @returns {Map} this | |
* @private | |
*/ | |
_update: function(updateStyle) { | |
if (!this.style) return this; | |
this._styleDirty = this._styleDirty || updateStyle; | |
this._sourcesDirty = true; | |
this._rerender(); | |
return this; | |
}, | |
/** | |
* Call when a (re-)render of the map is required, e.g. when the | |
* user panned or zoomed,f or new data is available. | |
* @returns {Map} this | |
* @private | |
*/ | |
_render: function() { | |
try { | |
if (this.style && this._styleDirty) { | |
this._styleDirty = false; | |
this.style.update(this._classes, this._classOptions); | |
this._classOptions = null; | |
this.style._recalculate(this.transform.zoom); | |
} | |
if (this.style && this._sourcesDirty) { | |
this._sourcesDirty = false; | |
this.style._updateSources(this.transform); | |
} | |
this.painter.render(this.style, { | |
debug: this.showTileBoundaries, | |
showOverdrawInspector: this._showOverdrawInspector, | |
vertices: this.vertices, | |
rotating: this.rotating, | |
zooming: this.zooming | |
}); | |
this.fire('render'); | |
if (this.loaded() && !this._loaded) { | |
this._loaded = true; | |
this.fire('load'); | |
} | |
this._frameId = null; | |
if (!this.animationLoop.stopped()) { | |
this._styleDirty = true; | |
} | |
if (this._sourcesDirty || this._repaint || !this.animationLoop.stopped()) { | |
this._rerender(); | |
} | |
} catch (error) { | |
this.fire('error', {error: error}); | |
} | |
return this; | |
}, | |
/** | |
* Destroys the map's underlying resources, including web workers and DOM elements. | |
* | |
* After calling this method, you must not call any other methods on the map. | |
*/ | |
remove: function() { | |
if (this._hash) this._hash.remove(); | |
browser.cancelFrame(this._frameId); | |
this.setStyle(null); | |
if (typeof window !== 'undefined') { | |
window.removeEventListener('resize', this._onWindowResize, false); | |
} | |
removeNode(this._canvasContainer); | |
removeNode(this._controlContainer); | |
this._container.classList.remove('mapboxgl-map'); | |
}, | |
/** | |
* Gets and sets an error handler for `style.error`, `source.error`, `layer.error`, | |
* and `tile.error` events. | |
* | |
* The default function logs errors with `console.error`. | |
* | |
* @example | |
* // Disable the default error handler | |
* map.off('error', map.onError); | |
* map.off('style.error', map.onError); | |
* map.off('source.error', map.onError); | |
* map.off('tile.error', map.onError); | |
* map.off('layer.error', map.onError); | |
*/ | |
onError: function(e) { | |
console.error(e.error); | |
}, | |
_rerender: function() { | |
if (this.style && !this._frameId) { | |
this._frameId = browser.frame(this._render); | |
} | |
}, | |
_forwardStyleEvent: function(e) { | |
this.fire('style.' + e.type, util.extend({style: e.target}, e)); | |
}, | |
_forwardSourceEvent: function(e) { | |
this.fire(e.type, util.extend({style: e.target}, e)); | |
}, | |
_forwardLayerEvent: function(e) { | |
this.fire(e.type, util.extend({style: e.target}, e)); | |
}, | |
_forwardTileEvent: function(e) { | |
this.fire(e.type, util.extend({style: e.target}, e)); | |
}, | |
_onStyleLoad: function(e) { | |
if (this.transform.unmodified) { | |
this.jumpTo(this.style.stylesheet); | |
} | |
this.style.update(this._classes, {transition: false}); | |
this._forwardStyleEvent(e); | |
}, | |
_onStyleChange: function(e) { | |
this._update(true); | |
this._forwardStyleEvent(e); | |
}, | |
_onSourceAdd: function(e) { | |
var source = e.source; | |
if (source.onAdd) | |
source.onAdd(this); | |
this._forwardSourceEvent(e); | |
}, | |
_onSourceRemove: function(e) { | |
var source = e.source; | |
if (source.onRemove) | |
source.onRemove(this); | |
this._forwardSourceEvent(e); | |
}, | |
_onSourceUpdate: function(e) { | |
this._update(); | |
this._forwardSourceEvent(e); | |
}, | |
_onWindowOnline: function() { | |
this._update(); | |
}, | |
_onWindowResize: function() { | |
if (this._trackResize) { | |
this.stop().resize()._update(); | |
} | |
} | |
}); | |
util.extendAll(Map.prototype, /** @lends Map.prototype */{ | |
/** | |
* Gets and sets a Boolean indicating whether the map will render an outline | |
* around each tile. These tile boundaries are useful for debugging. | |
* | |
* @name showTileBoundaries | |
* @type {boolean} | |
* @instance | |
* @memberof Map | |
*/ | |
_showTileBoundaries: false, | |
get showTileBoundaries() { return this._showTileBoundaries; }, | |
set showTileBoundaries(value) { | |
if (this._showTileBoundaries === value) return; | |
this._showTileBoundaries = value; | |
this._update(); | |
}, | |
/** | |
* Gets and sets a Boolean indicating whether the map will render boxes | |
* around all symbols in the data source, revealing which symbols | |
* were rendered or which were hidden due to collisions. | |
* This information is useful for debugging. | |
* | |
* @name showCollisionBoxes | |
* @type {boolean} | |
* @instance | |
* @memberof Map | |
*/ | |
_showCollisionBoxes: false, | |
get showCollisionBoxes() { return this._showCollisionBoxes; }, | |
set showCollisionBoxes(value) { | |
if (this._showCollisionBoxes === value) return; | |
this._showCollisionBoxes = value; | |
this.style._redoPlacement(); | |
}, | |
/* | |
* Gets and sets a Boolean indicating whether the map should color-code | |
* each fragment to show how many times it has been shaded. | |
* White fragments have been shaded 8 or more times. | |
* Black fragments have been shaded 0 times. | |
* This information is useful for debugging. | |
* | |
* @name showOverdraw | |
* @type {boolean} | |
* @instance | |
* @memberof Map | |
*/ | |
_showOverdrawInspector: false, | |
get showOverdrawInspector() { return this._showOverdrawInspector; }, | |
set showOverdrawInspector(value) { | |
if (this._showOverdrawInspector === value) return; | |
this._showOverdrawInspector = value; | |
this._update(); | |
}, | |
/** | |
* Gets and sets a Boolean indicating whether the map will | |
* continuously repaint. This information is useful for analyzing performance. | |
* | |
* @name repaint | |
* @type {boolean} | |
* @instance | |
* @memberof Map | |
*/ | |
_repaint: false, | |
get repaint() { return this._repaint; }, | |
set repaint(value) { this._repaint = value; this._update(); }, | |
// show vertices | |
_vertices: false, | |
get vertices() { return this._vertices; }, | |
set vertices(value) { this._vertices = value; this._update(); } | |
}); | |
function removeNode(node) { | |
if (node.parentNode) { | |
node.parentNode.removeChild(node); | |
} | |
} | |
/** | |
* Gets and sets the map's [access token](https://www.mapbox.com/help/define-access-token/). | |
* | |
* @var accessToken | |
* @example | |
* mapboxgl.accessToken = myAccessToken; | |
*/ | |
/** | |
* Returns a Boolean indicating whether the browser [supports Mapbox GL JS](https://www.mapbox.com/help/mapbox-browser-support/#mapbox-gl-js). | |
* | |
* @function supported | |
* @param {Object} options | |
* @param {boolean} [options.failIfMajorPerformanceCaveat=false] If `true`, | |
* the function will return `false` if the performance of Mapbox GL JS would | |
* be dramatically worse than expected (i.e. a software renderer would be used). | |
* @return {boolean} | |
* @example | |
* mapboxgl.supported() // = true | |
*/ | |
/** | |
* A [`LngLat`](#LngLat) object or an array of two numbers representing longitude and latitude. | |
* | |
* @typedef {(LngLat | Array<number>)} LngLatLike | |
* @example | |
* var v1 = new mapboxgl.LngLat(-122.420679, 37.772537); | |
* var v2 = [-122.420679, 37.772537]; | |
*/ | |
/** | |
* A [`LngLatBounds`](#LngLatBounds) object or an array of [`LngLatLike`](#LngLatLike) objects. | |
* | |
* @typedef {(LngLatBounds | Array<LngLatLike>)} LngLatBoundsLike | |
* @example | |
* var v1 = new mapboxgl.LngLatBounds( | |
* new mapboxgl.LngLat(-73.9876, 40.7661), | |
* new mapboxgl.LngLat(-73.9397, 40.8002) | |
* ); | |
* var v2 = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]) | |
* var v3 = [[-73.9876, 40.7661], [-73.9397, 40.8002]]; | |
*/ | |
/** | |
* A [`Point` geometry](https://github.com/mapbox/point-geometry) object, which has | |
* `x` and `y` properties representing coordinates. | |
* | |
* @typedef {Object} Point | |
*/ | |
/** | |
* A [`Point`](#Point) or an array of two numbers representing `x` and `y` coordinates. | |
* | |
* @typedef {(Point | Array<number>)} PointLike | |
*/ | |
/** | |
* Options common to {@link Map#addClass}, {@link Map#removeClass}, | |
* and {@link Map#setClasses}, controlling | |
* whether or not to smoothly transition property changes triggered by a class change. | |
* | |
* @typedef {Object} StyleOptions | |
* @property {boolean} transition If `true`, property changes will smootly transition. | |
*/ | |
/** | |
* Fired whenever the map is drawn to the screen, as the result of | |
* | |
* - a change to the map's position, zoom, pitch, or bearing | |
* - a change to the map's style | |
* - a change to a GeoJSON source | |
* - the loading of a vector tile, GeoJSON file, glyph, or sprite | |
* | |
* @event render | |
* @memberof Map | |
* @instance | |
*/ | |
/** | |
* Fired when a point device (usually a mouse) leaves the map's canvas. | |
* | |
* @event mouseout | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent} data | |
*/ | |
/** | |
* Fired when a pointing device (usually a mouse) is pressed within the map. | |
* | |
* @event mousedown | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent} data | |
*/ | |
/** | |
* Fired when a pointing device (usually a mouse) is released within the map. | |
* | |
* @event mouseup | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent} data | |
*/ | |
/** | |
* Fired when a pointing device (usually a mouse) is moved within the map. | |
* | |
* @event mousemove | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent} data | |
*/ | |
/** | |
* Fired when a touch point is placed on the map. | |
* | |
* @event touchstart | |
* @memberof Map | |
* @instance | |
* @property {MapTouchEvent} data | |
*/ | |
/** | |
* Fired when a touch point is removed from the map. | |
* | |
* @event touchend | |
* @memberof Map | |
* @instance | |
* @property {MapTouchEvent} data | |
*/ | |
/** | |
* Fired when a touch point is moved within the map. | |
* | |
* @event touchmove | |
* @memberof Map | |
* @instance | |
* @property {MapTouchEvent} data | |
*/ | |
/** | |
* Fired when a touch point has been disrupted. | |
* | |
* @event touchcancel | |
* @memberof Map | |
* @instance | |
* @property {MapTouchEvent} data | |
*/ | |
/** | |
* Fired when a pointing device (usually a mouse) is pressed and released at the same point on the map. | |
* | |
* @event click | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent} data | |
*/ | |
/** | |
* Fired when a pointing device (usually a mouse) is clicked twice at the same point on the map. | |
* | |
* @event dblclick | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent} data | |
*/ | |
/** | |
* Fired when the right button of the mouse is clicked or the context menu key is pressed within the map. | |
* | |
* @event contextmenu | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent} data | |
*/ | |
/** | |
* Fired immediately after all necessary resources have been downloaded | |
* and the first visually complete rendering of the map has occurred. | |
* | |
* @event load | |
* @memberof Map | |
* @instance | |
* @type {Object} | |
*/ | |
/** | |
* Fired just before the map begins a transition from one | |
* view to another, as the result of either user interaction or methods such as [Map#jumpTo](#Map#jumpTo). | |
* | |
* @event movestart | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired repeatedly during an animated transition from one view to | |
* another, as the result of either user interaction or methods such as [Map#flyTo](#Map#flyTo). | |
* | |
* @event move | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
/** | |
* Fired just after the map completes a transition from one | |
* view to another, as the result of either user interaction or methods such as [Map#jumpTo](#Map#jumpTo). | |
* | |
* @event moveend | |
* @memberof Map | |
* @instance | |
* @property {MapMouseEvent | MapTouchEvent} data | |
*/ | |
},{"../geo/lng_lat":10,"../geo/lng_lat_bounds":11,"../geo/transform":12,"../render/painter":26,"../style/animation_loop":42,"../style/style":45,"../util/browser":93,"../util/canvas":94,"../util/dom":96,"../util/evented":100,"../util/util":108,"./bind_handlers":74,"./camera":75,"./control/attribution":76,"./hash":87,"point-geometry":177}],89:[function(require,module,exports){ | |
/* eslint-disable */ | |
'use strict'; | |
module.exports = Marker; | |
var DOM = require('../util/dom'); | |
var LngLat = require('../geo/lng_lat'); | |
var Point = require('point-geometry'); | |
/** | |
* Creates a marker component | |
* @class Marker | |
* @param {HTMLElement=} element DOM element to use as a marker (creates a div element by default) | |
* @param {Object=} options | |
* @param {PointLike=} options.offset The offset in pixels as a [`PointLike`](#PointLike) object to apply relative to the element's top left corner. Negatives indicate left and up. | |
* @example | |
* var marker = new mapboxgl.Marker() | |
* .setLngLat([30.5, 50.5]) | |
* .addTo(map); | |
*/ | |
function Marker(element, options) { | |
if (!element) { | |
element = DOM.create('div'); | |
} | |
element.classList.add('mapboxgl-marker'); | |
this._el = element; | |
this._offset = Point.convert(options && options.offset || [0, 0]); | |
this._update = this._update.bind(this); | |
} | |
Marker.prototype = { | |
/** | |
* Attaches the marker to a map | |
* @param {Map} map | |
* @returns {Marker} `this` | |
*/ | |
addTo: function(map) { | |
this.remove(); | |
this._map = map; | |
map.getCanvasContainer().appendChild(this._el); | |
map.on('move', this._update); | |
this._update(); | |
return this; | |
}, | |
/** | |
* Removes the marker from a map | |
* @example | |
* var marker = new mapboxgl.Marker().addTo(map); | |
* marker.remove(); | |
* @returns {Marker} `this` | |
*/ | |
remove: function() { | |
if (this._map) { | |
this._map.off('move', this._update); | |
this._map = null; | |
} | |
var parent = this._el.parentNode; | |
if (parent) parent.removeChild(this._el); | |
return this; | |
}, | |
/** | |
* Get the marker's geographical location | |
* @returns {LngLat} | |
*/ | |
getLngLat: function() { | |
return this._lngLat; | |
}, | |
/** | |
* Set the marker's geographical position and move it. | |
* @param {LngLat} lnglat | |
* @returns {Popup} `this` | |
*/ | |
setLngLat: function(lnglat) { | |
this._lngLat = LngLat.convert(lnglat); | |
this._update(); | |
return this; | |
}, | |
getElement: function() { | |
return this._el; | |
}, | |
_update: function() { | |
if (!this._map) return; | |
var pos = this._map.project(this._lngLat).add(this._offset); | |
DOM.setTransform(this._el, 'translate(' + pos.x + 'px,' + pos.y + 'px)'); | |
} | |
}; | |
},{"../geo/lng_lat":10,"../util/dom":96,"point-geometry":177}],90:[function(require,module,exports){ | |
'use strict'; | |
module.exports = Popup; | |
var util = require('../util/util'); | |
var Evented = require('../util/evented'); | |
var DOM = require('../util/dom'); | |
var LngLat = require('../geo/lng_lat'); | |
/** | |
* A popup component. | |
* | |
* @class Popup | |
* @param {Object} [options] | |
* @param {boolean} [options.closeButton=true] If `true`, a close button will appear in the | |
* top right corner of the popup. | |
* @param {boolean} [options.closeOnClick=true] If `true`, the popup will closed when the | |
* map is clicked. | |
* @param {string} options.anchor - A string indicating the popup's location relative to | |
* the coordinate set via [Popup#setLngLat](#Popup#setLngLat). | |
* Options are `'top'`, `'bottom'`, `'left'`, `'right'`, `'top-left'`, | |
* `'top-right'`, `'bottom-left'`, and `'bottom-right'`. | |
* @example | |
* var popup = new mapboxgl.Popup() | |
* .setLngLat(e.lngLat) | |
* .setHTML("<h1>Hello World!</h1>") | |
* .addTo(map); | |
*/ | |
function Popup(options) { | |
util.setOptions(this, options); | |
util.bindAll([ | |
'_update', | |
'_onClickClose'], | |
this); | |
} | |
Popup.prototype = util.inherit(Evented, /** @lends Popup.prototype */{ | |
options: { | |
closeButton: true, | |
closeOnClick: true | |
}, | |
/** | |
* Adds the popup to a map. | |
* | |
* @param {Map} map The Mapbox GL JS map to add the popup to. | |
* @returns {Popup} `this` | |
*/ | |
addTo: function(map) { | |
this._map = map; | |
this._map.on('move', this._update); | |
if (this.options.closeOnClick) { | |
this._map.on('click', this._onClickClose); | |
} | |
this._update(); | |
return this; | |
}, | |
/** | |
* Removes the popup from the map it has been added to. | |
* | |
* @example | |
* var popup = new mapboxgl.Popup().addTo(map); | |
* popup.remove(); | |
* @returns {Popup} `this` | |
*/ | |
remove: function() { | |
if (this._content && this._content.parentNode) { | |
this._content.parentNode.removeChild(this._content); | |
} | |
if (this._container) { | |
this._container.parentNode.removeChild(this._container); | |
delete this._container; | |
} | |
if (this._map) { | |
this._map.off('move', this._update); | |
this._map.off('click', this._onClickClose); | |
delete this._map; | |
} | |
return this; | |
}, | |
/** | |
* Returns the geographical location of the popup's anchor. | |
* | |
* @returns {LngLat} The geographical location of the popup's anchor. | |
*/ | |
getLngLat: function() { | |
return this._lngLat; | |
}, | |
/** | |
* Sets the geographical location of the popup's anchor, and moves the popup to it. | |
* | |
* @param {LngLatLike} lnglat The geographical location to set as the popup's anchor. | |
* @returns {Popup} `this` | |
*/ | |
setLngLat: function(lnglat) { | |
this._lngLat = LngLat.convert(lnglat); | |
this._update(); | |
return this; | |
}, | |
/** | |
* Sets the popup's content to a string of text. | |
* | |
* This function creates a [Text](https://developer.mozilla.org/en-US/docs/Web/API/Text) node in the DOM, | |
* so it cannot insert raw HTML. Use this method for security against XSS | |
* if the popup content is user-provided. | |
* | |
* @param {string} text Textual content for the popup. | |
* @returns {Popup} `this` | |
* @example | |
* var popup = new mapboxgl.Popup() | |
* .setLngLat(e.lngLat) | |
* .setText('Hello, world!') | |
* .addTo(map); | |
*/ | |
setText: function(text) { | |
return this.setDOMContent(document.createTextNode(text)); | |
}, | |
/** | |
* Sets the popup's content to the HTML provided as a string. | |
* | |
* @param {string} html A string representing HTML content for the popup. | |
* @returns {Popup} `this` | |
*/ | |
setHTML: function(html) { | |
var frag = document.createDocumentFragment(); | |
var temp = document.createElement('body'), child; | |
temp.innerHTML = html; | |
while (true) { | |
child = temp.firstChild; | |
if (!child) break; | |
frag.appendChild(child); | |
} | |
return this.setDOMContent(frag); | |
}, | |
/** | |
* Sets the popup's content to the element provided as a DOM node. | |
* | |
* @param {Node} htmlNode A DOM node to be used as content for the popup. | |
* @returns {Popup} `this` | |
* @example | |
* // create an element with the popup content | |
* var div = document.createElement('div'); | |
* div.innerHTML = 'Hello, world!'; | |
* var popup = new mapboxgl.Popup() | |
* .setLngLat(e.lngLat) | |
* .setDOMContent(div) | |
* .addTo(map); | |
*/ | |
setDOMContent: function(htmlNode) { | |
this._createContent(); | |
this._content.appendChild(htmlNode); | |
this._update(); | |
return this; | |
}, | |
_createContent: function() { | |
if (this._content && this._content.parentNode) { | |
this._content.parentNode.removeChild(this._content); | |
} | |
this._content = DOM.create('div', 'mapboxgl-popup-content', this._container); | |
if (this.options.closeButton) { | |
this._closeButton = DOM.create('button', 'mapboxgl-popup-close-button', this._content); | |
this._closeButton.innerHTML = '×'; | |
this._closeButton.addEventListener('click', this._onClickClose); | |
} | |
}, | |
_update: function() { | |
if (!this._map || !this._lngLat || !this._content) { return; } | |
if (!this._container) { | |
this._container = DOM.create('div', 'mapboxgl-popup', this._map.getContainer()); | |
this._tip = DOM.create('div', 'mapboxgl-popup-tip', this._container); | |
this._container.appendChild(this._content); | |
} | |
var pos = this._map.project(this._lngLat).round(), | |
anchor = this.options.anchor; | |
if (!anchor) { | |
var width = this._container.offsetWidth, | |
height = this._container.offsetHeight; | |
if (pos.y < height) { | |
anchor = ['top']; | |
} else if (pos.y > this._map.transform.height - height) { | |
anchor = ['bottom']; | |
} else { | |
anchor = []; | |
} | |
if (pos.x < width / 2) { | |
anchor.push('left'); | |
} else if (pos.x > this._map.transform.width - width / 2) { | |
anchor.push('right'); | |
} | |
if (anchor.length === 0) { | |
anchor = 'bottom'; | |
} else { | |
anchor = anchor.join('-'); | |
} | |
} | |
var anchorTranslate = { | |
'top': 'translate(-50%,0)', | |
'top-left': 'translate(0,0)', | |
'top-right': 'translate(-100%,0)', | |
'bottom': 'translate(-50%,-100%)', | |
'bottom-left': 'translate(0,-100%)', | |
'bottom-right': 'translate(-100%,-100%)', | |
'left': 'translate(0,-50%)', | |
'right': 'translate(-100%,-50%)' | |
}; | |
var classList = this._container.classList; | |
for (var key in anchorTranslate) { | |
classList.remove('mapboxgl-popup-anchor-' + key); | |
} | |
classList.add('mapboxgl-popup-anchor-' + anchor); | |
DOM.setTransform(this._container, anchorTranslate[anchor] + ' translate(' + pos.x + 'px,' + pos.y + 'px)'); | |
}, | |
_onClickClose: function() { | |
this.remove(); | |
} | |
}); | |
},{"../geo/lng_lat":10,"../util/dom":96,"../util/evented":100,"../util/util":108}],91:[function(require,module,exports){ | |
'use strict'; | |
module.exports = Actor; | |
/** | |
* An implementation of the [Actor design pattern](http://en.wikipedia.org/wiki/Actor_model) | |
* that maintains the relationship between asynchronous tasks and the objects | |
* that spin them off - in this case, tasks like parsing parts of styles, | |
* owned by the styles | |
* | |
* @param {WebWorker} target | |
* @param {WebWorker} parent | |
* @private | |
*/ | |
function Actor(target, parent) { | |
this.target = target; | |
this.parent = parent; | |
this.callbacks = {}; | |
this.callbackID = 0; | |
this.receive = this.receive.bind(this); | |
this.target.addEventListener('message', this.receive, false); | |
} | |
Actor.prototype.receive = function(message) { | |
var data = message.data, | |
callback; | |
if (data.type === '<response>') { | |
callback = this.callbacks[data.id]; | |
delete this.callbacks[data.id]; | |
callback(data.error || null, data.data); | |
} else if (typeof data.id !== 'undefined') { | |
var id = data.id; | |
this.parent[data.type](data.data, function(err, data, buffers) { | |
this.postMessage({ | |
type: '<response>', | |
id: String(id), | |
error: err ? String(err) : null, | |
data: data | |
}, buffers); | |
}.bind(this)); | |
} else { | |
this.parent[data.type](data.data); | |
} | |
}; | |
Actor.prototype.send = function(type, data, callback, buffers) { | |
var id = null; | |
if (callback) this.callbacks[id = this.callbackID++] = callback; | |
this.postMessage({ type: type, id: String(id), data: data }, buffers); | |
}; | |
/** | |
* Wrapped postMessage API that abstracts around IE's lack of | |
* `transferList` support. | |
* | |
* @param {Object} message | |
* @param {Object} transferList | |
* @private | |
*/ | |
Actor.prototype.postMessage = function(message, transferList) { | |
this.target.postMessage(message, transferList); | |
}; | |
},{}],92:[function(require,module,exports){ | |
'use strict'; | |
exports.getJSON = function(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.setRequestHeader('Accept', 'application/json'); | |
xhr.onerror = function(e) { | |
callback(e); | |
}; | |
xhr.onload = function() { | |
if (xhr.status >= 200 && xhr.status < 300 && xhr.response) { | |
var data; | |
try { | |
data = JSON.parse(xhr.response); | |
} catch (err) { | |
return callback(err); | |
} | |
callback(null, data); | |
} else { | |
callback(new Error(xhr.statusText)); | |
} | |
}; | |
xhr.send(); | |
return xhr; | |
}; | |
exports.getArrayBuffer = function(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.responseType = 'arraybuffer'; | |
xhr.onerror = function(e) { | |
callback(e); | |
}; | |
xhr.onload = function() { | |
if (xhr.status >= 200 && xhr.status < 300 && xhr.response) { | |
callback(null, xhr.response); | |
} else { | |
callback(new Error(xhr.statusText)); | |
} | |
}; | |
xhr.send(); | |
return xhr; | |
}; | |
function sameOrigin(url) { | |
var a = document.createElement('a'); | |
a.href = url; | |
return a.protocol === document.location.protocol && a.host === document.location.host; | |
} | |
exports.getImage = function(url, callback) { | |
return exports.getArrayBuffer(url, function(err, imgData) { | |
if (err) return callback(err); | |
var img = new Image(); | |
img.onload = function() { | |
callback(null, img); | |
(window.URL || window.webkitURL).revokeObjectURL(img.src); | |
}; | |
var blob = new Blob([new Uint8Array(imgData)], { type: 'image/png' }); | |
img.src = (window.URL || window.webkitURL).createObjectURL(blob); | |
img.getData = function() { | |
var canvas = document.createElement('canvas'); | |
var context = canvas.getContext('2d'); | |
canvas.width = img.width; | |
canvas.height = img.height; | |
context.drawImage(img, 0, 0); | |
return context.getImageData(0, 0, img.width, img.height).data; | |
}; | |
return img; | |
}); | |
}; | |
exports.getVideo = function(urls, callback) { | |
var video = document.createElement('video'); | |
video.onloadstart = function() { | |
callback(null, video); | |
}; | |
for (var i = 0; i < urls.length; i++) { | |
var s = document.createElement('source'); | |
if (!sameOrigin(urls[i])) { | |
video.crossOrigin = 'Anonymous'; | |
} | |
s.src = urls[i]; | |
video.appendChild(s); | |
} | |
video.getData = function() { return video; }; | |
return video; | |
}; | |
},{}],93:[function(require,module,exports){ | |
'use strict'; | |
/** | |
* Unlike js/util/browser.js, this code is written with the expectation | |
* of a browser environment with a global 'window' object | |
* @module browser | |
* @private | |
*/ | |
exports.window = window; | |
/** | |
* Provides a function that outputs milliseconds: either performance.now() | |
* or a fallback to Date.now() | |
*/ | |
module.exports.now = (function() { | |
if (window.performance && | |
window.performance.now) { | |
return window.performance.now.bind(window.performance); | |
} else { | |
return Date.now.bind(Date); | |
} | |
}()); | |
var frame = window.requestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.msRequestAnimationFrame; | |
exports.frame = function(fn) { | |
return frame(fn); | |
}; | |
var cancel = window.cancelAnimationFrame || | |
window.mozCancelAnimationFrame || | |
window.webkitCancelAnimationFrame || | |
window.msCancelAnimationFrame; | |
exports.cancelFrame = function(id) { | |
cancel(id); | |
}; | |
exports.timed = function (fn, dur, ctx) { | |
if (!dur) { | |
fn.call(ctx, 1); | |
return null; | |
} | |
var abort = false, | |
start = module.exports.now(); | |
function tick(now) { | |
if (abort) return; | |
now = module.exports.now(); | |
if (now >= start + dur) { | |
fn.call(ctx, 1); | |
} else { | |
fn.call(ctx, (now - start) / dur); | |
exports.frame(tick); | |
} | |
} | |
exports.frame(tick); | |
return function() { abort = true; }; | |
}; | |
/** | |
* Test if the current browser supports Mapbox GL JS | |
* @param {Object} options | |
* @param {boolean} [options.failIfMajorPerformanceCaveat=false] Return `false` | |
* if the performance of Mapbox GL JS would be dramatically worse than | |
* expected (i.e. a software renderer would be used) | |
* @return {boolean} | |
*/ | |
exports.supported = require('mapbox-gl-supported'); | |
exports.hardwareConcurrency = navigator.hardwareConcurrency || 4; | |
Object.defineProperty(exports, 'devicePixelRatio', { | |
get: function() { return window.devicePixelRatio; } | |
}); | |
exports.supportsWebp = false; | |
var webpImgTest = document.createElement('img'); | |
webpImgTest.onload = function() { | |
exports.supportsWebp = true; | |
}; | |
webpImgTest.src = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAQAAAAfQ//73v/+BiOh/AAA='; | |
exports.supportsGeolocation = !!navigator.geolocation; | |
},{"mapbox-gl-supported":173}],94:[function(require,module,exports){ | |
'use strict'; | |
var util = require('../util'); | |
var isSupported = require('mapbox-gl-supported'); | |
module.exports = Canvas; | |
function Canvas(parent, container) { | |
this.canvas = document.createElement('canvas'); | |
if (parent && container) { | |
this.canvas.style.position = 'absolute'; | |
this.canvas.classList.add('mapboxgl-canvas'); | |
this.canvas.addEventListener('webglcontextlost', parent._contextLost.bind(parent), false); | |
this.canvas.addEventListener('webglcontextrestored', parent._contextRestored.bind(parent), false); | |
this.canvas.setAttribute('tabindex', 0); | |
container.appendChild(this.canvas); | |
} | |
} | |
Canvas.prototype.resize = function(width, height) { | |
var pixelRatio = window.devicePixelRatio || 1; | |
// Request the required canvas size taking the pixelratio into account. | |
this.canvas.width = pixelRatio * width; | |
this.canvas.height = pixelRatio * height; | |
// Maintain the same canvas size, potentially downscaling it for HiDPI displays | |
this.canvas.style.width = width + 'px'; | |
this.canvas.style.height = height + 'px'; | |
}; | |
Canvas.prototype.getWebGLContext = function(attributes) { | |
attributes = util.extend({}, attributes, isSupported.webGLContextAttributes); | |
return this.canvas.getContext('webgl', attributes) || | |
this.canvas.getContext('experimental-webgl', attributes); | |
}; | |
Canvas.prototype.getElement = function() { | |
return this.canvas; | |
}; | |
},{"../util":108,"mapbox-gl-supported":173}],95:[function(require,module,exports){ | |
'use strict'; | |
var Actor = require('../actor'); | |
var WebWorkify = require('webworkify'); | |
module.exports = Dispatcher; | |
function Dispatcher(length, parent) { | |
this.actors = []; | |
this.currentActor = 0; | |
for (var i = 0; i < length; i++) { | |
var worker = new WebWorkify(require('../../source/worker')); | |
var actor = new Actor(worker, parent); | |
actor.name = "Worker " + i; | |
this.actors.push(actor); | |
} | |
} | |
Dispatcher.prototype = { | |
broadcast: function(type, data) { | |
for (var i = 0; i < this.actors.length; i++) { | |
this.actors[i].send(type, data); | |
} | |
}, | |
send: function(type, data, callback, targetID, buffers) { | |
if (typeof targetID !== 'number' || isNaN(targetID)) { | |
// Use round robin to send requests to web workers. | |
targetID = this.currentActor = (this.currentActor + 1) % this.actors.length; | |
} | |
this.actors[targetID].send(type, data, callback, buffers); | |
return targetID; | |
}, | |
remove: function() { | |
for (var i = 0; i < this.actors.length; i++) { | |
this.actors[i].target.terminate(); | |
} | |
this.actors = []; | |
} | |
}; | |
},{"../../source/worker":40,"../actor":91,"webworkify":194}],96:[function(require,module,exports){ | |
'use strict'; | |
var Point = require('point-geometry'); | |
exports.create = function (tagName, className, container) { | |
var el = document.createElement(tagName); | |
if (className) el.className = className; | |
if (container) container.appendChild(el); | |
return el; | |
}; | |
var docStyle = document.documentElement.style; | |
function testProp(props) { | |
for (var i = 0; i < props.length; i++) { | |
if (props[i] in docStyle) { | |
return props[i]; | |
} | |
} | |
} | |
var selectProp = testProp(['userSelect', 'MozUserSelect', 'WebkitUserSelect', 'msUserSelect']), | |
userSelect; | |
exports.disableDrag = function () { | |
if (selectProp) { | |
userSelect = docStyle[selectProp]; | |
docStyle[selectProp] = 'none'; | |
} | |
}; | |
exports.enableDrag = function () { | |
if (selectProp) { | |
docStyle[selectProp] = userSelect; | |
} | |
}; | |
var transformProp = testProp(['transform', 'WebkitTransform']); | |
exports.setTransform = function(el, value) { | |
el.style[transformProp] = value; | |
}; | |
// Suppress the next click, but only if it's immediate. | |
function suppressClick(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
window.removeEventListener('click', suppressClick, true); | |
} | |
exports.suppressClick = function() { | |
window.addEventListener('click', suppressClick, true); | |
window.setTimeout(function() { | |
window.removeEventListener('click', suppressClick, true); | |
}, 0); | |
}; | |
exports.mousePos = function (el, e) { | |
var rect = el.getBoundingClientRect(); | |
e = e.touches ? e.touches[0] : e; | |
return new Point( | |
e.clientX - rect.left - el.clientLeft, | |
e.clientY - rect.top - el.clientTop | |
); | |
}; | |
exports.touchPos = function (el, e) { | |
var rect = el.getBoundingClientRect(), | |
points = []; | |
for (var i = 0; i < e.touches.length; i++) { | |
points.push(new Point( | |
e.touches[i].clientX - rect.left - el.clientLeft, | |
e.touches[i].clientY - rect.top - el.clientTop | |
)); | |
} | |
return points; | |
}; | |
},{"point-geometry":177}],97:[function(require,module,exports){ | |
'use strict'; | |
var quickselect = require('quickselect'); | |
// classifies an array of rings into polygons with outer rings and holes | |
module.exports = function classifyRings(rings, maxRings) { | |
var len = rings.length; | |
if (len <= 1) return [rings]; | |
var polygons = [], | |
polygon, | |
ccw; | |
for (var i = 0; i < len; i++) { | |
var area = calculateSignedArea(rings[i]); | |
if (area === 0) continue; | |
rings[i].area = Math.abs(area); | |
if (ccw === undefined) ccw = area < 0; | |
if (ccw === area < 0) { | |
if (polygon) polygons.push(polygon); | |
polygon = [rings[i]]; | |
} else { | |
polygon.push(rings[i]); | |
} | |
} | |
if (polygon) polygons.push(polygon); | |
// Earcut performance degrages with the # of rings in a polygon. For this | |
// reason, we limit strip out all but the `maxRings` largest rings. | |
if (maxRings > 1) { | |
for (var j = 0; j < polygons.length; j++) { | |
if (polygons[j].length <= maxRings) continue; | |
quickselect(polygons[j], maxRings, 1, polygons[j].length - 1, compareAreas); | |
polygons[j] = polygons[j].slice(0, maxRings); | |
} | |
} | |
return polygons; | |
}; | |
function compareAreas(a, b) { | |
return b.area - a.area; | |
} | |
function calculateSignedArea(ring) { | |
var sum = 0; | |
for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) { | |
p1 = ring[i]; | |
p2 = ring[j]; | |
sum += (p2.x - p1.x) * (p1.y + p2.y); | |
} | |
return sum; | |
} | |
},{"quickselect":178}],98:[function(require,module,exports){ | |
'use strict'; | |
module.exports = { | |
API_URL: 'https://api.mapbox.com', | |
REQUIRE_ACCESS_TOKEN: true | |
}; | |
},{}],99:[function(require,module,exports){ | |
'use strict'; | |
var assert = require('assert'); | |
module.exports = DictionaryCoder; | |
function DictionaryCoder(strings) { | |
this._stringToNumber = {}; | |
this._numberToString = []; | |
for (var i = 0; i < strings.length; i++) { | |
var string = strings[i]; | |
this._stringToNumber[string] = i; | |
this._numberToString[i] = string; | |
} | |
} | |
DictionaryCoder.prototype.encode = function(string) { | |
assert(string in this._stringToNumber); | |
return this._stringToNumber[string]; | |
}; | |
DictionaryCoder.prototype.decode = function(n) { | |
assert(n < this._numberToString.length); | |
return this._numberToString[n]; | |
}; | |
},{"assert":110}],100:[function(require,module,exports){ | |
'use strict'; | |
var util = require('./util'); | |
/** | |
* Methods mixed in to other classes for event capabilities. | |
* | |
* @mixin Evented | |
*/ | |
var Evented = { | |
/** | |
* Adds a listener to a specified event type. | |
* | |
* @param {string} type The event type to add a listen for. | |
* @param {Function} listener The function to be called when the event is fired. | |
* The listener function is called with the data object passed to `fire`, | |
* extended with `target` and `type` properties. | |
* @returns {Object} `this` | |
*/ | |
on: function(type, listener) { | |
this._events = this._events || {}; | |
this._events[type] = this._events[type] || []; | |
this._events[type].push(listener); | |
return this; | |
}, | |
/** | |
* Removes a previously registered event listener. | |
* | |
* @param {string} [type] The event type to remove listeners for. | |
* If none is specified, listeners will be removed for all event types. | |
* @param {Function} [listener] The listener function to remove. | |
* If none is specified, all listeners will be removed for the event type. | |
* @returns {Object} `this` | |
*/ | |
off: function(type, listener) { | |
if (!type) { | |
// clear all listeners if no arguments specified | |
delete this._events; | |
return this; | |
} | |
if (!this.listens(type)) return this; | |
if (listener) { | |
var idx = this._events[type].indexOf(listener); | |
if (idx >= 0) { | |
this._events[type].splice(idx, 1); | |
} | |
if (!this._events[type].length) { | |
delete this._events[type]; | |
} | |
} else { | |
delete this._events[type]; | |
} | |
return this; | |
}, | |
/** | |
* Adds a listener that will be called only once to a specified event type. | |
* | |
* The listener will be called first time the event fires after the listener is registered. | |
* | |
* @param {string} type The event type to listen for. | |
* @param {Function} listener The function to be called when the event is fired the first time. | |
* @returns {Object} `this` | |
*/ | |
once: function(type, listener) { | |
var wrapper = function(data) { | |
this.off(type, wrapper); | |
listener.call(this, data); | |
}.bind(this); | |
this.on(type, wrapper); | |
return this; | |
}, | |
/** | |
* Fires an event of the specified type. | |
* | |
* @param {string} type The type of event to fire. | |
* @param {Object} [data] Data to be passed to any listeners. | |
* @returns {Object} `this` | |
*/ | |
fire: function(type, data) { | |
if (!this.listens(type)) return this; | |
data = util.extend({}, data); | |
util.extend(data, {type: type, target: this}); | |
// make sure adding/removing listeners inside other listeners won't cause infinite loop | |
var listeners = this._events[type].slice(); | |
for (var i = 0; i < listeners.length; i++) { | |
listeners[i].call(this, data); | |
} | |
return this; | |
}, | |
/** | |
* Returns a Boolean indicating whether any listeners are registered for a specified event type. | |
* | |
* @param {string} type The event type to check. | |
* @returns {boolean} `true` if there is at least one registered listener for specified event type. | |
*/ | |
listens: function(type) { | |
return !!(this._events && this._events[type]); | |
} | |
}; | |
module.exports = Evented; | |
},{"./util":108}],101:[function(require,module,exports){ | |
'use strict'; | |
module.exports = Glyphs; | |
function Glyphs(pbf, end) { | |
this.stacks = pbf.readFields(readFontstacks, [], end); | |
} | |
function readFontstacks(tag, stacks, pbf) { | |
if (tag === 1) { | |
var fontstack = pbf.readMessage(readFontstack, {glyphs: {}}); | |
stacks.push(fontstack); | |
} | |
} | |
function readFontstack(tag, fontstack, pbf) { | |
if (tag === 1) fontstack.name = pbf.readString(); | |
else if (tag === 2) fontstack.range = pbf.readString(); | |
else if (tag === 3) { | |
var glyph = pbf.readMessage(readGlyph, {}); | |
fontstack.glyphs[glyph.id] = glyph; | |
} | |
} | |
function readGlyph(tag, glyph, pbf) { | |
if (tag === 1) glyph.id = pbf.readVarint(); | |
else if (tag === 2) glyph.bitmap = pbf.readBytes(); | |
else if (tag === 3) glyph.width = pbf.readVarint(); | |
else if (tag === 4) glyph.height = pbf.readVarint(); | |
else if (tag === 5) glyph.left = pbf.readSVarint(); | |
else if (tag === 6) glyph.top = pbf.readSVarint(); | |
else if (tag === 7) glyph.advance = pbf.readVarint(); | |
} | |
},{}],102:[function(require,module,exports){ | |
'use strict'; | |
module.exports = interpolate; | |
function interpolate(a, b, t) { | |
return (a * (1 - t)) + (b * t); | |
} | |
interpolate.number = interpolate; | |
interpolate.vec2 = function(from, to, t) { | |
return [ | |
interpolate(from[0], to[0], t), | |
interpolate(from[1], to[1], t) | |
]; | |
}; | |
/* | |
* Interpolate between two colors given as 4-element arrays. | |
* | |
* @param {Color} from | |
* @param {Color} to | |
* @param {number} t interpolation factor between 0 and 1 | |
* @returns {Color} interpolated color | |
*/ | |
interpolate.color = function(from, to, t) { | |
return [ | |
interpolate(from[0], to[0], t), | |
interpolate(from[1], to[1], t), | |
interpolate(from[2], to[2], t), | |
interpolate(from[3], to[3], t) | |
]; | |
}; | |
interpolate.array = function(from, to, t) { | |
return from.map(function(d, i) { | |
return interpolate(d, to[i], t); | |
}); | |
}; | |
},{}],103:[function(require,module,exports){ | |
'use strict'; | |
module.exports = { | |
multiPolygonIntersectsBufferedMultiPoint: multiPolygonIntersectsBufferedMultiPoint, | |
multiPolygonIntersectsMultiPolygon: multiPolygonIntersectsMultiPolygon, | |
multiPolygonIntersectsBufferedMultiLine: multiPolygonIntersectsBufferedMultiLine | |
}; | |
function multiPolygonIntersectsBufferedMultiPoint(multiPolygon, rings, radius) { | |
for (var j = 0; j < multiPolygon.length; j++) { | |
var polygon = multiPolygon[j]; | |
for (var i = 0; i < rings.length; i++) { | |
var ring = rings[i]; | |
for (var k = 0; k < ring.length; k++) { | |
var point = ring[k]; | |
if (polygonContainsPoint(polygon, point)) return true; | |
if (pointIntersectsBufferedLine(point, polygon, radius)) return true; | |
} | |
} | |
} | |
return false; | |
} | |
function multiPolygonIntersectsMultiPolygon(multiPolygonA, multiPolygonB) { | |
if (multiPolygonA.length === 1 && multiPolygonA[0].length === 1) { | |
return multiPolygonContainsPoint(multiPolygonB, multiPolygonA[0][0]); | |
} | |
for (var m = 0; m < multiPolygonB.length; m++) { | |
var ring = multiPolygonB[m]; | |
for (var n = 0; n < ring.length; n++) { | |
if (multiPolygonContainsPoint(multiPolygonA, ring[n])) return true; | |
} | |
} | |
for (var j = 0; j < multiPolygonA.length; j++) { | |
var polygon = multiPolygonA[j]; | |
for (var i = 0; i < polygon.length; i++) { | |
if (multiPolygonContainsPoint(multiPolygonB, polygon[i])) return true; | |
} | |
for (var k = 0; k < multiPolygonB.length; k++) { | |
if (lineIntersectsLine(polygon, multiPolygonB[k])) return true; | |
} | |
} | |
return false; | |
} | |
function multiPolygonIntersectsBufferedMultiLine(multiPolygon, multiLine, radius) { | |
for (var i = 0; i < multiLine.length; i++) { | |
var line = multiLine[i]; | |
for (var j = 0; j < multiPolygon.length; j++) { | |
var polygon = multiPolygon[j]; | |
if (polygon.length >= 3) { | |
for (var k = 0; k < line.length; k++) { | |
if (polygonContainsPoint(polygon, line[k])) return true; | |
} | |
} | |
if (lineIntersectsBufferedLine(polygon, line, radius)) return true; | |
} | |
} | |
return false; | |
} | |
function lineIntersectsBufferedLine(lineA, lineB, radius) { | |
if (lineA.length > 1) { | |
if (lineIntersectsLine(lineA, lineB)) return true; | |
// Check whether any point in either line is within radius of the other line | |
for (var j = 0; j < lineB.length; j++) { | |
if (pointIntersectsBufferedLine(lineB[j], lineA, radius)) return true; | |
} | |
} | |
for (var k = 0; k < lineA.length; k++) { | |
if (pointIntersectsBufferedLine(lineA[k], lineB, radius)) return true; | |
} | |
return false; | |
} | |
function lineIntersectsLine(lineA, lineB) { | |
for (var i = 0; i < lineA.length - 1; i++) { | |
var a0 = lineA[i]; | |
var a1 = lineA[i + 1]; | |
for (var j = 0; j < lineB.length - 1; j++) { | |
var b0 = lineB[j]; | |
var b1 = lineB[j + 1]; | |
if (lineSegmentIntersectsLineSegment(a0, a1, b0, b1)) return true; | |
} | |
} | |
return false; | |
} | |
// http://bryceboe.com/2006/10/23/line-segment-intersection-algorithm/ | |
function isCounterClockwise(a, b, c) { | |
return (c.y - a.y) * (b.x - a.x) > (b.y - a.y) * (c.x - a.x); | |
} | |
function lineSegmentIntersectsLineSegment(a0, a1, b0, b1) { | |
return isCounterClockwise(a0, b0, b1) !== isCounterClockwise(a1, b0, b1) && | |
isCounterClockwise(a0, a1, b0) !== isCounterClockwise(a0, a1, b1); | |
} | |
function pointIntersectsBufferedLine(p, line, radius) { | |
var radiusSquared = radius * radius; | |
if (line.length === 1) return p.distSqr(line[0]) < radiusSquared; | |
for (var i = 1; i < line.length; i++) { | |
// Find line segments that have a distance <= radius^2 to p | |
// In that case, we treat the line as "containing point p". | |
var v = line[i - 1], w = line[i]; | |
if (distToSegmentSquared(p, v, w) < radiusSquared) return true; | |
} | |
return false; | |
} | |
// Code from http://stackoverflow.com/a/1501725/331379. | |
function distToSegmentSquared(p, v, w) { | |
var l2 = v.distSqr(w); | |
if (l2 === 0) return p.distSqr(v); | |
var t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2; | |
if (t < 0) return p.distSqr(v); | |
if (t > 1) return p.distSqr(w); | |
return p.distSqr(w.sub(v)._mult(t)._add(v)); | |
} | |
// point in polygon ray casting algorithm | |
function multiPolygonContainsPoint(rings, p) { | |
var c = false, | |
ring, p1, p2; | |
for (var k = 0; k < rings.length; k++) { | |
ring = rings[k]; | |
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { | |
p1 = ring[i]; | |
p2 = ring[j]; | |
if (((p1.y > p.y) !== (p2.y > p.y)) && (p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x)) { | |
c = !c; | |
} | |
} | |
} | |
return c; | |
} | |
function polygonContainsPoint(ring, p) { | |
var c = false; | |
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { | |
var p1 = ring[i]; | |
var p2 = ring[j]; | |
if (((p1.y > p.y) !== (p2.y > p.y)) && (p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x)) { | |
c = !c; | |
} | |
} | |
return c; | |
} | |
},{}],104:[function(require,module,exports){ | |
'use strict'; | |
module.exports = LRUCache; | |
/** | |
* A [least-recently-used cache](http://en.wikipedia.org/wiki/Cache_algorithms) | |
* with hash lookup made possible by keeping a list of keys in parallel to | |
* an array of dictionary of values | |
* | |
* @param {number} max number of permitted values | |
* @param {Function} onRemove callback called with items when they expire | |
* @private | |
*/ | |
function LRUCache(max, onRemove) { | |
this.max = max; | |
this.onRemove = onRemove; | |
this.reset(); | |
} | |
/** | |
* Clear the cache | |
* | |
* @returns {LRUCache} this cache | |
* @private | |
*/ | |
LRUCache.prototype.reset = function() { | |
for (var key in this.data) { | |
this.onRemove(this.data[key]); | |
} | |
this.data = {}; | |
this.order = []; | |
return this; | |
}; | |
/** | |
* Add a key, value combination to the cache, trimming its size if this pushes | |
* it over max length. | |
* | |
* @param {string} key lookup key for the item | |
* @param {*} data any value | |
* | |
* @returns {LRUCache} this cache | |
* @private | |
*/ | |
LRUCache.prototype.add = function(key, data) { | |
if (this.has(key)) { | |
this.order.splice(this.order.indexOf(key), 1); | |
this.data[key] = data; | |
this.order.push(key); | |
} else { | |
this.data[key] = data; | |
this.order.push(key); | |
if (this.order.length > this.max) { | |
var removedData = this.get(this.order[0]); | |
if (removedData) this.onRemove(removedData); | |
} | |
} | |
return this; | |
}; | |
/** | |
* Determine whether the value attached to `key` is present | |
* | |
* @param {string} key the key to be looked-up | |
* @returns {boolean} whether the cache has this value | |
* @private | |
*/ | |
LRUCache.prototype.has = function(key) { | |
return key in this.data; | |
}; | |
/** | |
* List all keys in the cache | |
* | |
* @returns {Array<string>} an array of keys in this cache. | |
* @private | |
*/ | |
LRUCache.prototype.keys = function() { | |
return this.order; | |
}; | |
/** | |
* Get the value attached to a specific key. If the key is not found, | |
* returns `null` | |
* | |
* @param {string} key the key to look up | |
* @returns {*} the data, or null if it isn't found | |
* @private | |
*/ | |
LRUCache.prototype.get = function(key) { | |
if (!this.has(key)) { return null; } | |
var data = this.data[key]; | |
delete this.data[key]; | |
this.order.splice(this.order.indexOf(key), 1); | |
return data; | |
}; | |
/** | |
* Change the max size of the cache. | |
* | |
* @param {number} max the max size of the cache | |
* @returns {LRUCache} this cache | |
* @private | |
*/ | |
LRUCache.prototype.setMaxSize = function(max) { | |
this.max = max; | |
while (this.order.length > this.max) { | |
var removedData = this.get(this.order[0]); | |
if (removedData) this.onRemove(removedData); | |
} | |
return this; | |
}; | |
},{}],105:[function(require,module,exports){ | |
'use strict'; | |
var config = require('./config'); | |
var browser = require('./browser'); | |
var URL = require('url'); | |
var util = require('./util'); | |
function normalizeURL(url, pathPrefix, accessToken) { | |
accessToken = accessToken || config.ACCESS_TOKEN; | |
if (!accessToken && config.REQUIRE_ACCESS_TOKEN) { | |
throw new Error('An API access token is required to use Mapbox GL. ' + | |
'See https://www.mapbox.com/developers/api/#access-tokens'); | |
} | |
url = url.replace(/^mapbox:\/\//, config.API_URL + pathPrefix); | |
url += url.indexOf('?') !== -1 ? '&access_token=' : '?access_token='; | |
if (config.REQUIRE_ACCESS_TOKEN) { | |
if (accessToken[0] === 's') { | |
throw new Error('Use a public access token (pk.*) with Mapbox GL JS, not a secret access token (sk.*). ' + | |
'See https://www.mapbox.com/developers/api/#access-tokens'); | |
} | |
url += accessToken; | |
} | |
return url; | |
} | |
module.exports.normalizeStyleURL = function(url, accessToken) { | |
var urlObject = URL.parse(url); | |
if (urlObject.protocol !== 'mapbox:') { | |
return url; | |
} else { | |
return normalizeURL( | |
'mapbox:/' + urlObject.pathname + formatQuery(urlObject.query), | |
'/styles/v1/', | |
accessToken | |
); | |
} | |
}; | |
module.exports.normalizeSourceURL = function(url, accessToken) { | |
var urlObject = URL.parse(url); | |
if (urlObject.protocol !== 'mapbox:') { | |
return url; | |
} else { | |
// TileJSON requests need a secure flag appended to their URLs so | |
// that the server knows to send SSL-ified resource references. | |
return normalizeURL( | |
url + '.json', | |
'/v4/', | |
accessToken | |
) + '&secure'; | |
} | |
}; | |
module.exports.normalizeGlyphsURL = function(url, accessToken) { | |
var urlObject = URL.parse(url); | |
if (urlObject.protocol !== 'mapbox:') { | |
return url; | |
} else { | |
var user = urlObject.pathname.split('/')[1]; | |
return normalizeURL( | |
'mapbox://' + user + '/{fontstack}/{range}.pbf' + formatQuery(urlObject.query), | |
'/fonts/v1/', | |
accessToken | |
); | |
} | |
}; | |
module.exports.normalizeSpriteURL = function(url, format, extension, accessToken) { | |
var urlObject = URL.parse(url); | |
if (urlObject.protocol !== 'mapbox:') { | |
urlObject.pathname += format + extension; | |
return URL.format(urlObject); | |
} else { | |
return normalizeURL( | |
'mapbox:/' + urlObject.pathname + '/sprite' + format + extension + formatQuery(urlObject.query), | |
'/styles/v1/', | |
accessToken | |
); | |
} | |
}; | |
module.exports.normalizeTileURL = function(tileURL, sourceURL, tileSize) { | |
var tileURLObject = URL.parse(tileURL, true); | |
if (!sourceURL) return tileURL; | |
var sourceURLObject = URL.parse(sourceURL); | |
if (sourceURLObject.protocol !== 'mapbox:') return tileURL; | |
// The v4 mapbox tile API supports 512x512 image tiles only when @2x | |
// is appended to the tile URL. If `tileSize: 512` is specified for | |
// a Mapbox raster source force the @2x suffix even if a non hidpi | |
// device. | |
var extension = browser.supportsWebp ? '.webp' : '$1'; | |
var resolution = (browser.devicePixelRatio >= 2 || tileSize === 512) ? '@2x' : ''; | |
return URL.format({ | |
protocol: tileURLObject.protocol, | |
hostname: tileURLObject.hostname, | |
pathname: tileURLObject.pathname.replace(/(\.(?:png|jpg)\d*)/, resolution + extension), | |
query: replaceTempAccessToken(tileURLObject.query) | |
}); | |
}; | |
function formatQuery(query) { | |
return (query ? '?' + query : ''); | |
} | |
function replaceTempAccessToken(query) { | |
if (query.access_token && query.access_token.slice(0, 3) === 'tk.') { | |
return util.extend({}, query, { | |
'access_token': config.ACCESS_TOKEN | |
}); | |
} else { | |
return query; | |
} | |
} | |
},{"./browser":93,"./config":98,"./util":108,"url":118}],106:[function(require,module,exports){ | |
'use strict'; | |
// Note: all "sizes" are measured in bytes | |
var assert = require('assert'); | |
module.exports = StructArrayType; | |
var viewTypes = { | |
'Int8': Int8Array, | |
'Uint8': Uint8Array, | |
'Uint8Clamped': Uint8ClampedArray, | |
'Int16': Int16Array, | |
'Uint16': Uint16Array, | |
'Int32': Int32Array, | |
'Uint32': Uint32Array, | |
'Float32': Float32Array, | |
'Float64': Float64Array | |
}; | |
/** | |
* @typedef StructMember | |
* @private | |
* @property {string} name | |
* @property {string} type | |
* @property {number} components | |
*/ | |
var structArrayTypeCache = {}; | |
/** | |
* `StructArrayType` is used to create new `StructArray` types. | |
* | |
* `StructArray` provides an abstraction over `ArrayBuffer` and `TypedArray` making it behave like | |
* an array of typed structs. A StructArray is comprised of elements. Each element has a set of | |
* members that are defined when the `StructArrayType` is created. | |
* | |
* StructArrays useful for creating large arrays that: | |
* - can be transferred from workers as a Transferable object | |
* - can be copied cheaply | |
* - use less memory for lower-precision members | |
* - can be used as buffers in WebGL. | |
* | |
* @class StructArrayType | |
* @param {Array.<StructMember>} | |
* @param options | |
* @param {number} options.alignment Use `4` to align members to 4 byte boundaries. Default is 1. | |
* | |
* @example | |
* | |
* var PointArrayType = new StructArrayType({ | |
* members: [ | |
* { type: 'Int16', name: 'x' }, | |
* { type: 'Int16', name: 'y' } | |
* ]}); | |
* | |
* var pointArray = new PointArrayType(); | |
* pointArray.emplaceBack(10, 15); | |
* pointArray.emplaceBack(20, 35); | |
* | |
* point = pointArray.get(0); | |
* assert(point.x === 10); | |
* assert(point.y === 15); | |
* | |
* @private | |
*/ | |
function StructArrayType(options) { | |
var key = JSON.stringify(options); | |
if (structArrayTypeCache[key]) { | |
return structArrayTypeCache[key]; | |
} | |
if (options.alignment === undefined) options.alignment = 1; | |
function StructType() { | |
Struct.apply(this, arguments); | |
} | |
StructType.prototype = Object.create(Struct.prototype); | |
var offset = 0; | |
var maxSize = 0; | |
var usedTypes = ['Uint8']; | |
StructType.prototype.members = options.members.map(function(member) { | |
member = { | |
name: member.name, | |
type: member.type, | |
components: member.components || 1 | |
}; | |
assert(member.name.length); | |
assert(member.type in viewTypes); | |
if (usedTypes.indexOf(member.type) < 0) usedTypes.push(member.type); | |
var typeSize = sizeOf(member.type); | |
maxSize = Math.max(maxSize, typeSize); | |
member.offset = offset = align(offset, Math.max(options.alignment, typeSize)); | |
for (var c = 0; c < member.components; c++) { | |
Object.defineProperty(StructType.prototype, member.name + (member.components === 1 ? '' : c), { | |
get: createGetter(member, c), | |
set: createSetter(member, c) | |
}); | |
} | |
offset += typeSize * member.components; | |
return member; | |
}); | |
StructType.prototype.alignment = options.alignment; | |
StructType.prototype.size = align(offset, Math.max(maxSize, options.alignment)); | |
function StructArrayType() { | |
StructArray.apply(this, arguments); | |
this.members = StructType.prototype.members; | |
} | |
StructArrayType.serialize = serializeStructArrayType; | |
StructArrayType.prototype = Object.create(StructArray.prototype); | |
StructArrayType.prototype.StructType = StructType; | |
StructArrayType.prototype.bytesPerElement = StructType.prototype.size; | |
StructArrayType.prototype.emplaceBack = createEmplaceBack(StructType.prototype.members, StructType.prototype.size); | |
StructArrayType.prototype._usedTypes = usedTypes; | |
structArrayTypeCache[key] = StructArrayType; | |
return StructArrayType; | |
} | |
/** | |
* Serialize the StructArray type. This serializes the *type* not an instance of the type. | |
* @private | |
*/ | |
function serializeStructArrayType() { | |
return { | |
members: this.prototype.StructType.prototype.members, | |
alignment: this.prototype.StructType.prototype.alignment, | |
bytesPerElement: this.prototype.bytesPerElement | |
}; | |
} | |
function align(offset, size) { | |
return Math.ceil(offset / size) * size; | |
} | |
function sizeOf(type) { | |
return viewTypes[type].BYTES_PER_ELEMENT; | |
} | |
function getArrayViewName(type) { | |
return type.toLowerCase(); | |
} | |
/* | |
* > I saw major perf gains by shortening the source of these generated methods (i.e. renaming | |
* > elementIndex to i) (likely due to v8 inlining heuristics). | |
* - lucaswoj | |
*/ | |
function createEmplaceBack(members, bytesPerElement) { | |
var usedTypeSizes = []; | |
var argNames = []; | |
var body = '' + | |
'var i = this.length;\n' + | |
'this.resize(this.length + 1);\n'; | |
for (var m = 0; m < members.length; m++) { | |
var member = members[m]; | |
var size = sizeOf(member.type); | |
// array offsets to the end of current data for each type size | |
// var o{SIZE} = i * ROUNDED(bytesPerElement / size); | |
if (usedTypeSizes.indexOf(size) < 0) { | |
usedTypeSizes.push(size); | |
body += 'var o' + size.toFixed(0) + ' = i * ' + (bytesPerElement / size).toFixed(0) + ';\n'; | |
} | |
for (var c = 0; c < member.components; c++) { | |
// arguments v0, v1, v2, ... are, in order, the components of | |
// member 0, then the components of member 1, etc. | |
var argName = 'v' + argNames.length; | |
// The index for `member` component `c` into the appropriate type array is: | |
// this.{TYPE}[o{SIZE} + MEMBER_OFFSET + {c}] = v{X} | |
// where MEMBER_OFFSET = ROUND(member.offset / size) is the per-element | |
// offset of this member into the array | |
var index = 'o' + size.toFixed(0) + ' + ' + (member.offset / size + c).toFixed(0); | |
body += 'this.' + getArrayViewName(member.type) + '[' + index + '] = ' + argName + ';\n'; | |
argNames.push(argName); | |
} | |
} | |
body += 'return i;'; | |
return new Function(argNames, body); | |
} | |
function createMemberComponentString(member, component) { | |
var elementOffset = 'this._pos' + sizeOf(member.type).toFixed(0); | |
var componentOffset = (member.offset / sizeOf(member.type) + component).toFixed(0); | |
var index = elementOffset + ' + ' + componentOffset; | |
return 'this._structArray.' + getArrayViewName(member.type) + '[' + index + ']'; | |
} | |
function createGetter(member, c) { | |
return new Function([], 'return ' + createMemberComponentString(member, c) + ';'); | |
} | |
function createSetter(member, c) { | |
return new Function(['x'], createMemberComponentString(member, c) + ' = x;'); | |
} | |
/** | |
* @class Struct | |
* @param {StructArray} structArray The StructArray the struct is stored in | |
* @param {number} index The index of the struct in the StructArray. | |
* @private | |
*/ | |
function Struct(structArray, index) { | |
this._structArray = structArray; | |
this._pos1 = index * this.size; | |
this._pos2 = this._pos1 / 2; | |
this._pos4 = this._pos1 / 4; | |
this._pos8 = this._pos1 / 8; | |
} | |
/** | |
* @class StructArray | |
* The StructArray class is inherited by the custom StructArrayType classes created with | |
* `new StructArrayType(members, options)`. | |
* @private | |
*/ | |
function StructArray(serialized) { | |
if (serialized !== undefined) { | |
// Create from an serialized StructArray | |
this.arrayBuffer = serialized.arrayBuffer; | |
this.length = serialized.length; | |
this.capacity = this.arrayBuffer.byteLength / this.bytesPerElement; | |
this._refreshViews(); | |
// Create a new StructArray | |
} else { | |
this.capacity = -1; | |
this.resize(0); | |
} | |
} | |
/** | |
* @property {number} | |
* @private | |
* @readonly | |
*/ | |
StructArray.prototype.DEFAULT_CAPACITY = 128; | |
/** | |
* @property {number} | |
* @private | |
* @readonly | |
*/ | |
StructArray.prototype.RESIZE_MULTIPLIER = 5; | |
/** | |
* Serialize this StructArray instance | |
* @private | |
*/ | |
StructArray.prototype.serialize = function() { | |
this.trim(); | |
return { | |
length: this.length, | |
arrayBuffer: this.arrayBuffer | |
}; | |
}; | |
/** | |
* Return the Struct at the given location in the array. | |
* @private | |
* @param {number} index The index of the element. | |
*/ | |
StructArray.prototype.get = function(index) { | |
return new this.StructType(this, index); | |
}; | |
/** | |
* Resize the array to discard unused capacity. | |
* @private | |
*/ | |
StructArray.prototype.trim = function() { | |
if (this.length !== this.capacity) { | |
this.capacity = this.length; | |
this.arrayBuffer = this.arrayBuffer.slice(0, this.length * this.bytesPerElement); | |
this._refreshViews(); | |
} | |
}; | |
/** | |
* Resize the array. | |
* If `n` is greater than the current length then additional elements with undefined values are added. | |
* If `n` is less than the current length then the array will be reduced to the first `n` elements. | |
* @param {number} n The new size of the array. | |
*/ | |
StructArray.prototype.resize = function(n) { | |
this.length = n; | |
if (n > this.capacity) { | |
this.capacity = Math.max(n, Math.floor(this.capacity * this.RESIZE_MULTIPLIER), this.DEFAULT_CAPACITY); | |
this.arrayBuffer = new ArrayBuffer(this.capacity * this.bytesPerElement); | |
var oldUint8Array = this.uint8; | |
this._refreshViews(); | |
if (oldUint8Array) this.uint8.set(oldUint8Array); | |
} | |
}; | |
/** | |
* Create TypedArray views for the current ArrayBuffer. | |
* @private | |
*/ | |
StructArray.prototype._refreshViews = function() { | |
for (var t = 0; t < this._usedTypes.length; t++) { | |
var type = this._usedTypes[t]; | |
this[getArrayViewName(type)] = new viewTypes[type](this.arrayBuffer); | |
} | |
}; | |
/** | |
* Output the `StructArray` between indices `startIndex` and `endIndex` as an array of `StructTypes` to enable sorting | |
* @param {number} startIndex | |
* @param {number} endIndex | |
* @private | |
*/ | |
StructArray.prototype.toArray = function(startIndex, endIndex) { | |
var array = []; | |
for (var i = startIndex; i < endIndex; i++) { | |
var struct = this.get(i); | |
array.push(struct); | |
} | |
return array; | |
}; | |
},{"assert":110}],107:[function(require,module,exports){ | |
'use strict'; | |
module.exports = resolveTokens; | |
/** | |
* Replace tokens in a string template with values in an object | |
* | |
* @param {Object} properties a key/value relationship between tokens and replacements | |
* @param {string} text the template string | |
* @returns {string} the template with tokens replaced | |
* @private | |
*/ | |
function resolveTokens(properties, text) { | |
return text.replace(/{([^{}]+)}/g, function(match, key) { | |
return key in properties ? properties[key] : ''; | |
}); | |
} | |
},{}],108:[function(require,module,exports){ | |
'use strict'; | |
var UnitBezier = require('unitbezier'); | |
var Coordinate = require('../geo/coordinate'); | |
/** | |
* Given a value `t` that varies between 0 and 1, return | |
* an interpolation function that eases between 0 and 1 in a pleasing | |
* cubic in-out fashion. | |
* | |
* @param {number} t input | |
* @returns {number} input | |
* @private | |
*/ | |
exports.easeCubicInOut = function (t) { | |
if (t <= 0) return 0; | |
if (t >= 1) return 1; | |
var t2 = t * t, | |
t3 = t2 * t; | |
return 4 * (t < 0.5 ? t3 : 3 * (t - t2) + t3 - 0.75); | |
}; | |
/** | |
* Given given (x, y), (x1, y1) control points for a bezier curve, | |
* return a function that interpolates along that curve. | |
* | |
* @param {number} p1x control point 1 x coordinate | |
* @param {number} p1y control point 1 y coordinate | |
* @param {number} p2x control point 2 x coordinate | |
* @param {number} p2y control point 2 y coordinate | |
* @returns {Function} interpolator: receives number value, returns | |
* number value. | |
* @private | |
*/ | |
exports.bezier = function(p1x, p1y, p2x, p2y) { | |
var bezier = new UnitBezier(p1x, p1y, p2x, p2y); | |
return function(t) { | |
return bezier.solve(t); | |
}; | |
}; | |
/** | |
* A default bezier-curve powered easing function with | |
* control points (0.25, 0.1) and (0.25, 1) | |
* | |
* @param {number} t | |
* @returns {number} output | |
* @private | |
*/ | |
exports.ease = exports.bezier(0.25, 0.1, 0.25, 1); | |
/** | |
* constrain n to the given range via min + max | |
* | |
* @param {number} n value | |
* @param {number} min the minimum value to be returned | |
* @param {number} max the maximum value to be returned | |
* @returns {number} the clamped value | |
* @private | |
*/ | |
exports.clamp = function (n, min, max) { | |
return Math.min(max, Math.max(min, n)); | |
}; | |
/* | |
* constrain n to the given range, excluding the minimum, via modular arithmetic | |
* @param {number} n value | |
* @param {number} min the minimum value to be returned, exclusive | |
* @param {number} max the maximum value to be returned, inclusive | |
* @returns {number} constrained number | |
* @private | |
*/ | |
exports.wrap = function (n, min, max) { | |
var d = max - min; | |
var w = ((n - min) % d + d) % d + min; | |
return (w === min) ? max : w; | |
}; | |
/* | |
* return the first non-null and non-undefined argument to this function. | |
* @returns {*} argument | |
* @private | |
*/ | |
exports.coalesce = function() { | |
for (var i = 0; i < arguments.length; i++) { | |
var arg = arguments[i]; | |
if (arg !== null && arg !== undefined) | |
return arg; | |
} | |
}; | |
/* | |
* Call an asynchronous function on an array of arguments, | |
* calling `callback` with the completed results of all calls. | |
* | |
* @param {Array<*>} array input to each call of the async function. | |
* @param {Function} fn an async function with signature (data, callback) | |
* @param {Function} callback a callback run after all async work is done. | |
* called with an array, containing the results of each async call. | |
* @returns {undefined} | |
* @private | |
*/ | |
exports.asyncAll = function (array, fn, callback) { | |
if (!array.length) { return callback(null, []); } | |
var remaining = array.length; | |
var results = new Array(array.length); | |
var error = null; | |
array.forEach(function (item, i) { | |
fn(item, function (err, result) { | |
if (err) error = err; | |
results[i] = result; | |
if (--remaining === 0) callback(error, results); | |
}); | |
}); | |
}; | |
/* | |
* Compute the difference between the keys in one object and the keys | |
* in another object. | |
* | |
* @param {Object} obj | |
* @param {Object} other | |
* @returns {Array<string>} keys difference | |
* @private | |
*/ | |
exports.keysDifference = function (obj, other) { | |
var difference = []; | |
for (var i in obj) { | |
if (!(i in other)) { | |
difference.push(i); | |
} | |
} | |
return difference; | |
}; | |
/** | |
* Given a destination object and optionally many source objects, | |
* copy all properties from the source objects into the destination. | |
* The last source object given overrides properties from previous | |
* source objects. | |
* @param {Object} dest destination object | |
* @param {...Object} sources sources from which properties are pulled | |
* @returns {Object} dest | |
* @private | |
*/ | |
exports.extend = function (dest) { | |
for (var i = 1; i < arguments.length; i++) { | |
var src = arguments[i]; | |
for (var k in src) { | |
dest[k] = src[k]; | |
} | |
} | |
return dest; | |
}; | |
/** | |
* Extend a destination object with all properties of the src object, | |
* using defineProperty instead of simple assignment. | |
* @param {Object} dest | |
* @param {Object} src | |
* @returns {Object} dest | |
* @private | |
*/ | |
exports.extendAll = function (dest, src) { | |
for (var i in src) { | |
Object.defineProperty(dest, i, Object.getOwnPropertyDescriptor(src, i)); | |
} | |
return dest; | |
}; | |
/** | |
* Extend a parent's prototype with all properties in a properties | |
* object. | |
* | |
* @param {Object} parent | |
* @param {Object} props | |
* @returns {Object} | |
* @private | |
*/ | |
exports.inherit = function (parent, props) { | |
var parentProto = typeof parent === 'function' ? parent.prototype : parent, | |
proto = Object.create(parentProto); | |
exports.extendAll(proto, props); | |
return proto; | |
}; | |
/** | |
* Given an object and a number of properties as strings, return version | |
* of that object with only those properties. | |
* | |
* @param {Object} src the object | |
* @param {Array<string>} properties an array of property names chosen | |
* to appear on the resulting object. | |
* @returns {Object} object with limited properties. | |
* @example | |
* var foo = { name: 'Charlie', age: 10 }; | |
* var justName = pick(foo, ['name']); | |
* // justName = { name: 'Charlie' } | |
* @private | |
*/ | |
exports.pick = function (src, properties) { | |
var result = {}; | |
for (var i = 0; i < properties.length; i++) { | |
var k = properties[i]; | |
if (k in src) { | |
result[k] = src[k]; | |
} | |
} | |
return result; | |
}; | |
var id = 1; | |
/** | |
* Return a unique numeric id, starting at 1 and incrementing with | |
* each call. | |
* | |
* @returns {number} unique numeric id. | |
* @private | |
*/ | |
exports.uniqueId = function () { | |
return id++; | |
}; | |
/** | |
* Create a version of `fn` that is only called `time` milliseconds | |
* after its last invocation | |
* | |
* @param {Function} fn the function to be debounced | |
* @param {number} time millseconds after which the function will be invoked | |
* @returns {Function} debounced function | |
* @private | |
*/ | |
exports.debounce = function(fn, time) { | |
var timer, args; | |
return function() { | |
args = arguments; | |
clearTimeout(timer); | |
timer = setTimeout(function() { | |
fn.apply(null, args); | |
}, time); | |
}; | |
}; | |
/** | |
* Given an array of member function names as strings, replace all of them | |
* with bound versions that will always refer to `context` as `this`. This | |
* is useful for classes where otherwise event bindings would reassign | |
* `this` to the evented object or some other value: this lets you ensure | |
* the `this` value always. | |
* | |
* @param {Array<string>} fns list of member function names | |
* @param {*} context the context value | |
* @returns {undefined} changes functions in-place | |
* @example | |
* function MyClass() { | |
* bindAll(['ontimer'], this); | |
* this.name = 'Tom'; | |
* } | |
* MyClass.prototype.ontimer = function() { | |
* alert(this.name); | |
* }; | |
* var myClass = new MyClass(); | |
* setTimeout(myClass.ontimer, 100); | |
* @private | |
*/ | |
exports.bindAll = function(fns, context) { | |
fns.forEach(function(fn) { | |
context[fn] = context[fn].bind(context); | |
}); | |
}; | |
/** | |
* Given a class, bind all of the methods that look like handlers: that | |
* begin with _on, and bind them to the class. | |
* | |
* @param {Object} context an object with methods | |
* @private | |
*/ | |
exports.bindHandlers = function(context) { | |
for (var i in context) { | |
if (typeof context[i] === 'function' && i.indexOf('_on') === 0) { | |
context[i] = context[i].bind(context); | |
} | |
} | |
}; | |
/** | |
* Set the 'options' property on `obj` with properties | |
* from the `options` argument. Properties in the `options` | |
* object will override existing properties. | |
* | |
* @param {Object} obj destination object | |
* @param {Object} options object of override options | |
* @returns {Object} derived options object. | |
* @private | |
*/ | |
exports.setOptions = function(obj, options) { | |
if (!obj.hasOwnProperty('options')) { | |
obj.options = obj.options ? Object.create(obj.options) : {}; | |
} | |
for (var i in options) { | |
obj.options[i] = options[i]; | |
} | |
return obj.options; | |
}; | |
/** | |
* Given a list of coordinates, get their center as a coordinate. | |
* @param {Array<Coordinate>} coords | |
* @returns {Coordinate} centerpoint | |
* @private | |
*/ | |
exports.getCoordinatesCenter = function(coords) { | |
var minX = Infinity; | |
var minY = Infinity; | |
var maxX = -Infinity; | |
var maxY = -Infinity; | |
for (var i = 0; i < coords.length; i++) { | |
minX = Math.min(minX, coords[i].column); | |
minY = Math.min(minY, coords[i].row); | |
maxX = Math.max(maxX, coords[i].column); | |
maxY = Math.max(maxY, coords[i].row); | |
} | |
var dx = maxX - minX; | |
var dy = maxY - minY; | |
var dMax = Math.max(dx, dy); | |
return new Coordinate((minX + maxX) / 2, (minY + maxY) / 2, 0) | |
.zoomTo(Math.floor(-Math.log(dMax) / Math.LN2)); | |
}; | |
/** | |
* Determine if a string ends with a particular substring | |
* @param {string} string | |
* @param {string} suffix | |
* @returns {boolean} | |
* @private | |
*/ | |
exports.endsWith = function(string, suffix) { | |
return string.indexOf(suffix, string.length - suffix.length) !== -1; | |
}; | |
/** | |
* Determine if a string starts with a particular substring | |
* @param {string} string | |
* @param {string} prefix | |
* @returns {boolean} | |
* @private | |
*/ | |
exports.startsWith = function(string, prefix) { | |
return string.indexOf(prefix) === 0; | |
}; | |
/** | |
* Create an object by mapping all the values of an existing object while | |
* preserving their keys. | |
* @param {Object} input | |
* @param {Function} iterator | |
* @returns {Object} | |
* @private | |
*/ | |
exports.mapObject = function(input, iterator, context) { | |
var output = {}; | |
for (var key in input) { | |
output[key] = iterator.call(context || this, input[key], key, input); | |
} | |
return output; | |
}; | |
/** | |
* Create an object by filtering out values of an existing object | |
* @param {Object} input | |
* @param {Function} iterator | |
* @returns {Object} | |
* @private | |
*/ | |
exports.filterObject = function(input, iterator, context) { | |
var output = {}; | |
for (var key in input) { | |
if (iterator.call(context || this, input[key], key, input)) { | |
output[key] = input[key]; | |
} | |
} | |
return output; | |
}; | |
/** | |
* Deeply compares two object literals. | |
* @param {Object} obj1 | |
* @param {Object} obj2 | |
* @returns {boolean} | |
* @private | |
*/ | |
exports.deepEqual = function deepEqual(a, b) { | |
if (Array.isArray(a)) { | |
if (!Array.isArray(b) || a.length !== b.length) return false; | |
for (var i = 0; i < a.length; i++) { | |
if (!deepEqual(a[i], b[i])) return false; | |
} | |
return true; | |
} | |
if (typeof a === 'object' && a !== null && b !== null) { | |
if (!(typeof b === 'object')) return false; | |
var keys = Object.keys(a); | |
if (keys.length !== Object.keys(b).length) return false; | |
for (var key in a) { | |
if (!deepEqual(a[key], b[key])) return false; | |
} | |
return true; | |
} | |
return a === b; | |
}; | |
/** | |
* Deeply clones two objects. | |
* @param {Object} obj1 | |
* @param {Object} obj2 | |
* @returns {boolean} | |
* @private | |
*/ | |
exports.clone = function deepEqual(input) { | |
if (Array.isArray(input)) { | |
return input.map(exports.clone); | |
} else if (typeof input === 'object') { | |
return exports.mapObject(input, exports.clone); | |
} else { | |
return input; | |
} | |
}; | |
/** | |
* Check if two arrays have at least one common element. | |
* @param {Array} a | |
* @param {Array} b | |
* @returns {boolean} | |
* @private | |
*/ | |
exports.arraysIntersect = function(a, b) { | |
for (var l = 0; l < a.length; l++) { | |
if (b.indexOf(a[l]) >= 0) return true; | |
} | |
return false; | |
}; | |
var warnOnceHistory = {}; | |
exports.warnOnce = function(message) { | |
if (!warnOnceHistory[message]) { | |
// console isn't defined in some WebWorkers, see #2558 | |
if (typeof console !== "undefined") console.warn(message); | |
warnOnceHistory[message] = true; | |
} | |
}; | |
},{"../geo/coordinate":9,"unitbezier":186}],109:[function(require,module,exports){ | |
'use strict'; | |
module.exports = Feature; | |
function Feature(vectorTileFeature, z, x, y) { | |
this._vectorTileFeature = vectorTileFeature; | |
vectorTileFeature._z = z; | |
vectorTileFeature._x = x; | |
vectorTileFeature._y = y; | |
this.properties = vectorTileFeature.properties; | |
if (vectorTileFeature._id) { | |
this.id = vectorTileFeature._id; | |
} | |
} | |
Feature.prototype = { | |
type: "Feature", | |
get geometry() { | |
if (this._geometry === undefined) { | |
this._geometry = this._vectorTileFeature.toGeoJSON( | |
this._vectorTileFeature._x, | |
this._vectorTileFeature._y, | |
this._vectorTileFeature._z).geometry; | |
} | |
return this._geometry; | |
}, | |
set geometry(g) { | |
this._geometry = g; | |
}, | |
toJSON: function() { | |
var json = {}; | |
for (var i in this) { | |
if (i === '_geometry' || i === '_vectorTileFeature' || i === 'toJSON') continue; | |
json[i] = this[i]; | |
} | |
return json; | |
} | |
}; | |
},{}],110:[function(require,module,exports){ | |
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0 | |
// | |
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! | |
// | |
// Originally from narwhal.js (http://narwhaljs.org) | |
// Copyright (c) 2009 Thomas Robinson <280north.com> | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the 'Software'), to | |
// deal in the Software without restriction, including without limitation the | |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
// sell copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// when used in node, this will actually load the util module we depend on | |
// versus loading the builtin util module as happens otherwise | |
// this is a bug in node module loading as far as I am concerned | |
var util = require('util/'); | |
var pSlice = Array.prototype.slice; | |
var hasOwn = Object.prototype.hasOwnProperty; | |
// 1. The assert module provides functions that throw | |
// AssertionError's when particular conditions are not met. The | |
// assert module must conform to the following interface. | |
var assert = module.exports = ok; | |
// 2. The AssertionError is defined in assert. | |
// new assert.AssertionError({ message: message, | |
// actual: actual, | |
// expected: expected }) | |
assert.AssertionError = function AssertionError(options) { | |
this.name = 'AssertionError'; | |
this.actual = options.actual; | |
this.expected = options.expected; | |
this.operator = options.operator; | |
if (options.message) { | |
this.message = options.message; | |
this.generatedMessage = false; | |
} else { | |
this.message = getMessage(this); | |
this.generatedMessage = true; | |
} | |
var stackStartFunction = options.stackStartFunction || fail; | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(this, stackStartFunction); | |
} | |
else { | |
// non v8 browsers so we can have a stacktrace | |
var err = new Error(); | |
if (err.stack) { | |
var out = err.stack; | |
// try to strip useless frames | |
var fn_name = stackStartFunction.name; | |
var idx = out.indexOf('\n' + fn_name); | |
if (idx >= 0) { | |
// once we have located the function frame | |
// we need to strip out everything before it (and its line) | |
var next_line = out.indexOf('\n', idx + 1); | |
out = out.substring(next_line + 1); | |
} | |
this.stack = out; | |
} | |
} | |
}; | |
// assert.AssertionError instanceof Error | |
util.inherits(assert.AssertionError, Error); | |
function replacer(key, value) { | |
if (util.isUndefined(value)) { | |
return '' + value; | |
} | |
if (util.isNumber(value) && !isFinite(value)) { | |
return value.toString(); | |
} | |
if (util.isFunction(value) || util.isRegExp(value)) { | |
return value.toString(); | |
} | |
return value; | |
} | |
function truncate(s, n) { | |
if (util.isString(s)) { | |
return s.length < n ? s : s.slice(0, n); | |
} else { | |
return s; | |
} | |
} | |
function getMessage(self) { | |
return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' + | |
self.operator + ' ' + | |
truncate(JSON.stringify(self.expected, replacer), 128); | |
} | |
// At present only the three keys mentioned above are used and | |
// understood by the spec. Implementations or sub modules can pass | |
// other keys to the AssertionError's constructor - they will be | |
// ignored. | |
// 3. All of the following functions must throw an AssertionError | |
// when a corresponding condition is not met, with a message that | |
// may be undefined if not provided. All assertion methods provide | |
// both the actual and expected values to the assertion error for | |
// display purposes. | |
function fail(actual, expected, message, operator, stackStartFunction) { | |
throw new assert.AssertionError({ | |
message: message, | |
actual: actual, | |
expected: expected, | |
operator: operator, | |
stackStartFunction: stackStartFunction | |
}); | |
} | |
// EXTENSION! allows for well behaved errors defined elsewhere. | |
assert.fail = fail; | |
// 4. Pure assertion tests whether a value is truthy, as determined | |
// by !!guard. | |
// assert.ok(guard, message_opt); | |
// This statement is equivalent to assert.equal(true, !!guard, | |
// message_opt);. To test strictly for the value true, use | |
// assert.strictEqual(true, guard, message_opt);. | |
function ok(value, message) { | |
if (!value) fail(value, true, message, '==', assert.ok); | |
} | |
assert.ok = ok; | |
// 5. The equality assertion tests shallow, coercive equality with | |
// ==. | |
// assert.equal(actual, expected, message_opt); | |
assert.equal = function equal(actual, expected, message) { | |
if (actual != expected) fail(actual, expected, message, '==', assert.equal); | |
}; | |
// 6. The non-equality assertion tests for whether two objects are not equal | |
// with != assert.notEqual(actual, expected, message_opt); | |
assert.notEqual = function notEqual(actual, expected, message) { | |
if (actual == expected) { | |
fail(actual, expected, message, '!=', assert.notEqual); | |
} | |
}; | |
// 7. The equivalence assertion tests a deep equality relation. | |
// assert.deepEqual(actual, expected, message_opt); | |
assert.deepEqual = function deepEqual(actual, expected, message) { | |
if (!_deepEqual(actual, expected)) { | |
fail(actual, expected, message, 'deepEqual', assert.deepEqual); | |
} | |
}; | |
function _deepEqual(actual, expected) { | |
// 7.1. All identical values are equivalent, as determined by ===. | |
if (actual === expected) { | |
return true; | |
} else if (util.isBuffer(actual) && util.isBuffer(expected)) { | |
if (actual.length != expected.length) return false; | |
for (var i = 0; i < actual.length; i++) { | |
if (actual[i] !== expected[i]) return false; | |
} | |
return true; | |
// 7.2. If the expected value is a Date object, the actual value is | |
// equivalent if it is also a Date object that refers to the same time. | |
} else if (util.isDate(actual) && util.isDate(expected)) { | |
return actual.getTime() === expected.getTime(); | |
// 7.3 If the expected value is a RegExp object, the actual value is | |
// equivalent if it is also a RegExp object with the same source and | |
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). | |
} else if (util.isRegExp(actual) && util.isRegExp(expected)) { | |
return actual.source === expected.source && | |
actual.global === expected.global && | |
actual.multiline === expected.multiline && | |
actual.lastIndex === expected.lastIndex && | |
actual.ignoreCase === expected.ignoreCase; | |
// 7.4. Other pairs that do not both pass typeof value == 'object', | |
// equivalence is determined by ==. | |
} else if (!util.isObject(actual) && !util.isObject(expected)) { | |
return actual == expected; | |
// 7.5 For all other Object pairs, including Array objects, equivalence is | |
// determined by having the same number of owned properties (as verified | |
// with Object.prototype.hasOwnProperty.call), the same set of keys | |
// (although not necessarily the same order), equivalent values for every | |
// corresponding key, and an identical 'prototype' property. Note: this | |
// accounts for both named and indexed properties on Arrays. | |
} else { | |
return objEquiv(actual, expected); | |
} | |
} | |
function isArguments(object) { | |
return Object.prototype.toString.call(object) == '[object Arguments]'; | |
} | |
function objEquiv(a, b) { | |
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b)) | |
return false; | |
// an identical 'prototype' property. | |
if (a.prototype !== b.prototype) return false; | |
// if one is a primitive, the other must be same | |
if (util.isPrimitive(a) || util.isPrimitive(b)) { | |
return a === b; | |
} | |
var aIsArgs = isArguments(a), | |
bIsArgs = isArguments(b); | |
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs)) | |
return false; | |
if (aIsArgs) { | |
a = pSlice.call(a); | |
b = pSlice.call(b); | |
return _deepEqual(a, b); | |
} | |
var ka = objectKeys(a), | |
kb = objectKeys(b), | |
key, i; | |
// having the same number of owned properties (keys incorporates | |
// hasOwnProperty) | |
if (ka.length != kb.length) | |
return false; | |
//the same set of keys (although not necessarily the same order), | |
ka.sort(); | |
kb.sort(); | |
//~~~cheap key test | |
for (i = ka.length - 1; i >= 0; i--) { | |
if (ka[i] != kb[i]) | |
return false; | |
} | |
//equivalent values for every corresponding key, and | |
//~~~possibly expensive deep test | |
for (i = ka.length - 1; i >= 0; i--) { | |
key = ka[i]; | |
if (!_deepEqual(a[key], b[key])) return false; | |
} | |
return true; | |
} | |
// 8. The non-equivalence assertion tests for any deep inequality. | |
// assert.notDeepEqual(actual, expected, message_opt); | |
assert.notDeepEqual = function notDeepEqual(actual, expected, message) { | |
if (_deepEqual(actual, expected)) { | |
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); | |
} | |
}; | |
// 9. The strict equality assertion tests strict equality, as determined by ===. | |
// assert.strictEqual(actual, expected, message_opt); | |
assert.strictEqual = function strictEqual(actual, expected, message) { | |
if (actual !== expected) { | |
fail(actual, expected, message, '===', assert.strictEqual); | |
} | |
}; | |
// 10. The strict non-equality assertion tests for strict inequality, as | |
// determined by !==. assert.notStrictEqual(actual, expected, message_opt); | |
assert.notStrictEqual = function notStrictEqual(actual, expected, message) { | |
if (actual === expected) { | |
fail(actual, expected, message, '!==', assert.notStrictEqual); | |
} | |
}; | |
function expectedException(actual, expected) { | |
if (!actual || !expected) { | |
return false; | |
} | |
if (Object.prototype.toString.call(expected) == '[object RegExp]') { | |
return expected.test(actual); | |
} else if (actual instanceof expected) { | |
return true; | |
} else if (expected.call({}, actual) === true) { | |
return true; | |
} | |
return false; | |
} | |
function _throws(shouldThrow, block, expected, message) { | |
var actual; | |
if (util.isString(expected)) { | |
message = expected; | |
expected = null; | |
} | |
try { | |
block(); | |
} catch (e) { | |
actual = e; | |
} | |
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + | |
(message ? ' ' + message : '.'); | |
if (shouldThrow && !actual) { | |
fail(actual, expected, 'Missing expected exception' + message); | |
} | |
if (!shouldThrow && expectedException(actual, expected)) { | |
fail(actual, expected, 'Got unwanted exception' + message); | |
} | |
if ((shouldThrow && actual && expected && | |
!expectedException(actual, expected)) || (!shouldThrow && actual)) { | |
throw actual; | |
} | |
} | |
// 11. Expected to throw an error: | |
// assert.throws(block, Error_opt, message_opt); | |
assert.throws = function(block, /*optional*/error, /*optional*/message) { | |
_throws.apply(this, [true].concat(pSlice.call(arguments))); | |
}; | |
// EXTENSION! This is annoying to write outside this module. | |
assert.doesNotThrow = function(block, /*optional*/message) { | |
_throws.apply(this, [false].concat(pSlice.call(arguments))); | |
}; | |
assert.ifError = function(err) { if (err) {throw err;}}; | |
var objectKeys = Object.keys || function (obj) { | |
var keys = []; | |
for (var key in obj) { | |
if (hasOwn.call(obj, key)) keys.push(key); | |
} | |
return keys; | |
}; | |
},{"util/":121}],111:[function(require,module,exports){ | |
if (typeof Object.create === 'function') { | |
// implementation from standard node.js 'util' module | |
module.exports = function inherits(ctor, superCtor) { | |
ctor.super_ = superCtor | |
ctor.prototype = Object.create(superCtor.prototype, { | |
constructor: { | |
value: ctor, | |
enumerable: false, | |
writable: true, | |
configurable: true | |
} | |
}); | |
}; | |
} else { | |
// old school shim for old browsers | |
module.exports = function inherits(ctor, superCtor) { | |
ctor.super_ = superCtor | |
var TempCtor = function () {} | |
TempCtor.prototype = superCtor.prototype | |
ctor.prototype = new TempCtor() | |
ctor.prototype.constructor = ctor | |
} | |
} | |
},{}],112:[function(require,module,exports){ | |
(function (process){ | |
// Copyright Joyent, Inc. and other Node contributors. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the | |
// "Software"), to deal in the Software without restriction, including | |
// without limitation the rights to use, copy, modify, merge, publish, | |
// distribute, sublicense, and/or sell copies of the Software, and to permit | |
// persons to whom the Software is furnished to do so, subject to the | |
// following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included | |
// in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
// USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// resolves . and .. elements in a path array with directory names there | |
// must be no slashes, empty elements, or device names (c:\) in the array | |
// (so also no leading and trailing slashes - it does not distinguish | |
// relative and absolute paths) | |
function normalizeArray(parts, allowAboveRoot) { | |
// if the path tries to go above the root, `up` ends up > 0 | |
var up = 0; | |
for (var i = parts.length - 1; i >= 0; i--) { | |
var last = parts[i]; | |
if (last === '.') { | |
parts.splice(i, 1); | |
} else if (last === '..') { | |
parts.splice(i, 1); | |
up++; | |
} else if (up) { | |
parts.splice(i, 1); | |
up--; | |
} | |
} | |
// if the path is allowed to go above the root, restore leading ..s | |
if (allowAboveRoot) { | |
for (; up--; up) { | |
parts.unshift('..'); | |
} | |
} | |
return parts; | |
} | |
// Split a filename into [root, dir, basename, ext], unix version | |
// 'root' is just a slash, or nothing. | |
var splitPathRe = | |
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; | |
var splitPath = function(filename) { | |
return splitPathRe.exec(filename).slice(1); | |
}; | |
// path.resolve([from ...], to) | |
// posix version | |
exports.resolve = function() { | |
var resolvedPath = '', | |
resolvedAbsolute = false; | |
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { | |
var path = (i >= 0) ? arguments[i] : process.cwd(); | |
// Skip empty and invalid entries | |
if (typeof path !== 'string') { | |
throw new TypeError('Arguments to path.resolve must be strings'); | |
} else if (!path) { | |
continue; | |
} | |
resolvedPath = path + '/' + resolvedPath; | |
resolvedAbsolute = path.charAt(0) === '/'; | |
} | |
// At this point the path should be resolved to a full absolute path, but | |
// handle relative paths to be safe (might happen when process.cwd() fails) | |
// Normalize the path | |
resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) { | |
return !!p; | |
}), !resolvedAbsolute).join('/'); | |
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; | |
}; | |
// path.normalize(path) | |
// posix version | |
exports.normalize = function(path) { | |
var isAbsolute = exports.isAbsolute(path), | |
trailingSlash = substr(path, -1) === '/'; | |
// Normalize the path | |
path = normalizeArray(filter(path.split('/'), function(p) { | |
return !!p; | |
}), !isAbsolute).join('/'); | |
if (!path && !isAbsolute) { | |
path = '.'; | |
} | |
if (path && trailingSlash) { | |
path += '/'; | |
} | |
return (isAbsolute ? '/' : '') + path; | |
}; | |
// posix version | |
exports.isAbsolute = function(path) { | |
return path.charAt(0) === '/'; | |
}; | |
// posix version | |
exports.join = function() { | |
var paths = Array.prototype.slice.call(arguments, 0); | |
return exports.normalize(filter(paths, function(p, index) { | |
if (typeof p !== 'string') { | |
throw new TypeError('Arguments to path.join must be strings'); | |
} | |
return p; | |
}).join('/')); | |
}; | |
// path.relative(from, to) | |
// posix version | |
exports.relative = function(from, to) { | |
from = exports.resolve(from).substr(1); | |
to = exports.resolve(to).substr(1); | |
function trim(arr) { | |
var start = 0; | |
for (; start < arr.length; start++) { | |
if (arr[start] !== '') break; | |
} | |
var end = arr.length - 1; | |
for (; end >= 0; end--) { | |
if (arr[end] !== '') break; | |
} | |
if (start > end) return []; | |
return arr.slice(start, end - start + 1); | |
} | |
var fromParts = trim(from.split('/')); | |
var toParts = trim(to.split('/')); | |
var length = Math.min(fromParts.length, toParts.length); | |
var samePartsLength = length; | |
for (var i = 0; i < length; i++) { | |
if (fromParts[i] !== toParts[i]) { | |
samePartsLength = i; | |
break; | |
} | |
} | |
var outputParts = []; | |
for (var i = samePartsLength; i < fromParts.length; i++) { | |
outputParts.push('..'); | |
} | |
outputParts = outputParts.concat(toParts.slice(samePartsLength)); | |
return outputParts.join('/'); | |
}; | |
exports.sep = '/'; | |
exports.delimiter = ':'; | |
exports.dirname = function(path) { | |
var result = splitPath(path), | |
root = result[0], | |
dir = result[1]; | |
if (!root && !dir) { | |
// No dirname whatsoever | |
return '.'; | |
} | |
if (dir) { | |
// It has a dirname, strip trailing slash | |
dir = dir.substr(0, dir.length - 1); | |
} | |
return root + dir; | |
}; | |
exports.basename = function(path, ext) { | |
var f = splitPath(path)[2]; | |
// TODO: make this comparison case-insensitive on windows? | |
if (ext && f.substr(-1 * ext.length) === ext) { | |
f = f.substr(0, f.length - ext.length); | |
} | |
return f; | |
}; | |
exports.extname = function(path) { | |
return splitPath(path)[3]; | |
}; | |
function filter (xs, f) { | |
if (xs.filter) return xs.filter(f); | |
var res = []; | |
for (var i = 0; i < xs.length; i++) { | |
if (f(xs[i], i, xs)) res.push(xs[i]); | |
} | |
return res; | |
} | |
// String.prototype.substr - negative index don't work in IE8 | |
var substr = 'ab'.substr(-1) === 'b' | |
? function (str, start, len) { return str.substr(start, len) } | |
: function (str, start, len) { | |
if (start < 0) start = str.length + start; | |
return str.substr(start, len); | |
} | |
; | |
}).call(this,require('_process')) | |
},{"_process":113}],113:[function(require,module,exports){ | |
// shim for using process in browser | |
var process = module.exports = {}; | |
// cached from whatever global is present so that test runners that stub it | |
// don't break things. But we need to wrap it in a try catch in case it is | |
// wrapped in strict mode code which doesn't define any globals. It's inside a | |
// function because try/catches deoptimize in certain engines. | |
var cachedSetTimeout; | |
var cachedClearTimeout; | |
(function () { | |
try { | |
cachedSetTimeout = setTimeout; | |
} catch (e) { | |
cachedSetTimeout = function () { | |
throw new Error('setTimeout is not defined'); | |
} | |
} | |
try { | |
cachedClearTimeout = clearTimeout; | |
} catch (e) { | |
cachedClearTimeout = function () { | |
throw new Error('clearTimeout is not defined'); | |
} | |
} | |
} ()) | |
var queue = []; | |
var draining = false; | |
var currentQueue; | |
var queueIndex = -1; | |
function cleanUpNextTick() { | |
if (!draining || !currentQueue) { | |
return; | |
} | |
draining = false; | |
if (currentQueue.length) { | |
queue = currentQueue.concat(queue); | |
} else { | |
queueIndex = -1; | |
} | |
if (queue.length) { | |
drainQueue(); | |
} | |
} | |
function drainQueue() { | |
if (draining) { | |
return; | |
} | |
var timeout = cachedSetTimeout(cleanUpNextTick); | |
draining = true; | |
var len = queue.length; | |
while(len) { | |
currentQueue = queue; | |
queue = []; | |
while (++queueIndex < len) { | |
if (currentQueue) { | |
currentQueue[queueIndex].run(); | |
} | |
} | |
queueIndex = -1; | |
len = queue.length; | |
} | |
currentQueue = null; | |
draining = false; | |
cachedClearTimeout(timeout); | |
} | |
process.nextTick = function (fun) { | |
var args = new Array(arguments.length - 1); | |
if (arguments.length > 1) { | |
for (var i = 1; i < arguments.length; i++) { | |
args[i - 1] = arguments[i]; | |
} | |
} | |
queue.push(new Item(fun, args)); | |
if (queue.length === 1 && !draining) { | |
cachedSetTimeout(drainQueue, 0); | |
} | |
}; | |
// v8 likes predictible objects | |
function Item(fun, array) { | |
this.fun = fun; | |
this.array = array; | |
} | |
Item.prototype.run = function () { | |
this.fun.apply(null, this.array); | |
}; | |
process.title = 'browser'; | |
process.browser = true; | |
process.env = {}; | |
process.argv = []; | |
process.version = ''; // empty string to avoid regexp issues | |
process.versions = {}; | |
function noop() {} | |
process.on = noop; | |
process.addListener = noop; | |
process.once = noop; | |
process.off = noop; | |
process.removeListener = noop; | |
process.removeAllListeners = noop; | |
process.emit = noop; | |
process.binding = function (name) { | |
throw new Error('process.binding is not supported'); | |
}; | |
process.cwd = function () { return '/' }; | |
process.chdir = function (dir) { | |
throw new Error('process.chdir is not supported'); | |
}; | |
process.umask = function() { return 0; }; | |
},{}],114:[function(require,module,exports){ | |
(function (global){ | |
/*! https://mths.be/punycode v1.4.1 by @mathias */ | |
;(function(root) { | |
/** Detect free variables */ | |
var freeExports = typeof exports == 'object' && exports && | |
!exports.nodeType && exports; | |
var freeModule = typeof module == 'object' && module && | |
!module.nodeType && module; | |
var freeGlobal = typeof global == 'object' && global; | |
if ( | |
freeGlobal.global === freeGlobal || | |
freeGlobal.window === freeGlobal || | |
freeGlobal.self === freeGlobal | |
) { | |
root = freeGlobal; | |
} | |
/** | |
* The `punycode` object. | |
* @name punycode | |
* @type Object | |
*/ | |
var punycode, | |
/** Highest positive signed 32-bit float value */ | |
maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1 | |
/** Bootstring parameters */ | |
base = 36, | |
tMin = 1, | |
tMax = 26, | |
skew = 38, | |
damp = 700, | |
initialBias = 72, | |
initialN = 128, // 0x80 | |
delimiter = '-', // '\x2D' | |
/** Regular expressions */ | |
regexPunycode = /^xn--/, | |
regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars | |
regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators | |
/** Error messages */ | |
errors = { | |
'overflow': 'Overflow: input needs wider integers to process', | |
'not-basic': 'Illegal input >= 0x80 (not a basic code point)', | |
'invalid-input': 'Invalid input' | |
}, | |
/** Convenience shortcuts */ | |
baseMinusTMin = base - tMin, | |
floor = Math.floor, | |
stringFromCharCode = String.fromCharCode, | |
/** Temporary variable */ | |
key; | |
/*--------------------------------------------------------------------------*/ | |
/** | |
* A generic error utility function. | |
* @private | |
* @param {String} type The error type. | |
* @returns {Error} Throws a `RangeError` with the applicable error message. | |
*/ | |
function error(type) { | |
throw new RangeError(errors[type]); | |
} | |
/** | |
* A generic `Array#map` utility function. | |
* @private | |
* @param {Array} array The array to iterate over. | |
* @param {Function} callback The function that gets called for every array | |
* item. | |
* @returns {Array} A new array of values returned by the callback function. | |
*/ | |
function map(array, fn) { | |
var length = array.length; | |
var result = []; | |
while (length--) { | |
result[length] = fn(array[length]); | |
} | |
return result; | |
} | |
/** | |
* A simple `Array#map`-like wrapper to work with domain name strings or email | |
* addresses. | |
* @private | |
* @param {String} domain The domain name or email address. | |
* @param {Function} callback The function that gets called for every | |
* character. | |
* @returns {Array} A new string of characters returned by the callback | |
* function. | |
*/ | |
function mapDomain(string, fn) { | |
var parts = string.split('@'); | |
var result = ''; | |
if (parts.length > 1) { | |
// In email addresses, only the domain name should be punycoded. Leave | |
// the local part (i.e. everything up to `@`) intact. | |
result = parts[0] + '@'; | |
string = parts[1]; | |
} | |
// Avoid `split(regex)` for IE8 compatibility. See #17. | |
string = string.replace(regexSeparators, '\x2E'); | |
var labels = string.split('.'); | |
var encoded = map(labels, fn).join('.'); | |
return result + encoded; | |
} | |
/** | |
* Creates an array containing the numeric code points of each Unicode | |
* character in the string. While JavaScript uses UCS-2 internally, | |
* this function will convert a pair of surrogate halves (each of which | |
* UCS-2 exposes as separate characters) into a single code point, | |
* matching UTF-16. | |
* @see `punycode.ucs2.encode` | |
* @see <https://mathiasbynens.be/notes/javascript-encoding> | |
* @memberOf punycode.ucs2 | |
* @name decode | |
* @param {String} string The Unicode input string (UCS-2). | |
* @returns {Array} The new array of code points. | |
*/ | |
function ucs2decode(string) { | |
var output = [], | |
counter = 0, | |
length = string.length, | |
value, | |
extra; | |
while (counter < length) { | |
value = string.charCodeAt(counter++); | |
if (value >= 0xD800 && value <= 0xDBFF && counter < length) { | |
// high surrogate, and there is a next character | |
extra = string.charCodeAt(counter++); | |
if ((extra & 0xFC00) == 0xDC00) { // low surrogate | |
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); | |
} else { | |
// unmatched surrogate; only append this code unit, in case the next | |
// code unit is the high surrogate of a surrogate pair | |
output.push(value); | |
counter--; | |
} | |
} else { | |
output.push(value); | |
} | |
} | |
return output; | |
} | |
/** | |
* Creates a string based on an array of numeric code points. | |
* @see `punycode.ucs2.decode` | |
* @memberOf punycode.ucs2 | |
* @name encode | |
* @param {Array} codePoints The array of numeric code points. | |
* @returns {String} The new Unicode string (UCS-2). | |
*/ | |
function ucs2encode(array) { | |
return map(array, function(value) { | |
var output = ''; | |
if (value > 0xFFFF) { | |
value -= 0x10000; | |
output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); | |
value = 0xDC00 | value & 0x3FF; | |
} | |
output += stringFromCharCode(value); | |
return output; | |
}).join(''); | |
} | |
/** | |
* Converts a basic code point into a digit/integer. | |
* @see `digitToBasic()` | |
* @private | |
* @param {Number} codePoint The basic numeric code point value. | |
* @returns {Number} The numeric value of a basic code point (for use in | |
* representing integers) in the range `0` to `base - 1`, or `base` if | |
* the code point does not represent a value. | |
*/ | |
function basicToDigit(codePoint) { | |
if (codePoint - 48 < 10) { | |
return codePoint - 22; | |
} | |
if (codePoint - 65 < 26) { | |
return codePoint - 65; | |
} | |
if (codePoint - 97 < 26) { | |
return codePoint - 97; | |
} | |
return base; | |
} | |
/** | |
* Converts a digit/integer into a basic code point. | |
* @see `basicToDigit()` | |
* @private | |
* @param {Number} digit The numeric value of a basic code point. | |
* @returns {Number} The basic code point whose value (when used for | |
* representing integers) is `digit`, which needs to be in the range | |
* `0` to `base - 1`. If `flag` is non-zero, the uppercase form is | |
* used; else, the lowercase form is used. The behavior is undefined | |
* if `flag` is non-zero and `digit` has no uppercase form. | |
*/ | |
function digitToBasic(digit, flag) { | |
// 0..25 map to ASCII a..z or A..Z | |
// 26..35 map to ASCII 0..9 | |
return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5); | |
} | |
/** | |
* Bias adaptation function as per section 3.4 of RFC 3492. | |
* https://tools.ietf.org/html/rfc3492#section-3.4 | |
* @private | |
*/ | |
function adapt(delta, numPoints, firstTime) { | |
var k = 0; | |
delta = firstTime ? floor(delta / damp) : delta >> 1; | |
delta += floor(delta / numPoints); | |
for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) { | |
delta = floor(delta / baseMinusTMin); | |
} | |
return floor(k + (baseMinusTMin + 1) * delta / (delta + skew)); | |
} | |
/** | |
* Converts a Punycode string of ASCII-only symbols to a string of Unicode | |
* symbols. | |
* @memberOf punycode | |
* @param {String} input The Punycode string of ASCII-only symbols. | |
* @returns {String} The resulting string of Unicode symbols. | |
*/ | |
function decode(input) { | |
// Don't use UCS-2 | |
var output = [], | |
inputLength = input.length, | |
out, | |
i = 0, | |
n = initialN, | |
bias = initialBias, | |
basic, | |
j, | |
index, | |
oldi, | |
w, | |
k, | |
digit, | |
t, | |
/** Cached calculation results */ | |
baseMinusT; | |
// Handle the basic code points: let `basic` be the number of input code | |
// points before the last delimiter, or `0` if there is none, then copy | |
// the first basic code points to the output. | |
basic = input.lastIndexOf(delimiter); | |
if (basic < 0) { | |
basic = 0; | |
} | |
for (j = 0; j < basic; ++j) { | |
// if it's not a basic code point | |
if (input.charCodeAt(j) >= 0x80) { | |
error('not-basic'); | |
} | |
output.push(input.charCodeAt(j)); | |
} | |
// Main decoding loop: start just after the last delimiter if any basic code | |
// points were copied; start at the beginning otherwise. | |
for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) { | |
// `index` is the index of the next character to be consumed. | |
// Decode a generalized variable-length integer into `delta`, | |
// which gets added to `i`. The overflow checking is easier | |
// if we increase `i` as we go, then subtract off its starting | |
// value at the end to obtain `delta`. | |
for (oldi = i, w = 1, k = base; /* no condition */; k += base) { | |
if (index >= inputLength) { | |
error('invalid-input'); | |
} | |
digit = basicToDigit(input.charCodeAt(index++)); | |
if (digit >= base || digit > floor((maxInt - i) / w)) { | |
error('overflow'); | |
} | |
i += digit * w; | |
t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); | |
if (digit < t) { | |
break; | |
} | |
baseMinusT = base - t; | |
if (w > floor(maxInt / baseMinusT)) { | |
error('overflow'); | |
} | |
w *= baseMinusT; | |
} | |
out = output.length + 1; | |
bias = adapt(i - oldi, out, oldi == 0); | |
// `i` was supposed to wrap around from `out` to `0`, | |
// incrementing `n` each time, so we'll fix that now: | |
if (floor(i / out) > maxInt - n) { | |
error('overflow'); | |
} | |
n += floor(i / out); | |
i %= out; | |
// Insert `n` at position `i` of the output | |
output.splice(i++, 0, n); | |
} | |
return ucs2encode(output); | |
} | |
/** | |
* Converts a string of Unicode symbols (e.g. a domain name label) to a | |
* Punycode string of ASCII-only symbols. | |
* @memberOf punycode | |
* @param {String} input The string of Unicode symbols. | |
* @returns {String} The resulting Punycode string of ASCII-only symbols. | |
*/ | |
function encode(input) { | |
var n, | |
delta, | |
handledCPCount, | |
basicLength, | |
bias, | |
j, | |
m, | |
q, | |
k, | |
t, | |
currentValue, | |
output = [], | |
/** `inputLength` will hold the number of code points in `input`. */ | |
inputLength, | |
/** Cached calculation results */ | |
handledCPCountPlusOne, | |
baseMinusT, | |
qMinusT; | |
// Convert the input in UCS-2 to Unicode | |
input = ucs2decode(input); | |
// Cache the length | |
inputLength = input.length; | |
// Initialize the state | |
n = initialN; | |
delta = 0; | |
bias = initialBias; | |
// Handle the basic code points | |
for (j = 0; j < inputLength; ++j) { | |
currentValue = input[j]; | |
if (currentValue < 0x80) { | |
output.push(stringFromCharCode(currentValue)); | |
} | |
} | |
handledCPCount = basicLength = output.length; | |
// `handledCPCount` is the number of code points that have been handled; | |
// `basicLength` is the number of basic code points. | |
// Finish the basic string - if it is not empty - with a delimiter | |
if (basicLength) { | |
output.push(delimiter); | |
} | |
// Main encoding loop: | |
while (handledCPCount < inputLength) { | |
// All non-basic code points < n have been handled already. Find the next | |
// larger one: | |
for (m = maxInt, j = 0; j < inputLength; ++j) { | |
currentValue = input[j]; | |
if (currentValue >= n && currentValue < m) { | |
m = currentValue; | |
} | |
} | |
// Increase `delta` enough to advance the decoder's <n,i> state to <m,0>, | |
// but guard against overflow | |
handledCPCountPlusOne = handledCPCount + 1; | |
if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { | |
error('overflow'); | |
} | |
delta += (m - n) * handledCPCountPlusOne; | |
n = m; | |
for (j = 0; j < inputLength; ++j) { | |
currentValue = input[j]; | |
if (currentValue < n && ++delta > maxInt) { | |
error('overflow'); | |
} | |
if (currentValue == n) { | |
// Represent delta as a generalized variable-length integer | |
for (q = delta, k = base; /* no condition */; k += base) { | |
t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); | |
if (q < t) { | |
break; | |
} | |
qMinusT = q - t; | |
baseMinusT = base - t; | |
output.push( | |
stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)) | |
); | |
q = floor(qMinusT / baseMinusT); | |
} | |
output.push(stringFromCharCode(digitToBasic(q, 0))); | |
bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength); | |
delta = 0; | |
++handledCPCount; | |
} | |
} | |
++delta; | |
++n; | |
} | |
return output.join(''); | |
} | |
/** | |
* Converts a Punycode string representing a domain name or an email address | |
* to Unicode. Only the Punycoded parts of the input will be converted, i.e. | |
* it doesn't matter if you call it on a string that has already been | |
* converted to Unicode. | |
* @memberOf punycode | |
* @param {String} input The Punycoded domain name or email address to | |
* convert to Unicode. | |
* @returns {String} The Unicode representation of the given Punycode | |
* string. | |
*/ | |
function toUnicode(input) { | |
return mapDomain(input, function(string) { | |
return regexPunycode.test(string) | |
? decode(string.slice(4).toLowerCase()) | |
: string; | |
}); | |
} | |
/** | |
* Converts a Unicode string representing a domain name or an email address to | |
* Punycode. Only the non-ASCII parts of the domain name will be converted, | |
* i.e. it doesn't matter if you call it with a domain that's already in | |
* ASCII. | |
* @memberOf punycode | |
* @param {String} input The domain name or email address to convert, as a | |
* Unicode string. | |
* @returns {String} The Punycode representation of the given domain name or | |
* email address. | |
*/ | |
function toASCII(input) { | |
return mapDomain(input, function(string) { | |
return regexNonASCII.test(string) | |
? 'xn--' + encode(string) | |
: string; | |
}); | |
} | |
/*--------------------------------------------------------------------------*/ | |
/** Define the public API */ | |
punycode = { | |
/** | |
* A string representing the current Punycode.js version number. | |
* @memberOf punycode | |
* @type String | |
*/ | |
'version': '1.4.1', | |
/** | |
* An object of methods to convert from JavaScript's internal character | |
* representation (UCS-2) to Unicode code points, and back. | |
* @see <https://mathiasbynens.be/notes/javascript-encoding> | |
* @memberOf punycode | |
* @type Object | |
*/ | |
'ucs2': { | |
'decode': ucs2decode, | |
'encode': ucs2encode | |
}, | |
'decode': decode, | |
'encode': encode, | |
'toASCII': toASCII, | |
'toUnicode': toUnicode | |
}; | |
/** Expose `punycode` */ | |
// Some AMD build optimizers, like r.js, check for specific condition patterns | |
// like the following: | |
if ( | |
typeof define == 'function' && | |
typeof define.amd == 'object' && | |
define.amd | |
) { | |
define('punycode', function() { | |
return punycode; | |
}); | |
} else if (freeExports && freeModule) { | |
if (module.exports == freeExports) { | |
// in Node.js, io.js, or RingoJS v0.8.0+ | |
freeModule.exports = punycode; | |
} else { | |
// in Narwhal or RingoJS v0.7.0- | |
for (key in punycode) { | |
punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); | |
} | |
} | |
} else { | |
// in Rhino or a web browser | |
root.punycode = punycode; | |
} | |
}(this)); | |
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | |
},{}],115:[function(require,module,exports){ | |
// Copyright Joyent, Inc. and other Node contributors. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the | |
// "Software"), to deal in the Software without restriction, including | |
// without limitation the rights to use, copy, modify, merge, publish, | |
// distribute, sublicense, and/or sell copies of the Software, and to permit | |
// persons to whom the Software is furnished to do so, subject to the | |
// following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included | |
// in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
// USE OR OTHER DEALINGS IN THE SOFTWARE. | |
'use strict'; | |
// If obj.hasOwnProperty has been overridden, then calling | |
// obj.hasOwnProperty(prop) will break. | |
// See: https://github.com/joyent/node/issues/1707 | |
function hasOwnProperty(obj, prop) { | |
return Object.prototype.hasOwnProperty.call(obj, prop); | |
} | |
module.exports = function(qs, sep, eq, options) { | |
sep = sep || '&'; | |
eq = eq || '='; | |
var obj = {}; | |
if (typeof qs !== 'string' || qs.length === 0) { | |
return obj; | |
} | |
var regexp = /\+/g; | |
qs = qs.split(sep); | |
var maxKeys = 1000; | |
if (options && typeof options.maxKeys === 'number') { | |
maxKeys = options.maxKeys; | |
} | |
var len = qs.length; | |
// maxKeys <= 0 means that we should not limit keys count | |
if (maxKeys > 0 && len > maxKeys) { | |
len = maxKeys; | |
} | |
for (var i = 0; i < len; ++i) { | |
var x = qs[i].replace(regexp, '%20'), | |
idx = x.indexOf(eq), | |
kstr, vstr, k, v; | |
if (idx >= 0) { | |
kstr = x.substr(0, idx); | |
vstr = x.substr(idx + 1); | |
} else { | |
kstr = x; | |
vstr = ''; | |
} | |
k = decodeURIComponent(kstr); | |
v = decodeURIComponent(vstr); | |
if (!hasOwnProperty(obj, k)) { | |
obj[k] = v; | |
} else if (isArray(obj[k])) { | |
obj[k].push(v); | |
} else { | |
obj[k] = [obj[k], v]; | |
} | |
} | |
return obj; | |
}; | |
var isArray = Array.isArray || function (xs) { | |
return Object.prototype.toString.call(xs) === '[object Array]'; | |
}; | |
},{}],116:[function(require,module,exports){ | |
// Copyright Joyent, Inc. and other Node contributors. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the | |
// "Software"), to deal in the Software without restriction, including | |
// without limitation the rights to use, copy, modify, merge, publish, | |
// distribute, sublicense, and/or sell copies of the Software, and to permit | |
// persons to whom the Software is furnished to do so, subject to the | |
// following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included | |
// in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
// USE OR OTHER DEALINGS IN THE SOFTWARE. | |
'use strict'; | |
var stringifyPrimitive = function(v) { | |
switch (typeof v) { | |
case 'string': | |
return v; | |
case 'boolean': | |
return v ? 'true' : 'false'; | |
case 'number': | |
return isFinite(v) ? v : ''; | |
default: | |
return ''; | |
} | |
}; | |
module.exports = function(obj, sep, eq, name) { | |
sep = sep || '&'; | |
eq = eq || '='; | |
if (obj === null) { | |
obj = undefined; | |
} | |
if (typeof obj === 'object') { | |
return map(objectKeys(obj), function(k) { | |
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; | |
if (isArray(obj[k])) { | |
return map(obj[k], function(v) { | |
return ks + encodeURIComponent(stringifyPrimitive(v)); | |
}).join(sep); | |
} else { | |
return ks + encodeURIComponent(stringifyPrimitive(obj[k])); | |
} | |
}).join(sep); | |
} | |
if (!name) return ''; | |
return encodeURIComponent(stringifyPrimitive(name)) + eq + | |
encodeURIComponent(stringifyPrimitive(obj)); | |
}; | |
var isArray = Array.isArray || function (xs) { | |
return Object.prototype.toString.call(xs) === '[object Array]'; | |
}; | |
function map (xs, f) { | |
if (xs.map) return xs.map(f); | |
var res = []; | |
for (var i = 0; i < xs.length; i++) { | |
res.push(f(xs[i], i)); | |
} | |
return res; | |
} | |
var objectKeys = Object.keys || function (obj) { | |
var res = []; | |
for (var key in obj) { | |
if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key); | |
} | |
return res; | |
}; | |
},{}],117:[function(require,module,exports){ | |
'use strict'; | |
exports.decode = exports.parse = require('./decode'); | |
exports.encode = exports.stringify = require('./encode'); | |
},{"./decode":115,"./encode":116}],118:[function(require,module,exports){ | |
// Copyright Joyent, Inc. and other Node contributors. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the | |
// "Software"), to deal in the Software without restriction, including | |
// without limitation the rights to use, copy, modify, merge, publish, | |
// distribute, sublicense, and/or sell copies of the Software, and to permit | |
// persons to whom the Software is furnished to do so, subject to the | |
// following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included | |
// in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
// USE OR OTHER DEALINGS IN THE SOFTWARE. | |
'use strict'; | |
var punycode = require('punycode'); | |
var util = require('./util'); | |
exports.parse = urlParse; | |
exports.resolve = urlResolve; | |
exports.resolveObject = urlResolveObject; | |
exports.format = urlFormat; | |
exports.Url = Url; | |
function Url() { | |
this.protocol = null; | |
this.slashes = null; | |
this.auth = null; | |
this.host = null; | |
this.port = null; | |
this.hostname = null; | |
this.hash = null; | |
this.search = null; | |
this.query = null; | |
this.pathname = null; | |
this.path = null; | |
this.href = null; | |
} | |
// Reference: RFC 3986, RFC 1808, RFC 2396 | |
// define these here so at least they only have to be | |
// compiled once on the first module load. | |
var protocolPattern = /^([a-z0-9.+-]+:)/i, | |
portPattern = /:[0-9]*$/, | |
// Special case for a simple path URL | |
simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/, | |
// RFC 2396: characters reserved for delimiting URLs. | |
// We actually just auto-escape these. | |
delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'], | |
// RFC 2396: characters not allowed for various reasons. | |
unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims), | |
// Allowed by RFCs, but cause of XSS attacks. Always escape these. | |
autoEscape = ['\''].concat(unwise), | |
// Characters that are never ever allowed in a hostname. | |
// Note that any invalid chars are also handled, but these | |
// are the ones that are *expected* to be seen, so we fast-path | |
// them. | |
nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape), | |
hostEndingChars = ['/', '?', '#'], | |
hostnameMaxLen = 255, | |
hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/, | |
hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/, | |
// protocols that can allow "unsafe" and "unwise" chars. | |
unsafeProtocol = { | |
'javascript': true, | |
'javascript:': true | |
}, | |
// protocols that never have a hostname. | |
hostlessProtocol = { | |
'javascript': true, | |
'javascript:': true | |
}, | |
// protocols that always contain a // bit. | |
slashedProtocol = { | |
'http': true, | |
'https': true, | |
'ftp': true, | |
'gopher': true, | |
'file': true, | |
'http:': true, | |
'https:': true, | |
'ftp:': true, | |
'gopher:': true, | |
'file:': true | |
}, | |
querystring = require('querystring'); | |
function urlParse(url, parseQueryString, slashesDenoteHost) { | |
if (url && util.isObject(url) && url instanceof Url) return url; | |
var u = new Url; | |
u.parse(url, parseQueryString, slashesDenoteHost); | |
return u; | |
} | |
Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { | |
if (!util.isString(url)) { | |
throw new TypeError("Parameter 'url' must be a string, not " + typeof url); | |
} | |
// Copy chrome, IE, opera backslash-handling behavior. | |
// Back slashes before the query string get converted to forward slashes | |
// See: https://code.google.com/p/chromium/issues/detail?id=25916 | |
var queryIndex = url.indexOf('?'), | |
splitter = | |
(queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#', | |
uSplit = url.split(splitter), | |
slashRegex = /\\/g; | |
uSplit[0] = uSplit[0].replace(slashRegex, '/'); | |
url = uSplit.join(splitter); | |
var rest = url; | |
// trim before proceeding. | |
// This is to support parse stuff like " http://foo.com \n" | |
rest = rest.trim(); | |
if (!slashesDenoteHost && url.split('#').length === 1) { | |
// Try fast path regexp | |
var simplePath = simplePathPattern.exec(rest); | |
if (simplePath) { | |
this.path = rest; | |
this.href = rest; | |
this.pathname = simplePath[1]; | |
if (simplePath[2]) { | |
this.search = simplePath[2]; | |
if (parseQueryString) { | |
this.query = querystring.parse(this.search.substr(1)); | |
} else { | |
this.query = this.search.substr(1); | |
} | |
} else if (parseQueryString) { | |
this.search = ''; | |
this.query = {}; | |
} | |
return this; | |
} | |
} | |
var proto = protocolPattern.exec(rest); | |
if (proto) { | |
proto = proto[0]; | |
var lowerProto = proto.toLowerCase(); | |
this.protocol = lowerProto; | |
rest = rest.substr(proto.length); | |
} | |
// figure out if it's got a host | |
// user@server is *always* interpreted as a hostname, and url | |
// resolution will treat //foo/bar as host=foo,path=bar because that's | |
// how the browser resolves relative URLs. | |
if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) { | |
var slashes = rest.substr(0, 2) === '//'; | |
if (slashes && !(proto && hostlessProtocol[proto])) { | |
rest = rest.substr(2); | |
this.slashes = true; | |
} | |
} | |
if (!hostlessProtocol[proto] && | |
(slashes || (proto && !slashedProtocol[proto]))) { | |
// there's a hostname. | |
// the first instance of /, ?, ;, or # ends the host. | |
// | |
// If there is an @ in the hostname, then non-host chars *are* allowed | |
// to the left of the last @ sign, unless some host-ending character | |
// comes *before* the @-sign. | |
// URLs are obnoxious. | |
// | |
// ex: | |
// http://a@b@c/ => user:a@b host:c | |
// http://a@b?@c => user:a host:c path:/?@c | |
// v0.12 TODO(isaacs): This is not quite how Chrome does things. | |
// Review our test case against browsers more comprehensively. | |
// find the first instance of any hostEndingChars | |
var hostEnd = -1; | |
for (var i = 0; i < hostEndingChars.length; i++) { | |
var hec = rest.indexOf(hostEndingChars[i]); | |
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) | |
hostEnd = hec; | |
} | |
// at this point, either we have an explicit point where the | |
// auth portion cannot go past, or the last @ char is the decider. | |
var auth, atSign; | |
if (hostEnd === -1) { | |
// atSign can be anywhere. | |
atSign = rest.lastIndexOf('@'); | |
} else { | |
// atSign must be in auth portion. | |
// http://a@b/c@d => host:b auth:a path:/c@d | |
atSign = rest.lastIndexOf('@', hostEnd); | |
} | |
// Now we have a portion which is definitely the auth. | |
// Pull that off. | |
if (atSign !== -1) { | |
auth = rest.slice(0, atSign); | |
rest = rest.slice(atSign + 1); | |
this.auth = decodeURIComponent(auth); | |
} | |
// the host is the remaining to the left of the first non-host char | |
hostEnd = -1; | |
for (var i = 0; i < nonHostChars.length; i++) { | |
var hec = rest.indexOf(nonHostChars[i]); | |
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) | |
hostEnd = hec; | |
} | |
// if we still have not hit it, then the entire thing is a host. | |
if (hostEnd === -1) | |
hostEnd = rest.length; | |
this.host = rest.slice(0, hostEnd); | |
rest = rest.slice(hostEnd); | |
// pull out port. | |
this.parseHost(); | |
// we've indicated that there is a hostname, | |
// so even if it's empty, it has to be present. | |
this.hostname = this.hostname || ''; | |
// if hostname begins with [ and ends with ] | |
// assume that it's an IPv6 address. | |
var ipv6Hostname = this.hostname[0] === '[' && | |
this.hostname[this.hostname.length - 1] === ']'; | |
// validate a little. | |
if (!ipv6Hostname) { | |
var hostparts = this.hostname.split(/\./); | |
for (var i = 0, l = hostparts.length; i < l; i++) { | |
var part = hostparts[i]; | |
if (!part) continue; | |
if (!part.match(hostnamePartPattern)) { | |
var newpart = ''; | |
for (var j = 0, k = part.length; j < k; j++) { | |
if (part.charCodeAt(j) > 127) { | |
// we replace non-ASCII char with a temporary placeholder | |
// we need this to make sure size of hostname is not | |
// broken by replacing non-ASCII by nothing | |
newpart += 'x'; | |
} else { | |
newpart += part[j]; | |
} | |
} | |
// we test again with ASCII char only | |
if (!newpart.match(hostnamePartPattern)) { | |
var validParts = hostparts.slice(0, i); | |
var notHost = hostparts.slice(i + 1); | |
var bit = part.match(hostnamePartStart); | |
if (bit) { | |
validParts.push(bit[1]); | |
notHost.unshift(bit[2]); | |
} | |
if (notHost.length) { | |
rest = '/' + notHost.join('.') + rest; | |
} | |
this.hostname = validParts.join('.'); | |
break; | |
} | |
} | |
} | |
} | |
if (this.hostname.length > hostnameMaxLen) { | |
this.hostname = ''; | |
} else { | |
// hostnames are always lower case. | |
this.hostname = this.hostname.toLowerCase(); | |
} | |
if (!ipv6Hostname) { | |
// IDNA Support: Returns a punycoded representation of "domain". | |
// It only converts parts of the domain name that | |
// have non-ASCII characters, i.e. it doesn't matter if | |
// you call it with a domain that already is ASCII-only. | |
this.hostname = punycode.toASCII(this.hostname); | |
} | |
var p = this.port ? ':' + this.port : ''; | |
var h = this.hostname || ''; | |
this.host = h + p; | |
this.href += this.host; | |
// strip [ and ] from the hostname | |
// the host field still retains them, though | |
if (ipv6Hostname) { | |
this.hostname = this.hostname.substr(1, this.hostname.length - 2); | |
if (rest[0] !== '/') { | |
rest = '/' + rest; | |
} | |
} | |
} | |
// now rest is set to the post-host stuff. | |
// chop off any delim chars. | |
if (!unsafeProtocol[lowerProto]) { | |
// First, make 100% sure that any "autoEscape" chars get | |
// escaped, even if encodeURIComponent doesn't think they | |
// need to be. | |
for (var i = 0, l = autoEscape.length; i < l; i++) { | |
var ae = autoEscape[i]; | |
if (rest.indexOf(ae) === -1) | |
continue; | |
var esc = encodeURIComponent(ae); | |
if (esc === ae) { | |
esc = escape(ae); | |
} | |
rest = rest.split(ae).join(esc); | |
} | |
} | |
// chop off from the tail first. | |
var hash = rest.indexOf('#'); | |
if (hash !== -1) { | |
// got a fragment string. | |
this.hash = rest.substr(hash); | |
rest = rest.slice(0, hash); | |
} | |
var qm = rest.indexOf('?'); | |
if (qm !== -1) { | |
this.search = rest.substr(qm); | |
this.query = rest.substr(qm + 1); | |
if (parseQueryString) { | |
this.query = querystring.parse(this.query); | |
} | |
rest = rest.slice(0, qm); | |
} else if (parseQueryString) { | |
// no query string, but parseQueryString still requested | |
this.search = ''; | |
this.query = {}; | |
} | |
if (rest) this.pathname = rest; | |
if (slashedProtocol[lowerProto] && | |
this.hostname && !this.pathname) { | |
this.pathname = '/'; | |
} | |
//to support http.request | |
if (this.pathname || this.search) { | |
var p = this.pathname || ''; | |
var s = this.search || ''; | |
this.path = p + s; | |
} | |
// finally, reconstruct the href based on what has been validated. | |
this.href = this.format(); | |
return this; | |
}; | |
// format a parsed object into a url string | |
function urlFormat(obj) { | |
// ensure it's an object, and not a string url. | |
// If it's an obj, this is a no-op. | |
// this way, you can call url_format() on strings | |
// to clean up potentially wonky urls. | |
if (util.isString(obj)) obj = urlParse(obj); | |
if (!(obj instanceof Url)) return Url.prototype.format.call(obj); | |
return obj.format(); | |
} | |
Url.prototype.format = function() { | |
var auth = this.auth || ''; | |
if (auth) { | |
auth = encodeURIComponent(auth); | |
auth = auth.replace(/%3A/i, ':'); | |
auth += '@'; | |
} | |
var protocol = this.protocol || '', | |
pathname = this.pathname || '', | |
hash = this.hash || '', | |
host = false, | |
query = ''; | |
if (this.host) { | |
host = auth + this.host; | |
} else if (this.hostname) { | |
host = auth + (this.hostname.indexOf(':') === -1 ? | |
this.hostname : | |
'[' + this.hostname + ']'); | |
if (this.port) { | |
host += ':' + this.port; | |
} | |
} | |
if (this.query && | |
util.isObject(this.query) && | |
Object.keys(this.query).length) { | |
query = querystring.stringify(this.query); | |
} | |
var search = this.search || (query && ('?' + query)) || ''; | |
if (protocol && protocol.substr(-1) !== ':') protocol += ':'; | |
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc. | |
// unless they had them to begin with. | |
if (this.slashes || | |
(!protocol || slashedProtocol[protocol]) && host !== false) { | |
host = '//' + (host || ''); | |
if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname; | |
} else if (!host) { | |
host = ''; | |
} | |
if (hash && hash.charAt(0) !== '#') hash = '#' + hash; | |
if (search && search.charAt(0) !== '?') search = '?' + search; | |
pathname = pathname.replace(/[?#]/g, function(match) { | |
return encodeURIComponent(match); | |
}); | |
search = search.replace('#', '%23'); | |
return protocol + host + pathname + search + hash; | |
}; | |
function urlResolve(source, relative) { | |
return urlParse(source, false, true).resolve(relative); | |
} | |
Url.prototype.resolve = function(relative) { | |
return this.resolveObject(urlParse(relative, false, true)).format(); | |
}; | |
function urlResolveObject(source, relative) { | |
if (!source) return relative; | |
return urlParse(source, false, true).resolveObject(relative); | |
} | |
Url.prototype.resolveObject = function(relative) { | |
if (util.isString(relative)) { | |
var rel = new Url(); | |
rel.parse(relative, false, true); | |
relative = rel; | |
} | |
var result = new Url(); | |
var tkeys = Object.keys(this); | |
for (var tk = 0; tk < tkeys.length; tk++) { | |
var tkey = tkeys[tk]; | |
result[tkey] = this[tkey]; | |
} | |
// hash is always overridden, no matter what. | |
// even href="" will remove it. | |
result.hash = relative.hash; | |
// if the relative url is empty, then there's nothing left to do here. | |
if (relative.href === '') { | |
result.href = result.format(); | |
return result; | |
} | |
// hrefs like //foo/bar always cut to the protocol. | |
if (relative.slashes && !relative.protocol) { | |
// take everything except the protocol from relative | |
var rkeys = Object.keys(relative); | |
for (var rk = 0; rk < rkeys.length; rk++) { | |
var rkey = rkeys[rk]; | |
if (rkey !== 'protocol') | |
result[rkey] = relative[rkey]; | |
} | |
//urlParse appends trailing / to urls like http://www.example.com | |
if (slashedProtocol[result.protocol] && | |
result.hostname && !result.pathname) { | |
result.path = result.pathname = '/'; | |
} | |
result.href = result.format(); | |
return result; | |
} | |
if (relative.protocol && relative.protocol !== result.protocol) { | |
// if it's a known url protocol, then changing | |
// the protocol does weird things | |
// first, if it's not file:, then we MUST have a host, | |
// and if there was a path | |
// to begin with, then we MUST have a path. | |
// if it is file:, then the host is dropped, | |
// because that's known to be hostless. | |
// anything else is assumed to be absolute. | |
if (!slashedProtocol[relative.protocol]) { | |
var keys = Object.keys(relative); | |
for (var v = 0; v < keys.length; v++) { | |
var k = keys[v]; | |
result[k] = relative[k]; | |
} | |
result.href = result.format(); | |
return result; | |
} | |
result.protocol = relative.protocol; | |
if (!relative.host && !hostlessProtocol[relative.protocol]) { | |
var relPath = (relative.pathname || '').split('/'); | |
while (relPath.length && !(relative.host = relPath.shift())); | |
if (!relative.host) relative.host = ''; | |
if (!relative.hostname) relative.hostname = ''; | |
if (relPath[0] !== '') relPath.unshift(''); | |
if (relPath.length < 2) relPath.unshift(''); | |
result.pathname = relPath.join('/'); | |
} else { | |
result.pathname = relative.pathname; | |
} | |
result.search = relative.search; | |
result.query = relative.query; | |
result.host = relative.host || ''; | |
result.auth = relative.auth; | |
result.hostname = relative.hostname || relative.host; | |
result.port = relative.port; | |
// to support http.request | |
if (result.pathname || result.search) { | |
var p = result.pathname || ''; | |
var s = result.search || ''; | |
result.path = p + s; | |
} | |
result.slashes = result.slashes || relative.slashes; | |
result.href = result.format(); | |
return result; | |
} | |
var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'), | |
isRelAbs = ( | |
relative.host || | |
relative.pathname && relative.pathname.charAt(0) === '/' | |
), | |
mustEndAbs = (isRelAbs || isSourceAbs || | |
(result.host && relative.pathname)), | |
removeAllDots = mustEndAbs, | |
srcPath = result.pathname && result.pathname.split('/') || [], | |
relPath = relative.pathname && relative.pathname.split('/') || [], | |
psychotic = result.protocol && !slashedProtocol[result.protocol]; | |
// if the url is a non-slashed url, then relative | |
// links like ../.. should be able | |
// to crawl up to the hostname, as well. This is strange. | |
// result.protocol has already been set by now. | |
// Later on, put the first path part into the host field. | |
if (psychotic) { | |
result.hostname = ''; | |
result.port = null; | |
if (result.host) { | |
if (srcPath[0] === '') srcPath[0] = result.host; | |
else srcPath.unshift(result.host); | |
} | |
result.host = ''; | |
if (relative.protocol) { | |
relative.hostname = null; | |
relative.port = null; | |
if (relative.host) { | |
if (relPath[0] === '') relPath[0] = relative.host; | |
else relPath.unshift(relative.host); | |
} | |
relative.host = null; | |
} | |
mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === ''); | |
} | |
if (isRelAbs) { | |
// it's absolute. | |
result.host = (relative.host || relative.host === '') ? | |
relative.host : result.host; | |
result.hostname = (relative.hostname || relative.hostname === '') ? | |
relative.hostname : result.hostname; | |
result.search = relative.search; | |
result.query = relative.query; | |
srcPath = relPath; | |
// fall through to the dot-handling below. | |
} else if (relPath.length) { | |
// it's relative | |
// throw away the existing file, and take the new path instead. | |
if (!srcPath) srcPath = []; | |
srcPath.pop(); | |
srcPath = srcPath.concat(relPath); | |
result.search = relative.search; | |
result.query = relative.query; | |
} else if (!util.isNullOrUndefined(relative.search)) { | |
// just pull out the search. | |
// like href='?foo'. | |
// Put this after the other two cases because it simplifies the booleans | |
if (psychotic) { | |
result.hostname = result.host = srcPath.shift(); | |
//occationaly the auth can get stuck only in host | |
//this especially happens in cases like | |
//url.resolveObject('mailto:local1@domain1', 'local2@domain2') | |
var authInHost = result.host && result.host.indexOf('@') > 0 ? | |
result.host.split('@') : false; | |
if (authInHost) { | |
result.auth = authInHost.shift(); | |
result.host = result.hostname = authInHost.shift(); | |
} | |
} | |
result.search = relative.search; | |
result.query = relative.query; | |
//to support http.request | |
if (!util.isNull(result.pathname) || !util.isNull(result.search)) { | |
result.path = (result.pathname ? result.pathname : '') + | |
(result.search ? result.search : ''); | |
} | |
result.href = result.format(); | |
return result; | |
} | |
if (!srcPath.length) { | |
// no path at all. easy. | |
// we've already handled the other stuff above. | |
result.pathname = null; | |
//to support http.request | |
if (result.search) { | |
result.path = '/' + result.search; | |
} else { | |
result.path = null; | |
} | |
result.href = result.format(); | |
return result; | |
} | |
// if a url ENDs in . or .., then it must get a trailing slash. | |
// however, if it ends in anything else non-slashy, | |
// then it must NOT get a trailing slash. | |
var last = srcPath.slice(-1)[0]; | |
var hasTrailingSlash = ( | |
(result.host || relative.host || srcPath.length > 1) && | |
(last === '.' || last === '..') || last === ''); | |
// strip single dots, resolve double dots to parent dir | |
// if the path tries to go above the root, `up` ends up > 0 | |
var up = 0; | |
for (var i = srcPath.length; i >= 0; i--) { | |
last = srcPath[i]; | |
if (last === '.') { | |
srcPath.splice(i, 1); | |
} else if (last === '..') { | |
srcPath.splice(i, 1); | |
up++; | |
} else if (up) { | |
srcPath.splice(i, 1); | |
up--; | |
} | |
} | |
// if the path is allowed to go above the root, restore leading ..s | |
if (!mustEndAbs && !removeAllDots) { | |
for (; up--; up) { | |
srcPath.unshift('..'); | |
} | |
} | |
if (mustEndAbs && srcPath[0] !== '' && | |
(!srcPath[0] || srcPath[0].charAt(0) !== '/')) { | |
srcPath.unshift(''); | |
} | |
if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) { | |
srcPath.push(''); | |
} | |
var isAbsolute = srcPath[0] === '' || | |
(srcPath[0] && srcPath[0].charAt(0) === '/'); | |
// put the host back | |
if (psychotic) { | |
result.hostname = result.host = isAbsolute ? '' : | |
srcPath.length ? srcPath.shift() : ''; | |
//occationaly the auth can get stuck only in host | |
//this especially happens in cases like | |
//url.resolveObject('mailto:local1@domain1', 'local2@domain2') | |
var authInHost = result.host && result.host.indexOf('@') > 0 ? | |
result.host.split('@') : false; | |
if (authInHost) { | |
result.auth = authInHost.shift(); | |
result.host = result.hostname = authInHost.shift(); | |
} | |
} | |
mustEndAbs = mustEndAbs || (result.host && srcPath.length); | |
if (mustEndAbs && !isAbsolute) { | |
srcPath.unshift(''); | |
} | |
if (!srcPath.length) { | |
result.pathname = null; | |
result.path = null; | |
} else { | |
result.pathname = srcPath.join('/'); | |
} | |
//to support request.http | |
if (!util.isNull(result.pathname) || !util.isNull(result.search)) { | |
result.path = (result.pathname ? result.pathname : '') + | |
(result.search ? result.search : ''); | |
} | |
result.auth = relative.auth || result.auth; | |
result.slashes = result.slashes || relative.slashes; | |
result.href = result.format(); | |
return result; | |
}; | |
Url.prototype.parseHost = function() { | |
var host = this.host; | |
var port = portPattern.exec(host); | |
if (port) { | |
port = port[0]; | |
if (port !== ':') { | |
this.port = port.substr(1); | |
} | |
host = host.substr(0, host.length - port.length); | |
} | |
if (host) this.hostname = host; | |
}; | |
},{"./util":119,"punycode":114,"querystring":117}],119:[function(require,module,exports){ | |
'use strict'; | |
module.exports = { | |
isString: function(arg) { | |
return typeof(arg) === 'string'; | |
}, | |
isObject: function(arg) { | |
return typeof(arg) === 'object' && arg !== null; | |
}, | |
isNull: function(arg) { | |
return arg === null; | |
}, | |
isNullOrUndefined: function(arg) { | |
return arg == null; | |
} | |
}; | |
},{}],120:[function(require,module,exports){ | |
module.exports = function isBuffer(arg) { | |
return arg && typeof arg === 'object' | |
&& typeof arg.copy === 'function' | |
&& typeof arg.fill === 'function' | |
&& typeof arg.readUInt8 === 'function'; | |
} | |
},{}],121:[function(require,module,exports){ | |
(function (process,global){ | |
// Copyright Joyent, Inc. and other Node contributors. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the | |
// "Software"), to deal in the Software without restriction, including | |
// without limitation the rights to use, copy, modify, merge, publish, | |
// distribute, sublicense, and/or sell copies of the Software, and to permit | |
// persons to whom the Software is furnished to do so, subject to the | |
// following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included | |
// in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
// USE OR OTHER DEALINGS IN THE SOFTWARE. | |
var formatRegExp = /%[sdj%]/g; | |
exports.format = function(f) { | |
if (!isString(f)) { | |
var objects = []; | |
for (var i = 0; i < arguments.length; i++) { | |
objects.push(inspect(arguments[i])); | |
} | |
return objects.join(' '); | |
} | |
var i = 1; | |
var args = arguments; | |
var len = args.length; | |
var str = String(f).replace(formatRegExp, function(x) { | |
if (x === '%%') return '%'; | |
if (i >= len) return x; | |
switch (x) { | |
case '%s': return String(args[i++]); | |
case '%d': return Number(args[i++]); | |
case '%j': | |
try { | |
return JSON.stringify(args[i++]); | |
} catch (_) { | |
return '[Circular]'; | |
} | |
default: | |
return x; | |
} | |
}); | |
for (var x = args[i]; i < len; x = args[++i]) { | |
if (isNull(x) || !isObject(x)) { | |
str += ' ' + x; | |
} else { | |
str += ' ' + inspect(x); | |
} | |
} | |
return str; | |
}; | |
// Mark that a method should not be used. | |
// Returns a modified function which warns once by default. | |
// If --no-deprecation is set, then it is a no-op. | |
exports.deprecate = function(fn, msg) { | |
// Allow for deprecating things in the process of starting up. | |
if (isUndefined(global.process)) { | |
return function() { | |
return exports.deprecate(fn, msg).apply(this, arguments); | |
}; | |
} | |
if (process.noDeprecation === true) { | |
return fn; | |
} | |
var warned = false; | |
function deprecated() { | |
if (!warned) { | |
if (process.throwDeprecation) { | |
throw new Error(msg); | |
} else if (process.traceDeprecation) { | |
console.trace(msg); | |
} else { | |
console.error(msg); | |
} | |
warned = true; | |
} | |
return fn.apply(this, arguments); | |
} | |
return deprecated; | |
}; | |
var debugs = {}; | |
var debugEnviron; | |
exports.debuglog = function(set) { | |
if (isUndefined(debugEnviron)) | |
debugEnviron = process.env.NODE_DEBUG || ''; | |
set = set.toUpperCase(); | |
if (!debugs[set]) { | |
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { | |
var pid = process.pid; | |
debugs[set] = function() { | |
var msg = exports.format.apply(exports, arguments); | |
console.error('%s %d: %s', set, pid, msg); | |
}; | |
} else { | |
debugs[set] = function() {}; | |
} | |
} | |
return debugs[set]; | |
}; | |
/** | |
* Echos the value of a value. Trys to print the value out | |
* in the best way possible given the different types. | |
* | |
* @param {Object} obj The object to print out. | |
* @param {Object} opts Optional options object that alters the output. | |
*/ | |
/* legacy: obj, showHidden, depth, colors*/ | |
function inspect(obj, opts) { | |
// default options | |
var ctx = { | |
seen: [], | |
stylize: stylizeNoColor | |
}; | |
// legacy... | |
if (arguments.length >= 3) ctx.depth = arguments[2]; | |
if (arguments.length >= 4) ctx.colors = arguments[3]; | |
if (isBoolean(opts)) { | |
// legacy... | |
ctx.showHidden = opts; | |
} else if (opts) { | |
// got an "options" object | |
exports._extend(ctx, opts); | |
} | |
// set default options | |
if (isUndefined(ctx.showHidden)) ctx.showHidden = false; | |
if (isUndefined(ctx.depth)) ctx.depth = 2; | |
if (isUndefined(ctx.colors)) ctx.colors = false; | |
if (isUndefined(ctx.customInspect)) ctx.customInspect = true; | |
if (ctx.colors) ctx.stylize = stylizeWithColor; | |
return formatValue(ctx, obj, ctx.depth); | |
} | |
exports.inspect = inspect; | |
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics | |
inspect.colors = { | |
'bold' : [1, 22], | |
'italic' : [3, 23], | |
'underline' : [4, 24], | |
'inverse' : [7, 27], | |
'white' : [37, 39], | |
'grey' : [90, 39], | |
'black' : [30, 39], | |
'blue' : [34, 39], | |
'cyan' : [36, 39], | |
'green' : [32, 39], | |
'magenta' : [35, 39], | |
'red' : [31, 39], | |
'yellow' : [33, 39] | |
}; | |
// Don't use 'blue' not visible on cmd.exe | |
inspect.styles = { | |
'special': 'cyan', | |
'number': 'yellow', | |
'boolean': 'yellow', | |
'undefined': 'grey', | |
'null': 'bold', | |
'string': 'green', | |
'date': 'magenta', | |
// "name": intentionally not styling | |
'regexp': 'red' | |
}; | |
function stylizeWithColor(str, styleType) { | |
var style = inspect.styles[styleType]; | |
if (style) { | |
return '\u001b[' + inspect.colors[style][0] + 'm' + str + | |
'\u001b[' + inspect.colors[style][1] + 'm'; | |
} else { | |
return str; | |
} | |
} | |
function stylizeNoColor(str, styleType) { | |
return str; | |
} | |
function arrayToHash(array) { | |
var hash = {}; | |
array.forEach(function(val, idx) { | |
hash[val] = true; | |
}); | |
return hash; | |
} | |
function formatValue(ctx, value, recurseTimes) { | |
// Provide a hook for user-specified inspect functions. | |
// Check that value is an object with an inspect function on it | |
if (ctx.customInspect && | |
value && | |
isFunction(value.inspect) && | |
// Filter out the util module, it's inspect function is special | |
value.inspect !== exports.inspect && | |
// Also filter out any prototype objects using the circular check. | |
!(value.constructor && value.constructor.prototype === value)) { | |
var ret = value.inspect(recurseTimes, ctx); | |
if (!isString(ret)) { | |
ret = formatValue(ctx, ret, recurseTimes); | |
} | |
return ret; | |
} | |
// Primitive types cannot have properties | |
var primitive = formatPrimitive(ctx, value); | |
if (primitive) { | |
return primitive; | |
} | |
// Look up the keys of the object. | |
var keys = Object.keys(value); | |
var visibleKeys = arrayToHash(keys); | |
if (ctx.showHidden) { | |
keys = Object.getOwnPropertyNames(value); | |
} | |
// IE doesn't make error fields non-enumerable | |
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx | |
if (isError(value) | |
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { | |
return formatError(value); | |
} | |
// Some type of object without properties can be shortcutted. | |
if (keys.length === 0) { | |
if (isFunction(value)) { | |
var name = value.name ? ': ' + value.name : ''; | |
return ctx.stylize('[Function' + name + ']', 'special'); | |
} | |
if (isRegExp(value)) { | |
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); | |
} | |
if (isDate(value)) { | |
return ctx.stylize(Date.prototype.toString.call(value), 'date'); | |
} | |
if (isError(value)) { | |
return formatError(value); | |
} | |
} | |
var base = '', array = false, braces = ['{', '}']; | |
// Make Array say that they are Array | |
if (isArray(value)) { | |
array = true; | |
braces = ['[', ']']; | |
} | |
// Make functions say that they are functions | |
if (isFunction(value)) { | |
var n = value.name ? ': ' + value.name : ''; | |
base = ' [Function' + n + ']'; | |
} | |
// Make RegExps say that they are RegExps | |
if (isRegExp(value)) { | |
base = ' ' + RegExp.prototype.toString.call(value); | |
} | |
// Make dates with properties first say the date | |
if (isDate(value)) { | |
base = ' ' + Date.prototype.toUTCString.call(value); | |
} | |
// Make error with message first say the error | |
if (isError(value)) { | |
base = ' ' + formatError(value); | |
} | |
if (keys.length === 0 && (!array || value.length == 0)) { | |
return braces[0] + base + braces[1]; | |
} | |
if (recurseTimes < 0) { | |
if (isRegExp(value)) { | |
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); | |
} else { | |
return ctx.stylize('[Object]', 'special'); | |
} | |
} | |
ctx.seen.push(value); | |
var output; | |
if (array) { | |
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); | |
} else { | |
output = keys.map(function(key) { | |
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); | |
}); | |
} | |
ctx.seen.pop(); | |
return reduceToSingleString(output, base, braces); | |
} | |
function formatPrimitive(ctx, value) { | |
if (isUndefined(value)) | |
return ctx.stylize('undefined', 'undefined'); | |
if (isString(value)) { | |
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') | |
.replace(/'/g, "\\'") | |
.replace(/\\"/g, '"') + '\''; | |
return ctx.stylize(simple, 'string'); | |
} | |
if (isNumber(value)) | |
return ctx.stylize('' + value, 'number'); | |
if (isBoolean(value)) | |
return ctx.stylize('' + value, 'boolean'); | |
// For some reason typeof null is "object", so special case here. | |
if (isNull(value)) | |
return ctx.stylize('null', 'null'); | |
} | |
function formatError(value) { | |
return '[' + Error.prototype.toString.call(value) + ']'; | |
} | |
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { | |
var output = []; | |
for (var i = 0, l = value.length; i < l; ++i) { | |
if (hasOwnProperty(value, String(i))) { | |
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, | |
String(i), true)); | |
} else { | |
output.push(''); | |
} | |
} | |
keys.forEach(function(key) { | |
if (!key.match(/^\d+$/)) { | |
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, | |
key, true)); | |
} | |
}); | |
return output; | |
} | |
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { | |
var name, str, desc; | |
desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; | |
if (desc.get) { | |
if (desc.set) { | |
str = ctx.stylize('[Getter/Setter]', 'special'); | |
} else { | |
str = ctx.stylize('[Getter]', 'special'); | |
} | |
} else { | |
if (desc.set) { | |
str = ctx.stylize('[Setter]', 'special'); | |
} | |
} | |
if (!hasOwnProperty(visibleKeys, key)) { | |
name = '[' + key + ']'; | |
} | |
if (!str) { | |
if (ctx.seen.indexOf(desc.value) < 0) { | |
if (isNull(recurseTimes)) { | |
str = formatValue(ctx, desc.value, null); | |
} else { | |
str = formatValue(ctx, desc.value, recurseTimes - 1); | |
} | |
if (str.indexOf('\n') > -1) { | |
if (array) { | |
str = str.split('\n').map(function(line) { | |
return ' ' + line; | |
}).join('\n').substr(2); | |
} else { | |
str = '\n' + str.split('\n').map(function(line) { | |
return ' ' + line; | |
}).join('\n'); | |
} | |
} | |
} else { | |
str = ctx.stylize('[Circular]', 'special'); | |
} | |
} | |
if (isUndefined(name)) { | |
if (array && key.match(/^\d+$/)) { | |
return str; | |
} | |
name = JSON.stringify('' + key); | |
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { | |
name = name.substr(1, name.length - 2); | |
name = ctx.stylize(name, 'name'); | |
} else { | |
name = name.replace(/'/g, "\\'") | |
.replace(/\\"/g, '"') | |
.replace(/(^"|"$)/g, "'"); | |
name = ctx.stylize(name, 'string'); | |
} | |
} | |
return name + ': ' + str; | |
} | |
function reduceToSingleString(output, base, braces) { | |
var numLinesEst = 0; | |
var length = output.reduce(function(prev, cur) { | |
numLinesEst++; | |
if (cur.indexOf('\n') >= 0) numLinesEst++; | |
return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; | |
}, 0); | |
if (length > 60) { | |
return braces[0] + | |
(base === '' ? '' : base + '\n ') + | |
' ' + | |
output.join(',\n ') + | |
' ' + | |
braces[1]; | |
} | |
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; | |
} | |
// NOTE: These type checking functions intentionally don't use `instanceof` | |
// because it is fragile and can be easily faked with `Object.create()`. | |
function isArray(ar) { | |
return Array.isArray(ar); | |
} | |
exports.isArray = isArray; | |
function isBoolean(arg) { | |
return typeof arg === 'boolean'; | |
} | |
exports.isBoolean = isBoolean; | |
function isNull(arg) { | |
return arg === null; | |
} | |
exports.isNull = isNull; | |
function isNullOrUndefined(arg) { | |
return arg == null; | |
} | |
exports.isNullOrUndefined = isNullOrUndefined; | |
function isNumber(arg) { | |
return typeof arg === 'number'; | |
} | |
exports.isNumber = isNumber; | |
function isString(arg) { | |
return typeof arg === 'string'; | |
} | |
exports.isString = isString; | |
function isSymbol(arg) { | |
return typeof arg === 'symbol'; | |
} | |
exports.isSymbol = isSymbol; | |
function isUndefined(arg) { | |
return arg === void 0; | |
} | |
exports.isUndefined = isUndefined; | |
function isRegExp(re) { | |
return isObject(re) && objectToString(re) === '[object RegExp]'; | |
} | |
exports.isRegExp = isRegExp; | |
function isObject(arg) { | |
return typeof arg === 'object' && arg !== null; | |
} | |
exports.isObject = isObject; | |
function isDate(d) { | |
return isObject(d) && objectToString(d) === '[object Date]'; | |
} | |
exports.isDate = isDate; | |
function isError(e) { | |
return isObject(e) && | |
(objectToString(e) === '[object Error]' || e instanceof Error); | |
} | |
exports.isError = isError; | |
function isFunction(arg) { | |
return typeof arg === 'function'; | |
} | |
exports.isFunction = isFunction; | |
function isPrimitive(arg) { | |
return arg === null || | |
typeof arg === 'boolean' || | |
typeof arg === 'number' || | |
typeof arg === 'string' || | |
typeof arg === 'symbol' || // ES6 symbol | |
typeof arg === 'undefined'; | |
} | |
exports.isPrimitive = isPrimitive; | |
exports.isBuffer = require('./support/isBuffer'); | |
function objectToString(o) { | |
return Object.prototype.toString.call(o); | |
} | |
function pad(n) { | |
return n < 10 ? '0' + n.toString(10) : n.toString(10); | |
} | |
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', | |
'Oct', 'Nov', 'Dec']; | |
// 26 Feb 16:19:34 | |
function timestamp() { | |
var d = new Date(); | |
var time = [pad(d.getHours()), | |
pad(d.getMinutes()), | |
pad(d.getSeconds())].join(':'); | |
return [d.getDate(), months[d.getMonth()], time].join(' '); | |
} | |
// log is just a thin wrapper to console.log that prepends a timestamp | |
exports.log = function() { | |
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); | |
}; | |
/** | |
* Inherit the prototype methods from one constructor into another. | |
* | |
* The Function.prototype.inherits from lang.js rewritten as a standalone | |
* function (not on Function.prototype). NOTE: If this file is to be loaded | |
* during bootstrapping this function needs to be rewritten using some native | |
* functions as prototype setup using normal JavaScript does not work as | |
* expected during bootstrapping (see mirror.js in r114903). | |
* | |
* @param {function} ctor Constructor function which needs to inherit the | |
* prototype. | |
* @param {function} superCtor Constructor function to inherit prototype from. | |
*/ | |
exports.inherits = require('inherits'); | |
exports._extend = function(origin, add) { | |
// Don't do anything if add isn't an object | |
if (!add || !isObject(add)) return origin; | |
var keys = Object.keys(add); | |
var i = keys.length; | |
while (i--) { | |
origin[keys[i]] = add[keys[i]]; | |
} | |
return origin; | |
}; | |
function hasOwnProperty(obj, prop) { | |
return Object.prototype.hasOwnProperty.call(obj, prop); | |
} | |
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | |
},{"./support/isBuffer":120,"_process":113,"inherits":111}],122:[function(require,module,exports){ | |
// (c) Dean McNamee <[email protected]>, 2012. | |
// | |
// https://github.com/deanm/css-color-parser-js | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to | |
// deal in the Software without restriction, including without limitation the | |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
// sell copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
// IN THE SOFTWARE. | |
// http://www.w3.org/TR/css3-color/ | |
var kCSSColorTable = { | |
"transparent": [0,0,0,0], "aliceblue": [240,248,255,1], | |
"antiquewhite": [250,235,215,1], "aqua": [0,255,255,1], | |
"aquamarine": [127,255,212,1], "azure": [240,255,255,1], | |
"beige": [245,245,220,1], "bisque": [255,228,196,1], | |
"black": [0,0,0,1], "blanchedalmond": [255,235,205,1], | |
"blue": [0,0,255,1], "blueviolet": [138,43,226,1], | |
"brown": [165,42,42,1], "burlywood": [222,184,135,1], | |
"cadetblue": [95,158,160,1], "chartreuse": [127,255,0,1], | |
"chocolate": [210,105,30,1], "coral": [255,127,80,1], | |
"cornflowerblue": [100,149,237,1], "cornsilk": [255,248,220,1], | |
"crimson": [220,20,60,1], "cyan": [0,255,255,1], | |
"darkblue": [0,0,139,1], "darkcyan": [0,139,139,1], | |
"darkgoldenrod": [184,134,11,1], "darkgray": [169,169,169,1], | |
"darkgreen": [0,100,0,1], "darkgrey": [169,169,169,1], | |
"darkkhaki": [189,183,107,1], "darkmagenta": [139,0,139,1], | |
"darkolivegreen": [85,107,47,1], "darkorange": [255,140,0,1], | |
"darkorchid": [153,50,204,1], "darkred": [139,0,0,1], | |
"darksalmon": [233,150,122,1], "darkseagreen": [143,188,143,1], | |
"darkslateblue": [72,61,139,1], "darkslategray": [47,79,79,1], | |
"darkslategrey": [47,79,79,1], "darkturquoise": [0,206,209,1], | |
"darkviolet": [148,0,211,1], "deeppink": [255,20,147,1], | |
"deepskyblue": [0,191,255,1], "dimgray": [105,105,105,1], | |
"dimgrey": [105,105,105,1], "dodgerblue": [30,144,255,1], | |
"firebrick": [178,34,34,1], "floralwhite": [255,250,240,1], | |
"forestgreen": [34,139,34,1], "fuchsia": [255,0,255,1], | |
"gainsboro": [220,220,220,1], "ghostwhite": [248,248,255,1], | |
"gold": [255,215,0,1], "goldenrod": [218,165,32,1], | |
"gray": [128,128,128,1], "green": [0,128,0,1], | |
"greenyellow": [173,255,47,1], "grey": [128,128,128,1], | |
"honeydew": [240,255,240,1], "hotpink": [255,105,180,1], | |
"indianred": [205,92,92,1], "indigo": [75,0,130,1], | |
"ivory": [255,255,240,1], "khaki": [240,230,140,1], | |
"lavender": [230,230,250,1], "lavenderblush": [255,240,245,1], | |
"lawngreen": [124,252,0,1], "lemonchiffon": [255,250,205,1], | |
"lightblue": [173,216,230,1], "lightcoral": [240,128,128,1], | |
"lightcyan": [224,255,255,1], "lightgoldenrodyellow": [250,250,210,1], | |
"lightgray": [211,211,211,1], "lightgreen": [144,238,144,1], | |
"lightgrey": [211,211,211,1], "lightpink": [255,182,193,1], | |
"lightsalmon": [255,160,122,1], "lightseagreen": [32,178,170,1], | |
"lightskyblue": [135,206,250,1], "lightslategray": [119,136,153,1], | |
"lightslategrey": [119,136,153,1], "lightsteelblue": [176,196,222,1], | |
"lightyellow": [255,255,224,1], "lime": [0,255,0,1], | |
"limegreen": [50,205,50,1], "linen": [250,240,230,1], | |
"magenta": [255,0,255,1], "maroon": [128,0,0,1], | |
"mediumaquamarine": [102,205,170,1], "mediumblue": [0,0,205,1], | |
"mediumorchid": [186,85,211,1], "mediumpurple": [147,112,219,1], | |
"mediumseagreen": [60,179,113,1], "mediumslateblue": [123,104,238,1], | |
"mediumspringgreen": [0,250,154,1], "mediumturquoise": [72,209,204,1], | |
"mediumvioletred": [199,21,133,1], "midnightblue": [25,25,112,1], | |
"mintcream": [245,255,250,1], "mistyrose": [255,228,225,1], | |
"moccasin": [255,228,181,1], "navajowhite": [255,222,173,1], | |
"navy": [0,0,128,1], "oldlace": [253,245,230,1], | |
"olive": [128,128,0,1], "olivedrab": [107,142,35,1], | |
"orange": [255,165,0,1], "orangered": [255,69,0,1], | |
"orchid": [218,112,214,1], "palegoldenrod": [238,232,170,1], | |
"palegreen": [152,251,152,1], "paleturquoise": [175,238,238,1], | |
"palevioletred": [219,112,147,1], "papayawhip": [255,239,213,1], | |
"peachpuff": [255,218,185,1], "peru": [205,133,63,1], | |
"pink": [255,192,203,1], "plum": [221,160,221,1], | |
"powderblue": [176,224,230,1], "purple": [128,0,128,1], | |
"red": [255,0,0,1], "rosybrown": [188,143,143,1], | |
"royalblue": [65,105,225,1], "saddlebrown": [139,69,19,1], | |
"salmon": [250,128,114,1], "sandybrown": [244,164,96,1], | |
"seagreen": [46,139,87,1], "seashell": [255,245,238,1], | |
"sienna": [160,82,45,1], "silver": [192,192,192,1], | |
"skyblue": [135,206,235,1], "slateblue": [106,90,205,1], | |
"slategray": [112,128,144,1], "slategrey": [112,128,144,1], | |
"snow": [255,250,250,1], "springgreen": [0,255,127,1], | |
"steelblue": [70,130,180,1], "tan": [210,180,140,1], | |
"teal": [0,128,128,1], "thistle": [216,191,216,1], | |
"tomato": [255,99,71,1], "turquoise": [64,224,208,1], | |
"violet": [238,130,238,1], "wheat": [245,222,179,1], | |
"white": [255,255,255,1], "whitesmoke": [245,245,245,1], | |
"yellow": [255,255,0,1], "yellowgreen": [154,205,50,1]} | |
function clamp_css_byte(i) { // Clamp to integer 0 .. 255. | |
i = Math.round(i); // Seems to be what Chrome does (vs truncation). | |
return i < 0 ? 0 : i > 255 ? 255 : i; | |
} | |
function clamp_css_float(f) { // Clamp to float 0.0 .. 1.0. | |
return f < 0 ? 0 : f > 1 ? 1 : f; | |
} | |
function parse_css_int(str) { // int or percentage. | |
if (str[str.length - 1] === '%') | |
return clamp_css_byte(parseFloat(str) / 100 * 255); | |
return clamp_css_byte(parseInt(str)); | |
} | |
function parse_css_float(str) { // float or percentage. | |
if (str[str.length - 1] === '%') | |
return clamp_css_float(parseFloat(str) / 100); | |
return clamp_css_float(parseFloat(str)); | |
} | |
function css_hue_to_rgb(m1, m2, h) { | |
if (h < 0) h += 1; | |
else if (h > 1) h -= 1; | |
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; | |
if (h * 2 < 1) return m2; | |
if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6; | |
return m1; | |
} | |
function parseCSSColor(css_str) { | |
// Remove all whitespace, not compliant, but should just be more accepting. | |
var str = css_str.replace(/ /g, '').toLowerCase(); | |
// Color keywords (and transparent) lookup. | |
if (str in kCSSColorTable) return kCSSColorTable[str].slice(); // dup. | |
// #abc and #abc123 syntax. | |
if (str[0] === '#') { | |
if (str.length === 4) { | |
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. | |
if (!(iv >= 0 && iv <= 0xfff)) return null; // Covers NaN. | |
return [((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8), | |
(iv & 0xf0) | ((iv & 0xf0) >> 4), | |
(iv & 0xf) | ((iv & 0xf) << 4), | |
1]; | |
} else if (str.length === 7) { | |
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing. | |
if (!(iv >= 0 && iv <= 0xffffff)) return null; // Covers NaN. | |
return [(iv & 0xff0000) >> 16, | |
(iv & 0xff00) >> 8, | |
iv & 0xff, | |
1]; | |
} | |
return null; | |
} | |
var op = str.indexOf('('), ep = str.indexOf(')'); | |
if (op !== -1 && ep + 1 === str.length) { | |
var fname = str.substr(0, op); | |
var params = str.substr(op+1, ep-(op+1)).split(','); | |
var alpha = 1; // To allow case fallthrough. | |
switch (fname) { | |
case 'rgba': | |
if (params.length !== 4) return null; | |
alpha = parse_css_float(params.pop()); | |
// Fall through. | |
case 'rgb': | |
if (params.length !== 3) return null; | |
return [parse_css_int(params[0]), | |
parse_css_int(params[1]), | |
parse_css_int(params[2]), | |
alpha]; | |
case 'hsla': | |
if (params.length !== 4) return null; | |
alpha = parse_css_float(params.pop()); | |
// Fall through. | |
case 'hsl': | |
if (params.length !== 3) return null; | |
var h = (((parseFloat(params[0]) % 360) + 360) % 360) / 360; // 0 .. 1 | |
// NOTE(deanm): According to the CSS spec s/l should only be | |
// percentages, but we don't bother and let float or percentage. | |
var s = parse_css_float(params[1]); | |
var l = parse_css_float(params[2]); | |
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s; | |
var m1 = l * 2 - m2; | |
return [clamp_css_byte(css_hue_to_rgb(m1, m2, h+1/3) * 255), | |
clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255), | |
clamp_css_byte(css_hue_to_rgb(m1, m2, h-1/3) * 255), | |
alpha]; | |
default: | |
return null; | |
} | |
} | |
return null; | |
} | |
try { exports.parseCSSColor = parseCSSColor } catch(e) { } | |
},{}],123:[function(require,module,exports){ | |
'use strict'; | |
module.exports = earcut; | |
function earcut(data, holeIndices, dim) { | |
dim = dim || 2; | |
var hasHoles = holeIndices && holeIndices.length, | |
outerLen = hasHoles ? holeIndices[0] * dim : data.length, | |
outerNode = linkedList(data, 0, outerLen, dim, true), | |
triangles = []; | |
if (!outerNode) return triangles; | |
var minX, minY, maxX, maxY, x, y, size; | |
if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim); | |
// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox | |
if (data.length > 80 * dim) { | |
minX = maxX = data[0]; | |
minY = maxY = data[1]; | |
for (var i = dim; i < outerLen; i += dim) { | |
x = data[i]; | |
y = data[i + 1]; | |
if (x < minX) minX = x; | |
if (y < minY) minY = y; | |
if (x > maxX) maxX = x; | |
if (y > maxY) maxY = y; | |
} | |
// minX, minY and size are later used to transform coords into integers for z-order calculation | |
size = Math.max(maxX - minX, maxY - minY); | |
} | |
earcutLinked(outerNode, triangles, dim, minX, minY, size); | |
return triangles; | |
} | |
// create a circular doubly linked list from polygon points in the specified winding order | |
function linkedList(data, start, end, dim, clockwise) { | |
var i, last; | |
if (clockwise === (signedArea(data, start, end, dim) > 0)) { | |
for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last); | |
} else { | |
for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last); | |
} | |
if (last && equals(last, last.next)) { | |
removeNode(last); | |
last = last.next; | |
} | |
return last; | |
} | |
// eliminate colinear or duplicate points | |
function filterPoints(start, end) { | |
if (!start) return start; | |
if (!end) end = start; | |
var p = start, | |
again; | |
do { | |
again = false; | |
if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) { | |
removeNode(p); | |
p = end = p.prev; | |
if (p === p.next) return null; | |
again = true; | |
} else { | |
p = p.next; | |
} | |
} while (again || p !== end); | |
return end; | |
} | |
// main ear slicing loop which triangulates a polygon (given as a linked list) | |
function earcutLinked(ear, triangles, dim, minX, minY, size, pass) { | |
if (!ear) return; | |
// interlink polygon nodes in z-order | |
if (!pass && size) indexCurve(ear, minX, minY, size); | |
var stop = ear, | |
prev, next; | |
// iterate through ears, slicing them one by one | |
while (ear.prev !== ear.next) { | |
prev = ear.prev; | |
next = ear.next; | |
if (size ? isEarHashed(ear, minX, minY, size) : isEar(ear)) { | |
// cut off the triangle | |
triangles.push(prev.i / dim); | |
triangles.push(ear.i / dim); | |
triangles.push(next.i / dim); | |
removeNode(ear); | |
// skipping the next vertice leads to less sliver triangles | |
ear = next.next; | |
stop = next.next; | |
continue; | |
} | |
ear = next; | |
// if we looped through the whole remaining polygon and can't find any more ears | |
if (ear === stop) { | |
// try filtering points and slicing again | |
if (!pass) { | |
earcutLinked(filterPoints(ear), triangles, dim, minX, minY, size, 1); | |
// if this didn't work, try curing all small self-intersections locally | |
} else if (pass === 1) { | |
ear = cureLocalIntersections(ear, triangles, dim); | |
earcutLinked(ear, triangles, dim, minX, minY, size, 2); | |
// as a last resort, try splitting the remaining polygon into two | |
} else if (pass === 2) { | |
splitEarcut(ear, triangles, dim, minX, minY, size); | |
} | |
break; | |
} | |
} | |
} | |
// check whether a polygon node forms a valid ear with adjacent nodes | |
function isEar(ear) { | |
var a = ear.prev, | |
b = ear, | |
c = ear.next; | |
if (area(a, b, c) >= 0) return false; // reflex, can't be an ear | |
// now make sure we don't have other points inside the potential ear | |
var p = ear.next.next; | |
while (p !== ear.prev) { | |
if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && | |
area(p.prev, p, p.next) >= 0) return false; | |
p = p.next; | |
} | |
return true; | |
} | |
function isEarHashed(ear, minX, minY, size) { | |
var a = ear.prev, | |
b = ear, | |
c = ear.next; | |
if (area(a, b, c) >= 0) return false; // reflex, can't be an ear | |
// triangle bbox; min & max are calculated like this for speed | |
var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x), | |
minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y), | |
maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x), | |
maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y); | |
// z-order range for the current triangle bbox; | |
var minZ = zOrder(minTX, minTY, minX, minY, size), | |
maxZ = zOrder(maxTX, maxTY, minX, minY, size); | |
// first look for points inside the triangle in increasing z-order | |
var p = ear.nextZ; | |
while (p && p.z <= maxZ) { | |
if (p !== ear.prev && p !== ear.next && | |
pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && | |
area(p.prev, p, p.next) >= 0) return false; | |
p = p.nextZ; | |
} | |
// then look for points in decreasing z-order | |
p = ear.prevZ; | |
while (p && p.z >= minZ) { | |
if (p !== ear.prev && p !== ear.next && | |
pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) && | |
area(p.prev, p, p.next) >= 0) return false; | |
p = p.prevZ; | |
} | |
return true; | |
} | |
// go through all polygon nodes and cure small local self-intersections | |
function cureLocalIntersections(start, triangles, dim) { | |
var p = start; | |
do { | |
var a = p.prev, | |
b = p.next.next; | |
if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) { | |
triangles.push(a.i / dim); | |
triangles.push(p.i / dim); | |
triangles.push(b.i / dim); | |
// remove two nodes involved | |
removeNode(p); | |
removeNode(p.next); | |
p = start = b; | |
} | |
p = p.next; | |
} while (p !== start); | |
return p; | |
} | |
// try splitting polygon into two and triangulate them independently | |
function splitEarcut(start, triangles, dim, minX, minY, size) { | |
// look for a valid diagonal that divides the polygon into two | |
var a = start; | |
do { | |
var b = a.next.next; | |
while (b !== a.prev) { | |
if (a.i !== b.i && isValidDiagonal(a, b)) { | |
// split the polygon in two by the diagonal | |
var c = splitPolygon(a, b); | |
// filter colinear points around the cuts | |
a = filterPoints(a, a.next); | |
c = filterPoints(c, c.next); | |
// run earcut on each half | |
earcutLinked(a, triangles, dim, minX, minY, size); | |
earcutLinked(c, triangles, dim, minX, minY, size); | |
return; | |
} | |
b = b.next; | |
} | |
a = a.next; | |
} while (a !== start); | |
} | |
// link every hole into the outer loop, producing a single-ring polygon without holes | |
function eliminateHoles(data, holeIndices, outerNode, dim) { | |
var queue = [], | |
i, len, start, end, list; | |
for (i = 0, len = holeIndices.length; i < len; i++) { | |
start = holeIndices[i] * dim; | |
end = i < len - 1 ? holeIndices[i + 1] * dim : data.length; | |
list = linkedList(data, start, end, dim, false); | |
if (list === list.next) list.steiner = true; | |
queue.push(getLeftmost(list)); | |
} | |
queue.sort(compareX); | |
// process holes from left to right | |
for (i = 0; i < queue.length; i++) { | |
eliminateHole(queue[i], outerNode); | |
outerNode = filterPoints(outerNode, outerNode.next); | |
} | |
return outerNode; | |
} | |
function compareX(a, b) { | |
return a.x - b.x; | |
} | |
// find a bridge between vertices that connects hole with an outer ring and and link it | |
function eliminateHole(hole, outerNode) { | |
outerNode = findHoleBridge(hole, outerNode); | |
if (outerNode) { | |
var b = splitPolygon(outerNode, hole); | |
filterPoints(b, b.next); | |
} | |
} | |
// David Eberly's algorithm for finding a bridge between hole and outer polygon | |
function findHoleBridge(hole, outerNode) { | |
var p = outerNode, | |
hx = hole.x, | |
hy = hole.y, | |
qx = -Infinity, | |
m; | |
// find a segment intersected by a ray from the hole's leftmost point to the left; | |
// segment's endpoint with lesser x will be potential connection point | |
do { | |
if (hy <= p.y && hy >= p.next.y) { | |
var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y); | |
if (x <= hx && x > qx) { | |
qx = x; | |
if (x === hx) { | |
if (hy === p.y) return p; | |
if (hy === p.next.y) return p.next; | |
} | |
m = p.x < p.next.x ? p : p.next; | |
} | |
} | |
p = p.next; | |
} while (p !== outerNode); | |
if (!m) return null; | |
if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint | |
// look for points inside the triangle of hole point, segment intersection and endpoint; | |
// if there are no points found, we have a valid connection; | |
// otherwise choose the point of the minimum angle with the ray as connection point | |
var stop = m, | |
mx = m.x, | |
my = m.y, | |
tanMin = Infinity, | |
tan; | |
p = m.next; | |
while (p !== stop) { | |
if (hx >= p.x && p.x >= mx && | |
pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) { | |
tan = Math.abs(hy - p.y) / (hx - p.x); // tangential | |
if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) { | |
m = p; | |
tanMin = tan; | |
} | |
} | |
p = p.next; | |
} | |
return m; | |
} | |
// interlink polygon nodes in z-order | |
function indexCurve(start, minX, minY, size) { | |
var p = start; | |
do { | |
if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, size); | |
p.prevZ = p.prev; | |
p.nextZ = p.next; | |
p = p.next; | |
} while (p !== start); | |
p.prevZ.nextZ = null; | |
p.prevZ = null; | |
sortLinked(p); | |
} | |
// Simon Tatham's linked list merge sort algorithm | |
// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html | |
function sortLinked(list) { | |
var i, p, q, e, tail, numMerges, pSize, qSize, | |
inSize = 1; | |
do { | |
p = list; | |
list = null; | |
tail = null; | |
numMerges = 0; | |
while (p) { | |
numMerges++; | |
q = p; | |
pSize = 0; | |
for (i = 0; i < inSize; i++) { | |
pSize++; | |
q = q.nextZ; | |
if (!q) break; | |
} | |
qSize = inSize; | |
while (pSize > 0 || (qSize > 0 && q)) { | |
if (pSize === 0) { | |
e = q; | |
q = q.nextZ; | |
qSize--; | |
} else if (qSize === 0 || !q) { | |
e = p; | |
p = p.nextZ; | |
pSize--; | |
} else if (p.z <= q.z) { | |
e = p; | |
p = p.nextZ; | |
pSize--; | |
} else { | |
e = q; | |
q = q.nextZ; | |
qSize--; | |
} | |
if (tail) tail.nextZ = e; | |
else list = e; | |
e.prevZ = tail; | |
tail = e; | |
} | |
p = q; | |
} | |
tail.nextZ = null; | |
inSize *= 2; | |
} while (numMerges > 1); | |
return list; | |
} | |
// z-order of a point given coords and size of the data bounding box | |
function zOrder(x, y, minX, minY, size) { | |
// coords are transformed into non-negative 15-bit integer range | |
x = 32767 * (x - minX) / size; | |
y = 32767 * (y - minY) / size; | |
x = (x | (x << 8)) & 0x00FF00FF; | |
x = (x | (x << 4)) & 0x0F0F0F0F; | |
x = (x | (x << 2)) & 0x33333333; | |
x = (x | (x << 1)) & 0x55555555; | |
y = (y | (y << 8)) & 0x00FF00FF; | |
y = (y | (y << 4)) & 0x0F0F0F0F; | |
y = (y | (y << 2)) & 0x33333333; | |
y = (y | (y << 1)) & 0x55555555; | |
return x | (y << 1); | |
} | |
// find the leftmost node of a polygon ring | |
function getLeftmost(start) { | |
var p = start, | |
leftmost = start; | |
do { | |
if (p.x < leftmost.x) leftmost = p; | |
p = p.next; | |
} while (p !== start); | |
return leftmost; | |
} | |
// check if a point lies within a convex triangle | |
function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) { | |
return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 && | |
(ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 && | |
(bx - px) * (cy - py) - (cx - px) * (by - py) >= 0; | |
} | |
// check if a diagonal between two polygon nodes is valid (lies in polygon interior) | |
function isValidDiagonal(a, b) { | |
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && | |
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b); | |
} | |
// signed area of a triangle | |
function area(p, q, r) { | |
return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); | |
} | |
// check if two points are equal | |
function equals(p1, p2) { | |
return p1.x === p2.x && p1.y === p2.y; | |
} | |
// check if two segments intersect | |
function intersects(p1, q1, p2, q2) { | |
if ((equals(p1, q1) && equals(p2, q2)) || | |
(equals(p1, q2) && equals(p2, q1))) return true; | |
return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 && | |
area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0; | |
} | |
// check if a polygon diagonal intersects any polygon segments | |
function intersectsPolygon(a, b) { | |
var p = a; | |
do { | |
if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && | |
intersects(p, p.next, a, b)) return true; | |
p = p.next; | |
} while (p !== a); | |
return false; | |
} | |
// check if a polygon diagonal is locally inside the polygon | |
function locallyInside(a, b) { | |
return area(a.prev, a, a.next) < 0 ? | |
area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 : | |
area(a, b, a.prev) < 0 || area(a, a.next, b) < 0; | |
} | |
// check if the middle point of a polygon diagonal is inside the polygon | |
function middleInside(a, b) { | |
var p = a, | |
inside = false, | |
px = (a.x + b.x) / 2, | |
py = (a.y + b.y) / 2; | |
do { | |
if (((p.y > py) !== (p.next.y > py)) && (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x)) | |
inside = !inside; | |
p = p.next; | |
} while (p !== a); | |
return inside; | |
} | |
// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two; | |
// if one belongs to the outer ring and another to a hole, it merges it into a single ring | |
function splitPolygon(a, b) { | |
var a2 = new Node(a.i, a.x, a.y), | |
b2 = new Node(b.i, b.x, b.y), | |
an = a.next, | |
bp = b.prev; | |
a.next = b; | |
b.prev = a; | |
a2.next = an; | |
an.prev = a2; | |
b2.next = a2; | |
a2.prev = b2; | |
bp.next = b2; | |
b2.prev = bp; | |
return b2; | |
} | |
// create a node and optionally link it with previous one (in a circular doubly linked list) | |
function insertNode(i, x, y, last) { | |
var p = new Node(i, x, y); | |
if (!last) { | |
p.prev = p; | |
p.next = p; | |
} else { | |
p.next = last.next; | |
p.prev = last; | |
last.next.prev = p; | |
last.next = p; | |
} | |
return p; | |
} | |
function removeNode(p) { | |
p.next.prev = p.prev; | |
p.prev.next = p.next; | |
if (p.prevZ) p.prevZ.nextZ = p.nextZ; | |
if (p.nextZ) p.nextZ.prevZ = p.prevZ; | |
} | |
function Node(i, x, y) { | |
// vertice index in coordinates array | |
this.i = i; | |
// vertex coordinates | |
this.x = x; | |
this.y = y; | |
// previous and next vertice nodes in a polygon ring | |
this.prev = null; | |
this.next = null; | |
// z-order curve value | |
this.z = null; | |
// previous and next nodes in z-order | |
this.prevZ = null; | |
this.nextZ = null; | |
// indicates whether this is a steiner point | |
this.steiner = false; | |
} | |
// return a percentage difference between the polygon area and its triangulation area; | |
// used to verify correctness of triangulation | |
earcut.deviation = function (data, holeIndices, dim, triangles) { | |
var hasHoles = holeIndices && holeIndices.length; | |
var outerLen = hasHoles ? holeIndices[0] * dim : data.length; | |
var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim)); | |
if (hasHoles) { | |
for (var i = 0, len = holeIndices.length; i < len; i++) { | |
var start = holeIndices[i] * dim; | |
var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length; | |
polygonArea -= Math.abs(signedArea(data, start, end, dim)); | |
} | |
} | |
var trianglesArea = 0; | |
for (i = 0; i < triangles.length; i += 3) { | |
var a = triangles[i] * dim; | |
var b = triangles[i + 1] * dim; | |
var c = triangles[i + 2] * dim; | |
trianglesArea += Math.abs( | |
(data[a] - data[c]) * (data[b + 1] - data[a + 1]) - | |
(data[a] - data[b]) * (data[c + 1] - data[a + 1])); | |
} | |
return polygonArea === 0 && trianglesArea === 0 ? 0 : | |
Math.abs((trianglesArea - polygonArea) / polygonArea); | |
}; | |
function signedArea(data, start, end, dim) { | |
var sum = 0; | |
for (var i = start, j = end - dim; i < end; i += dim) { | |
sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]); | |
j = i; | |
} | |
return sum; | |
} | |
// turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts | |
earcut.flatten = function (data) { | |
var dim = data[0][0].length, | |
result = {vertices: [], holes: [], dimensions: dim}, | |
holeIndex = 0; | |
for (var i = 0; i < data.length; i++) { | |
for (var j = 0; j < data[i].length; j++) { | |
for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]); | |
} | |
if (i > 0) { | |
holeIndex += data[i - 1].length; | |
result.holes.push(holeIndex); | |
} | |
} | |
return result; | |
}; | |
},{}],124:[function(require,module,exports){ | |
'use strict'; | |
module.exports = createFilter; | |
var types = ['Unknown', 'Point', 'LineString', 'Polygon']; | |
/** | |
* Given a filter expressed as nested arrays, return a new function | |
* that evaluates whether a given feature (with a .properties or .tags property) | |
* passes its test. | |
* | |
* @param {Array} filter mapbox gl filter | |
* @returns {Function} filter-evaluating function | |
*/ | |
function createFilter(filter) { | |
return new Function('f', 'var p = (f && f.properties || {}); return ' + compile(filter)); | |
} | |
function compile(filter) { | |
if (!filter) return 'true'; | |
var op = filter[0]; | |
if (filter.length <= 1) return op === 'any' ? 'false' : 'true'; | |
var str = | |
op === '==' ? compileComparisonOp(filter[1], filter[2], '===', false) : | |
op === '!=' ? compileComparisonOp(filter[1], filter[2], '!==', false) : | |
op === '<' || | |
op === '>' || | |
op === '<=' || | |
op === '>=' ? compileComparisonOp(filter[1], filter[2], op, true) : | |
op === 'any' ? compileLogicalOp(filter.slice(1), '||') : | |
op === 'all' ? compileLogicalOp(filter.slice(1), '&&') : | |
op === 'none' ? compileNegation(compileLogicalOp(filter.slice(1), '||')) : | |
op === 'in' ? compileInOp(filter[1], filter.slice(2)) : | |
op === '!in' ? compileNegation(compileInOp(filter[1], filter.slice(2))) : | |
op === 'has' ? compileHasOp(filter[1]) : | |
op === '!has' ? compileNegation(compileHasOp([filter[1]])) : | |
'true'; | |
return '(' + str + ')'; | |
} | |
function compilePropertyReference(property) { | |
return property === '$type' ? 'f.type' : | |
property === '$id' ? 'f.id' : | |
'p[' + JSON.stringify(property) + ']'; | |
} | |
function compileComparisonOp(property, value, op, checkType) { | |
var left = compilePropertyReference(property); | |
var right = property === '$type' ? types.indexOf(value) : JSON.stringify(value); | |
return (checkType ? 'typeof ' + left + '=== typeof ' + right + '&&' : '') + left + op + right; | |
} | |
function compileLogicalOp(expressions, op) { | |
return expressions.map(compile).join(op); | |
} | |
function compileInOp(property, values) { | |
if (property === '$type') values = values.map(function(value) { return types.indexOf(value); }); | |
var left = JSON.stringify(values.sort(compare)); | |
var right = compilePropertyReference(property); | |
if (values.length <= 200) return left + '.indexOf(' + right + ') !== -1'; | |
return 'function(v, a, i, j) {' + | |
'while (i <= j) { var m = (i + j) >> 1;' + | |
' if (a[m] === v) return true; if (a[m] > v) j = m - 1; else i = m + 1;' + | |
'}' + | |
'return false; }(' + right + ', ' + left + ',0,' + (values.length - 1) + ')'; | |
} | |
function compileHasOp(property) { | |
return JSON.stringify(property) + ' in p'; | |
} | |
function compileNegation(expression) { | |
return '!(' + expression + ')'; | |
} | |
// Comparison function to sort numbers and strings | |
function compare(a, b) { | |
return a < b ? -1 : a > b ? 1 : 0; | |
} | |
},{}],125:[function(require,module,exports){ | |
var geojsonArea = require('geojson-area'); | |
module.exports = rewind; | |
function rewind(gj, outer) { | |
switch ((gj && gj.type) || null) { | |
case 'FeatureCollection': | |
gj.features = gj.features.map(curryOuter(rewind, outer)); | |
return gj; | |
case 'Feature': | |
gj.geometry = rewind(gj.geometry, outer); | |
return gj; | |
case 'Polygon': | |
case 'MultiPolygon': | |
return correct(gj, outer); | |
default: | |
return gj; | |
} | |
} | |
function curryOuter(a, b) { | |
return function(_) { return a(_, b); }; | |
} | |
function correct(_, outer) { | |
if (_.type === 'Polygon') { | |
_.coordinates = correctRings(_.coordinates, outer); | |
} else if (_.type === 'MultiPolygon') { | |
_.coordinates = _.coordinates.map(curryOuter(correctRings, outer)); | |
} | |
return _; | |
} | |
function correctRings(_, outer) { | |
outer = !!outer; | |
_[0] = wind(_[0], !outer); | |
for (var i = 1; i < _.length; i++) { | |
_[i] = wind(_[i], outer); | |
} | |
return _; | |
} | |
function wind(_, dir) { | |
return cw(_) === dir ? _ : _.reverse(); | |
} | |
function cw(_) { | |
return geojsonArea.ring(_) >= 0; | |
} | |
},{"geojson-area":126}],126:[function(require,module,exports){ | |
var wgs84 = require('wgs84'); | |
module.exports.geometry = geometry; | |
module.exports.ring = ringArea; | |
function geometry(_) { | |
if (_.type === 'Polygon') return polygonArea(_.coordinates); | |
else if (_.type === 'MultiPolygon') { | |
var area = 0; | |
for (var i = 0; i < _.coordinates.length; i++) { | |
area += polygonArea(_.coordinates[i]); | |
} | |
return area; | |
} else { | |
return null; | |
} | |
} | |
function polygonArea(coords) { | |
var area = 0; | |
if (coords && coords.length > 0) { | |
area += Math.abs(ringArea(coords[0])); | |
for (var i = 1; i < coords.length; i++) { | |
area -= Math.abs(ringArea(coords[i])); | |
} | |
} | |
return area; | |
} | |
/** | |
* Calculate the approximate area of the polygon were it projected onto | |
* the earth. Note that this area will be positive if ring is oriented | |
* clockwise, otherwise it will be negative. | |
* | |
* Reference: | |
* Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for | |
* Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion | |
* Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409 | |
* | |
* Returns: | |
* {float} The approximate signed geodesic area of the polygon in square | |
* meters. | |
*/ | |
function ringArea(coords) { | |
var area = 0; | |
if (coords.length > 2) { | |
var p1, p2; | |
for (var i = 0; i < coords.length - 1; i++) { | |
p1 = coords[i]; | |
p2 = coords[i + 1]; | |
area += rad(p2[0] - p1[0]) * (2 + Math.sin(rad(p1[1])) + Math.sin(rad(p2[1]))); | |
} | |
area = area * wgs84.RADIUS * wgs84.RADIUS / 2; | |
} | |
return area; | |
} | |
function rad(_) { | |
return _ * Math.PI / 180; | |
} | |
},{"wgs84":127}],127:[function(require,module,exports){ | |
module.exports.RADIUS = 6378137; | |
module.exports.FLATTENING = 1/298.257223563; | |
module.exports.POLAR_RADIUS = 6356752.3142; | |
},{}],128:[function(require,module,exports){ | |
'use strict'; | |
module.exports = clip; | |
/* clip features between two axis-parallel lines: | |
* | | | |
* ___|___ | / | |
* / | \____|____/ | |
* | | | |
*/ | |
function clip(features, scale, k1, k2, axis, intersect, minAll, maxAll) { | |
k1 /= scale; | |
k2 /= scale; | |
if (minAll >= k1 && maxAll <= k2) return features; // trivial accept | |
else if (minAll > k2 || maxAll < k1) return null; // trivial reject | |
var clipped = []; | |
for (var i = 0; i < features.length; i++) { | |
var feature = features[i], | |
geometry = feature.geometry, | |
type = feature.type, | |
min, max; | |
min = feature.min[axis]; | |
max = feature.max[axis]; | |
if (min >= k1 && max <= k2) { // trivial accept | |
clipped.push(feature); | |
continue; | |
} else if (min > k2 || max < k1) continue; // trivial reject | |
var slices = type === 1 ? | |
clipPoints(geometry, k1, k2, axis) : | |
clipGeometry(geometry, k1, k2, axis, intersect, type === 3); | |
if (slices.length) { | |
// if a feature got clipped, it will likely get clipped on the next zoom level as well, | |
// so there's no need to recalculate bboxes | |
clipped.push({ | |
geometry: slices, | |
type: type, | |
tags: features[i].tags || null, | |
min: feature.min, | |
max: feature.max | |
}); | |
} | |
} | |
return clipped.length ? clipped : null; | |
} | |
function clipPoints(geometry, k1, k2, axis) { | |
var slice = []; | |
for (var i = 0; i < geometry.length; i++) { | |
var a = geometry[i], | |
ak = a[axis]; | |
if (ak >= k1 && ak <= k2) slice.push(a); | |
} | |
return slice; | |
} | |
function clipGeometry(geometry, k1, k2, axis, intersect, closed) { | |
var slices = []; | |
for (var i = 0; i < geometry.length; i++) { | |
var ak = 0, | |
bk = 0, | |
b = null, | |
points = geometry[i], | |
area = points.area, | |
dist = points.dist, | |
outer = points.outer, | |
len = points.length, | |
a, j, last; | |
var slice = []; | |
for (j = 0; j < len - 1; j++) { | |
a = b || points[j]; | |
b = points[j + 1]; | |
ak = bk || a[axis]; | |
bk = b[axis]; | |
if (ak < k1) { | |
if ((bk > k2)) { // ---|-----|--> | |
slice.push(intersect(a, b, k1), intersect(a, b, k2)); | |
if (!closed) slice = newSlice(slices, slice, area, dist, outer); | |
} else if (bk >= k1) slice.push(intersect(a, b, k1)); // ---|--> | | |
} else if (ak > k2) { | |
if ((bk < k1)) { // <--|-----|--- | |
slice.push(intersect(a, b, k2), intersect(a, b, k1)); | |
if (!closed) slice = newSlice(slices, slice, area, dist, outer); | |
} else if (bk <= k2) slice.push(intersect(a, b, k2)); // | <--|--- | |
} else { | |
slice.push(a); | |
if (bk < k1) { // <--|--- | | |
slice.push(intersect(a, b, k1)); | |
if (!closed) slice = newSlice(slices, slice, area, dist, outer); | |
} else if (bk > k2) { // | ---|--> | |
slice.push(intersect(a, b, k2)); | |
if (!closed) slice = newSlice(slices, slice, area, dist, outer); | |
} | |
// | --> | | |
} | |
} | |
// add the last point | |
a = points[len - 1]; | |
ak = a[axis]; | |
if (ak >= k1 && ak <= k2) slice.push(a); | |
// close the polygon if its endpoints are not the same after clipping | |
last = slice[slice.length - 1]; | |
if (closed && last && (slice[0][0] !== last[0] || slice[0][1] !== last[1])) slice.push(slice[0]); | |
// add the final slice | |
newSlice(slices, slice, area, dist, outer); | |
} | |
return slices; | |
} | |
function newSlice(slices, slice, area, dist, outer) { | |
if (slice.length) { | |
// we don't recalculate the area/length of the unclipped geometry because the case where it goes | |
// below the visibility threshold as a result of clipping is rare, so we avoid doing unnecessary work | |
slice.area = area; | |
slice.dist = dist; | |
if (outer !== undefined) slice.outer = outer; | |
slices.push(slice); | |
} | |
return []; | |
} | |
},{}],129:[function(require,module,exports){ | |
'use strict'; | |
module.exports = convert; | |
var simplify = require('./simplify'); | |
// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data | |
function convert(data, tolerance) { | |
var features = []; | |
if (data.type === 'FeatureCollection') { | |
for (var i = 0; i < data.features.length; i++) { | |
convertFeature(features, data.features[i], tolerance); | |
} | |
} else if (data.type === 'Feature') { | |
convertFeature(features, data, tolerance); | |
} else { | |
// single geometry or a geometry collection | |
convertFeature(features, {geometry: data}, tolerance); | |
} | |
return features; | |
} | |
function convertFeature(features, feature, tolerance) { | |
if (feature.geometry === null) { | |
// ignore features with null geometry | |
return; | |
} | |
var geom = feature.geometry, | |
type = geom.type, | |
coords = geom.coordinates, | |
tags = feature.properties, | |
i, j, rings, projectedRing; | |
if (type === 'Point') { | |
features.push(create(tags, 1, [projectPoint(coords)])); | |
} else if (type === 'MultiPoint') { | |
features.push(create(tags, 1, project(coords))); | |
} else if (type === 'LineString') { | |
features.push(create(tags, 2, [project(coords, tolerance)])); | |
} else if (type === 'MultiLineString' || type === 'Polygon') { | |
rings = []; | |
for (i = 0; i < coords.length; i++) { | |
projectedRing = project(coords[i], tolerance); | |
if (type === 'Polygon') projectedRing.outer = (i === 0); | |
rings.push(projectedRing); | |
} | |
features.push(create(tags, type === 'Polygon' ? 3 : 2, rings)); | |
} else if (type === 'MultiPolygon') { | |
rings = []; | |
for (i = 0; i < coords.length; i++) { | |
for (j = 0; j < coords[i].length; j++) { | |
projectedRing = project(coords[i][j], tolerance); | |
projectedRing.outer = (j === 0); | |
rings.push(projectedRing); | |
} | |
} | |
features.push(create(tags, 3, rings)); | |
} else if (type === 'GeometryCollection') { | |
for (i = 0; i < geom.geometries.length; i++) { | |
convertFeature(features, { | |
geometry: geom.geometries[i], | |
properties: tags | |
}, tolerance); | |
} | |
} else { | |
throw new Error('Input data is not a valid GeoJSON object.'); | |
} | |
} | |
function create(tags, type, geometry) { | |
var feature = { | |
geometry: geometry, | |
type: type, | |
tags: tags || null, | |
min: [2, 1], // initial bbox values; | |
max: [-1, 0] // note that coords are usually in [0..1] range | |
}; | |
calcBBox(feature); | |
return feature; | |
} | |
function project(lonlats, tolerance) { | |
var projected = []; | |
for (var i = 0; i < lonlats.length; i++) { | |
projected.push(projectPoint(lonlats[i])); | |
} | |
if (tolerance) { | |
simplify(projected, tolerance); | |
calcSize(projected); | |
} | |
return projected; | |
} | |
function projectPoint(p) { | |
var sin = Math.sin(p[1] * Math.PI / 180), | |
x = (p[0] / 360 + 0.5), | |
y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI); | |
y = y < 0 ? 0 : | |
y > 1 ? 1 : y; | |
return [x, y, 0]; | |
} | |
// calculate area and length of the poly | |
function calcSize(points) { | |
var area = 0, | |
dist = 0; | |
for (var i = 0, a, b; i < points.length - 1; i++) { | |
a = b || points[i]; | |
b = points[i + 1]; | |
area += a[0] * b[1] - b[0] * a[1]; | |
// use Manhattan distance instead of Euclidian one to avoid expensive square root computation | |
dist += Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1]); | |
} | |
points.area = Math.abs(area / 2); | |
points.dist = dist; | |
} | |
// calculate the feature bounding box for faster clipping later | |
function calcBBox(feature) { | |
var geometry = feature.geometry, | |
min = feature.min, | |
max = feature.max; | |
if (feature.type === 1) calcRingBBox(min, max, geometry); | |
else for (var i = 0; i < geometry.length; i++) calcRingBBox(min, max, geometry[i]); | |
return feature; | |
} | |
function calcRingBBox(min, max, points) { | |
for (var i = 0, p; i < points.length; i++) { | |
p = points[i]; | |
min[0] = Math.min(p[0], min[0]); | |
max[0] = Math.max(p[0], max[0]); | |
min[1] = Math.min(p[1], min[1]); | |
max[1] = Math.max(p[1], max[1]); | |
} | |
} | |
},{"./simplify":131}],130:[function(require,module,exports){ | |
'use strict'; | |
module.exports = geojsonvt; | |
var convert = require('./convert'), // GeoJSON conversion and preprocessing | |
transform = require('./transform'), // coordinate transformation | |
clip = require('./clip'), // stripe clipping algorithm | |
wrap = require('./wrap'), // date line processing | |
createTile = require('./tile'); // final simplified tile generation | |
function geojsonvt(data, options) { | |
return new GeoJSONVT(data, options); | |
} | |
function GeoJSONVT(data, options) { | |
options = this.options = extend(Object.create(this.options), options); | |
var debug = options.debug; | |
if (debug) console.time('preprocess data'); | |
var z2 = 1 << options.maxZoom, // 2^z | |
features = convert(data, options.tolerance / (z2 * options.extent)); | |
this.tiles = {}; | |
this.tileCoords = []; | |
if (debug) { | |
console.timeEnd('preprocess data'); | |
console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints); | |
console.time('generate tiles'); | |
this.stats = {}; | |
this.total = 0; | |
} | |
features = wrap(features, options.buffer / options.extent, intersectX); | |
// start slicing from the top tile down | |
if (features.length) this.splitTile(features, 0, 0, 0); | |
if (debug) { | |
if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints); | |
console.timeEnd('generate tiles'); | |
console.log('tiles generated:', this.total, JSON.stringify(this.stats)); | |
} | |
} | |
GeoJSONVT.prototype.options = { | |
maxZoom: 14, // max zoom to preserve detail on | |
indexMaxZoom: 5, // max zoom in the tile index | |
indexMaxPoints: 100000, // max number of points per tile in the tile index | |
solidChildren: false, // whether to tile solid square tiles further | |
tolerance: 3, // simplification tolerance (higher means simpler) | |
extent: 4096, // tile extent | |
buffer: 64, // tile buffer on each side | |
debug: 0 // logging level (0, 1 or 2) | |
}; | |
GeoJSONVT.prototype.splitTile = function (features, z, x, y, cz, cx, cy) { | |
var stack = [features, z, x, y], | |
options = this.options, | |
debug = options.debug, | |
solid = null; | |
// avoid recursion by using a processing queue | |
while (stack.length) { | |
y = stack.pop(); | |
x = stack.pop(); | |
z = stack.pop(); | |
features = stack.pop(); | |
var z2 = 1 << z, | |
id = toID(z, x, y), | |
tile = this.tiles[id], | |
tileTolerance = z === options.maxZoom ? 0 : options.tolerance / (z2 * options.extent); | |
if (!tile) { | |
if (debug > 1) console.time('creation'); | |
tile = this.tiles[id] = createTile(features, z2, x, y, tileTolerance, z === options.maxZoom); | |
this.tileCoords.push({z: z, x: x, y: y}); | |
if (debug) { | |
if (debug > 1) { | |
console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)', | |
z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified); | |
console.timeEnd('creation'); | |
} | |
var key = 'z' + z; | |
this.stats[key] = (this.stats[key] || 0) + 1; | |
this.total++; | |
} | |
} | |
// save reference to original geometry in tile so that we can drill down later if we stop now | |
tile.source = features; | |
// if it's the first-pass tiling | |
if (!cz) { | |
// stop tiling if we reached max zoom, or if the tile is too simple | |
if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue; | |
// if a drilldown to a specific tile | |
} else { | |
// stop tiling if we reached base zoom or our target tile zoom | |
if (z === options.maxZoom || z === cz) continue; | |
// stop tiling if it's not an ancestor of the target tile | |
var m = 1 << (cz - z); | |
if (x !== Math.floor(cx / m) || y !== Math.floor(cy / m)) continue; | |
} | |
// stop tiling if the tile is solid clipped square | |
if (!options.solidChildren && isClippedSquare(tile, options.extent, options.buffer)) { | |
if (cz) solid = z; // and remember the zoom if we're drilling down | |
continue; | |
} | |
// if we slice further down, no need to keep source geometry | |
tile.source = null; | |
if (debug > 1) console.time('clipping'); | |
// values we'll use for clipping | |
var k1 = 0.5 * options.buffer / options.extent, | |
k2 = 0.5 - k1, | |
k3 = 0.5 + k1, | |
k4 = 1 + k1, | |
tl, bl, tr, br, left, right; | |
tl = bl = tr = br = null; | |
left = clip(features, z2, x - k1, x + k3, 0, intersectX, tile.min[0], tile.max[0]); | |
right = clip(features, z2, x + k2, x + k4, 0, intersectX, tile.min[0], tile.max[0]); | |
if (left) { | |
tl = clip(left, z2, y - k1, y + k3, 1, intersectY, tile.min[1], tile.max[1]); | |
bl = clip(left, z2, y + k2, y + k4, 1, intersectY, tile.min[1], tile.max[1]); | |
} | |
if (right) { | |
tr = clip(right, z2, y - k1, y + k3, 1, intersectY, tile.min[1], tile.max[1]); | |
br = clip(right, z2, y + k2, y + k4, 1, intersectY, tile.min[1], tile.max[1]); | |
} | |
if (debug > 1) console.timeEnd('clipping'); | |
if (tl) stack.push(tl, z + 1, x * 2, y * 2); | |
if (bl) stack.push(bl, z + 1, x * 2, y * 2 + 1); | |
if (tr) stack.push(tr, z + 1, x * 2 + 1, y * 2); | |
if (br) stack.push(br, z + 1, x * 2 + 1, y * 2 + 1); | |
} | |
return solid; | |
}; | |
GeoJSONVT.prototype.getTile = function (z, x, y) { | |
var options = this.options, | |
extent = options.extent, | |
debug = options.debug; | |
var z2 = 1 << z; | |
x = ((x % z2) + z2) % z2; // wrap tile x coordinate | |
var id = toID(z, x, y); | |
if (this.tiles[id]) return transform.tile(this.tiles[id], extent); | |
if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y); | |
var z0 = z, | |
x0 = x, | |
y0 = y, | |
parent; | |
while (!parent && z0 > 0) { | |
z0--; | |
x0 = Math.floor(x0 / 2); | |
y0 = Math.floor(y0 / 2); | |
parent = this.tiles[toID(z0, x0, y0)]; | |
} | |
if (!parent || !parent.source) return null; | |
// if we found a parent tile containing the original geometry, we can drill down from it | |
if (debug > 1) console.log('found parent tile z%d-%d-%d', z0, x0, y0); | |
// it parent tile is a solid clipped square, return it instead since it's identical | |
if (isClippedSquare(parent, extent, options.buffer)) return transform.tile(parent, extent); | |
if (debug > 1) console.time('drilling down'); | |
var solid = this.splitTile(parent.source, z0, x0, y0, z, x, y); | |
if (debug > 1) console.timeEnd('drilling down'); | |
// one of the parent tiles was a solid clipped square | |
if (solid !== null) { | |
var m = 1 << (z - solid); | |
id = toID(solid, Math.floor(x / m), Math.floor(y / m)); | |
} | |
return this.tiles[id] ? transform.tile(this.tiles[id], extent) : null; | |
}; | |
function toID(z, x, y) { | |
return (((1 << z) * y + x) * 32) + z; | |
} | |
function intersectX(a, b, x) { | |
return [x, (x - a[0]) * (b[1] - a[1]) / (b[0] - a[0]) + a[1], 1]; | |
} | |
function intersectY(a, b, y) { | |
return [(y - a[1]) * (b[0] - a[0]) / (b[1] - a[1]) + a[0], y, 1]; | |
} | |
function extend(dest, src) { | |
for (var i in src) dest[i] = src[i]; | |
return dest; | |
} | |
// checks whether a tile is a whole-area fill after clipping; if it is, there's no sense slicing it further | |
function isClippedSquare(tile, extent, buffer) { | |
var features = tile.source; | |
if (features.length !== 1) return false; | |
var feature = features[0]; | |
if (feature.type !== 3 || feature.geometry.length > 1) return false; | |
var len = feature.geometry[0].length; | |
if (len !== 5) return false; | |
for (var i = 0; i < len; i++) { | |
var p = transform.point(feature.geometry[0][i], extent, tile.z2, tile.x, tile.y); | |
if ((p[0] !== -buffer && p[0] !== extent + buffer) || | |
(p[1] !== -buffer && p[1] !== extent + buffer)) return false; | |
} | |
return true; | |
} | |
},{"./clip":128,"./convert":129,"./tile":132,"./transform":133,"./wrap":134}],131:[function(require,module,exports){ | |
'use strict'; | |
module.exports = simplify; | |
// calculate simplification data using optimized Douglas-Peucker algorithm | |
function simplify(points, tolerance) { | |
var sqTolerance = tolerance * tolerance, | |
len = points.length, | |
first = 0, | |
last = len - 1, | |
stack = [], | |
i, maxSqDist, sqDist, index; | |
// always retain the endpoints (1 is the max value) | |
points[first][2] = 1; | |
points[last][2] = 1; | |
// avoid recursion by using a stack | |
while (last) { | |
maxSqDist = 0; | |
for (i = first + 1; i < last; i++) { | |
sqDist = getSqSegDist(points[i], points[first], points[last]); | |
if (sqDist > maxSqDist) { | |
index = i; | |
maxSqDist = sqDist; | |
} | |
} | |
if (maxSqDist > sqTolerance) { | |
points[index][2] = maxSqDist; // save the point importance in squared pixels as a z coordinate | |
stack.push(first); | |
stack.push(index); | |
first = index; | |
} else { | |
last = stack.pop(); | |
first = stack.pop(); | |
} | |
} | |
} | |
// square distance from a point to a segment | |
function getSqSegDist(p, a, b) { | |
var x = a[0], y = a[1], | |
bx = b[0], by = b[1], | |
px = p[0], py = p[1], | |
dx = bx - x, | |
dy = by - y; | |
if (dx !== 0 || dy !== 0) { | |
var t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy); | |
if (t > 1) { | |
x = bx; | |
y = by; | |
} else if (t > 0) { | |
x += dx * t; | |
y += dy * t; | |
} | |
} | |
dx = px - x; | |
dy = py - y; | |
return dx * dx + dy * dy; | |
} | |
},{}],132:[function(require,module,exports){ | |
'use strict'; | |
module.exports = createTile; | |
function createTile(features, z2, tx, ty, tolerance, noSimplify) { | |
var tile = { | |
features: [], | |
numPoints: 0, | |
numSimplified: 0, | |
numFeatures: 0, | |
source: null, | |
x: tx, | |
y: ty, | |
z2: z2, | |
transformed: false, | |
min: [2, 1], | |
max: [-1, 0] | |
}; | |
for (var i = 0; i < features.length; i++) { | |
tile.numFeatures++; | |
addFeature(tile, features[i], tolerance, noSimplify); | |
var min = features[i].min, | |
max = features[i].max; | |
if (min[0] < tile.min[0]) tile.min[0] = min[0]; | |
if (min[1] < tile.min[1]) tile.min[1] = min[1]; | |
if (max[0] > tile.max[0]) tile.max[0] = max[0]; | |
if (max[1] > tile.max[1]) tile.max[1] = max[1]; | |
} | |
return tile; | |
} | |
function addFeature(tile, feature, tolerance, noSimplify) { | |
var geom = feature.geometry, | |
type = feature.type, | |
simplified = [], | |
sqTolerance = tolerance * tolerance, | |
i, j, ring, p; | |
if (type === 1) { | |
for (i = 0; i < geom.length; i++) { | |
simplified.push(geom[i]); | |
tile.numPoints++; | |
tile.numSimplified++; | |
} | |
} else { | |
// simplify and transform projected coordinates for tile geometry | |
for (i = 0; i < geom.length; i++) { | |
ring = geom[i]; | |
// filter out tiny polylines & polygons | |
if (!noSimplify && ((type === 2 && ring.dist < tolerance) || | |
(type === 3 && ring.area < sqTolerance))) { | |
tile.numPoints += ring.length; | |
continue; | |
} | |
var simplifiedRing = []; | |
for (j = 0; j < ring.length; j++) { | |
p = ring[j]; | |
// keep points with importance > tolerance | |
if (noSimplify || p[2] > sqTolerance) { | |
simplifiedRing.push(p); | |
tile.numSimplified++; | |
} | |
tile.numPoints++; | |
} | |
if (type === 3) rewind(simplifiedRing, ring.outer); | |
simplified.push(simplifiedRing); | |
} | |
} | |
if (simplified.length) { | |
tile.features.push({ | |
geometry: simplified, | |
type: type, | |
tags: feature.tags || null | |
}); | |
} | |
} | |
function rewind(ring, clockwise) { | |
var area = signedArea(ring); | |
if (area < 0 === clockwise) ring.reverse(); | |
} | |
function signedArea(ring) { | |
var sum = 0; | |
for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) { | |
p1 = ring[i]; | |
p2 = ring[j]; | |
sum += (p2[0] - p1[0]) * (p1[1] + p2[1]); | |
} | |
return sum; | |
} | |
},{}],133:[function(require,module,exports){ | |
'use strict'; | |
exports.tile = transformTile; | |
exports.point = transformPoint; | |
// Transforms the coordinates of each feature in the given tile from | |
// mercator-projected space into (extent x extent) tile space. | |
function transformTile(tile, extent) { | |
if (tile.transformed) return tile; | |
var z2 = tile.z2, | |
tx = tile.x, | |
ty = tile.y, | |
i, j, k; | |
for (i = 0; i < tile.features.length; i++) { | |
var feature = tile.features[i], | |
geom = feature.geometry, | |
type = feature.type; | |
if (type === 1) { | |
for (j = 0; j < geom.length; j++) geom[j] = transformPoint(geom[j], extent, z2, tx, ty); | |
} else { | |
for (j = 0; j < geom.length; j++) { | |
var ring = geom[j]; | |
for (k = 0; k < ring.length; k++) ring[k] = transformPoint(ring[k], extent, z2, tx, ty); | |
} | |
} | |
} | |
tile.transformed = true; | |
return tile; | |
} | |
function transformPoint(p, extent, z2, tx, ty) { | |
var x = Math.round(extent * (p[0] * z2 - tx)), | |
y = Math.round(extent * (p[1] * z2 - ty)); | |
return [x, y]; | |
} | |
},{}],134:[function(require,module,exports){ | |
'use strict'; | |
var clip = require('./clip'); | |
module.exports = wrap; | |
function wrap(features, buffer, intersectX) { | |
var merged = features, | |
left = clip(features, 1, -1 - buffer, buffer, 0, intersectX, -1, 2), // left world copy | |
right = clip(features, 1, 1 - buffer, 2 + buffer, 0, intersectX, -1, 2); // right world copy | |
if (left || right) { | |
merged = clip(features, 1, -buffer, 1 + buffer, 0, intersectX, -1, 2); // center world copy | |
if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center | |
if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center | |
} | |
return merged; | |
} | |
function shiftFeatureCoords(features, offset) { | |
var newFeatures = []; | |
for (var i = 0; i < features.length; i++) { | |
var feature = features[i], | |
type = feature.type; | |
var newGeometry; | |
if (type === 1) { | |
newGeometry = shiftCoords(feature.geometry, offset); | |
} else { | |
newGeometry = []; | |
for (var j = 0; j < feature.geometry.length; j++) { | |
newGeometry.push(shiftCoords(feature.geometry[j], offset)); | |
} | |
} | |
newFeatures.push({ | |
geometry: newGeometry, | |
type: type, | |
tags: feature.tags, | |
min: [feature.min[0] + offset, feature.min[1]], | |
max: [feature.max[0] + offset, feature.max[1]] | |
}); | |
} | |
return newFeatures; | |
} | |
function shiftCoords(points, offset) { | |
var newPoints = []; | |
newPoints.area = points.area; | |
newPoints.dist = points.dist; | |
for (var i = 0; i < points.length; i++) { | |
newPoints.push([points[i][0] + offset, points[i][1], points[i][2]]); | |
} | |
return newPoints; | |
} | |
},{"./clip":128}],135:[function(require,module,exports){ | |
/** | |
* @fileoverview gl-matrix - High performance matrix and vector operations | |
* @author Brandon Jones | |
* @author Colin MacKenzie IV | |
* @version 2.3.2 | |
*/ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
// END HEADER | |
exports.glMatrix = require("./gl-matrix/common.js"); | |
exports.mat2 = require("./gl-matrix/mat2.js"); | |
exports.mat2d = require("./gl-matrix/mat2d.js"); | |
exports.mat3 = require("./gl-matrix/mat3.js"); | |
exports.mat4 = require("./gl-matrix/mat4.js"); | |
exports.quat = require("./gl-matrix/quat.js"); | |
exports.vec2 = require("./gl-matrix/vec2.js"); | |
exports.vec3 = require("./gl-matrix/vec3.js"); | |
exports.vec4 = require("./gl-matrix/vec4.js"); | |
},{"./gl-matrix/common.js":136,"./gl-matrix/mat2.js":137,"./gl-matrix/mat2d.js":138,"./gl-matrix/mat3.js":139,"./gl-matrix/mat4.js":140,"./gl-matrix/quat.js":141,"./gl-matrix/vec2.js":142,"./gl-matrix/vec3.js":143,"./gl-matrix/vec4.js":144}],136:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
/** | |
* @class Common utilities | |
* @name glMatrix | |
*/ | |
var glMatrix = {}; | |
// Configuration Constants | |
glMatrix.EPSILON = 0.000001; | |
glMatrix.ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array; | |
glMatrix.RANDOM = Math.random; | |
glMatrix.ENABLE_SIMD = false; | |
// Capability detection | |
glMatrix.SIMD_AVAILABLE = (glMatrix.ARRAY_TYPE === Float32Array) && ('SIMD' in this); | |
glMatrix.USE_SIMD = glMatrix.ENABLE_SIMD && glMatrix.SIMD_AVAILABLE; | |
/** | |
* Sets the type of array used when creating new vectors and matrices | |
* | |
* @param {Type} type Array type, such as Float32Array or Array | |
*/ | |
glMatrix.setMatrixArrayType = function(type) { | |
glMatrix.ARRAY_TYPE = type; | |
} | |
var degree = Math.PI / 180; | |
/** | |
* Convert Degree To Radian | |
* | |
* @param {Number} Angle in Degrees | |
*/ | |
glMatrix.toRadian = function(a){ | |
return a * degree; | |
} | |
/** | |
* Tests whether or not the arguments have approximately the same value, within an absolute | |
* or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less | |
* than or equal to 1.0, and a relative tolerance is used for larger values) | |
* | |
* @param {Number} a The first number to test. | |
* @param {Number} b The second number to test. | |
* @returns {Boolean} True if the numbers are approximately equal, false otherwise. | |
*/ | |
glMatrix.equals = function(a, b) { | |
return Math.abs(a - b) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a), Math.abs(b)); | |
} | |
module.exports = glMatrix; | |
},{}],137:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
/** | |
* @class 2x2 Matrix | |
* @name mat2 | |
*/ | |
var mat2 = {}; | |
/** | |
* Creates a new identity mat2 | |
* | |
* @returns {mat2} a new 2x2 matrix | |
*/ | |
mat2.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(4); | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
return out; | |
}; | |
/** | |
* Creates a new mat2 initialized with values from an existing matrix | |
* | |
* @param {mat2} a matrix to clone | |
* @returns {mat2} a new 2x2 matrix | |
*/ | |
mat2.clone = function(a) { | |
var out = new glMatrix.ARRAY_TYPE(4); | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
return out; | |
}; | |
/** | |
* Copy the values from one mat2 to another | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the source matrix | |
* @returns {mat2} out | |
*/ | |
mat2.copy = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
return out; | |
}; | |
/** | |
* Set a mat2 to the identity matrix | |
* | |
* @param {mat2} out the receiving matrix | |
* @returns {mat2} out | |
*/ | |
mat2.identity = function(out) { | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
return out; | |
}; | |
/** | |
* Create a new mat2 with the given values | |
* | |
* @param {Number} m00 Component in column 0, row 0 position (index 0) | |
* @param {Number} m01 Component in column 0, row 1 position (index 1) | |
* @param {Number} m10 Component in column 1, row 0 position (index 2) | |
* @param {Number} m11 Component in column 1, row 1 position (index 3) | |
* @returns {mat2} out A new 2x2 matrix | |
*/ | |
mat2.fromValues = function(m00, m01, m10, m11) { | |
var out = new glMatrix.ARRAY_TYPE(4); | |
out[0] = m00; | |
out[1] = m01; | |
out[2] = m10; | |
out[3] = m11; | |
return out; | |
}; | |
/** | |
* Set the components of a mat2 to the given values | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {Number} m00 Component in column 0, row 0 position (index 0) | |
* @param {Number} m01 Component in column 0, row 1 position (index 1) | |
* @param {Number} m10 Component in column 1, row 0 position (index 2) | |
* @param {Number} m11 Component in column 1, row 1 position (index 3) | |
* @returns {mat2} out | |
*/ | |
mat2.set = function(out, m00, m01, m10, m11) { | |
out[0] = m00; | |
out[1] = m01; | |
out[2] = m10; | |
out[3] = m11; | |
return out; | |
}; | |
/** | |
* Transpose the values of a mat2 | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the source matrix | |
* @returns {mat2} out | |
*/ | |
mat2.transpose = function(out, a) { | |
// If we are transposing ourselves we can skip a few steps but have to cache some values | |
if (out === a) { | |
var a1 = a[1]; | |
out[1] = a[2]; | |
out[2] = a1; | |
} else { | |
out[0] = a[0]; | |
out[1] = a[2]; | |
out[2] = a[1]; | |
out[3] = a[3]; | |
} | |
return out; | |
}; | |
/** | |
* Inverts a mat2 | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the source matrix | |
* @returns {mat2} out | |
*/ | |
mat2.invert = function(out, a) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], | |
// Calculate the determinant | |
det = a0 * a3 - a2 * a1; | |
if (!det) { | |
return null; | |
} | |
det = 1.0 / det; | |
out[0] = a3 * det; | |
out[1] = -a1 * det; | |
out[2] = -a2 * det; | |
out[3] = a0 * det; | |
return out; | |
}; | |
/** | |
* Calculates the adjugate of a mat2 | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the source matrix | |
* @returns {mat2} out | |
*/ | |
mat2.adjoint = function(out, a) { | |
// Caching this value is nessecary if out == a | |
var a0 = a[0]; | |
out[0] = a[3]; | |
out[1] = -a[1]; | |
out[2] = -a[2]; | |
out[3] = a0; | |
return out; | |
}; | |
/** | |
* Calculates the determinant of a mat2 | |
* | |
* @param {mat2} a the source matrix | |
* @returns {Number} determinant of a | |
*/ | |
mat2.determinant = function (a) { | |
return a[0] * a[3] - a[2] * a[1]; | |
}; | |
/** | |
* Multiplies two mat2's | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the first operand | |
* @param {mat2} b the second operand | |
* @returns {mat2} out | |
*/ | |
mat2.multiply = function (out, a, b) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; | |
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; | |
out[0] = a0 * b0 + a2 * b1; | |
out[1] = a1 * b0 + a3 * b1; | |
out[2] = a0 * b2 + a2 * b3; | |
out[3] = a1 * b2 + a3 * b3; | |
return out; | |
}; | |
/** | |
* Alias for {@link mat2.multiply} | |
* @function | |
*/ | |
mat2.mul = mat2.multiply; | |
/** | |
* Rotates a mat2 by the given angle | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat2} out | |
*/ | |
mat2.rotate = function (out, a, rad) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], | |
s = Math.sin(rad), | |
c = Math.cos(rad); | |
out[0] = a0 * c + a2 * s; | |
out[1] = a1 * c + a3 * s; | |
out[2] = a0 * -s + a2 * c; | |
out[3] = a1 * -s + a3 * c; | |
return out; | |
}; | |
/** | |
* Scales the mat2 by the dimensions in the given vec2 | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the matrix to rotate | |
* @param {vec2} v the vec2 to scale the matrix by | |
* @returns {mat2} out | |
**/ | |
mat2.scale = function(out, a, v) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], | |
v0 = v[0], v1 = v[1]; | |
out[0] = a0 * v0; | |
out[1] = a1 * v0; | |
out[2] = a2 * v1; | |
out[3] = a3 * v1; | |
return out; | |
}; | |
/** | |
* Creates a matrix from a given angle | |
* This is equivalent to (but much faster than): | |
* | |
* mat2.identity(dest); | |
* mat2.rotate(dest, dest, rad); | |
* | |
* @param {mat2} out mat2 receiving operation result | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat2} out | |
*/ | |
mat2.fromRotation = function(out, rad) { | |
var s = Math.sin(rad), | |
c = Math.cos(rad); | |
out[0] = c; | |
out[1] = s; | |
out[2] = -s; | |
out[3] = c; | |
return out; | |
} | |
/** | |
* Creates a matrix from a vector scaling | |
* This is equivalent to (but much faster than): | |
* | |
* mat2.identity(dest); | |
* mat2.scale(dest, dest, vec); | |
* | |
* @param {mat2} out mat2 receiving operation result | |
* @param {vec2} v Scaling vector | |
* @returns {mat2} out | |
*/ | |
mat2.fromScaling = function(out, v) { | |
out[0] = v[0]; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = v[1]; | |
return out; | |
} | |
/** | |
* Returns a string representation of a mat2 | |
* | |
* @param {mat2} mat matrix to represent as a string | |
* @returns {String} string representation of the matrix | |
*/ | |
mat2.str = function (a) { | |
return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; | |
}; | |
/** | |
* Returns Frobenius norm of a mat2 | |
* | |
* @param {mat2} a the matrix to calculate Frobenius norm of | |
* @returns {Number} Frobenius norm | |
*/ | |
mat2.frob = function (a) { | |
return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2))) | |
}; | |
/** | |
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix | |
* @param {mat2} L the lower triangular matrix | |
* @param {mat2} D the diagonal matrix | |
* @param {mat2} U the upper triangular matrix | |
* @param {mat2} a the input matrix to factorize | |
*/ | |
mat2.LDU = function (L, D, U, a) { | |
L[2] = a[2]/a[0]; | |
U[0] = a[0]; | |
U[1] = a[1]; | |
U[3] = a[3] - L[2] * U[1]; | |
return [L, D, U]; | |
}; | |
/** | |
* Adds two mat2's | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the first operand | |
* @param {mat2} b the second operand | |
* @returns {mat2} out | |
*/ | |
mat2.add = function(out, a, b) { | |
out[0] = a[0] + b[0]; | |
out[1] = a[1] + b[1]; | |
out[2] = a[2] + b[2]; | |
out[3] = a[3] + b[3]; | |
return out; | |
}; | |
/** | |
* Subtracts matrix b from matrix a | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the first operand | |
* @param {mat2} b the second operand | |
* @returns {mat2} out | |
*/ | |
mat2.subtract = function(out, a, b) { | |
out[0] = a[0] - b[0]; | |
out[1] = a[1] - b[1]; | |
out[2] = a[2] - b[2]; | |
out[3] = a[3] - b[3]; | |
return out; | |
}; | |
/** | |
* Alias for {@link mat2.subtract} | |
* @function | |
*/ | |
mat2.sub = mat2.subtract; | |
/** | |
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | |
* | |
* @param {mat2} a The first matrix. | |
* @param {mat2} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat2.exactEquals = function (a, b) { | |
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; | |
}; | |
/** | |
* Returns whether or not the matrices have approximately the same elements in the same position. | |
* | |
* @param {mat2} a The first matrix. | |
* @param {mat2} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat2.equals = function (a, b) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; | |
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; | |
return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | |
Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | |
Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | |
Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3))); | |
}; | |
/** | |
* Multiply each element of the matrix by a scalar. | |
* | |
* @param {mat2} out the receiving matrix | |
* @param {mat2} a the matrix to scale | |
* @param {Number} b amount to scale the matrix's elements by | |
* @returns {mat2} out | |
*/ | |
mat2.multiplyScalar = function(out, a, b) { | |
out[0] = a[0] * b; | |
out[1] = a[1] * b; | |
out[2] = a[2] * b; | |
out[3] = a[3] * b; | |
return out; | |
}; | |
/** | |
* Adds two mat2's after multiplying each element of the second operand by a scalar value. | |
* | |
* @param {mat2} out the receiving vector | |
* @param {mat2} a the first operand | |
* @param {mat2} b the second operand | |
* @param {Number} scale the amount to scale b's elements by before adding | |
* @returns {mat2} out | |
*/ | |
mat2.multiplyScalarAndAdd = function(out, a, b, scale) { | |
out[0] = a[0] + (b[0] * scale); | |
out[1] = a[1] + (b[1] * scale); | |
out[2] = a[2] + (b[2] * scale); | |
out[3] = a[3] + (b[3] * scale); | |
return out; | |
}; | |
module.exports = mat2; | |
},{"./common.js":136}],138:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
/** | |
* @class 2x3 Matrix | |
* @name mat2d | |
* | |
* @description | |
* A mat2d contains six elements defined as: | |
* <pre> | |
* [a, c, tx, | |
* b, d, ty] | |
* </pre> | |
* This is a short form for the 3x3 matrix: | |
* <pre> | |
* [a, c, tx, | |
* b, d, ty, | |
* 0, 0, 1] | |
* </pre> | |
* The last row is ignored so the array is shorter and operations are faster. | |
*/ | |
var mat2d = {}; | |
/** | |
* Creates a new identity mat2d | |
* | |
* @returns {mat2d} a new 2x3 matrix | |
*/ | |
mat2d.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(6); | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
out[4] = 0; | |
out[5] = 0; | |
return out; | |
}; | |
/** | |
* Creates a new mat2d initialized with values from an existing matrix | |
* | |
* @param {mat2d} a matrix to clone | |
* @returns {mat2d} a new 2x3 matrix | |
*/ | |
mat2d.clone = function(a) { | |
var out = new glMatrix.ARRAY_TYPE(6); | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[4] = a[4]; | |
out[5] = a[5]; | |
return out; | |
}; | |
/** | |
* Copy the values from one mat2d to another | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the source matrix | |
* @returns {mat2d} out | |
*/ | |
mat2d.copy = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[4] = a[4]; | |
out[5] = a[5]; | |
return out; | |
}; | |
/** | |
* Set a mat2d to the identity matrix | |
* | |
* @param {mat2d} out the receiving matrix | |
* @returns {mat2d} out | |
*/ | |
mat2d.identity = function(out) { | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
out[4] = 0; | |
out[5] = 0; | |
return out; | |
}; | |
/** | |
* Create a new mat2d with the given values | |
* | |
* @param {Number} a Component A (index 0) | |
* @param {Number} b Component B (index 1) | |
* @param {Number} c Component C (index 2) | |
* @param {Number} d Component D (index 3) | |
* @param {Number} tx Component TX (index 4) | |
* @param {Number} ty Component TY (index 5) | |
* @returns {mat2d} A new mat2d | |
*/ | |
mat2d.fromValues = function(a, b, c, d, tx, ty) { | |
var out = new glMatrix.ARRAY_TYPE(6); | |
out[0] = a; | |
out[1] = b; | |
out[2] = c; | |
out[3] = d; | |
out[4] = tx; | |
out[5] = ty; | |
return out; | |
}; | |
/** | |
* Set the components of a mat2d to the given values | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {Number} a Component A (index 0) | |
* @param {Number} b Component B (index 1) | |
* @param {Number} c Component C (index 2) | |
* @param {Number} d Component D (index 3) | |
* @param {Number} tx Component TX (index 4) | |
* @param {Number} ty Component TY (index 5) | |
* @returns {mat2d} out | |
*/ | |
mat2d.set = function(out, a, b, c, d, tx, ty) { | |
out[0] = a; | |
out[1] = b; | |
out[2] = c; | |
out[3] = d; | |
out[4] = tx; | |
out[5] = ty; | |
return out; | |
}; | |
/** | |
* Inverts a mat2d | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the source matrix | |
* @returns {mat2d} out | |
*/ | |
mat2d.invert = function(out, a) { | |
var aa = a[0], ab = a[1], ac = a[2], ad = a[3], | |
atx = a[4], aty = a[5]; | |
var det = aa * ad - ab * ac; | |
if(!det){ | |
return null; | |
} | |
det = 1.0 / det; | |
out[0] = ad * det; | |
out[1] = -ab * det; | |
out[2] = -ac * det; | |
out[3] = aa * det; | |
out[4] = (ac * aty - ad * atx) * det; | |
out[5] = (ab * atx - aa * aty) * det; | |
return out; | |
}; | |
/** | |
* Calculates the determinant of a mat2d | |
* | |
* @param {mat2d} a the source matrix | |
* @returns {Number} determinant of a | |
*/ | |
mat2d.determinant = function (a) { | |
return a[0] * a[3] - a[1] * a[2]; | |
}; | |
/** | |
* Multiplies two mat2d's | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the first operand | |
* @param {mat2d} b the second operand | |
* @returns {mat2d} out | |
*/ | |
mat2d.multiply = function (out, a, b) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], | |
b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; | |
out[0] = a0 * b0 + a2 * b1; | |
out[1] = a1 * b0 + a3 * b1; | |
out[2] = a0 * b2 + a2 * b3; | |
out[3] = a1 * b2 + a3 * b3; | |
out[4] = a0 * b4 + a2 * b5 + a4; | |
out[5] = a1 * b4 + a3 * b5 + a5; | |
return out; | |
}; | |
/** | |
* Alias for {@link mat2d.multiply} | |
* @function | |
*/ | |
mat2d.mul = mat2d.multiply; | |
/** | |
* Rotates a mat2d by the given angle | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat2d} out | |
*/ | |
mat2d.rotate = function (out, a, rad) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], | |
s = Math.sin(rad), | |
c = Math.cos(rad); | |
out[0] = a0 * c + a2 * s; | |
out[1] = a1 * c + a3 * s; | |
out[2] = a0 * -s + a2 * c; | |
out[3] = a1 * -s + a3 * c; | |
out[4] = a4; | |
out[5] = a5; | |
return out; | |
}; | |
/** | |
* Scales the mat2d by the dimensions in the given vec2 | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the matrix to translate | |
* @param {vec2} v the vec2 to scale the matrix by | |
* @returns {mat2d} out | |
**/ | |
mat2d.scale = function(out, a, v) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], | |
v0 = v[0], v1 = v[1]; | |
out[0] = a0 * v0; | |
out[1] = a1 * v0; | |
out[2] = a2 * v1; | |
out[3] = a3 * v1; | |
out[4] = a4; | |
out[5] = a5; | |
return out; | |
}; | |
/** | |
* Translates the mat2d by the dimensions in the given vec2 | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the matrix to translate | |
* @param {vec2} v the vec2 to translate the matrix by | |
* @returns {mat2d} out | |
**/ | |
mat2d.translate = function(out, a, v) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], | |
v0 = v[0], v1 = v[1]; | |
out[0] = a0; | |
out[1] = a1; | |
out[2] = a2; | |
out[3] = a3; | |
out[4] = a0 * v0 + a2 * v1 + a4; | |
out[5] = a1 * v0 + a3 * v1 + a5; | |
return out; | |
}; | |
/** | |
* Creates a matrix from a given angle | |
* This is equivalent to (but much faster than): | |
* | |
* mat2d.identity(dest); | |
* mat2d.rotate(dest, dest, rad); | |
* | |
* @param {mat2d} out mat2d receiving operation result | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat2d} out | |
*/ | |
mat2d.fromRotation = function(out, rad) { | |
var s = Math.sin(rad), c = Math.cos(rad); | |
out[0] = c; | |
out[1] = s; | |
out[2] = -s; | |
out[3] = c; | |
out[4] = 0; | |
out[5] = 0; | |
return out; | |
} | |
/** | |
* Creates a matrix from a vector scaling | |
* This is equivalent to (but much faster than): | |
* | |
* mat2d.identity(dest); | |
* mat2d.scale(dest, dest, vec); | |
* | |
* @param {mat2d} out mat2d receiving operation result | |
* @param {vec2} v Scaling vector | |
* @returns {mat2d} out | |
*/ | |
mat2d.fromScaling = function(out, v) { | |
out[0] = v[0]; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = v[1]; | |
out[4] = 0; | |
out[5] = 0; | |
return out; | |
} | |
/** | |
* Creates a matrix from a vector translation | |
* This is equivalent to (but much faster than): | |
* | |
* mat2d.identity(dest); | |
* mat2d.translate(dest, dest, vec); | |
* | |
* @param {mat2d} out mat2d receiving operation result | |
* @param {vec2} v Translation vector | |
* @returns {mat2d} out | |
*/ | |
mat2d.fromTranslation = function(out, v) { | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
out[4] = v[0]; | |
out[5] = v[1]; | |
return out; | |
} | |
/** | |
* Returns a string representation of a mat2d | |
* | |
* @param {mat2d} a matrix to represent as a string | |
* @returns {String} string representation of the matrix | |
*/ | |
mat2d.str = function (a) { | |
return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + | |
a[3] + ', ' + a[4] + ', ' + a[5] + ')'; | |
}; | |
/** | |
* Returns Frobenius norm of a mat2d | |
* | |
* @param {mat2d} a the matrix to calculate Frobenius norm of | |
* @returns {Number} Frobenius norm | |
*/ | |
mat2d.frob = function (a) { | |
return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + 1)) | |
}; | |
/** | |
* Adds two mat2d's | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the first operand | |
* @param {mat2d} b the second operand | |
* @returns {mat2d} out | |
*/ | |
mat2d.add = function(out, a, b) { | |
out[0] = a[0] + b[0]; | |
out[1] = a[1] + b[1]; | |
out[2] = a[2] + b[2]; | |
out[3] = a[3] + b[3]; | |
out[4] = a[4] + b[4]; | |
out[5] = a[5] + b[5]; | |
return out; | |
}; | |
/** | |
* Subtracts matrix b from matrix a | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the first operand | |
* @param {mat2d} b the second operand | |
* @returns {mat2d} out | |
*/ | |
mat2d.subtract = function(out, a, b) { | |
out[0] = a[0] - b[0]; | |
out[1] = a[1] - b[1]; | |
out[2] = a[2] - b[2]; | |
out[3] = a[3] - b[3]; | |
out[4] = a[4] - b[4]; | |
out[5] = a[5] - b[5]; | |
return out; | |
}; | |
/** | |
* Alias for {@link mat2d.subtract} | |
* @function | |
*/ | |
mat2d.sub = mat2d.subtract; | |
/** | |
* Multiply each element of the matrix by a scalar. | |
* | |
* @param {mat2d} out the receiving matrix | |
* @param {mat2d} a the matrix to scale | |
* @param {Number} b amount to scale the matrix's elements by | |
* @returns {mat2d} out | |
*/ | |
mat2d.multiplyScalar = function(out, a, b) { | |
out[0] = a[0] * b; | |
out[1] = a[1] * b; | |
out[2] = a[2] * b; | |
out[3] = a[3] * b; | |
out[4] = a[4] * b; | |
out[5] = a[5] * b; | |
return out; | |
}; | |
/** | |
* Adds two mat2d's after multiplying each element of the second operand by a scalar value. | |
* | |
* @param {mat2d} out the receiving vector | |
* @param {mat2d} a the first operand | |
* @param {mat2d} b the second operand | |
* @param {Number} scale the amount to scale b's elements by before adding | |
* @returns {mat2d} out | |
*/ | |
mat2d.multiplyScalarAndAdd = function(out, a, b, scale) { | |
out[0] = a[0] + (b[0] * scale); | |
out[1] = a[1] + (b[1] * scale); | |
out[2] = a[2] + (b[2] * scale); | |
out[3] = a[3] + (b[3] * scale); | |
out[4] = a[4] + (b[4] * scale); | |
out[5] = a[5] + (b[5] * scale); | |
return out; | |
}; | |
/** | |
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | |
* | |
* @param {mat2d} a The first matrix. | |
* @param {mat2d} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat2d.exactEquals = function (a, b) { | |
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5]; | |
}; | |
/** | |
* Returns whether or not the matrices have approximately the same elements in the same position. | |
* | |
* @param {mat2d} a The first matrix. | |
* @param {mat2d} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat2d.equals = function (a, b) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; | |
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; | |
return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | |
Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | |
Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | |
Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && | |
Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && | |
Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5))); | |
}; | |
module.exports = mat2d; | |
},{"./common.js":136}],139:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
/** | |
* @class 3x3 Matrix | |
* @name mat3 | |
*/ | |
var mat3 = {}; | |
/** | |
* Creates a new identity mat3 | |
* | |
* @returns {mat3} a new 3x3 matrix | |
*/ | |
mat3.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(9); | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 1; | |
out[5] = 0; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 1; | |
return out; | |
}; | |
/** | |
* Copies the upper-left 3x3 values into the given mat3. | |
* | |
* @param {mat3} out the receiving 3x3 matrix | |
* @param {mat4} a the source 4x4 matrix | |
* @returns {mat3} out | |
*/ | |
mat3.fromMat4 = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[4]; | |
out[4] = a[5]; | |
out[5] = a[6]; | |
out[6] = a[8]; | |
out[7] = a[9]; | |
out[8] = a[10]; | |
return out; | |
}; | |
/** | |
* Creates a new mat3 initialized with values from an existing matrix | |
* | |
* @param {mat3} a matrix to clone | |
* @returns {mat3} a new 3x3 matrix | |
*/ | |
mat3.clone = function(a) { | |
var out = new glMatrix.ARRAY_TYPE(9); | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[4] = a[4]; | |
out[5] = a[5]; | |
out[6] = a[6]; | |
out[7] = a[7]; | |
out[8] = a[8]; | |
return out; | |
}; | |
/** | |
* Copy the values from one mat3 to another | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the source matrix | |
* @returns {mat3} out | |
*/ | |
mat3.copy = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[4] = a[4]; | |
out[5] = a[5]; | |
out[6] = a[6]; | |
out[7] = a[7]; | |
out[8] = a[8]; | |
return out; | |
}; | |
/** | |
* Create a new mat3 with the given values | |
* | |
* @param {Number} m00 Component in column 0, row 0 position (index 0) | |
* @param {Number} m01 Component in column 0, row 1 position (index 1) | |
* @param {Number} m02 Component in column 0, row 2 position (index 2) | |
* @param {Number} m10 Component in column 1, row 0 position (index 3) | |
* @param {Number} m11 Component in column 1, row 1 position (index 4) | |
* @param {Number} m12 Component in column 1, row 2 position (index 5) | |
* @param {Number} m20 Component in column 2, row 0 position (index 6) | |
* @param {Number} m21 Component in column 2, row 1 position (index 7) | |
* @param {Number} m22 Component in column 2, row 2 position (index 8) | |
* @returns {mat3} A new mat3 | |
*/ | |
mat3.fromValues = function(m00, m01, m02, m10, m11, m12, m20, m21, m22) { | |
var out = new glMatrix.ARRAY_TYPE(9); | |
out[0] = m00; | |
out[1] = m01; | |
out[2] = m02; | |
out[3] = m10; | |
out[4] = m11; | |
out[5] = m12; | |
out[6] = m20; | |
out[7] = m21; | |
out[8] = m22; | |
return out; | |
}; | |
/** | |
* Set the components of a mat3 to the given values | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {Number} m00 Component in column 0, row 0 position (index 0) | |
* @param {Number} m01 Component in column 0, row 1 position (index 1) | |
* @param {Number} m02 Component in column 0, row 2 position (index 2) | |
* @param {Number} m10 Component in column 1, row 0 position (index 3) | |
* @param {Number} m11 Component in column 1, row 1 position (index 4) | |
* @param {Number} m12 Component in column 1, row 2 position (index 5) | |
* @param {Number} m20 Component in column 2, row 0 position (index 6) | |
* @param {Number} m21 Component in column 2, row 1 position (index 7) | |
* @param {Number} m22 Component in column 2, row 2 position (index 8) | |
* @returns {mat3} out | |
*/ | |
mat3.set = function(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) { | |
out[0] = m00; | |
out[1] = m01; | |
out[2] = m02; | |
out[3] = m10; | |
out[4] = m11; | |
out[5] = m12; | |
out[6] = m20; | |
out[7] = m21; | |
out[8] = m22; | |
return out; | |
}; | |
/** | |
* Set a mat3 to the identity matrix | |
* | |
* @param {mat3} out the receiving matrix | |
* @returns {mat3} out | |
*/ | |
mat3.identity = function(out) { | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 1; | |
out[5] = 0; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 1; | |
return out; | |
}; | |
/** | |
* Transpose the values of a mat3 | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the source matrix | |
* @returns {mat3} out | |
*/ | |
mat3.transpose = function(out, a) { | |
// If we are transposing ourselves we can skip a few steps but have to cache some values | |
if (out === a) { | |
var a01 = a[1], a02 = a[2], a12 = a[5]; | |
out[1] = a[3]; | |
out[2] = a[6]; | |
out[3] = a01; | |
out[5] = a[7]; | |
out[6] = a02; | |
out[7] = a12; | |
} else { | |
out[0] = a[0]; | |
out[1] = a[3]; | |
out[2] = a[6]; | |
out[3] = a[1]; | |
out[4] = a[4]; | |
out[5] = a[7]; | |
out[6] = a[2]; | |
out[7] = a[5]; | |
out[8] = a[8]; | |
} | |
return out; | |
}; | |
/** | |
* Inverts a mat3 | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the source matrix | |
* @returns {mat3} out | |
*/ | |
mat3.invert = function(out, a) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], | |
a10 = a[3], a11 = a[4], a12 = a[5], | |
a20 = a[6], a21 = a[7], a22 = a[8], | |
b01 = a22 * a11 - a12 * a21, | |
b11 = -a22 * a10 + a12 * a20, | |
b21 = a21 * a10 - a11 * a20, | |
// Calculate the determinant | |
det = a00 * b01 + a01 * b11 + a02 * b21; | |
if (!det) { | |
return null; | |
} | |
det = 1.0 / det; | |
out[0] = b01 * det; | |
out[1] = (-a22 * a01 + a02 * a21) * det; | |
out[2] = (a12 * a01 - a02 * a11) * det; | |
out[3] = b11 * det; | |
out[4] = (a22 * a00 - a02 * a20) * det; | |
out[5] = (-a12 * a00 + a02 * a10) * det; | |
out[6] = b21 * det; | |
out[7] = (-a21 * a00 + a01 * a20) * det; | |
out[8] = (a11 * a00 - a01 * a10) * det; | |
return out; | |
}; | |
/** | |
* Calculates the adjugate of a mat3 | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the source matrix | |
* @returns {mat3} out | |
*/ | |
mat3.adjoint = function(out, a) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], | |
a10 = a[3], a11 = a[4], a12 = a[5], | |
a20 = a[6], a21 = a[7], a22 = a[8]; | |
out[0] = (a11 * a22 - a12 * a21); | |
out[1] = (a02 * a21 - a01 * a22); | |
out[2] = (a01 * a12 - a02 * a11); | |
out[3] = (a12 * a20 - a10 * a22); | |
out[4] = (a00 * a22 - a02 * a20); | |
out[5] = (a02 * a10 - a00 * a12); | |
out[6] = (a10 * a21 - a11 * a20); | |
out[7] = (a01 * a20 - a00 * a21); | |
out[8] = (a00 * a11 - a01 * a10); | |
return out; | |
}; | |
/** | |
* Calculates the determinant of a mat3 | |
* | |
* @param {mat3} a the source matrix | |
* @returns {Number} determinant of a | |
*/ | |
mat3.determinant = function (a) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], | |
a10 = a[3], a11 = a[4], a12 = a[5], | |
a20 = a[6], a21 = a[7], a22 = a[8]; | |
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20); | |
}; | |
/** | |
* Multiplies two mat3's | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the first operand | |
* @param {mat3} b the second operand | |
* @returns {mat3} out | |
*/ | |
mat3.multiply = function (out, a, b) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], | |
a10 = a[3], a11 = a[4], a12 = a[5], | |
a20 = a[6], a21 = a[7], a22 = a[8], | |
b00 = b[0], b01 = b[1], b02 = b[2], | |
b10 = b[3], b11 = b[4], b12 = b[5], | |
b20 = b[6], b21 = b[7], b22 = b[8]; | |
out[0] = b00 * a00 + b01 * a10 + b02 * a20; | |
out[1] = b00 * a01 + b01 * a11 + b02 * a21; | |
out[2] = b00 * a02 + b01 * a12 + b02 * a22; | |
out[3] = b10 * a00 + b11 * a10 + b12 * a20; | |
out[4] = b10 * a01 + b11 * a11 + b12 * a21; | |
out[5] = b10 * a02 + b11 * a12 + b12 * a22; | |
out[6] = b20 * a00 + b21 * a10 + b22 * a20; | |
out[7] = b20 * a01 + b21 * a11 + b22 * a21; | |
out[8] = b20 * a02 + b21 * a12 + b22 * a22; | |
return out; | |
}; | |
/** | |
* Alias for {@link mat3.multiply} | |
* @function | |
*/ | |
mat3.mul = mat3.multiply; | |
/** | |
* Translate a mat3 by the given vector | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the matrix to translate | |
* @param {vec2} v vector to translate by | |
* @returns {mat3} out | |
*/ | |
mat3.translate = function(out, a, v) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], | |
a10 = a[3], a11 = a[4], a12 = a[5], | |
a20 = a[6], a21 = a[7], a22 = a[8], | |
x = v[0], y = v[1]; | |
out[0] = a00; | |
out[1] = a01; | |
out[2] = a02; | |
out[3] = a10; | |
out[4] = a11; | |
out[5] = a12; | |
out[6] = x * a00 + y * a10 + a20; | |
out[7] = x * a01 + y * a11 + a21; | |
out[8] = x * a02 + y * a12 + a22; | |
return out; | |
}; | |
/** | |
* Rotates a mat3 by the given angle | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat3} out | |
*/ | |
mat3.rotate = function (out, a, rad) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], | |
a10 = a[3], a11 = a[4], a12 = a[5], | |
a20 = a[6], a21 = a[7], a22 = a[8], | |
s = Math.sin(rad), | |
c = Math.cos(rad); | |
out[0] = c * a00 + s * a10; | |
out[1] = c * a01 + s * a11; | |
out[2] = c * a02 + s * a12; | |
out[3] = c * a10 - s * a00; | |
out[4] = c * a11 - s * a01; | |
out[5] = c * a12 - s * a02; | |
out[6] = a20; | |
out[7] = a21; | |
out[8] = a22; | |
return out; | |
}; | |
/** | |
* Scales the mat3 by the dimensions in the given vec2 | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the matrix to rotate | |
* @param {vec2} v the vec2 to scale the matrix by | |
* @returns {mat3} out | |
**/ | |
mat3.scale = function(out, a, v) { | |
var x = v[0], y = v[1]; | |
out[0] = x * a[0]; | |
out[1] = x * a[1]; | |
out[2] = x * a[2]; | |
out[3] = y * a[3]; | |
out[4] = y * a[4]; | |
out[5] = y * a[5]; | |
out[6] = a[6]; | |
out[7] = a[7]; | |
out[8] = a[8]; | |
return out; | |
}; | |
/** | |
* Creates a matrix from a vector translation | |
* This is equivalent to (but much faster than): | |
* | |
* mat3.identity(dest); | |
* mat3.translate(dest, dest, vec); | |
* | |
* @param {mat3} out mat3 receiving operation result | |
* @param {vec2} v Translation vector | |
* @returns {mat3} out | |
*/ | |
mat3.fromTranslation = function(out, v) { | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 1; | |
out[5] = 0; | |
out[6] = v[0]; | |
out[7] = v[1]; | |
out[8] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from a given angle | |
* This is equivalent to (but much faster than): | |
* | |
* mat3.identity(dest); | |
* mat3.rotate(dest, dest, rad); | |
* | |
* @param {mat3} out mat3 receiving operation result | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat3} out | |
*/ | |
mat3.fromRotation = function(out, rad) { | |
var s = Math.sin(rad), c = Math.cos(rad); | |
out[0] = c; | |
out[1] = s; | |
out[2] = 0; | |
out[3] = -s; | |
out[4] = c; | |
out[5] = 0; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from a vector scaling | |
* This is equivalent to (but much faster than): | |
* | |
* mat3.identity(dest); | |
* mat3.scale(dest, dest, vec); | |
* | |
* @param {mat3} out mat3 receiving operation result | |
* @param {vec2} v Scaling vector | |
* @returns {mat3} out | |
*/ | |
mat3.fromScaling = function(out, v) { | |
out[0] = v[0]; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = v[1]; | |
out[5] = 0; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 1; | |
return out; | |
} | |
/** | |
* Copies the values from a mat2d into a mat3 | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat2d} a the matrix to copy | |
* @returns {mat3} out | |
**/ | |
mat3.fromMat2d = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = 0; | |
out[3] = a[2]; | |
out[4] = a[3]; | |
out[5] = 0; | |
out[6] = a[4]; | |
out[7] = a[5]; | |
out[8] = 1; | |
return out; | |
}; | |
/** | |
* Calculates a 3x3 matrix from the given quaternion | |
* | |
* @param {mat3} out mat3 receiving operation result | |
* @param {quat} q Quaternion to create matrix from | |
* | |
* @returns {mat3} out | |
*/ | |
mat3.fromQuat = function (out, q) { | |
var x = q[0], y = q[1], z = q[2], w = q[3], | |
x2 = x + x, | |
y2 = y + y, | |
z2 = z + z, | |
xx = x * x2, | |
yx = y * x2, | |
yy = y * y2, | |
zx = z * x2, | |
zy = z * y2, | |
zz = z * z2, | |
wx = w * x2, | |
wy = w * y2, | |
wz = w * z2; | |
out[0] = 1 - yy - zz; | |
out[3] = yx - wz; | |
out[6] = zx + wy; | |
out[1] = yx + wz; | |
out[4] = 1 - xx - zz; | |
out[7] = zy - wx; | |
out[2] = zx - wy; | |
out[5] = zy + wx; | |
out[8] = 1 - xx - yy; | |
return out; | |
}; | |
/** | |
* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix | |
* | |
* @param {mat3} out mat3 receiving operation result | |
* @param {mat4} a Mat4 to derive the normal matrix from | |
* | |
* @returns {mat3} out | |
*/ | |
mat3.normalFromMat4 = function (out, a) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], | |
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], | |
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], | |
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], | |
b00 = a00 * a11 - a01 * a10, | |
b01 = a00 * a12 - a02 * a10, | |
b02 = a00 * a13 - a03 * a10, | |
b03 = a01 * a12 - a02 * a11, | |
b04 = a01 * a13 - a03 * a11, | |
b05 = a02 * a13 - a03 * a12, | |
b06 = a20 * a31 - a21 * a30, | |
b07 = a20 * a32 - a22 * a30, | |
b08 = a20 * a33 - a23 * a30, | |
b09 = a21 * a32 - a22 * a31, | |
b10 = a21 * a33 - a23 * a31, | |
b11 = a22 * a33 - a23 * a32, | |
// Calculate the determinant | |
det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | |
if (!det) { | |
return null; | |
} | |
det = 1.0 / det; | |
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; | |
out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det; | |
out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det; | |
out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det; | |
out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det; | |
out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det; | |
out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det; | |
out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det; | |
out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det; | |
return out; | |
}; | |
/** | |
* Returns a string representation of a mat3 | |
* | |
* @param {mat3} mat matrix to represent as a string | |
* @returns {String} string representation of the matrix | |
*/ | |
mat3.str = function (a) { | |
return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + | |
a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + | |
a[6] + ', ' + a[7] + ', ' + a[8] + ')'; | |
}; | |
/** | |
* Returns Frobenius norm of a mat3 | |
* | |
* @param {mat3} a the matrix to calculate Frobenius norm of | |
* @returns {Number} Frobenius norm | |
*/ | |
mat3.frob = function (a) { | |
return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2))) | |
}; | |
/** | |
* Adds two mat3's | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the first operand | |
* @param {mat3} b the second operand | |
* @returns {mat3} out | |
*/ | |
mat3.add = function(out, a, b) { | |
out[0] = a[0] + b[0]; | |
out[1] = a[1] + b[1]; | |
out[2] = a[2] + b[2]; | |
out[3] = a[3] + b[3]; | |
out[4] = a[4] + b[4]; | |
out[5] = a[5] + b[5]; | |
out[6] = a[6] + b[6]; | |
out[7] = a[7] + b[7]; | |
out[8] = a[8] + b[8]; | |
return out; | |
}; | |
/** | |
* Subtracts matrix b from matrix a | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the first operand | |
* @param {mat3} b the second operand | |
* @returns {mat3} out | |
*/ | |
mat3.subtract = function(out, a, b) { | |
out[0] = a[0] - b[0]; | |
out[1] = a[1] - b[1]; | |
out[2] = a[2] - b[2]; | |
out[3] = a[3] - b[3]; | |
out[4] = a[4] - b[4]; | |
out[5] = a[5] - b[5]; | |
out[6] = a[6] - b[6]; | |
out[7] = a[7] - b[7]; | |
out[8] = a[8] - b[8]; | |
return out; | |
}; | |
/** | |
* Alias for {@link mat3.subtract} | |
* @function | |
*/ | |
mat3.sub = mat3.subtract; | |
/** | |
* Multiply each element of the matrix by a scalar. | |
* | |
* @param {mat3} out the receiving matrix | |
* @param {mat3} a the matrix to scale | |
* @param {Number} b amount to scale the matrix's elements by | |
* @returns {mat3} out | |
*/ | |
mat3.multiplyScalar = function(out, a, b) { | |
out[0] = a[0] * b; | |
out[1] = a[1] * b; | |
out[2] = a[2] * b; | |
out[3] = a[3] * b; | |
out[4] = a[4] * b; | |
out[5] = a[5] * b; | |
out[6] = a[6] * b; | |
out[7] = a[7] * b; | |
out[8] = a[8] * b; | |
return out; | |
}; | |
/** | |
* Adds two mat3's after multiplying each element of the second operand by a scalar value. | |
* | |
* @param {mat3} out the receiving vector | |
* @param {mat3} a the first operand | |
* @param {mat3} b the second operand | |
* @param {Number} scale the amount to scale b's elements by before adding | |
* @returns {mat3} out | |
*/ | |
mat3.multiplyScalarAndAdd = function(out, a, b, scale) { | |
out[0] = a[0] + (b[0] * scale); | |
out[1] = a[1] + (b[1] * scale); | |
out[2] = a[2] + (b[2] * scale); | |
out[3] = a[3] + (b[3] * scale); | |
out[4] = a[4] + (b[4] * scale); | |
out[5] = a[5] + (b[5] * scale); | |
out[6] = a[6] + (b[6] * scale); | |
out[7] = a[7] + (b[7] * scale); | |
out[8] = a[8] + (b[8] * scale); | |
return out; | |
}; | |
/* | |
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | |
* | |
* @param {mat3} a The first matrix. | |
* @param {mat3} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat3.exactEquals = function (a, b) { | |
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && | |
a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && | |
a[6] === b[6] && a[7] === b[7] && a[8] === b[8]; | |
}; | |
/** | |
* Returns whether or not the matrices have approximately the same elements in the same position. | |
* | |
* @param {mat3} a The first matrix. | |
* @param {mat3} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat3.equals = function (a, b) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], a6 = a[6], a7 = a[7], a8 = a[8]; | |
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = a[6], b7 = b[7], b8 = b[8]; | |
return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | |
Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | |
Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | |
Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && | |
Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && | |
Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5)) && | |
Math.abs(a6 - b6) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a6), Math.abs(b6)) && | |
Math.abs(a7 - b7) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a7), Math.abs(b7)) && | |
Math.abs(a8 - b8) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a8), Math.abs(b8))); | |
}; | |
module.exports = mat3; | |
},{"./common.js":136}],140:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
/** | |
* @class 4x4 Matrix | |
* @name mat4 | |
*/ | |
var mat4 = { | |
scalar: {}, | |
SIMD: {}, | |
}; | |
/** | |
* Creates a new identity mat4 | |
* | |
* @returns {mat4} a new 4x4 matrix | |
*/ | |
mat4.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(16); | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = 1; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = 0; | |
out[10] = 1; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Creates a new mat4 initialized with values from an existing matrix | |
* | |
* @param {mat4} a matrix to clone | |
* @returns {mat4} a new 4x4 matrix | |
*/ | |
mat4.clone = function(a) { | |
var out = new glMatrix.ARRAY_TYPE(16); | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[4] = a[4]; | |
out[5] = a[5]; | |
out[6] = a[6]; | |
out[7] = a[7]; | |
out[8] = a[8]; | |
out[9] = a[9]; | |
out[10] = a[10]; | |
out[11] = a[11]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
return out; | |
}; | |
/** | |
* Copy the values from one mat4 to another | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.copy = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[4] = a[4]; | |
out[5] = a[5]; | |
out[6] = a[6]; | |
out[7] = a[7]; | |
out[8] = a[8]; | |
out[9] = a[9]; | |
out[10] = a[10]; | |
out[11] = a[11]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
return out; | |
}; | |
/** | |
* Create a new mat4 with the given values | |
* | |
* @param {Number} m00 Component in column 0, row 0 position (index 0) | |
* @param {Number} m01 Component in column 0, row 1 position (index 1) | |
* @param {Number} m02 Component in column 0, row 2 position (index 2) | |
* @param {Number} m03 Component in column 0, row 3 position (index 3) | |
* @param {Number} m10 Component in column 1, row 0 position (index 4) | |
* @param {Number} m11 Component in column 1, row 1 position (index 5) | |
* @param {Number} m12 Component in column 1, row 2 position (index 6) | |
* @param {Number} m13 Component in column 1, row 3 position (index 7) | |
* @param {Number} m20 Component in column 2, row 0 position (index 8) | |
* @param {Number} m21 Component in column 2, row 1 position (index 9) | |
* @param {Number} m22 Component in column 2, row 2 position (index 10) | |
* @param {Number} m23 Component in column 2, row 3 position (index 11) | |
* @param {Number} m30 Component in column 3, row 0 position (index 12) | |
* @param {Number} m31 Component in column 3, row 1 position (index 13) | |
* @param {Number} m32 Component in column 3, row 2 position (index 14) | |
* @param {Number} m33 Component in column 3, row 3 position (index 15) | |
* @returns {mat4} A new mat4 | |
*/ | |
mat4.fromValues = function(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | |
var out = new glMatrix.ARRAY_TYPE(16); | |
out[0] = m00; | |
out[1] = m01; | |
out[2] = m02; | |
out[3] = m03; | |
out[4] = m10; | |
out[5] = m11; | |
out[6] = m12; | |
out[7] = m13; | |
out[8] = m20; | |
out[9] = m21; | |
out[10] = m22; | |
out[11] = m23; | |
out[12] = m30; | |
out[13] = m31; | |
out[14] = m32; | |
out[15] = m33; | |
return out; | |
}; | |
/** | |
* Set the components of a mat4 to the given values | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {Number} m00 Component in column 0, row 0 position (index 0) | |
* @param {Number} m01 Component in column 0, row 1 position (index 1) | |
* @param {Number} m02 Component in column 0, row 2 position (index 2) | |
* @param {Number} m03 Component in column 0, row 3 position (index 3) | |
* @param {Number} m10 Component in column 1, row 0 position (index 4) | |
* @param {Number} m11 Component in column 1, row 1 position (index 5) | |
* @param {Number} m12 Component in column 1, row 2 position (index 6) | |
* @param {Number} m13 Component in column 1, row 3 position (index 7) | |
* @param {Number} m20 Component in column 2, row 0 position (index 8) | |
* @param {Number} m21 Component in column 2, row 1 position (index 9) | |
* @param {Number} m22 Component in column 2, row 2 position (index 10) | |
* @param {Number} m23 Component in column 2, row 3 position (index 11) | |
* @param {Number} m30 Component in column 3, row 0 position (index 12) | |
* @param {Number} m31 Component in column 3, row 1 position (index 13) | |
* @param {Number} m32 Component in column 3, row 2 position (index 14) | |
* @param {Number} m33 Component in column 3, row 3 position (index 15) | |
* @returns {mat4} out | |
*/ | |
mat4.set = function(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | |
out[0] = m00; | |
out[1] = m01; | |
out[2] = m02; | |
out[3] = m03; | |
out[4] = m10; | |
out[5] = m11; | |
out[6] = m12; | |
out[7] = m13; | |
out[8] = m20; | |
out[9] = m21; | |
out[10] = m22; | |
out[11] = m23; | |
out[12] = m30; | |
out[13] = m31; | |
out[14] = m32; | |
out[15] = m33; | |
return out; | |
}; | |
/** | |
* Set a mat4 to the identity matrix | |
* | |
* @param {mat4} out the receiving matrix | |
* @returns {mat4} out | |
*/ | |
mat4.identity = function(out) { | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = 1; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = 0; | |
out[10] = 1; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Transpose the values of a mat4 not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.transpose = function(out, a) { | |
// If we are transposing ourselves we can skip a few steps but have to cache some values | |
if (out === a) { | |
var a01 = a[1], a02 = a[2], a03 = a[3], | |
a12 = a[6], a13 = a[7], | |
a23 = a[11]; | |
out[1] = a[4]; | |
out[2] = a[8]; | |
out[3] = a[12]; | |
out[4] = a01; | |
out[6] = a[9]; | |
out[7] = a[13]; | |
out[8] = a02; | |
out[9] = a12; | |
out[11] = a[14]; | |
out[12] = a03; | |
out[13] = a13; | |
out[14] = a23; | |
} else { | |
out[0] = a[0]; | |
out[1] = a[4]; | |
out[2] = a[8]; | |
out[3] = a[12]; | |
out[4] = a[1]; | |
out[5] = a[5]; | |
out[6] = a[9]; | |
out[7] = a[13]; | |
out[8] = a[2]; | |
out[9] = a[6]; | |
out[10] = a[10]; | |
out[11] = a[14]; | |
out[12] = a[3]; | |
out[13] = a[7]; | |
out[14] = a[11]; | |
out[15] = a[15]; | |
} | |
return out; | |
}; | |
/** | |
* Transpose the values of a mat4 using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.transpose = function(out, a) { | |
var a0, a1, a2, a3, | |
tmp01, tmp23, | |
out0, out1, out2, out3; | |
a0 = SIMD.Float32x4.load(a, 0); | |
a1 = SIMD.Float32x4.load(a, 4); | |
a2 = SIMD.Float32x4.load(a, 8); | |
a3 = SIMD.Float32x4.load(a, 12); | |
tmp01 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); | |
tmp23 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); | |
out0 = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6); | |
out1 = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7); | |
SIMD.Float32x4.store(out, 0, out0); | |
SIMD.Float32x4.store(out, 4, out1); | |
tmp01 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); | |
tmp23 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); | |
out2 = SIMD.Float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6); | |
out3 = SIMD.Float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7); | |
SIMD.Float32x4.store(out, 8, out2); | |
SIMD.Float32x4.store(out, 12, out3); | |
return out; | |
}; | |
/** | |
* Transpse a mat4 using SIMD if available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.transpose = glMatrix.USE_SIMD ? mat4.SIMD.transpose : mat4.scalar.transpose; | |
/** | |
* Inverts a mat4 not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.invert = function(out, a) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], | |
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], | |
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], | |
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], | |
b00 = a00 * a11 - a01 * a10, | |
b01 = a00 * a12 - a02 * a10, | |
b02 = a00 * a13 - a03 * a10, | |
b03 = a01 * a12 - a02 * a11, | |
b04 = a01 * a13 - a03 * a11, | |
b05 = a02 * a13 - a03 * a12, | |
b06 = a20 * a31 - a21 * a30, | |
b07 = a20 * a32 - a22 * a30, | |
b08 = a20 * a33 - a23 * a30, | |
b09 = a21 * a32 - a22 * a31, | |
b10 = a21 * a33 - a23 * a31, | |
b11 = a22 * a33 - a23 * a32, | |
// Calculate the determinant | |
det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | |
if (!det) { | |
return null; | |
} | |
det = 1.0 / det; | |
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; | |
out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; | |
out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; | |
out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; | |
out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; | |
out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; | |
out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; | |
out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; | |
out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; | |
out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; | |
out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; | |
out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; | |
out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; | |
out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; | |
out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; | |
out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; | |
return out; | |
}; | |
/** | |
* Inverts a mat4 using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.invert = function(out, a) { | |
var row0, row1, row2, row3, | |
tmp1, | |
minor0, minor1, minor2, minor3, | |
det, | |
a0 = SIMD.Float32x4.load(a, 0), | |
a1 = SIMD.Float32x4.load(a, 4), | |
a2 = SIMD.Float32x4.load(a, 8), | |
a3 = SIMD.Float32x4.load(a, 12); | |
// Compute matrix adjugate | |
tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); | |
row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); | |
row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6); | |
row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7); | |
tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); | |
row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); | |
row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6); | |
row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7); | |
tmp1 = SIMD.Float32x4.mul(row2, row3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor0 = SIMD.Float32x4.mul(row1, tmp1); | |
minor1 = SIMD.Float32x4.mul(row0, tmp1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0); | |
minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1); | |
minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1); | |
tmp1 = SIMD.Float32x4.mul(row1, row2); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0); | |
minor3 = SIMD.Float32x4.mul(row0, tmp1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1)); | |
minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3); | |
minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1); | |
tmp1 = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
row2 = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0); | |
minor2 = SIMD.Float32x4.mul(row0, tmp1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1)); | |
minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2); | |
minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1); | |
tmp1 = SIMD.Float32x4.mul(row0, row1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2); | |
minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2); | |
minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1)); | |
tmp1 = SIMD.Float32x4.mul(row0, row3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1)); | |
minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1); | |
minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1)); | |
tmp1 = SIMD.Float32x4.mul(row0, row2); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1); | |
minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1)); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1)); | |
minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3); | |
// Compute matrix determinant | |
det = SIMD.Float32x4.mul(row0, minor0); | |
det = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 2, 3, 0, 1), det); | |
det = SIMD.Float32x4.add(SIMD.Float32x4.swizzle(det, 1, 0, 3, 2), det); | |
tmp1 = SIMD.Float32x4.reciprocalApproximation(det); | |
det = SIMD.Float32x4.sub( | |
SIMD.Float32x4.add(tmp1, tmp1), | |
SIMD.Float32x4.mul(det, SIMD.Float32x4.mul(tmp1, tmp1))); | |
det = SIMD.Float32x4.swizzle(det, 0, 0, 0, 0); | |
if (!det) { | |
return null; | |
} | |
// Compute matrix inverse | |
SIMD.Float32x4.store(out, 0, SIMD.Float32x4.mul(det, minor0)); | |
SIMD.Float32x4.store(out, 4, SIMD.Float32x4.mul(det, minor1)); | |
SIMD.Float32x4.store(out, 8, SIMD.Float32x4.mul(det, minor2)); | |
SIMD.Float32x4.store(out, 12, SIMD.Float32x4.mul(det, minor3)); | |
return out; | |
} | |
/** | |
* Inverts a mat4 using SIMD if available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.invert = glMatrix.USE_SIMD ? mat4.SIMD.invert : mat4.scalar.invert; | |
/** | |
* Calculates the adjugate of a mat4 not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.adjoint = function(out, a) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], | |
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], | |
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], | |
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; | |
out[0] = (a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22)); | |
out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)); | |
out[2] = (a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12)); | |
out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)); | |
out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)); | |
out[5] = (a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22)); | |
out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)); | |
out[7] = (a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12)); | |
out[8] = (a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21)); | |
out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)); | |
out[10] = (a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11)); | |
out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)); | |
out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)); | |
out[13] = (a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21)); | |
out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)); | |
out[15] = (a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11)); | |
return out; | |
}; | |
/** | |
* Calculates the adjugate of a mat4 using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.adjoint = function(out, a) { | |
var a0, a1, a2, a3; | |
var row0, row1, row2, row3; | |
var tmp1; | |
var minor0, minor1, minor2, minor3; | |
var a0 = SIMD.Float32x4.load(a, 0); | |
var a1 = SIMD.Float32x4.load(a, 4); | |
var a2 = SIMD.Float32x4.load(a, 8); | |
var a3 = SIMD.Float32x4.load(a, 12); | |
// Transpose the source matrix. Sort of. Not a true transpose operation | |
tmp1 = SIMD.Float32x4.shuffle(a0, a1, 0, 1, 4, 5); | |
row1 = SIMD.Float32x4.shuffle(a2, a3, 0, 1, 4, 5); | |
row0 = SIMD.Float32x4.shuffle(tmp1, row1, 0, 2, 4, 6); | |
row1 = SIMD.Float32x4.shuffle(row1, tmp1, 1, 3, 5, 7); | |
tmp1 = SIMD.Float32x4.shuffle(a0, a1, 2, 3, 6, 7); | |
row3 = SIMD.Float32x4.shuffle(a2, a3, 2, 3, 6, 7); | |
row2 = SIMD.Float32x4.shuffle(tmp1, row3, 0, 2, 4, 6); | |
row3 = SIMD.Float32x4.shuffle(row3, tmp1, 1, 3, 5, 7); | |
tmp1 = SIMD.Float32x4.mul(row2, row3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor0 = SIMD.Float32x4.mul(row1, tmp1); | |
minor1 = SIMD.Float32x4.mul(row0, tmp1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row1, tmp1), minor0); | |
minor1 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor1); | |
minor1 = SIMD.Float32x4.swizzle(minor1, 2, 3, 0, 1); | |
tmp1 = SIMD.Float32x4.mul(row1, row2); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor0); | |
minor3 = SIMD.Float32x4.mul(row0, tmp1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row3, tmp1)); | |
minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor3); | |
minor3 = SIMD.Float32x4.swizzle(minor3, 2, 3, 0, 1); | |
tmp1 = SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(row1, 2, 3, 0, 1), row3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
row2 = SIMD.Float32x4.swizzle(row2, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor0); | |
minor2 = SIMD.Float32x4.mul(row0, tmp1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor0 = SIMD.Float32x4.sub(minor0, SIMD.Float32x4.mul(row2, tmp1)); | |
minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row0, tmp1), minor2); | |
minor2 = SIMD.Float32x4.swizzle(minor2, 2, 3, 0, 1); | |
tmp1 = SIMD.Float32x4.mul(row0, row1); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor2); | |
minor3 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row2, tmp1), minor3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor2 = SIMD.Float32x4.sub(SIMD.Float32x4.mul(row3, tmp1), minor2); | |
minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row2, tmp1)); | |
tmp1 = SIMD.Float32x4.mul(row0, row3); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row2, tmp1)); | |
minor2 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor2); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row2, tmp1), minor1); | |
minor2 = SIMD.Float32x4.sub(minor2, SIMD.Float32x4.mul(row1, tmp1)); | |
tmp1 = SIMD.Float32x4.mul(row0, row2); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 1, 0, 3, 2); | |
minor1 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row3, tmp1), minor1); | |
minor3 = SIMD.Float32x4.sub(minor3, SIMD.Float32x4.mul(row1, tmp1)); | |
tmp1 = SIMD.Float32x4.swizzle(tmp1, 2, 3, 0, 1); | |
minor1 = SIMD.Float32x4.sub(minor1, SIMD.Float32x4.mul(row3, tmp1)); | |
minor3 = SIMD.Float32x4.add(SIMD.Float32x4.mul(row1, tmp1), minor3); | |
SIMD.Float32x4.store(out, 0, minor0); | |
SIMD.Float32x4.store(out, 4, minor1); | |
SIMD.Float32x4.store(out, 8, minor2); | |
SIMD.Float32x4.store(out, 12, minor3); | |
return out; | |
}; | |
/** | |
* Calculates the adjugate of a mat4 using SIMD if available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the source matrix | |
* @returns {mat4} out | |
*/ | |
mat4.adjoint = glMatrix.USE_SIMD ? mat4.SIMD.adjoint : mat4.scalar.adjoint; | |
/** | |
* Calculates the determinant of a mat4 | |
* | |
* @param {mat4} a the source matrix | |
* @returns {Number} determinant of a | |
*/ | |
mat4.determinant = function (a) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], | |
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], | |
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], | |
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], | |
b00 = a00 * a11 - a01 * a10, | |
b01 = a00 * a12 - a02 * a10, | |
b02 = a00 * a13 - a03 * a10, | |
b03 = a01 * a12 - a02 * a11, | |
b04 = a01 * a13 - a03 * a11, | |
b05 = a02 * a13 - a03 * a12, | |
b06 = a20 * a31 - a21 * a30, | |
b07 = a20 * a32 - a22 * a30, | |
b08 = a20 * a33 - a23 * a30, | |
b09 = a21 * a32 - a22 * a31, | |
b10 = a21 * a33 - a23 * a31, | |
b11 = a22 * a33 - a23 * a32; | |
// Calculate the determinant | |
return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | |
}; | |
/** | |
* Multiplies two mat4's explicitly using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the first operand, must be a Float32Array | |
* @param {mat4} b the second operand, must be a Float32Array | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.multiply = function (out, a, b) { | |
var a0 = SIMD.Float32x4.load(a, 0); | |
var a1 = SIMD.Float32x4.load(a, 4); | |
var a2 = SIMD.Float32x4.load(a, 8); | |
var a3 = SIMD.Float32x4.load(a, 12); | |
var b0 = SIMD.Float32x4.load(b, 0); | |
var out0 = SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 0, 0, 0, 0), a0), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 1, 1, 1, 1), a1), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 2, 2, 2, 2), a2), | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b0, 3, 3, 3, 3), a3)))); | |
SIMD.Float32x4.store(out, 0, out0); | |
var b1 = SIMD.Float32x4.load(b, 4); | |
var out1 = SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 0, 0, 0, 0), a0), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 1, 1, 1, 1), a1), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 2, 2, 2, 2), a2), | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b1, 3, 3, 3, 3), a3)))); | |
SIMD.Float32x4.store(out, 4, out1); | |
var b2 = SIMD.Float32x4.load(b, 8); | |
var out2 = SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 0, 0, 0, 0), a0), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 1, 1, 1, 1), a1), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 2, 2, 2, 2), a2), | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b2, 3, 3, 3, 3), a3)))); | |
SIMD.Float32x4.store(out, 8, out2); | |
var b3 = SIMD.Float32x4.load(b, 12); | |
var out3 = SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 0, 0, 0, 0), a0), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 1, 1, 1, 1), a1), | |
SIMD.Float32x4.add( | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 2, 2, 2, 2), a2), | |
SIMD.Float32x4.mul(SIMD.Float32x4.swizzle(b3, 3, 3, 3, 3), a3)))); | |
SIMD.Float32x4.store(out, 12, out3); | |
return out; | |
}; | |
/** | |
* Multiplies two mat4's explicitly not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the first operand | |
* @param {mat4} b the second operand | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.multiply = function (out, a, b) { | |
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], | |
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], | |
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], | |
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; | |
// Cache only the current line of the second matrix | |
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; | |
out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30; | |
out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31; | |
out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32; | |
out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33; | |
b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7]; | |
out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30; | |
out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31; | |
out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32; | |
out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33; | |
b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11]; | |
out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30; | |
out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31; | |
out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32; | |
out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33; | |
b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15]; | |
out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30; | |
out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31; | |
out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32; | |
out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33; | |
return out; | |
}; | |
/** | |
* Multiplies two mat4's using SIMD if available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the first operand | |
* @param {mat4} b the second operand | |
* @returns {mat4} out | |
*/ | |
mat4.multiply = glMatrix.USE_SIMD ? mat4.SIMD.multiply : mat4.scalar.multiply; | |
/** | |
* Alias for {@link mat4.multiply} | |
* @function | |
*/ | |
mat4.mul = mat4.multiply; | |
/** | |
* Translate a mat4 by the given vector not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to translate | |
* @param {vec3} v vector to translate by | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.translate = function (out, a, v) { | |
var x = v[0], y = v[1], z = v[2], | |
a00, a01, a02, a03, | |
a10, a11, a12, a13, | |
a20, a21, a22, a23; | |
if (a === out) { | |
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; | |
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; | |
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; | |
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; | |
} else { | |
a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; | |
a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; | |
a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; | |
out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03; | |
out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13; | |
out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23; | |
out[12] = a00 * x + a10 * y + a20 * z + a[12]; | |
out[13] = a01 * x + a11 * y + a21 * z + a[13]; | |
out[14] = a02 * x + a12 * y + a22 * z + a[14]; | |
out[15] = a03 * x + a13 * y + a23 * z + a[15]; | |
} | |
return out; | |
}; | |
/** | |
* Translates a mat4 by the given vector using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to translate | |
* @param {vec3} v vector to translate by | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.translate = function (out, a, v) { | |
var a0 = SIMD.Float32x4.load(a, 0), | |
a1 = SIMD.Float32x4.load(a, 4), | |
a2 = SIMD.Float32x4.load(a, 8), | |
a3 = SIMD.Float32x4.load(a, 12), | |
vec = SIMD.Float32x4(v[0], v[1], v[2] , 0); | |
if (a !== out) { | |
out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; out[3] = a[3]; | |
out[4] = a[4]; out[5] = a[5]; out[6] = a[6]; out[7] = a[7]; | |
out[8] = a[8]; out[9] = a[9]; out[10] = a[10]; out[11] = a[11]; | |
} | |
a0 = SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0)); | |
a1 = SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1)); | |
a2 = SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2)); | |
var t0 = SIMD.Float32x4.add(a0, SIMD.Float32x4.add(a1, SIMD.Float32x4.add(a2, a3))); | |
SIMD.Float32x4.store(out, 12, t0); | |
return out; | |
}; | |
/** | |
* Translates a mat4 by the given vector using SIMD if available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to translate | |
* @param {vec3} v vector to translate by | |
* @returns {mat4} out | |
*/ | |
mat4.translate = glMatrix.USE_SIMD ? mat4.SIMD.translate : mat4.scalar.translate; | |
/** | |
* Scales the mat4 by the dimensions in the given vec3 not using vectorization | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to scale | |
* @param {vec3} v the vec3 to scale the matrix by | |
* @returns {mat4} out | |
**/ | |
mat4.scalar.scale = function(out, a, v) { | |
var x = v[0], y = v[1], z = v[2]; | |
out[0] = a[0] * x; | |
out[1] = a[1] * x; | |
out[2] = a[2] * x; | |
out[3] = a[3] * x; | |
out[4] = a[4] * y; | |
out[5] = a[5] * y; | |
out[6] = a[6] * y; | |
out[7] = a[7] * y; | |
out[8] = a[8] * z; | |
out[9] = a[9] * z; | |
out[10] = a[10] * z; | |
out[11] = a[11] * z; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
return out; | |
}; | |
/** | |
* Scales the mat4 by the dimensions in the given vec3 using vectorization | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to scale | |
* @param {vec3} v the vec3 to scale the matrix by | |
* @returns {mat4} out | |
**/ | |
mat4.SIMD.scale = function(out, a, v) { | |
var a0, a1, a2; | |
var vec = SIMD.Float32x4(v[0], v[1], v[2], 0); | |
a0 = SIMD.Float32x4.load(a, 0); | |
SIMD.Float32x4.store( | |
out, 0, SIMD.Float32x4.mul(a0, SIMD.Float32x4.swizzle(vec, 0, 0, 0, 0))); | |
a1 = SIMD.Float32x4.load(a, 4); | |
SIMD.Float32x4.store( | |
out, 4, SIMD.Float32x4.mul(a1, SIMD.Float32x4.swizzle(vec, 1, 1, 1, 1))); | |
a2 = SIMD.Float32x4.load(a, 8); | |
SIMD.Float32x4.store( | |
out, 8, SIMD.Float32x4.mul(a2, SIMD.Float32x4.swizzle(vec, 2, 2, 2, 2))); | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
return out; | |
}; | |
/** | |
* Scales the mat4 by the dimensions in the given vec3 using SIMD if available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to scale | |
* @param {vec3} v the vec3 to scale the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.scale = glMatrix.USE_SIMD ? mat4.SIMD.scale : mat4.scalar.scale; | |
/** | |
* Rotates a mat4 by the given angle around the given axis | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @param {vec3} axis the axis to rotate around | |
* @returns {mat4} out | |
*/ | |
mat4.rotate = function (out, a, rad, axis) { | |
var x = axis[0], y = axis[1], z = axis[2], | |
len = Math.sqrt(x * x + y * y + z * z), | |
s, c, t, | |
a00, a01, a02, a03, | |
a10, a11, a12, a13, | |
a20, a21, a22, a23, | |
b00, b01, b02, | |
b10, b11, b12, | |
b20, b21, b22; | |
if (Math.abs(len) < glMatrix.EPSILON) { return null; } | |
len = 1 / len; | |
x *= len; | |
y *= len; | |
z *= len; | |
s = Math.sin(rad); | |
c = Math.cos(rad); | |
t = 1 - c; | |
a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; | |
a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; | |
a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; | |
// Construct the elements of the rotation matrix | |
b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s; | |
b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s; | |
b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c; | |
// Perform rotation-specific matrix multiplication | |
out[0] = a00 * b00 + a10 * b01 + a20 * b02; | |
out[1] = a01 * b00 + a11 * b01 + a21 * b02; | |
out[2] = a02 * b00 + a12 * b01 + a22 * b02; | |
out[3] = a03 * b00 + a13 * b01 + a23 * b02; | |
out[4] = a00 * b10 + a10 * b11 + a20 * b12; | |
out[5] = a01 * b10 + a11 * b11 + a21 * b12; | |
out[6] = a02 * b10 + a12 * b11 + a22 * b12; | |
out[7] = a03 * b10 + a13 * b11 + a23 * b12; | |
out[8] = a00 * b20 + a10 * b21 + a20 * b22; | |
out[9] = a01 * b20 + a11 * b21 + a21 * b22; | |
out[10] = a02 * b20 + a12 * b21 + a22 * b22; | |
out[11] = a03 * b20 + a13 * b21 + a23 * b22; | |
if (a !== out) { // If the source and destination differ, copy the unchanged last row | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
} | |
return out; | |
}; | |
/** | |
* Rotates a matrix by the given angle around the X axis not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.rotateX = function (out, a, rad) { | |
var s = Math.sin(rad), | |
c = Math.cos(rad), | |
a10 = a[4], | |
a11 = a[5], | |
a12 = a[6], | |
a13 = a[7], | |
a20 = a[8], | |
a21 = a[9], | |
a22 = a[10], | |
a23 = a[11]; | |
if (a !== out) { // If the source and destination differ, copy the unchanged rows | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
} | |
// Perform axis-specific matrix multiplication | |
out[4] = a10 * c + a20 * s; | |
out[5] = a11 * c + a21 * s; | |
out[6] = a12 * c + a22 * s; | |
out[7] = a13 * c + a23 * s; | |
out[8] = a20 * c - a10 * s; | |
out[9] = a21 * c - a11 * s; | |
out[10] = a22 * c - a12 * s; | |
out[11] = a23 * c - a13 * s; | |
return out; | |
}; | |
/** | |
* Rotates a matrix by the given angle around the X axis using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.rotateX = function (out, a, rad) { | |
var s = SIMD.Float32x4.splat(Math.sin(rad)), | |
c = SIMD.Float32x4.splat(Math.cos(rad)); | |
if (a !== out) { // If the source and destination differ, copy the unchanged rows | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
} | |
// Perform axis-specific matrix multiplication | |
var a_1 = SIMD.Float32x4.load(a, 4); | |
var a_2 = SIMD.Float32x4.load(a, 8); | |
SIMD.Float32x4.store(out, 4, | |
SIMD.Float32x4.add(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_2, s))); | |
SIMD.Float32x4.store(out, 8, | |
SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_2, c), SIMD.Float32x4.mul(a_1, s))); | |
return out; | |
}; | |
/** | |
* Rotates a matrix by the given angle around the X axis using SIMD if availabe and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.rotateX = glMatrix.USE_SIMD ? mat4.SIMD.rotateX : mat4.scalar.rotateX; | |
/** | |
* Rotates a matrix by the given angle around the Y axis not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.rotateY = function (out, a, rad) { | |
var s = Math.sin(rad), | |
c = Math.cos(rad), | |
a00 = a[0], | |
a01 = a[1], | |
a02 = a[2], | |
a03 = a[3], | |
a20 = a[8], | |
a21 = a[9], | |
a22 = a[10], | |
a23 = a[11]; | |
if (a !== out) { // If the source and destination differ, copy the unchanged rows | |
out[4] = a[4]; | |
out[5] = a[5]; | |
out[6] = a[6]; | |
out[7] = a[7]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
} | |
// Perform axis-specific matrix multiplication | |
out[0] = a00 * c - a20 * s; | |
out[1] = a01 * c - a21 * s; | |
out[2] = a02 * c - a22 * s; | |
out[3] = a03 * c - a23 * s; | |
out[8] = a00 * s + a20 * c; | |
out[9] = a01 * s + a21 * c; | |
out[10] = a02 * s + a22 * c; | |
out[11] = a03 * s + a23 * c; | |
return out; | |
}; | |
/** | |
* Rotates a matrix by the given angle around the Y axis using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.rotateY = function (out, a, rad) { | |
var s = SIMD.Float32x4.splat(Math.sin(rad)), | |
c = SIMD.Float32x4.splat(Math.cos(rad)); | |
if (a !== out) { // If the source and destination differ, copy the unchanged rows | |
out[4] = a[4]; | |
out[5] = a[5]; | |
out[6] = a[6]; | |
out[7] = a[7]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
} | |
// Perform axis-specific matrix multiplication | |
var a_0 = SIMD.Float32x4.load(a, 0); | |
var a_2 = SIMD.Float32x4.load(a, 8); | |
SIMD.Float32x4.store(out, 0, | |
SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_2, s))); | |
SIMD.Float32x4.store(out, 8, | |
SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, s), SIMD.Float32x4.mul(a_2, c))); | |
return out; | |
}; | |
/** | |
* Rotates a matrix by the given angle around the Y axis if SIMD available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.rotateY = glMatrix.USE_SIMD ? mat4.SIMD.rotateY : mat4.scalar.rotateY; | |
/** | |
* Rotates a matrix by the given angle around the Z axis not using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.scalar.rotateZ = function (out, a, rad) { | |
var s = Math.sin(rad), | |
c = Math.cos(rad), | |
a00 = a[0], | |
a01 = a[1], | |
a02 = a[2], | |
a03 = a[3], | |
a10 = a[4], | |
a11 = a[5], | |
a12 = a[6], | |
a13 = a[7]; | |
if (a !== out) { // If the source and destination differ, copy the unchanged last row | |
out[8] = a[8]; | |
out[9] = a[9]; | |
out[10] = a[10]; | |
out[11] = a[11]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
} | |
// Perform axis-specific matrix multiplication | |
out[0] = a00 * c + a10 * s; | |
out[1] = a01 * c + a11 * s; | |
out[2] = a02 * c + a12 * s; | |
out[3] = a03 * c + a13 * s; | |
out[4] = a10 * c - a00 * s; | |
out[5] = a11 * c - a01 * s; | |
out[6] = a12 * c - a02 * s; | |
out[7] = a13 * c - a03 * s; | |
return out; | |
}; | |
/** | |
* Rotates a matrix by the given angle around the Z axis using SIMD | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.SIMD.rotateZ = function (out, a, rad) { | |
var s = SIMD.Float32x4.splat(Math.sin(rad)), | |
c = SIMD.Float32x4.splat(Math.cos(rad)); | |
if (a !== out) { // If the source and destination differ, copy the unchanged last row | |
out[8] = a[8]; | |
out[9] = a[9]; | |
out[10] = a[10]; | |
out[11] = a[11]; | |
out[12] = a[12]; | |
out[13] = a[13]; | |
out[14] = a[14]; | |
out[15] = a[15]; | |
} | |
// Perform axis-specific matrix multiplication | |
var a_0 = SIMD.Float32x4.load(a, 0); | |
var a_1 = SIMD.Float32x4.load(a, 4); | |
SIMD.Float32x4.store(out, 0, | |
SIMD.Float32x4.add(SIMD.Float32x4.mul(a_0, c), SIMD.Float32x4.mul(a_1, s))); | |
SIMD.Float32x4.store(out, 4, | |
SIMD.Float32x4.sub(SIMD.Float32x4.mul(a_1, c), SIMD.Float32x4.mul(a_0, s))); | |
return out; | |
}; | |
/** | |
* Rotates a matrix by the given angle around the Z axis if SIMD available and enabled | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to rotate | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.rotateZ = glMatrix.USE_SIMD ? mat4.SIMD.rotateZ : mat4.scalar.rotateZ; | |
/** | |
* Creates a matrix from a vector translation | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.translate(dest, dest, vec); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {vec3} v Translation vector | |
* @returns {mat4} out | |
*/ | |
mat4.fromTranslation = function(out, v) { | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = 1; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = 0; | |
out[10] = 1; | |
out[11] = 0; | |
out[12] = v[0]; | |
out[13] = v[1]; | |
out[14] = v[2]; | |
out[15] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from a vector scaling | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.scale(dest, dest, vec); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {vec3} v Scaling vector | |
* @returns {mat4} out | |
*/ | |
mat4.fromScaling = function(out, v) { | |
out[0] = v[0]; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = v[1]; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = 0; | |
out[10] = v[2]; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from a given angle around a given axis | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.rotate(dest, dest, rad, axis); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {Number} rad the angle to rotate the matrix by | |
* @param {vec3} axis the axis to rotate around | |
* @returns {mat4} out | |
*/ | |
mat4.fromRotation = function(out, rad, axis) { | |
var x = axis[0], y = axis[1], z = axis[2], | |
len = Math.sqrt(x * x + y * y + z * z), | |
s, c, t; | |
if (Math.abs(len) < glMatrix.EPSILON) { return null; } | |
len = 1 / len; | |
x *= len; | |
y *= len; | |
z *= len; | |
s = Math.sin(rad); | |
c = Math.cos(rad); | |
t = 1 - c; | |
// Perform rotation-specific matrix multiplication | |
out[0] = x * x * t + c; | |
out[1] = y * x * t + z * s; | |
out[2] = z * x * t - y * s; | |
out[3] = 0; | |
out[4] = x * y * t - z * s; | |
out[5] = y * y * t + c; | |
out[6] = z * y * t + x * s; | |
out[7] = 0; | |
out[8] = x * z * t + y * s; | |
out[9] = y * z * t - x * s; | |
out[10] = z * z * t + c; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from the given angle around the X axis | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.rotateX(dest, dest, rad); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.fromXRotation = function(out, rad) { | |
var s = Math.sin(rad), | |
c = Math.cos(rad); | |
// Perform axis-specific matrix multiplication | |
out[0] = 1; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = c; | |
out[6] = s; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = -s; | |
out[10] = c; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from the given angle around the Y axis | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.rotateY(dest, dest, rad); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.fromYRotation = function(out, rad) { | |
var s = Math.sin(rad), | |
c = Math.cos(rad); | |
// Perform axis-specific matrix multiplication | |
out[0] = c; | |
out[1] = 0; | |
out[2] = -s; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = 1; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = s; | |
out[9] = 0; | |
out[10] = c; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from the given angle around the Z axis | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.rotateZ(dest, dest, rad); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {Number} rad the angle to rotate the matrix by | |
* @returns {mat4} out | |
*/ | |
mat4.fromZRotation = function(out, rad) { | |
var s = Math.sin(rad), | |
c = Math.cos(rad); | |
// Perform axis-specific matrix multiplication | |
out[0] = c; | |
out[1] = s; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = -s; | |
out[5] = c; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = 0; | |
out[10] = 1; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
} | |
/** | |
* Creates a matrix from a quaternion rotation and vector translation | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.translate(dest, vec); | |
* var quatMat = mat4.create(); | |
* quat4.toMat4(quat, quatMat); | |
* mat4.multiply(dest, quatMat); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {quat4} q Rotation quaternion | |
* @param {vec3} v Translation vector | |
* @returns {mat4} out | |
*/ | |
mat4.fromRotationTranslation = function (out, q, v) { | |
// Quaternion math | |
var x = q[0], y = q[1], z = q[2], w = q[3], | |
x2 = x + x, | |
y2 = y + y, | |
z2 = z + z, | |
xx = x * x2, | |
xy = x * y2, | |
xz = x * z2, | |
yy = y * y2, | |
yz = y * z2, | |
zz = z * z2, | |
wx = w * x2, | |
wy = w * y2, | |
wz = w * z2; | |
out[0] = 1 - (yy + zz); | |
out[1] = xy + wz; | |
out[2] = xz - wy; | |
out[3] = 0; | |
out[4] = xy - wz; | |
out[5] = 1 - (xx + zz); | |
out[6] = yz + wx; | |
out[7] = 0; | |
out[8] = xz + wy; | |
out[9] = yz - wx; | |
out[10] = 1 - (xx + yy); | |
out[11] = 0; | |
out[12] = v[0]; | |
out[13] = v[1]; | |
out[14] = v[2]; | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Returns the translation vector component of a transformation | |
* matrix. If a matrix is built with fromRotationTranslation, | |
* the returned vector will be the same as the translation vector | |
* originally supplied. | |
* @param {vec3} out Vector to receive translation component | |
* @param {mat4} mat Matrix to be decomposed (input) | |
* @return {vec3} out | |
*/ | |
mat4.getTranslation = function (out, mat) { | |
out[0] = mat[12]; | |
out[1] = mat[13]; | |
out[2] = mat[14]; | |
return out; | |
}; | |
/** | |
* Returns a quaternion representing the rotational component | |
* of a transformation matrix. If a matrix is built with | |
* fromRotationTranslation, the returned quaternion will be the | |
* same as the quaternion originally supplied. | |
* @param {quat} out Quaternion to receive the rotation component | |
* @param {mat4} mat Matrix to be decomposed (input) | |
* @return {quat} out | |
*/ | |
mat4.getRotation = function (out, mat) { | |
// Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm | |
var trace = mat[0] + mat[5] + mat[10]; | |
var S = 0; | |
if (trace > 0) { | |
S = Math.sqrt(trace + 1.0) * 2; | |
out[3] = 0.25 * S; | |
out[0] = (mat[6] - mat[9]) / S; | |
out[1] = (mat[8] - mat[2]) / S; | |
out[2] = (mat[1] - mat[4]) / S; | |
} else if ((mat[0] > mat[5])&(mat[0] > mat[10])) { | |
S = Math.sqrt(1.0 + mat[0] - mat[5] - mat[10]) * 2; | |
out[3] = (mat[6] - mat[9]) / S; | |
out[0] = 0.25 * S; | |
out[1] = (mat[1] + mat[4]) / S; | |
out[2] = (mat[8] + mat[2]) / S; | |
} else if (mat[5] > mat[10]) { | |
S = Math.sqrt(1.0 + mat[5] - mat[0] - mat[10]) * 2; | |
out[3] = (mat[8] - mat[2]) / S; | |
out[0] = (mat[1] + mat[4]) / S; | |
out[1] = 0.25 * S; | |
out[2] = (mat[6] + mat[9]) / S; | |
} else { | |
S = Math.sqrt(1.0 + mat[10] - mat[0] - mat[5]) * 2; | |
out[3] = (mat[1] - mat[4]) / S; | |
out[0] = (mat[8] + mat[2]) / S; | |
out[1] = (mat[6] + mat[9]) / S; | |
out[2] = 0.25 * S; | |
} | |
return out; | |
}; | |
/** | |
* Creates a matrix from a quaternion rotation, vector translation and vector scale | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.translate(dest, vec); | |
* var quatMat = mat4.create(); | |
* quat4.toMat4(quat, quatMat); | |
* mat4.multiply(dest, quatMat); | |
* mat4.scale(dest, scale) | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {quat4} q Rotation quaternion | |
* @param {vec3} v Translation vector | |
* @param {vec3} s Scaling vector | |
* @returns {mat4} out | |
*/ | |
mat4.fromRotationTranslationScale = function (out, q, v, s) { | |
// Quaternion math | |
var x = q[0], y = q[1], z = q[2], w = q[3], | |
x2 = x + x, | |
y2 = y + y, | |
z2 = z + z, | |
xx = x * x2, | |
xy = x * y2, | |
xz = x * z2, | |
yy = y * y2, | |
yz = y * z2, | |
zz = z * z2, | |
wx = w * x2, | |
wy = w * y2, | |
wz = w * z2, | |
sx = s[0], | |
sy = s[1], | |
sz = s[2]; | |
out[0] = (1 - (yy + zz)) * sx; | |
out[1] = (xy + wz) * sx; | |
out[2] = (xz - wy) * sx; | |
out[3] = 0; | |
out[4] = (xy - wz) * sy; | |
out[5] = (1 - (xx + zz)) * sy; | |
out[6] = (yz + wx) * sy; | |
out[7] = 0; | |
out[8] = (xz + wy) * sz; | |
out[9] = (yz - wx) * sz; | |
out[10] = (1 - (xx + yy)) * sz; | |
out[11] = 0; | |
out[12] = v[0]; | |
out[13] = v[1]; | |
out[14] = v[2]; | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin | |
* This is equivalent to (but much faster than): | |
* | |
* mat4.identity(dest); | |
* mat4.translate(dest, vec); | |
* mat4.translate(dest, origin); | |
* var quatMat = mat4.create(); | |
* quat4.toMat4(quat, quatMat); | |
* mat4.multiply(dest, quatMat); | |
* mat4.scale(dest, scale) | |
* mat4.translate(dest, negativeOrigin); | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {quat4} q Rotation quaternion | |
* @param {vec3} v Translation vector | |
* @param {vec3} s Scaling vector | |
* @param {vec3} o The origin vector around which to scale and rotate | |
* @returns {mat4} out | |
*/ | |
mat4.fromRotationTranslationScaleOrigin = function (out, q, v, s, o) { | |
// Quaternion math | |
var x = q[0], y = q[1], z = q[2], w = q[3], | |
x2 = x + x, | |
y2 = y + y, | |
z2 = z + z, | |
xx = x * x2, | |
xy = x * y2, | |
xz = x * z2, | |
yy = y * y2, | |
yz = y * z2, | |
zz = z * z2, | |
wx = w * x2, | |
wy = w * y2, | |
wz = w * z2, | |
sx = s[0], | |
sy = s[1], | |
sz = s[2], | |
ox = o[0], | |
oy = o[1], | |
oz = o[2]; | |
out[0] = (1 - (yy + zz)) * sx; | |
out[1] = (xy + wz) * sx; | |
out[2] = (xz - wy) * sx; | |
out[3] = 0; | |
out[4] = (xy - wz) * sy; | |
out[5] = (1 - (xx + zz)) * sy; | |
out[6] = (yz + wx) * sy; | |
out[7] = 0; | |
out[8] = (xz + wy) * sz; | |
out[9] = (yz - wx) * sz; | |
out[10] = (1 - (xx + yy)) * sz; | |
out[11] = 0; | |
out[12] = v[0] + ox - (out[0] * ox + out[4] * oy + out[8] * oz); | |
out[13] = v[1] + oy - (out[1] * ox + out[5] * oy + out[9] * oz); | |
out[14] = v[2] + oz - (out[2] * ox + out[6] * oy + out[10] * oz); | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Calculates a 4x4 matrix from the given quaternion | |
* | |
* @param {mat4} out mat4 receiving operation result | |
* @param {quat} q Quaternion to create matrix from | |
* | |
* @returns {mat4} out | |
*/ | |
mat4.fromQuat = function (out, q) { | |
var x = q[0], y = q[1], z = q[2], w = q[3], | |
x2 = x + x, | |
y2 = y + y, | |
z2 = z + z, | |
xx = x * x2, | |
yx = y * x2, | |
yy = y * y2, | |
zx = z * x2, | |
zy = z * y2, | |
zz = z * z2, | |
wx = w * x2, | |
wy = w * y2, | |
wz = w * z2; | |
out[0] = 1 - yy - zz; | |
out[1] = yx + wz; | |
out[2] = zx - wy; | |
out[3] = 0; | |
out[4] = yx - wz; | |
out[5] = 1 - xx - zz; | |
out[6] = zy + wx; | |
out[7] = 0; | |
out[8] = zx + wy; | |
out[9] = zy - wx; | |
out[10] = 1 - xx - yy; | |
out[11] = 0; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = 0; | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Generates a frustum matrix with the given bounds | |
* | |
* @param {mat4} out mat4 frustum matrix will be written into | |
* @param {Number} left Left bound of the frustum | |
* @param {Number} right Right bound of the frustum | |
* @param {Number} bottom Bottom bound of the frustum | |
* @param {Number} top Top bound of the frustum | |
* @param {Number} near Near bound of the frustum | |
* @param {Number} far Far bound of the frustum | |
* @returns {mat4} out | |
*/ | |
mat4.frustum = function (out, left, right, bottom, top, near, far) { | |
var rl = 1 / (right - left), | |
tb = 1 / (top - bottom), | |
nf = 1 / (near - far); | |
out[0] = (near * 2) * rl; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = (near * 2) * tb; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = (right + left) * rl; | |
out[9] = (top + bottom) * tb; | |
out[10] = (far + near) * nf; | |
out[11] = -1; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = (far * near * 2) * nf; | |
out[15] = 0; | |
return out; | |
}; | |
/** | |
* Generates a perspective projection matrix with the given bounds | |
* | |
* @param {mat4} out mat4 frustum matrix will be written into | |
* @param {number} fovy Vertical field of view in radians | |
* @param {number} aspect Aspect ratio. typically viewport width/height | |
* @param {number} near Near bound of the frustum | |
* @param {number} far Far bound of the frustum | |
* @returns {mat4} out | |
*/ | |
mat4.perspective = function (out, fovy, aspect, near, far) { | |
var f = 1.0 / Math.tan(fovy / 2), | |
nf = 1 / (near - far); | |
out[0] = f / aspect; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = f; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = 0; | |
out[10] = (far + near) * nf; | |
out[11] = -1; | |
out[12] = 0; | |
out[13] = 0; | |
out[14] = (2 * far * near) * nf; | |
out[15] = 0; | |
return out; | |
}; | |
/** | |
* Generates a perspective projection matrix with the given field of view. | |
* This is primarily useful for generating projection matrices to be used | |
* with the still experiemental WebVR API. | |
* | |
* @param {mat4} out mat4 frustum matrix will be written into | |
* @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees | |
* @param {number} near Near bound of the frustum | |
* @param {number} far Far bound of the frustum | |
* @returns {mat4} out | |
*/ | |
mat4.perspectiveFromFieldOfView = function (out, fov, near, far) { | |
var upTan = Math.tan(fov.upDegrees * Math.PI/180.0), | |
downTan = Math.tan(fov.downDegrees * Math.PI/180.0), | |
leftTan = Math.tan(fov.leftDegrees * Math.PI/180.0), | |
rightTan = Math.tan(fov.rightDegrees * Math.PI/180.0), | |
xScale = 2.0 / (leftTan + rightTan), | |
yScale = 2.0 / (upTan + downTan); | |
out[0] = xScale; | |
out[1] = 0.0; | |
out[2] = 0.0; | |
out[3] = 0.0; | |
out[4] = 0.0; | |
out[5] = yScale; | |
out[6] = 0.0; | |
out[7] = 0.0; | |
out[8] = -((leftTan - rightTan) * xScale * 0.5); | |
out[9] = ((upTan - downTan) * yScale * 0.5); | |
out[10] = far / (near - far); | |
out[11] = -1.0; | |
out[12] = 0.0; | |
out[13] = 0.0; | |
out[14] = (far * near) / (near - far); | |
out[15] = 0.0; | |
return out; | |
} | |
/** | |
* Generates a orthogonal projection matrix with the given bounds | |
* | |
* @param {mat4} out mat4 frustum matrix will be written into | |
* @param {number} left Left bound of the frustum | |
* @param {number} right Right bound of the frustum | |
* @param {number} bottom Bottom bound of the frustum | |
* @param {number} top Top bound of the frustum | |
* @param {number} near Near bound of the frustum | |
* @param {number} far Far bound of the frustum | |
* @returns {mat4} out | |
*/ | |
mat4.ortho = function (out, left, right, bottom, top, near, far) { | |
var lr = 1 / (left - right), | |
bt = 1 / (bottom - top), | |
nf = 1 / (near - far); | |
out[0] = -2 * lr; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
out[4] = 0; | |
out[5] = -2 * bt; | |
out[6] = 0; | |
out[7] = 0; | |
out[8] = 0; | |
out[9] = 0; | |
out[10] = 2 * nf; | |
out[11] = 0; | |
out[12] = (left + right) * lr; | |
out[13] = (top + bottom) * bt; | |
out[14] = (far + near) * nf; | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Generates a look-at matrix with the given eye position, focal point, and up axis | |
* | |
* @param {mat4} out mat4 frustum matrix will be written into | |
* @param {vec3} eye Position of the viewer | |
* @param {vec3} center Point the viewer is looking at | |
* @param {vec3} up vec3 pointing up | |
* @returns {mat4} out | |
*/ | |
mat4.lookAt = function (out, eye, center, up) { | |
var x0, x1, x2, y0, y1, y2, z0, z1, z2, len, | |
eyex = eye[0], | |
eyey = eye[1], | |
eyez = eye[2], | |
upx = up[0], | |
upy = up[1], | |
upz = up[2], | |
centerx = center[0], | |
centery = center[1], | |
centerz = center[2]; | |
if (Math.abs(eyex - centerx) < glMatrix.EPSILON && | |
Math.abs(eyey - centery) < glMatrix.EPSILON && | |
Math.abs(eyez - centerz) < glMatrix.EPSILON) { | |
return mat4.identity(out); | |
} | |
z0 = eyex - centerx; | |
z1 = eyey - centery; | |
z2 = eyez - centerz; | |
len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); | |
z0 *= len; | |
z1 *= len; | |
z2 *= len; | |
x0 = upy * z2 - upz * z1; | |
x1 = upz * z0 - upx * z2; | |
x2 = upx * z1 - upy * z0; | |
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); | |
if (!len) { | |
x0 = 0; | |
x1 = 0; | |
x2 = 0; | |
} else { | |
len = 1 / len; | |
x0 *= len; | |
x1 *= len; | |
x2 *= len; | |
} | |
y0 = z1 * x2 - z2 * x1; | |
y1 = z2 * x0 - z0 * x2; | |
y2 = z0 * x1 - z1 * x0; | |
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); | |
if (!len) { | |
y0 = 0; | |
y1 = 0; | |
y2 = 0; | |
} else { | |
len = 1 / len; | |
y0 *= len; | |
y1 *= len; | |
y2 *= len; | |
} | |
out[0] = x0; | |
out[1] = y0; | |
out[2] = z0; | |
out[3] = 0; | |
out[4] = x1; | |
out[5] = y1; | |
out[6] = z1; | |
out[7] = 0; | |
out[8] = x2; | |
out[9] = y2; | |
out[10] = z2; | |
out[11] = 0; | |
out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); | |
out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); | |
out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); | |
out[15] = 1; | |
return out; | |
}; | |
/** | |
* Returns a string representation of a mat4 | |
* | |
* @param {mat4} mat matrix to represent as a string | |
* @returns {String} string representation of the matrix | |
*/ | |
mat4.str = function (a) { | |
return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + | |
a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' + | |
a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' + | |
a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')'; | |
}; | |
/** | |
* Returns Frobenius norm of a mat4 | |
* | |
* @param {mat4} a the matrix to calculate Frobenius norm of | |
* @returns {Number} Frobenius norm | |
*/ | |
mat4.frob = function (a) { | |
return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2) + Math.pow(a[9], 2) + Math.pow(a[10], 2) + Math.pow(a[11], 2) + Math.pow(a[12], 2) + Math.pow(a[13], 2) + Math.pow(a[14], 2) + Math.pow(a[15], 2) )) | |
}; | |
/** | |
* Adds two mat4's | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the first operand | |
* @param {mat4} b the second operand | |
* @returns {mat4} out | |
*/ | |
mat4.add = function(out, a, b) { | |
out[0] = a[0] + b[0]; | |
out[1] = a[1] + b[1]; | |
out[2] = a[2] + b[2]; | |
out[3] = a[3] + b[3]; | |
out[4] = a[4] + b[4]; | |
out[5] = a[5] + b[5]; | |
out[6] = a[6] + b[6]; | |
out[7] = a[7] + b[7]; | |
out[8] = a[8] + b[8]; | |
out[9] = a[9] + b[9]; | |
out[10] = a[10] + b[10]; | |
out[11] = a[11] + b[11]; | |
out[12] = a[12] + b[12]; | |
out[13] = a[13] + b[13]; | |
out[14] = a[14] + b[14]; | |
out[15] = a[15] + b[15]; | |
return out; | |
}; | |
/** | |
* Subtracts matrix b from matrix a | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the first operand | |
* @param {mat4} b the second operand | |
* @returns {mat4} out | |
*/ | |
mat4.subtract = function(out, a, b) { | |
out[0] = a[0] - b[0]; | |
out[1] = a[1] - b[1]; | |
out[2] = a[2] - b[2]; | |
out[3] = a[3] - b[3]; | |
out[4] = a[4] - b[4]; | |
out[5] = a[5] - b[5]; | |
out[6] = a[6] - b[6]; | |
out[7] = a[7] - b[7]; | |
out[8] = a[8] - b[8]; | |
out[9] = a[9] - b[9]; | |
out[10] = a[10] - b[10]; | |
out[11] = a[11] - b[11]; | |
out[12] = a[12] - b[12]; | |
out[13] = a[13] - b[13]; | |
out[14] = a[14] - b[14]; | |
out[15] = a[15] - b[15]; | |
return out; | |
}; | |
/** | |
* Alias for {@link mat4.subtract} | |
* @function | |
*/ | |
mat4.sub = mat4.subtract; | |
/** | |
* Multiply each element of the matrix by a scalar. | |
* | |
* @param {mat4} out the receiving matrix | |
* @param {mat4} a the matrix to scale | |
* @param {Number} b amount to scale the matrix's elements by | |
* @returns {mat4} out | |
*/ | |
mat4.multiplyScalar = function(out, a, b) { | |
out[0] = a[0] * b; | |
out[1] = a[1] * b; | |
out[2] = a[2] * b; | |
out[3] = a[3] * b; | |
out[4] = a[4] * b; | |
out[5] = a[5] * b; | |
out[6] = a[6] * b; | |
out[7] = a[7] * b; | |
out[8] = a[8] * b; | |
out[9] = a[9] * b; | |
out[10] = a[10] * b; | |
out[11] = a[11] * b; | |
out[12] = a[12] * b; | |
out[13] = a[13] * b; | |
out[14] = a[14] * b; | |
out[15] = a[15] * b; | |
return out; | |
}; | |
/** | |
* Adds two mat4's after multiplying each element of the second operand by a scalar value. | |
* | |
* @param {mat4} out the receiving vector | |
* @param {mat4} a the first operand | |
* @param {mat4} b the second operand | |
* @param {Number} scale the amount to scale b's elements by before adding | |
* @returns {mat4} out | |
*/ | |
mat4.multiplyScalarAndAdd = function(out, a, b, scale) { | |
out[0] = a[0] + (b[0] * scale); | |
out[1] = a[1] + (b[1] * scale); | |
out[2] = a[2] + (b[2] * scale); | |
out[3] = a[3] + (b[3] * scale); | |
out[4] = a[4] + (b[4] * scale); | |
out[5] = a[5] + (b[5] * scale); | |
out[6] = a[6] + (b[6] * scale); | |
out[7] = a[7] + (b[7] * scale); | |
out[8] = a[8] + (b[8] * scale); | |
out[9] = a[9] + (b[9] * scale); | |
out[10] = a[10] + (b[10] * scale); | |
out[11] = a[11] + (b[11] * scale); | |
out[12] = a[12] + (b[12] * scale); | |
out[13] = a[13] + (b[13] * scale); | |
out[14] = a[14] + (b[14] * scale); | |
out[15] = a[15] + (b[15] * scale); | |
return out; | |
}; | |
/** | |
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | |
* | |
* @param {mat4} a The first matrix. | |
* @param {mat4} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat4.exactEquals = function (a, b) { | |
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && | |
a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && | |
a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && | |
a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15]; | |
}; | |
/** | |
* Returns whether or not the matrices have approximately the same elements in the same position. | |
* | |
* @param {mat4} a The first matrix. | |
* @param {mat4} b The second matrix. | |
* @returns {Boolean} True if the matrices are equal, false otherwise. | |
*/ | |
mat4.equals = function (a, b) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], | |
a4 = a[4], a5 = a[5], a6 = a[6], a7 = a[7], | |
a8 = a[8], a9 = a[9], a10 = a[10], a11 = a[11], | |
a12 = a[12], a13 = a[13], a14 = a[14], a15 = a[15]; | |
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], | |
b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], | |
b8 = b[8], b9 = b[9], b10 = b[10], b11 = b[11], | |
b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15]; | |
return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | |
Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | |
Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | |
Math.abs(a3 - b3) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) && | |
Math.abs(a4 - b4) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) && | |
Math.abs(a5 - b5) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5)) && | |
Math.abs(a6 - b6) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a6), Math.abs(b6)) && | |
Math.abs(a7 - b7) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a7), Math.abs(b7)) && | |
Math.abs(a8 - b8) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a8), Math.abs(b8)) && | |
Math.abs(a9 - b9) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a9), Math.abs(b9)) && | |
Math.abs(a10 - b10) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a10), Math.abs(b10)) && | |
Math.abs(a11 - b11) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a11), Math.abs(b11)) && | |
Math.abs(a12 - b12) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a12), Math.abs(b12)) && | |
Math.abs(a13 - b13) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a13), Math.abs(b13)) && | |
Math.abs(a14 - b14) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a14), Math.abs(b14)) && | |
Math.abs(a15 - b15) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a15), Math.abs(b15))); | |
}; | |
module.exports = mat4; | |
},{"./common.js":136}],141:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
var mat3 = require("./mat3.js"); | |
var vec3 = require("./vec3.js"); | |
var vec4 = require("./vec4.js"); | |
/** | |
* @class Quaternion | |
* @name quat | |
*/ | |
var quat = {}; | |
/** | |
* Creates a new identity quat | |
* | |
* @returns {quat} a new quaternion | |
*/ | |
quat.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(4); | |
out[0] = 0; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
return out; | |
}; | |
/** | |
* Sets a quaternion to represent the shortest rotation from one | |
* vector to another. | |
* | |
* Both vectors are assumed to be unit length. | |
* | |
* @param {quat} out the receiving quaternion. | |
* @param {vec3} a the initial vector | |
* @param {vec3} b the destination vector | |
* @returns {quat} out | |
*/ | |
quat.rotationTo = (function() { | |
var tmpvec3 = vec3.create(); | |
var xUnitVec3 = vec3.fromValues(1,0,0); | |
var yUnitVec3 = vec3.fromValues(0,1,0); | |
return function(out, a, b) { | |
var dot = vec3.dot(a, b); | |
if (dot < -0.999999) { | |
vec3.cross(tmpvec3, xUnitVec3, a); | |
if (vec3.length(tmpvec3) < 0.000001) | |
vec3.cross(tmpvec3, yUnitVec3, a); | |
vec3.normalize(tmpvec3, tmpvec3); | |
quat.setAxisAngle(out, tmpvec3, Math.PI); | |
return out; | |
} else if (dot > 0.999999) { | |
out[0] = 0; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
return out; | |
} else { | |
vec3.cross(tmpvec3, a, b); | |
out[0] = tmpvec3[0]; | |
out[1] = tmpvec3[1]; | |
out[2] = tmpvec3[2]; | |
out[3] = 1 + dot; | |
return quat.normalize(out, out); | |
} | |
}; | |
})(); | |
/** | |
* Sets the specified quaternion with values corresponding to the given | |
* axes. Each axis is a vec3 and is expected to be unit length and | |
* perpendicular to all other specified axes. | |
* | |
* @param {vec3} view the vector representing the viewing direction | |
* @param {vec3} right the vector representing the local "right" direction | |
* @param {vec3} up the vector representing the local "up" direction | |
* @returns {quat} out | |
*/ | |
quat.setAxes = (function() { | |
var matr = mat3.create(); | |
return function(out, view, right, up) { | |
matr[0] = right[0]; | |
matr[3] = right[1]; | |
matr[6] = right[2]; | |
matr[1] = up[0]; | |
matr[4] = up[1]; | |
matr[7] = up[2]; | |
matr[2] = -view[0]; | |
matr[5] = -view[1]; | |
matr[8] = -view[2]; | |
return quat.normalize(out, quat.fromMat3(out, matr)); | |
}; | |
})(); | |
/** | |
* Creates a new quat initialized with values from an existing quaternion | |
* | |
* @param {quat} a quaternion to clone | |
* @returns {quat} a new quaternion | |
* @function | |
*/ | |
quat.clone = vec4.clone; | |
/** | |
* Creates a new quat initialized with the given values | |
* | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @param {Number} z Z component | |
* @param {Number} w W component | |
* @returns {quat} a new quaternion | |
* @function | |
*/ | |
quat.fromValues = vec4.fromValues; | |
/** | |
* Copy the values from one quat to another | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a the source quaternion | |
* @returns {quat} out | |
* @function | |
*/ | |
quat.copy = vec4.copy; | |
/** | |
* Set the components of a quat to the given values | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @param {Number} z Z component | |
* @param {Number} w W component | |
* @returns {quat} out | |
* @function | |
*/ | |
quat.set = vec4.set; | |
/** | |
* Set a quat to the identity quaternion | |
* | |
* @param {quat} out the receiving quaternion | |
* @returns {quat} out | |
*/ | |
quat.identity = function(out) { | |
out[0] = 0; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 1; | |
return out; | |
}; | |
/** | |
* Sets a quat from the given angle and rotation axis, | |
* then returns it. | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {vec3} axis the axis around which to rotate | |
* @param {Number} rad the angle in radians | |
* @returns {quat} out | |
**/ | |
quat.setAxisAngle = function(out, axis, rad) { | |
rad = rad * 0.5; | |
var s = Math.sin(rad); | |
out[0] = s * axis[0]; | |
out[1] = s * axis[1]; | |
out[2] = s * axis[2]; | |
out[3] = Math.cos(rad); | |
return out; | |
}; | |
/** | |
* Gets the rotation axis and angle for a given | |
* quaternion. If a quaternion is created with | |
* setAxisAngle, this method will return the same | |
* values as providied in the original parameter list | |
* OR functionally equivalent values. | |
* Example: The quaternion formed by axis [0, 0, 1] and | |
* angle -90 is the same as the quaternion formed by | |
* [0, 0, 1] and 270. This method favors the latter. | |
* @param {vec3} out_axis Vector receiving the axis of rotation | |
* @param {quat} q Quaternion to be decomposed | |
* @return {Number} Angle, in radians, of the rotation | |
*/ | |
quat.getAxisAngle = function(out_axis, q) { | |
var rad = Math.acos(q[3]) * 2.0; | |
var s = Math.sin(rad / 2.0); | |
if (s != 0.0) { | |
out_axis[0] = q[0] / s; | |
out_axis[1] = q[1] / s; | |
out_axis[2] = q[2] / s; | |
} else { | |
// If s is zero, return any axis (no rotation - axis does not matter) | |
out_axis[0] = 1; | |
out_axis[1] = 0; | |
out_axis[2] = 0; | |
} | |
return rad; | |
}; | |
/** | |
* Adds two quat's | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a the first operand | |
* @param {quat} b the second operand | |
* @returns {quat} out | |
* @function | |
*/ | |
quat.add = vec4.add; | |
/** | |
* Multiplies two quat's | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a the first operand | |
* @param {quat} b the second operand | |
* @returns {quat} out | |
*/ | |
quat.multiply = function(out, a, b) { | |
var ax = a[0], ay = a[1], az = a[2], aw = a[3], | |
bx = b[0], by = b[1], bz = b[2], bw = b[3]; | |
out[0] = ax * bw + aw * bx + ay * bz - az * by; | |
out[1] = ay * bw + aw * by + az * bx - ax * bz; | |
out[2] = az * bw + aw * bz + ax * by - ay * bx; | |
out[3] = aw * bw - ax * bx - ay * by - az * bz; | |
return out; | |
}; | |
/** | |
* Alias for {@link quat.multiply} | |
* @function | |
*/ | |
quat.mul = quat.multiply; | |
/** | |
* Scales a quat by a scalar number | |
* | |
* @param {quat} out the receiving vector | |
* @param {quat} a the vector to scale | |
* @param {Number} b amount to scale the vector by | |
* @returns {quat} out | |
* @function | |
*/ | |
quat.scale = vec4.scale; | |
/** | |
* Rotates a quaternion by the given angle about the X axis | |
* | |
* @param {quat} out quat receiving operation result | |
* @param {quat} a quat to rotate | |
* @param {number} rad angle (in radians) to rotate | |
* @returns {quat} out | |
*/ | |
quat.rotateX = function (out, a, rad) { | |
rad *= 0.5; | |
var ax = a[0], ay = a[1], az = a[2], aw = a[3], | |
bx = Math.sin(rad), bw = Math.cos(rad); | |
out[0] = ax * bw + aw * bx; | |
out[1] = ay * bw + az * bx; | |
out[2] = az * bw - ay * bx; | |
out[3] = aw * bw - ax * bx; | |
return out; | |
}; | |
/** | |
* Rotates a quaternion by the given angle about the Y axis | |
* | |
* @param {quat} out quat receiving operation result | |
* @param {quat} a quat to rotate | |
* @param {number} rad angle (in radians) to rotate | |
* @returns {quat} out | |
*/ | |
quat.rotateY = function (out, a, rad) { | |
rad *= 0.5; | |
var ax = a[0], ay = a[1], az = a[2], aw = a[3], | |
by = Math.sin(rad), bw = Math.cos(rad); | |
out[0] = ax * bw - az * by; | |
out[1] = ay * bw + aw * by; | |
out[2] = az * bw + ax * by; | |
out[3] = aw * bw - ay * by; | |
return out; | |
}; | |
/** | |
* Rotates a quaternion by the given angle about the Z axis | |
* | |
* @param {quat} out quat receiving operation result | |
* @param {quat} a quat to rotate | |
* @param {number} rad angle (in radians) to rotate | |
* @returns {quat} out | |
*/ | |
quat.rotateZ = function (out, a, rad) { | |
rad *= 0.5; | |
var ax = a[0], ay = a[1], az = a[2], aw = a[3], | |
bz = Math.sin(rad), bw = Math.cos(rad); | |
out[0] = ax * bw + ay * bz; | |
out[1] = ay * bw - ax * bz; | |
out[2] = az * bw + aw * bz; | |
out[3] = aw * bw - az * bz; | |
return out; | |
}; | |
/** | |
* Calculates the W component of a quat from the X, Y, and Z components. | |
* Assumes that quaternion is 1 unit in length. | |
* Any existing W component will be ignored. | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a quat to calculate W component of | |
* @returns {quat} out | |
*/ | |
quat.calculateW = function (out, a) { | |
var x = a[0], y = a[1], z = a[2]; | |
out[0] = x; | |
out[1] = y; | |
out[2] = z; | |
out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); | |
return out; | |
}; | |
/** | |
* Calculates the dot product of two quat's | |
* | |
* @param {quat} a the first operand | |
* @param {quat} b the second operand | |
* @returns {Number} dot product of a and b | |
* @function | |
*/ | |
quat.dot = vec4.dot; | |
/** | |
* Performs a linear interpolation between two quat's | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a the first operand | |
* @param {quat} b the second operand | |
* @param {Number} t interpolation amount between the two inputs | |
* @returns {quat} out | |
* @function | |
*/ | |
quat.lerp = vec4.lerp; | |
/** | |
* Performs a spherical linear interpolation between two quat | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a the first operand | |
* @param {quat} b the second operand | |
* @param {Number} t interpolation amount between the two inputs | |
* @returns {quat} out | |
*/ | |
quat.slerp = function (out, a, b, t) { | |
// benchmarks: | |
// http://jsperf.com/quaternion-slerp-implementations | |
var ax = a[0], ay = a[1], az = a[2], aw = a[3], | |
bx = b[0], by = b[1], bz = b[2], bw = b[3]; | |
var omega, cosom, sinom, scale0, scale1; | |
// calc cosine | |
cosom = ax * bx + ay * by + az * bz + aw * bw; | |
// adjust signs (if necessary) | |
if ( cosom < 0.0 ) { | |
cosom = -cosom; | |
bx = - bx; | |
by = - by; | |
bz = - bz; | |
bw = - bw; | |
} | |
// calculate coefficients | |
if ( (1.0 - cosom) > 0.000001 ) { | |
// standard case (slerp) | |
omega = Math.acos(cosom); | |
sinom = Math.sin(omega); | |
scale0 = Math.sin((1.0 - t) * omega) / sinom; | |
scale1 = Math.sin(t * omega) / sinom; | |
} else { | |
// "from" and "to" quaternions are very close | |
// ... so we can do a linear interpolation | |
scale0 = 1.0 - t; | |
scale1 = t; | |
} | |
// calculate final values | |
out[0] = scale0 * ax + scale1 * bx; | |
out[1] = scale0 * ay + scale1 * by; | |
out[2] = scale0 * az + scale1 * bz; | |
out[3] = scale0 * aw + scale1 * bw; | |
return out; | |
}; | |
/** | |
* Performs a spherical linear interpolation with two control points | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a the first operand | |
* @param {quat} b the second operand | |
* @param {quat} c the third operand | |
* @param {quat} d the fourth operand | |
* @param {Number} t interpolation amount | |
* @returns {quat} out | |
*/ | |
quat.sqlerp = (function () { | |
var temp1 = quat.create(); | |
var temp2 = quat.create(); | |
return function (out, a, b, c, d, t) { | |
quat.slerp(temp1, a, d, t); | |
quat.slerp(temp2, b, c, t); | |
quat.slerp(out, temp1, temp2, 2 * t * (1 - t)); | |
return out; | |
}; | |
}()); | |
/** | |
* Calculates the inverse of a quat | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a quat to calculate inverse of | |
* @returns {quat} out | |
*/ | |
quat.invert = function(out, a) { | |
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], | |
dot = a0*a0 + a1*a1 + a2*a2 + a3*a3, | |
invDot = dot ? 1.0/dot : 0; | |
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 | |
out[0] = -a0*invDot; | |
out[1] = -a1*invDot; | |
out[2] = -a2*invDot; | |
out[3] = a3*invDot; | |
return out; | |
}; | |
/** | |
* Calculates the conjugate of a quat | |
* If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a quat to calculate conjugate of | |
* @returns {quat} out | |
*/ | |
quat.conjugate = function (out, a) { | |
out[0] = -a[0]; | |
out[1] = -a[1]; | |
out[2] = -a[2]; | |
out[3] = a[3]; | |
return out; | |
}; | |
/** | |
* Calculates the length of a quat | |
* | |
* @param {quat} a vector to calculate length of | |
* @returns {Number} length of a | |
* @function | |
*/ | |
quat.length = vec4.length; | |
/** | |
* Alias for {@link quat.length} | |
* @function | |
*/ | |
quat.len = quat.length; | |
/** | |
* Calculates the squared length of a quat | |
* | |
* @param {quat} a vector to calculate squared length of | |
* @returns {Number} squared length of a | |
* @function | |
*/ | |
quat.squaredLength = vec4.squaredLength; | |
/** | |
* Alias for {@link quat.squaredLength} | |
* @function | |
*/ | |
quat.sqrLen = quat.squaredLength; | |
/** | |
* Normalize a quat | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {quat} a quaternion to normalize | |
* @returns {quat} out | |
* @function | |
*/ | |
quat.normalize = vec4.normalize; | |
/** | |
* Creates a quaternion from the given 3x3 rotation matrix. | |
* | |
* NOTE: The resultant quaternion is not normalized, so you should be sure | |
* to renormalize the quaternion yourself where necessary. | |
* | |
* @param {quat} out the receiving quaternion | |
* @param {mat3} m rotation matrix | |
* @returns {quat} out | |
* @function | |
*/ | |
quat.fromMat3 = function(out, m) { | |
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes | |
// article "Quaternion Calculus and Fast Animation". | |
var fTrace = m[0] + m[4] + m[8]; | |
var fRoot; | |
if ( fTrace > 0.0 ) { | |
// |w| > 1/2, may as well choose w > 1/2 | |
fRoot = Math.sqrt(fTrace + 1.0); // 2w | |
out[3] = 0.5 * fRoot; | |
fRoot = 0.5/fRoot; // 1/(4w) | |
out[0] = (m[5]-m[7])*fRoot; | |
out[1] = (m[6]-m[2])*fRoot; | |
out[2] = (m[1]-m[3])*fRoot; | |
} else { | |
// |w| <= 1/2 | |
var i = 0; | |
if ( m[4] > m[0] ) | |
i = 1; | |
if ( m[8] > m[i*3+i] ) | |
i = 2; | |
var j = (i+1)%3; | |
var k = (i+2)%3; | |
fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0); | |
out[i] = 0.5 * fRoot; | |
fRoot = 0.5 / fRoot; | |
out[3] = (m[j*3+k] - m[k*3+j]) * fRoot; | |
out[j] = (m[j*3+i] + m[i*3+j]) * fRoot; | |
out[k] = (m[k*3+i] + m[i*3+k]) * fRoot; | |
} | |
return out; | |
}; | |
/** | |
* Returns a string representation of a quatenion | |
* | |
* @param {quat} vec vector to represent as a string | |
* @returns {String} string representation of the vector | |
*/ | |
quat.str = function (a) { | |
return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; | |
}; | |
/** | |
* Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===) | |
* | |
* @param {quat} a The first quaternion. | |
* @param {quat} b The second quaternion. | |
* @returns {Boolean} True if the vectors are equal, false otherwise. | |
*/ | |
quat.exactEquals = vec4.exactEquals; | |
/** | |
* Returns whether or not the quaternions have approximately the same elements in the same position. | |
* | |
* @param {quat} a The first vector. | |
* @param {quat} b The second vector. | |
* @returns {Boolean} True if the vectors are equal, false otherwise. | |
*/ | |
quat.equals = vec4.equals; | |
module.exports = quat; | |
},{"./common.js":136,"./mat3.js":139,"./vec3.js":143,"./vec4.js":144}],142:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
/** | |
* @class 2 Dimensional Vector | |
* @name vec2 | |
*/ | |
var vec2 = {}; | |
/** | |
* Creates a new, empty vec2 | |
* | |
* @returns {vec2} a new 2D vector | |
*/ | |
vec2.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(2); | |
out[0] = 0; | |
out[1] = 0; | |
return out; | |
}; | |
/** | |
* Creates a new vec2 initialized with values from an existing vector | |
* | |
* @param {vec2} a vector to clone | |
* @returns {vec2} a new 2D vector | |
*/ | |
vec2.clone = function(a) { | |
var out = new glMatrix.ARRAY_TYPE(2); | |
out[0] = a[0]; | |
out[1] = a[1]; | |
return out; | |
}; | |
/** | |
* Creates a new vec2 initialized with the given values | |
* | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @returns {vec2} a new 2D vector | |
*/ | |
vec2.fromValues = function(x, y) { | |
var out = new glMatrix.ARRAY_TYPE(2); | |
out[0] = x; | |
out[1] = y; | |
return out; | |
}; | |
/** | |
* Copy the values from one vec2 to another | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the source vector | |
* @returns {vec2} out | |
*/ | |
vec2.copy = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
return out; | |
}; | |
/** | |
* Set the components of a vec2 to the given values | |
* | |
* @param {vec2} out the receiving vector | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @returns {vec2} out | |
*/ | |
vec2.set = function(out, x, y) { | |
out[0] = x; | |
out[1] = y; | |
return out; | |
}; | |
/** | |
* Adds two vec2's | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {vec2} out | |
*/ | |
vec2.add = function(out, a, b) { | |
out[0] = a[0] + b[0]; | |
out[1] = a[1] + b[1]; | |
return out; | |
}; | |
/** | |
* Subtracts vector b from vector a | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {vec2} out | |
*/ | |
vec2.subtract = function(out, a, b) { | |
out[0] = a[0] - b[0]; | |
out[1] = a[1] - b[1]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec2.subtract} | |
* @function | |
*/ | |
vec2.sub = vec2.subtract; | |
/** | |
* Multiplies two vec2's | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {vec2} out | |
*/ | |
vec2.multiply = function(out, a, b) { | |
out[0] = a[0] * b[0]; | |
out[1] = a[1] * b[1]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec2.multiply} | |
* @function | |
*/ | |
vec2.mul = vec2.multiply; | |
/** | |
* Divides two vec2's | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {vec2} out | |
*/ | |
vec2.divide = function(out, a, b) { | |
out[0] = a[0] / b[0]; | |
out[1] = a[1] / b[1]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec2.divide} | |
* @function | |
*/ | |
vec2.div = vec2.divide; | |
/** | |
* Math.ceil the components of a vec2 | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a vector to ceil | |
* @returns {vec2} out | |
*/ | |
vec2.ceil = function (out, a) { | |
out[0] = Math.ceil(a[0]); | |
out[1] = Math.ceil(a[1]); | |
return out; | |
}; | |
/** | |
* Math.floor the components of a vec2 | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a vector to floor | |
* @returns {vec2} out | |
*/ | |
vec2.floor = function (out, a) { | |
out[0] = Math.floor(a[0]); | |
out[1] = Math.floor(a[1]); | |
return out; | |
}; | |
/** | |
* Returns the minimum of two vec2's | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {vec2} out | |
*/ | |
vec2.min = function(out, a, b) { | |
out[0] = Math.min(a[0], b[0]); | |
out[1] = Math.min(a[1], b[1]); | |
return out; | |
}; | |
/** | |
* Returns the maximum of two vec2's | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {vec2} out | |
*/ | |
vec2.max = function(out, a, b) { | |
out[0] = Math.max(a[0], b[0]); | |
out[1] = Math.max(a[1], b[1]); | |
return out; | |
}; | |
/** | |
* Math.round the components of a vec2 | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a vector to round | |
* @returns {vec2} out | |
*/ | |
vec2.round = function (out, a) { | |
out[0] = Math.round(a[0]); | |
out[1] = Math.round(a[1]); | |
return out; | |
}; | |
/** | |
* Scales a vec2 by a scalar number | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the vector to scale | |
* @param {Number} b amount to scale the vector by | |
* @returns {vec2} out | |
*/ | |
vec2.scale = function(out, a, b) { | |
out[0] = a[0] * b; | |
out[1] = a[1] * b; | |
return out; | |
}; | |
/** | |
* Adds two vec2's after scaling the second operand by a scalar value | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @param {Number} scale the amount to scale b by before adding | |
* @returns {vec2} out | |
*/ | |
vec2.scaleAndAdd = function(out, a, b, scale) { | |
out[0] = a[0] + (b[0] * scale); | |
out[1] = a[1] + (b[1] * scale); | |
return out; | |
}; | |
/** | |
* Calculates the euclidian distance between two vec2's | |
* | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {Number} distance between a and b | |
*/ | |
vec2.distance = function(a, b) { | |
var x = b[0] - a[0], | |
y = b[1] - a[1]; | |
return Math.sqrt(x*x + y*y); | |
}; | |
/** | |
* Alias for {@link vec2.distance} | |
* @function | |
*/ | |
vec2.dist = vec2.distance; | |
/** | |
* Calculates the squared euclidian distance between two vec2's | |
* | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {Number} squared distance between a and b | |
*/ | |
vec2.squaredDistance = function(a, b) { | |
var x = b[0] - a[0], | |
y = b[1] - a[1]; | |
return x*x + y*y; | |
}; | |
/** | |
* Alias for {@link vec2.squaredDistance} | |
* @function | |
*/ | |
vec2.sqrDist = vec2.squaredDistance; | |
/** | |
* Calculates the length of a vec2 | |
* | |
* @param {vec2} a vector to calculate length of | |
* @returns {Number} length of a | |
*/ | |
vec2.length = function (a) { | |
var x = a[0], | |
y = a[1]; | |
return Math.sqrt(x*x + y*y); | |
}; | |
/** | |
* Alias for {@link vec2.length} | |
* @function | |
*/ | |
vec2.len = vec2.length; | |
/** | |
* Calculates the squared length of a vec2 | |
* | |
* @param {vec2} a vector to calculate squared length of | |
* @returns {Number} squared length of a | |
*/ | |
vec2.squaredLength = function (a) { | |
var x = a[0], | |
y = a[1]; | |
return x*x + y*y; | |
}; | |
/** | |
* Alias for {@link vec2.squaredLength} | |
* @function | |
*/ | |
vec2.sqrLen = vec2.squaredLength; | |
/** | |
* Negates the components of a vec2 | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a vector to negate | |
* @returns {vec2} out | |
*/ | |
vec2.negate = function(out, a) { | |
out[0] = -a[0]; | |
out[1] = -a[1]; | |
return out; | |
}; | |
/** | |
* Returns the inverse of the components of a vec2 | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a vector to invert | |
* @returns {vec2} out | |
*/ | |
vec2.inverse = function(out, a) { | |
out[0] = 1.0 / a[0]; | |
out[1] = 1.0 / a[1]; | |
return out; | |
}; | |
/** | |
* Normalize a vec2 | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a vector to normalize | |
* @returns {vec2} out | |
*/ | |
vec2.normalize = function(out, a) { | |
var x = a[0], | |
y = a[1]; | |
var len = x*x + y*y; | |
if (len > 0) { | |
//TODO: evaluate use of glm_invsqrt here? | |
len = 1 / Math.sqrt(len); | |
out[0] = a[0] * len; | |
out[1] = a[1] * len; | |
} | |
return out; | |
}; | |
/** | |
* Calculates the dot product of two vec2's | |
* | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {Number} dot product of a and b | |
*/ | |
vec2.dot = function (a, b) { | |
return a[0] * b[0] + a[1] * b[1]; | |
}; | |
/** | |
* Computes the cross product of two vec2's | |
* Note that the cross product must by definition produce a 3D vector | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec2.cross = function(out, a, b) { | |
var z = a[0] * b[1] - a[1] * b[0]; | |
out[0] = out[1] = 0; | |
out[2] = z; | |
return out; | |
}; | |
/** | |
* Performs a linear interpolation between two vec2's | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the first operand | |
* @param {vec2} b the second operand | |
* @param {Number} t interpolation amount between the two inputs | |
* @returns {vec2} out | |
*/ | |
vec2.lerp = function (out, a, b, t) { | |
var ax = a[0], | |
ay = a[1]; | |
out[0] = ax + t * (b[0] - ax); | |
out[1] = ay + t * (b[1] - ay); | |
return out; | |
}; | |
/** | |
* Generates a random vector with the given scale | |
* | |
* @param {vec2} out the receiving vector | |
* @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned | |
* @returns {vec2} out | |
*/ | |
vec2.random = function (out, scale) { | |
scale = scale || 1.0; | |
var r = glMatrix.RANDOM() * 2.0 * Math.PI; | |
out[0] = Math.cos(r) * scale; | |
out[1] = Math.sin(r) * scale; | |
return out; | |
}; | |
/** | |
* Transforms the vec2 with a mat2 | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the vector to transform | |
* @param {mat2} m matrix to transform with | |
* @returns {vec2} out | |
*/ | |
vec2.transformMat2 = function(out, a, m) { | |
var x = a[0], | |
y = a[1]; | |
out[0] = m[0] * x + m[2] * y; | |
out[1] = m[1] * x + m[3] * y; | |
return out; | |
}; | |
/** | |
* Transforms the vec2 with a mat2d | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the vector to transform | |
* @param {mat2d} m matrix to transform with | |
* @returns {vec2} out | |
*/ | |
vec2.transformMat2d = function(out, a, m) { | |
var x = a[0], | |
y = a[1]; | |
out[0] = m[0] * x + m[2] * y + m[4]; | |
out[1] = m[1] * x + m[3] * y + m[5]; | |
return out; | |
}; | |
/** | |
* Transforms the vec2 with a mat3 | |
* 3rd vector component is implicitly '1' | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the vector to transform | |
* @param {mat3} m matrix to transform with | |
* @returns {vec2} out | |
*/ | |
vec2.transformMat3 = function(out, a, m) { | |
var x = a[0], | |
y = a[1]; | |
out[0] = m[0] * x + m[3] * y + m[6]; | |
out[1] = m[1] * x + m[4] * y + m[7]; | |
return out; | |
}; | |
/** | |
* Transforms the vec2 with a mat4 | |
* 3rd vector component is implicitly '0' | |
* 4th vector component is implicitly '1' | |
* | |
* @param {vec2} out the receiving vector | |
* @param {vec2} a the vector to transform | |
* @param {mat4} m matrix to transform with | |
* @returns {vec2} out | |
*/ | |
vec2.transformMat4 = function(out, a, m) { | |
var x = a[0], | |
y = a[1]; | |
out[0] = m[0] * x + m[4] * y + m[12]; | |
out[1] = m[1] * x + m[5] * y + m[13]; | |
return out; | |
}; | |
/** | |
* Perform some operation over an array of vec2s. | |
* | |
* @param {Array} a the array of vectors to iterate over | |
* @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed | |
* @param {Number} offset Number of elements to skip at the beginning of the array | |
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array | |
* @param {Function} fn Function to call for each vector in the array | |
* @param {Object} [arg] additional argument to pass to fn | |
* @returns {Array} a | |
* @function | |
*/ | |
vec2.forEach = (function() { | |
var vec = vec2.create(); | |
return function(a, stride, offset, count, fn, arg) { | |
var i, l; | |
if(!stride) { | |
stride = 2; | |
} | |
if(!offset) { | |
offset = 0; | |
} | |
if(count) { | |
l = Math.min((count * stride) + offset, a.length); | |
} else { | |
l = a.length; | |
} | |
for(i = offset; i < l; i += stride) { | |
vec[0] = a[i]; vec[1] = a[i+1]; | |
fn(vec, vec, arg); | |
a[i] = vec[0]; a[i+1] = vec[1]; | |
} | |
return a; | |
}; | |
})(); | |
/** | |
* Returns a string representation of a vector | |
* | |
* @param {vec2} vec vector to represent as a string | |
* @returns {String} string representation of the vector | |
*/ | |
vec2.str = function (a) { | |
return 'vec2(' + a[0] + ', ' + a[1] + ')'; | |
}; | |
/** | |
* Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===) | |
* | |
* @param {vec2} a The first vector. | |
* @param {vec2} b The second vector. | |
* @returns {Boolean} True if the vectors are equal, false otherwise. | |
*/ | |
vec2.exactEquals = function (a, b) { | |
return a[0] === b[0] && a[1] === b[1]; | |
}; | |
/** | |
* Returns whether or not the vectors have approximately the same elements in the same position. | |
* | |
* @param {vec2} a The first vector. | |
* @param {vec2} b The second vector. | |
* @returns {Boolean} True if the vectors are equal, false otherwise. | |
*/ | |
vec2.equals = function (a, b) { | |
var a0 = a[0], a1 = a[1]; | |
var b0 = b[0], b1 = b[1]; | |
return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | |
Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1))); | |
}; | |
module.exports = vec2; | |
},{"./common.js":136}],143:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
/** | |
* @class 3 Dimensional Vector | |
* @name vec3 | |
*/ | |
var vec3 = {}; | |
/** | |
* Creates a new, empty vec3 | |
* | |
* @returns {vec3} a new 3D vector | |
*/ | |
vec3.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(3); | |
out[0] = 0; | |
out[1] = 0; | |
out[2] = 0; | |
return out; | |
}; | |
/** | |
* Creates a new vec3 initialized with values from an existing vector | |
* | |
* @param {vec3} a vector to clone | |
* @returns {vec3} a new 3D vector | |
*/ | |
vec3.clone = function(a) { | |
var out = new glMatrix.ARRAY_TYPE(3); | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
return out; | |
}; | |
/** | |
* Creates a new vec3 initialized with the given values | |
* | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @param {Number} z Z component | |
* @returns {vec3} a new 3D vector | |
*/ | |
vec3.fromValues = function(x, y, z) { | |
var out = new glMatrix.ARRAY_TYPE(3); | |
out[0] = x; | |
out[1] = y; | |
out[2] = z; | |
return out; | |
}; | |
/** | |
* Copy the values from one vec3 to another | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the source vector | |
* @returns {vec3} out | |
*/ | |
vec3.copy = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
return out; | |
}; | |
/** | |
* Set the components of a vec3 to the given values | |
* | |
* @param {vec3} out the receiving vector | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @param {Number} z Z component | |
* @returns {vec3} out | |
*/ | |
vec3.set = function(out, x, y, z) { | |
out[0] = x; | |
out[1] = y; | |
out[2] = z; | |
return out; | |
}; | |
/** | |
* Adds two vec3's | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec3.add = function(out, a, b) { | |
out[0] = a[0] + b[0]; | |
out[1] = a[1] + b[1]; | |
out[2] = a[2] + b[2]; | |
return out; | |
}; | |
/** | |
* Subtracts vector b from vector a | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec3.subtract = function(out, a, b) { | |
out[0] = a[0] - b[0]; | |
out[1] = a[1] - b[1]; | |
out[2] = a[2] - b[2]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec3.subtract} | |
* @function | |
*/ | |
vec3.sub = vec3.subtract; | |
/** | |
* Multiplies two vec3's | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec3.multiply = function(out, a, b) { | |
out[0] = a[0] * b[0]; | |
out[1] = a[1] * b[1]; | |
out[2] = a[2] * b[2]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec3.multiply} | |
* @function | |
*/ | |
vec3.mul = vec3.multiply; | |
/** | |
* Divides two vec3's | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec3.divide = function(out, a, b) { | |
out[0] = a[0] / b[0]; | |
out[1] = a[1] / b[1]; | |
out[2] = a[2] / b[2]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec3.divide} | |
* @function | |
*/ | |
vec3.div = vec3.divide; | |
/** | |
* Math.ceil the components of a vec3 | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a vector to ceil | |
* @returns {vec3} out | |
*/ | |
vec3.ceil = function (out, a) { | |
out[0] = Math.ceil(a[0]); | |
out[1] = Math.ceil(a[1]); | |
out[2] = Math.ceil(a[2]); | |
return out; | |
}; | |
/** | |
* Math.floor the components of a vec3 | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a vector to floor | |
* @returns {vec3} out | |
*/ | |
vec3.floor = function (out, a) { | |
out[0] = Math.floor(a[0]); | |
out[1] = Math.floor(a[1]); | |
out[2] = Math.floor(a[2]); | |
return out; | |
}; | |
/** | |
* Returns the minimum of two vec3's | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec3.min = function(out, a, b) { | |
out[0] = Math.min(a[0], b[0]); | |
out[1] = Math.min(a[1], b[1]); | |
out[2] = Math.min(a[2], b[2]); | |
return out; | |
}; | |
/** | |
* Returns the maximum of two vec3's | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec3.max = function(out, a, b) { | |
out[0] = Math.max(a[0], b[0]); | |
out[1] = Math.max(a[1], b[1]); | |
out[2] = Math.max(a[2], b[2]); | |
return out; | |
}; | |
/** | |
* Math.round the components of a vec3 | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a vector to round | |
* @returns {vec3} out | |
*/ | |
vec3.round = function (out, a) { | |
out[0] = Math.round(a[0]); | |
out[1] = Math.round(a[1]); | |
out[2] = Math.round(a[2]); | |
return out; | |
}; | |
/** | |
* Scales a vec3 by a scalar number | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the vector to scale | |
* @param {Number} b amount to scale the vector by | |
* @returns {vec3} out | |
*/ | |
vec3.scale = function(out, a, b) { | |
out[0] = a[0] * b; | |
out[1] = a[1] * b; | |
out[2] = a[2] * b; | |
return out; | |
}; | |
/** | |
* Adds two vec3's after scaling the second operand by a scalar value | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @param {Number} scale the amount to scale b by before adding | |
* @returns {vec3} out | |
*/ | |
vec3.scaleAndAdd = function(out, a, b, scale) { | |
out[0] = a[0] + (b[0] * scale); | |
out[1] = a[1] + (b[1] * scale); | |
out[2] = a[2] + (b[2] * scale); | |
return out; | |
}; | |
/** | |
* Calculates the euclidian distance between two vec3's | |
* | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {Number} distance between a and b | |
*/ | |
vec3.distance = function(a, b) { | |
var x = b[0] - a[0], | |
y = b[1] - a[1], | |
z = b[2] - a[2]; | |
return Math.sqrt(x*x + y*y + z*z); | |
}; | |
/** | |
* Alias for {@link vec3.distance} | |
* @function | |
*/ | |
vec3.dist = vec3.distance; | |
/** | |
* Calculates the squared euclidian distance between two vec3's | |
* | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {Number} squared distance between a and b | |
*/ | |
vec3.squaredDistance = function(a, b) { | |
var x = b[0] - a[0], | |
y = b[1] - a[1], | |
z = b[2] - a[2]; | |
return x*x + y*y + z*z; | |
}; | |
/** | |
* Alias for {@link vec3.squaredDistance} | |
* @function | |
*/ | |
vec3.sqrDist = vec3.squaredDistance; | |
/** | |
* Calculates the length of a vec3 | |
* | |
* @param {vec3} a vector to calculate length of | |
* @returns {Number} length of a | |
*/ | |
vec3.length = function (a) { | |
var x = a[0], | |
y = a[1], | |
z = a[2]; | |
return Math.sqrt(x*x + y*y + z*z); | |
}; | |
/** | |
* Alias for {@link vec3.length} | |
* @function | |
*/ | |
vec3.len = vec3.length; | |
/** | |
* Calculates the squared length of a vec3 | |
* | |
* @param {vec3} a vector to calculate squared length of | |
* @returns {Number} squared length of a | |
*/ | |
vec3.squaredLength = function (a) { | |
var x = a[0], | |
y = a[1], | |
z = a[2]; | |
return x*x + y*y + z*z; | |
}; | |
/** | |
* Alias for {@link vec3.squaredLength} | |
* @function | |
*/ | |
vec3.sqrLen = vec3.squaredLength; | |
/** | |
* Negates the components of a vec3 | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a vector to negate | |
* @returns {vec3} out | |
*/ | |
vec3.negate = function(out, a) { | |
out[0] = -a[0]; | |
out[1] = -a[1]; | |
out[2] = -a[2]; | |
return out; | |
}; | |
/** | |
* Returns the inverse of the components of a vec3 | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a vector to invert | |
* @returns {vec3} out | |
*/ | |
vec3.inverse = function(out, a) { | |
out[0] = 1.0 / a[0]; | |
out[1] = 1.0 / a[1]; | |
out[2] = 1.0 / a[2]; | |
return out; | |
}; | |
/** | |
* Normalize a vec3 | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a vector to normalize | |
* @returns {vec3} out | |
*/ | |
vec3.normalize = function(out, a) { | |
var x = a[0], | |
y = a[1], | |
z = a[2]; | |
var len = x*x + y*y + z*z; | |
if (len > 0) { | |
//TODO: evaluate use of glm_invsqrt here? | |
len = 1 / Math.sqrt(len); | |
out[0] = a[0] * len; | |
out[1] = a[1] * len; | |
out[2] = a[2] * len; | |
} | |
return out; | |
}; | |
/** | |
* Calculates the dot product of two vec3's | |
* | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {Number} dot product of a and b | |
*/ | |
vec3.dot = function (a, b) { | |
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; | |
}; | |
/** | |
* Computes the cross product of two vec3's | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @returns {vec3} out | |
*/ | |
vec3.cross = function(out, a, b) { | |
var ax = a[0], ay = a[1], az = a[2], | |
bx = b[0], by = b[1], bz = b[2]; | |
out[0] = ay * bz - az * by; | |
out[1] = az * bx - ax * bz; | |
out[2] = ax * by - ay * bx; | |
return out; | |
}; | |
/** | |
* Performs a linear interpolation between two vec3's | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @param {Number} t interpolation amount between the two inputs | |
* @returns {vec3} out | |
*/ | |
vec3.lerp = function (out, a, b, t) { | |
var ax = a[0], | |
ay = a[1], | |
az = a[2]; | |
out[0] = ax + t * (b[0] - ax); | |
out[1] = ay + t * (b[1] - ay); | |
out[2] = az + t * (b[2] - az); | |
return out; | |
}; | |
/** | |
* Performs a hermite interpolation with two control points | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @param {vec3} c the third operand | |
* @param {vec3} d the fourth operand | |
* @param {Number} t interpolation amount between the two inputs | |
* @returns {vec3} out | |
*/ | |
vec3.hermite = function (out, a, b, c, d, t) { | |
var factorTimes2 = t * t, | |
factor1 = factorTimes2 * (2 * t - 3) + 1, | |
factor2 = factorTimes2 * (t - 2) + t, | |
factor3 = factorTimes2 * (t - 1), | |
factor4 = factorTimes2 * (3 - 2 * t); | |
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; | |
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; | |
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; | |
return out; | |
}; | |
/** | |
* Performs a bezier interpolation with two control points | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the first operand | |
* @param {vec3} b the second operand | |
* @param {vec3} c the third operand | |
* @param {vec3} d the fourth operand | |
* @param {Number} t interpolation amount between the two inputs | |
* @returns {vec3} out | |
*/ | |
vec3.bezier = function (out, a, b, c, d, t) { | |
var inverseFactor = 1 - t, | |
inverseFactorTimesTwo = inverseFactor * inverseFactor, | |
factorTimes2 = t * t, | |
factor1 = inverseFactorTimesTwo * inverseFactor, | |
factor2 = 3 * t * inverseFactorTimesTwo, | |
factor3 = 3 * factorTimes2 * inverseFactor, | |
factor4 = factorTimes2 * t; | |
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; | |
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; | |
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; | |
return out; | |
}; | |
/** | |
* Generates a random vector with the given scale | |
* | |
* @param {vec3} out the receiving vector | |
* @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned | |
* @returns {vec3} out | |
*/ | |
vec3.random = function (out, scale) { | |
scale = scale || 1.0; | |
var r = glMatrix.RANDOM() * 2.0 * Math.PI; | |
var z = (glMatrix.RANDOM() * 2.0) - 1.0; | |
var zScale = Math.sqrt(1.0-z*z) * scale; | |
out[0] = Math.cos(r) * zScale; | |
out[1] = Math.sin(r) * zScale; | |
out[2] = z * scale; | |
return out; | |
}; | |
/** | |
* Transforms the vec3 with a mat4. | |
* 4th vector component is implicitly '1' | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the vector to transform | |
* @param {mat4} m matrix to transform with | |
* @returns {vec3} out | |
*/ | |
vec3.transformMat4 = function(out, a, m) { | |
var x = a[0], y = a[1], z = a[2], | |
w = m[3] * x + m[7] * y + m[11] * z + m[15]; | |
w = w || 1.0; | |
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; | |
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; | |
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w; | |
return out; | |
}; | |
/** | |
* Transforms the vec3 with a mat3. | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the vector to transform | |
* @param {mat4} m the 3x3 matrix to transform with | |
* @returns {vec3} out | |
*/ | |
vec3.transformMat3 = function(out, a, m) { | |
var x = a[0], y = a[1], z = a[2]; | |
out[0] = x * m[0] + y * m[3] + z * m[6]; | |
out[1] = x * m[1] + y * m[4] + z * m[7]; | |
out[2] = x * m[2] + y * m[5] + z * m[8]; | |
return out; | |
}; | |
/** | |
* Transforms the vec3 with a quat | |
* | |
* @param {vec3} out the receiving vector | |
* @param {vec3} a the vector to transform | |
* @param {quat} q quaternion to transform with | |
* @returns {vec3} out | |
*/ | |
vec3.transformQuat = function(out, a, q) { | |
// benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations | |
var x = a[0], y = a[1], z = a[2], | |
qx = q[0], qy = q[1], qz = q[2], qw = q[3], | |
// calculate quat * vec | |
ix = qw * x + qy * z - qz * y, | |
iy = qw * y + qz * x - qx * z, | |
iz = qw * z + qx * y - qy * x, | |
iw = -qx * x - qy * y - qz * z; | |
// calculate result * inverse quat | |
out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; | |
out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; | |
out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; | |
return out; | |
}; | |
/** | |
* Rotate a 3D vector around the x-axis | |
* @param {vec3} out The receiving vec3 | |
* @param {vec3} a The vec3 point to rotate | |
* @param {vec3} b The origin of the rotation | |
* @param {Number} c The angle of rotation | |
* @returns {vec3} out | |
*/ | |
vec3.rotateX = function(out, a, b, c){ | |
var p = [], r=[]; | |
//Translate point to the origin | |
p[0] = a[0] - b[0]; | |
p[1] = a[1] - b[1]; | |
p[2] = a[2] - b[2]; | |
//perform rotation | |
r[0] = p[0]; | |
r[1] = p[1]*Math.cos(c) - p[2]*Math.sin(c); | |
r[2] = p[1]*Math.sin(c) + p[2]*Math.cos(c); | |
//translate to correct position | |
out[0] = r[0] + b[0]; | |
out[1] = r[1] + b[1]; | |
out[2] = r[2] + b[2]; | |
return out; | |
}; | |
/** | |
* Rotate a 3D vector around the y-axis | |
* @param {vec3} out The receiving vec3 | |
* @param {vec3} a The vec3 point to rotate | |
* @param {vec3} b The origin of the rotation | |
* @param {Number} c The angle of rotation | |
* @returns {vec3} out | |
*/ | |
vec3.rotateY = function(out, a, b, c){ | |
var p = [], r=[]; | |
//Translate point to the origin | |
p[0] = a[0] - b[0]; | |
p[1] = a[1] - b[1]; | |
p[2] = a[2] - b[2]; | |
//perform rotation | |
r[0] = p[2]*Math.sin(c) + p[0]*Math.cos(c); | |
r[1] = p[1]; | |
r[2] = p[2]*Math.cos(c) - p[0]*Math.sin(c); | |
//translate to correct position | |
out[0] = r[0] + b[0]; | |
out[1] = r[1] + b[1]; | |
out[2] = r[2] + b[2]; | |
return out; | |
}; | |
/** | |
* Rotate a 3D vector around the z-axis | |
* @param {vec3} out The receiving vec3 | |
* @param {vec3} a The vec3 point to rotate | |
* @param {vec3} b The origin of the rotation | |
* @param {Number} c The angle of rotation | |
* @returns {vec3} out | |
*/ | |
vec3.rotateZ = function(out, a, b, c){ | |
var p = [], r=[]; | |
//Translate point to the origin | |
p[0] = a[0] - b[0]; | |
p[1] = a[1] - b[1]; | |
p[2] = a[2] - b[2]; | |
//perform rotation | |
r[0] = p[0]*Math.cos(c) - p[1]*Math.sin(c); | |
r[1] = p[0]*Math.sin(c) + p[1]*Math.cos(c); | |
r[2] = p[2]; | |
//translate to correct position | |
out[0] = r[0] + b[0]; | |
out[1] = r[1] + b[1]; | |
out[2] = r[2] + b[2]; | |
return out; | |
}; | |
/** | |
* Perform some operation over an array of vec3s. | |
* | |
* @param {Array} a the array of vectors to iterate over | |
* @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed | |
* @param {Number} offset Number of elements to skip at the beginning of the array | |
* @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array | |
* @param {Function} fn Function to call for each vector in the array | |
* @param {Object} [arg] additional argument to pass to fn | |
* @returns {Array} a | |
* @function | |
*/ | |
vec3.forEach = (function() { | |
var vec = vec3.create(); | |
return function(a, stride, offset, count, fn, arg) { | |
var i, l; | |
if(!stride) { | |
stride = 3; | |
} | |
if(!offset) { | |
offset = 0; | |
} | |
if(count) { | |
l = Math.min((count * stride) + offset, a.length); | |
} else { | |
l = a.length; | |
} | |
for(i = offset; i < l; i += stride) { | |
vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; | |
fn(vec, vec, arg); | |
a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; | |
} | |
return a; | |
}; | |
})(); | |
/** | |
* Get the angle between two 3D vectors | |
* @param {vec3} a The first operand | |
* @param {vec3} b The second operand | |
* @returns {Number} The angle in radians | |
*/ | |
vec3.angle = function(a, b) { | |
var tempA = vec3.fromValues(a[0], a[1], a[2]); | |
var tempB = vec3.fromValues(b[0], b[1], b[2]); | |
vec3.normalize(tempA, tempA); | |
vec3.normalize(tempB, tempB); | |
var cosine = vec3.dot(tempA, tempB); | |
if(cosine > 1.0){ | |
return 0; | |
} else { | |
return Math.acos(cosine); | |
} | |
}; | |
/** | |
* Returns a string representation of a vector | |
* | |
* @param {vec3} vec vector to represent as a string | |
* @returns {String} string representation of the vector | |
*/ | |
vec3.str = function (a) { | |
return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')'; | |
}; | |
/** | |
* Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) | |
* | |
* @param {vec3} a The first vector. | |
* @param {vec3} b The second vector. | |
* @returns {Boolean} True if the vectors are equal, false otherwise. | |
*/ | |
vec3.exactEquals = function (a, b) { | |
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; | |
}; | |
/** | |
* Returns whether or not the vectors have approximately the same elements in the same position. | |
* | |
* @param {vec3} a The first vector. | |
* @param {vec3} b The second vector. | |
* @returns {Boolean} True if the vectors are equal, false otherwise. | |
*/ | |
vec3.equals = function (a, b) { | |
var a0 = a[0], a1 = a[1], a2 = a[2]; | |
var b0 = b[0], b1 = b[1], b2 = b[2]; | |
return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | |
Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | |
Math.abs(a2 - b2) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2))); | |
}; | |
module.exports = vec3; | |
},{"./common.js":136}],144:[function(require,module,exports){ | |
/* Copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. */ | |
var glMatrix = require("./common.js"); | |
/** | |
* @class 4 Dimensional Vector | |
* @name vec4 | |
*/ | |
var vec4 = {}; | |
/** | |
* Creates a new, empty vec4 | |
* | |
* @returns {vec4} a new 4D vector | |
*/ | |
vec4.create = function() { | |
var out = new glMatrix.ARRAY_TYPE(4); | |
out[0] = 0; | |
out[1] = 0; | |
out[2] = 0; | |
out[3] = 0; | |
return out; | |
}; | |
/** | |
* Creates a new vec4 initialized with values from an existing vector | |
* | |
* @param {vec4} a vector to clone | |
* @returns {vec4} a new 4D vector | |
*/ | |
vec4.clone = function(a) { | |
var out = new glMatrix.ARRAY_TYPE(4); | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
return out; | |
}; | |
/** | |
* Creates a new vec4 initialized with the given values | |
* | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @param {Number} z Z component | |
* @param {Number} w W component | |
* @returns {vec4} a new 4D vector | |
*/ | |
vec4.fromValues = function(x, y, z, w) { | |
var out = new glMatrix.ARRAY_TYPE(4); | |
out[0] = x; | |
out[1] = y; | |
out[2] = z; | |
out[3] = w; | |
return out; | |
}; | |
/** | |
* Copy the values from one vec4 to another | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the source vector | |
* @returns {vec4} out | |
*/ | |
vec4.copy = function(out, a) { | |
out[0] = a[0]; | |
out[1] = a[1]; | |
out[2] = a[2]; | |
out[3] = a[3]; | |
return out; | |
}; | |
/** | |
* Set the components of a vec4 to the given values | |
* | |
* @param {vec4} out the receiving vector | |
* @param {Number} x X component | |
* @param {Number} y Y component | |
* @param {Number} z Z component | |
* @param {Number} w W component | |
* @returns {vec4} out | |
*/ | |
vec4.set = function(out, x, y, z, w) { | |
out[0] = x; | |
out[1] = y; | |
out[2] = z; | |
out[3] = w; | |
return out; | |
}; | |
/** | |
* Adds two vec4's | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {vec4} out | |
*/ | |
vec4.add = function(out, a, b) { | |
out[0] = a[0] + b[0]; | |
out[1] = a[1] + b[1]; | |
out[2] = a[2] + b[2]; | |
out[3] = a[3] + b[3]; | |
return out; | |
}; | |
/** | |
* Subtracts vector b from vector a | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {vec4} out | |
*/ | |
vec4.subtract = function(out, a, b) { | |
out[0] = a[0] - b[0]; | |
out[1] = a[1] - b[1]; | |
out[2] = a[2] - b[2]; | |
out[3] = a[3] - b[3]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec4.subtract} | |
* @function | |
*/ | |
vec4.sub = vec4.subtract; | |
/** | |
* Multiplies two vec4's | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {vec4} out | |
*/ | |
vec4.multiply = function(out, a, b) { | |
out[0] = a[0] * b[0]; | |
out[1] = a[1] * b[1]; | |
out[2] = a[2] * b[2]; | |
out[3] = a[3] * b[3]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec4.multiply} | |
* @function | |
*/ | |
vec4.mul = vec4.multiply; | |
/** | |
* Divides two vec4's | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {vec4} out | |
*/ | |
vec4.divide = function(out, a, b) { | |
out[0] = a[0] / b[0]; | |
out[1] = a[1] / b[1]; | |
out[2] = a[2] / b[2]; | |
out[3] = a[3] / b[3]; | |
return out; | |
}; | |
/** | |
* Alias for {@link vec4.divide} | |
* @function | |
*/ | |
vec4.div = vec4.divide; | |
/** | |
* Math.ceil the components of a vec4 | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a vector to ceil | |
* @returns {vec4} out | |
*/ | |
vec4.ceil = function (out, a) { | |
out[0] = Math.ceil(a[0]); | |
out[1] = Math.ceil(a[1]); | |
out[2] = Math.ceil(a[2]); | |
out[3] = Math.ceil(a[3]); | |
return out; | |
}; | |
/** | |
* Math.floor the components of a vec4 | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a vector to floor | |
* @returns {vec4} out | |
*/ | |
vec4.floor = function (out, a) { | |
out[0] = Math.floor(a[0]); | |
out[1] = Math.floor(a[1]); | |
out[2] = Math.floor(a[2]); | |
out[3] = Math.floor(a[3]); | |
return out; | |
}; | |
/** | |
* Returns the minimum of two vec4's | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {vec4} out | |
*/ | |
vec4.min = function(out, a, b) { | |
out[0] = Math.min(a[0], b[0]); | |
out[1] = Math.min(a[1], b[1]); | |
out[2] = Math.min(a[2], b[2]); | |
out[3] = Math.min(a[3], b[3]); | |
return out; | |
}; | |
/** | |
* Returns the maximum of two vec4's | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {vec4} out | |
*/ | |
vec4.max = function(out, a, b) { | |
out[0] = Math.max(a[0], b[0]); | |
out[1] = Math.max(a[1], b[1]); | |
out[2] = Math.max(a[2], b[2]); | |
out[3] = Math.max(a[3], b[3]); | |
return out; | |
}; | |
/** | |
* Math.round the components of a vec4 | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a vector to round | |
* @returns {vec4} out | |
*/ | |
vec4.round = function (out, a) { | |
out[0] = Math.round(a[0]); | |
out[1] = Math.round(a[1]); | |
out[2] = Math.round(a[2]); | |
out[3] = Math.round(a[3]); | |
return out; | |
}; | |
/** | |
* Scales a vec4 by a scalar number | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the vector to scale | |
* @param {Number} b amount to scale the vector by | |
* @returns {vec4} out | |
*/ | |
vec4.scale = function(out, a, b) { | |
out[0] = a[0] * b; | |
out[1] = a[1] * b; | |
out[2] = a[2] * b; | |
out[3] = a[3] * b; | |
return out; | |
}; | |
/** | |
* Adds two vec4's after scaling the second operand by a scalar value | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @param {Number} scale the amount to scale b by before adding | |
* @returns {vec4} out | |
*/ | |
vec4.scaleAndAdd = function(out, a, b, scale) { | |
out[0] = a[0] + (b[0] * scale); | |
out[1] = a[1] + (b[1] * scale); | |
out[2] = a[2] + (b[2] * scale); | |
out[3] = a[3] + (b[3] * scale); | |
return out; | |
}; | |
/** | |
* Calculates the euclidian distance between two vec4's | |
* | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {Number} distance between a and b | |
*/ | |
vec4.distance = function(a, b) { | |
var x = b[0] - a[0], | |
y = b[1] - a[1], | |
z = b[2] - a[2], | |
w = b[3] - a[3]; | |
return Math.sqrt(x*x + y*y + z*z + w*w); | |
}; | |
/** | |
* Alias for {@link vec4.distance} | |
* @function | |
*/ | |
vec4.dist = vec4.distance; | |
/** | |
* Calculates the squared euclidian distance between two vec4's | |
* | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {Number} squared distance between a and b | |
*/ | |
vec4.squaredDistance = function(a, b) { | |
var x = b[0] - a[0], | |
y = b[1] - a[1], | |
z = b[2] - a[2], | |
w = b[3] - a[3]; | |
return x*x + y*y + z*z + w*w; | |
}; | |
/** | |
* Alias for {@link vec4.squaredDistance} | |
* @function | |
*/ | |
vec4.sqrDist = vec4.squaredDistance; | |
/** | |
* Calculates the length of a vec4 | |
* | |
* @param {vec4} a vector to calculate length of | |
* @returns {Number} length of a | |
*/ | |
vec4.length = function (a) { | |
var x = a[0], | |
y = a[1], | |
z = a[2], | |
w = a[3]; | |
return Math.sqrt(x*x + y*y + z*z + w*w); | |
}; | |
/** | |
* Alias for {@link vec4.length} | |
* @function | |
*/ | |
vec4.len = vec4.length; | |
/** | |
* Calculates the squared length of a vec4 | |
* | |
* @param {vec4} a vector to calculate squared length of | |
* @returns {Number} squared length of a | |
*/ | |
vec4.squaredLength = function (a) { | |
var x = a[0], | |
y = a[1], | |
z = a[2], | |
w = a[3]; | |
return x*x + y*y + z*z + w*w; | |
}; | |
/** | |
* Alias for {@link vec4.squaredLength} | |
* @function | |
*/ | |
vec4.sqrLen = vec4.squaredLength; | |
/** | |
* Negates the components of a vec4 | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a vector to negate | |
* @returns {vec4} out | |
*/ | |
vec4.negate = function(out, a) { | |
out[0] = -a[0]; | |
out[1] = -a[1]; | |
out[2] = -a[2]; | |
out[3] = -a[3]; | |
return out; | |
}; | |
/** | |
* Returns the inverse of the components of a vec4 | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a vector to invert | |
* @returns {vec4} out | |
*/ | |
vec4.inverse = function(out, a) { | |
out[0] = 1.0 / a[0]; | |
out[1] = 1.0 / a[1]; | |
out[2] = 1.0 / a[2]; | |
out[3] = 1.0 / a[3]; | |
return out; | |
}; | |
/** | |
* Normalize a vec4 | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a vector to normalize | |
* @returns {vec4} out | |
*/ | |
vec4.normalize = function(out, a) { | |
var x = a[0], | |
y = a[1], | |
z = a[2], | |
w = a[3]; | |
var len = x*x + y*y + z*z + w*w; | |
if (len > 0) { | |
len = 1 / Math.sqrt(len); | |
out[0] = x * len; | |
out[1] = y * len; | |
out[2] = z * len; | |
out[3] = w * len; | |
} | |
return out; | |
}; | |
/** | |
* Calculates the dot product of two vec4's | |
* | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @returns {Number} dot product of a and b | |
*/ | |
vec4.dot = function (a, b) { | |
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; | |
}; | |
/** | |
* Performs a linear interpolation between two vec4's | |
* | |
* @param {vec4} out the receiving vector | |
* @param {vec4} a the first operand | |
* @param {vec4} b the second operand | |
* @param {Number} t interpolation amount between the two inputs | |
* @returns {vec4} out | |
*/ | |
vec4.lerp = function (out, a, b, t) { | |
var ax = a[0], | |
ay = a[1], | |
az = a[2], | |
aw = a[3]; | |
out[0] = ax + t * (b[0] - ax); | |
out[1] = ay + t * (b[1] - ay); | |
out[2] = az + t * (b[2] - az); | |
out[3] = aw + t * (b[3] - aw); | |
return out; | |
}; | |
/** | |
* Generates a random vector with the given scale | |
* | |
* @param {vec4} out the receiving vector | |
* @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned | |
* @returns {vec4} out | |
*/ | |
vec4.random = function (out, scale) { | |
scale = scale || 1.0; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment