mapboxgl-spiderifier
Advanced tools
Comparing version 1.0.3 to 1.0.7
@@ -1,193 +0,203 @@ | ||
module.exports = function MapboxglSpiderfier(Marker, map, userOptions) { | ||
var util = { | ||
each: eachFn, | ||
map: mapFn, | ||
mapTimes: mapTimesFn, | ||
eachTimes: eachTimesFn | ||
}, | ||
NULL_FUNCTION = function () {}, | ||
options = { | ||
circleSpiralSwitchover: 9, // show spiral instead of circle from this marker count upwards | ||
// 0 -> always spiral; Infinity -> always circle | ||
circleFootSeparation: 25, // related to circumference of circle | ||
spiralFootSeparation: 28, // related to size of spiral (experiment!) | ||
spiralLengthStart: 15, // ditto | ||
spiralLengthFactor: 4, // ditto | ||
onClick: userOptions.onClick || NULL_FUNCTION, | ||
initializeMarker: userOptions.initializeMarker || NULL_FUNCTION, | ||
animate: userOptions.animate || false, // to animate the spiral | ||
animationSpeed: userOptions.animationSpeed || (userOptions.animationSpeed ? 200 : 0) // animation speed in milliseconds | ||
}, | ||
twoPi = Math.PI * 2, | ||
previousMarkerObjects = []; | ||
// Public: | ||
this.spiderfy = spiderfy; | ||
this.unspiderfy = unspiderfy; | ||
this.each = function (callback) { | ||
util.each(previousMarkerObjects, callback); | ||
;(function(root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define(['MapboxglSpiderfier'], factory); | ||
} else if (typeof exports === 'object') { | ||
module.exports = factory(require('mapbox-gl')); | ||
} else { | ||
root.MapboxglSpiderfier = factory(root.mapboxgl); | ||
} | ||
}(this, function(mapboxgl) { | ||
return function MapboxglSpiderfier(map, userOptions) { | ||
var util = { | ||
each: eachFn, | ||
map: mapFn, | ||
mapTimes: mapTimesFn, | ||
eachTimes: eachTimesFn | ||
}, | ||
NULL_FUNCTION = function () {}, | ||
options = { | ||
circleSpiralSwitchover: 9, // show spiral instead of circle from this marker count upwards | ||
// 0 -> always spiral; Infinity -> always circle | ||
circleFootSeparation: 25, // related to circumference of circle | ||
spiralFootSeparation: 28, // related to size of spiral (experiment!) | ||
spiralLengthStart: 15, // ditto | ||
spiralLengthFactor: 4, // ditto | ||
onClick: userOptions.onClick || NULL_FUNCTION, | ||
initializeMarker: userOptions.initializeMarker || NULL_FUNCTION, | ||
animate: userOptions.animate || false, // to animate the spiral | ||
animationSpeed: userOptions.animationSpeed || (userOptions.animationSpeed ? 200 : 0) // animation speed in milliseconds | ||
}, | ||
twoPi = Math.PI * 2, | ||
previousMarkerObjects = []; | ||
// Private: | ||
function spiderfy(latLng, markers) { | ||
var spiderParams = generateSpiderParams(markers.length), | ||
markerObjects; | ||
// Public: | ||
this.spiderfy = spiderfy; | ||
this.unspiderfy = unspiderfy; | ||
this.each = function (callback) { | ||
util.each(previousMarkerObjects, callback); | ||
} | ||
unspiderfy(); | ||
// Private: | ||
function spiderfy(latLng, markers) { | ||
var spiderParams = generateSpiderParams(markers.length), | ||
markerObjects; | ||
markerObjects = util.map(markers, function (marker, index) { | ||
var spiderParam = spiderParams[index], | ||
elements = createMarkerElements(spiderParam, marker), | ||
mapboxMarker, | ||
markerObject; | ||
unspiderfy(); | ||
mapboxMarker = Marker(elements.parent) | ||
.setLngLat(latLng); | ||
markerObjects = util.map(markers, function (marker, index) { | ||
var spiderParam = spiderParams[index], | ||
elements = createMarkerElements(spiderParam, marker), | ||
mapboxMarker, | ||
markerObject; | ||
markerObject = { | ||
marker: marker, | ||
elements: elements, | ||
mapboxMarker: mapboxMarker, | ||
spiderParam: spiderParam | ||
}; | ||
mapboxMarker = new mapboxgl.Marker(elements.parent) | ||
.setLngLat(latLng); | ||
options.initializeMarker(markerObject); | ||
markerObject = { | ||
marker: marker, | ||
elements: elements, | ||
mapboxMarker: mapboxMarker, | ||
spiderParam: spiderParam | ||
}; | ||
elements.parent.onclick = function (e) { | ||
options.onClick(e, markerObject); | ||
}; | ||
options.initializeMarker(markerObject); | ||
return markerObject; | ||
}); | ||
elements.parent.onclick = function (e) { | ||
options.onClick(e, markerObject); | ||
}; | ||
util.each(markerObjects.reverse(), function (markerObject) { | ||
markerObject.mapboxMarker.addTo(map); | ||
}); | ||
return markerObject; | ||
}); | ||
if (options.animate) { | ||
setTimeout(function () { | ||
util.each(markerObjects.reverse(), function (markerObject, index) { | ||
markerObject.elements.parent.className = (markerObject.elements.parent.className || '').replace('initial', ''); | ||
markerObject.elements.parent.style['transitionDelay'] = ((options.animationSpeed / 1000) / markerObjects.length * index) + 's'; | ||
}); | ||
util.each(markerObjects.reverse(), function (markerObject) { | ||
markerObject.mapboxMarker.addTo(map); | ||
}); | ||
} | ||
previousMarkerObjects = markerObjects; | ||
}; | ||
function unspiderfy() { | ||
util.each(previousMarkerObjects.reverse(), function (oldMarkerObject, index) { | ||
if (options.animate) { | ||
oldMarkerObject.elements.parent.style['transitionDelay'] = ((options.animationSpeed / 1000) / previousMarkerObjects.length * index) + 's'; | ||
oldMarkerObject.elements.parent.className += ' exit'; | ||
setTimeout(function () { | ||
util.each(markerObjects.reverse(), function (markerObject, index) { | ||
markerObject.elements.parent.className = (markerObject.elements.parent.className || '').replace('initial', ''); | ||
markerObject.elements.parent.style['transitionDelay'] = ((options.animationSpeed / 1000) / markerObjects.length * index) + 's'; | ||
}); | ||
}); | ||
} | ||
setTimeout(function () { | ||
oldMarkerObject.mapboxMarker.remove(); | ||
}, options.animationSpeed + 100); //Wait for 100ms more before clearing the DOM | ||
}); | ||
previousMarkerObjects = []; | ||
} | ||
previousMarkerObjects = markerObjects; | ||
}; | ||
function generateSpiderParams(count) { | ||
if (count >= options.circleSpiralSwitchover) { | ||
return generateSpiralParams(count); | ||
} else { | ||
return generateCircleParams(count); | ||
function unspiderfy() { | ||
util.each(previousMarkerObjects.reverse(), function (oldMarkerObject, index) { | ||
if (options.animate) { | ||
oldMarkerObject.elements.parent.style['transitionDelay'] = ((options.animationSpeed / 1000) / previousMarkerObjects.length * index) + 's'; | ||
oldMarkerObject.elements.parent.className += ' exit'; | ||
} | ||
setTimeout(function () { | ||
oldMarkerObject.mapboxMarker.remove(); | ||
}, options.animationSpeed + 100); //Wait for 100ms more before clearing the DOM | ||
}); | ||
previousMarkerObjects = []; | ||
} | ||
} | ||
function generateSpiralParams(count, centerPt) { | ||
var legLength = options.spiralLengthStart, | ||
angle = 0; | ||
return util.mapTimes(count, function (index) { | ||
var pt; | ||
angle = angle + (options.spiralFootSeparation / legLength + index * 0.0005); | ||
pt = { | ||
x: legLength * Math.cos(angle), | ||
y: legLength * Math.sin(angle), | ||
angle: angle, | ||
legLength: legLength, | ||
index: index | ||
}; | ||
legLength = legLength + (twoPi * options.spiralLengthFactor / angle) | ||
return pt; | ||
}); | ||
} | ||
function generateSpiderParams(count) { | ||
if (count >= options.circleSpiralSwitchover) { | ||
return generateSpiralParams(count); | ||
} else { | ||
return generateCircleParams(count); | ||
} | ||
} | ||
function generateCircleParams(count, centerPt) { | ||
var circumference = options.circleFootSeparation * (2 + count), | ||
legLength = circumference / twoPi, // = radius from circumference | ||
angleStep = twoPi / count; | ||
function generateSpiralParams(count, centerPt) { | ||
var legLength = options.spiralLengthStart, | ||
angle = 0; | ||
return util.mapTimes(count, function (index) { | ||
var pt; | ||
angle = angle + (options.spiralFootSeparation / legLength + index * 0.0005); | ||
pt = { | ||
x: legLength * Math.cos(angle), | ||
y: legLength * Math.sin(angle), | ||
angle: angle, | ||
legLength: legLength, | ||
index: index | ||
}; | ||
legLength = legLength + (twoPi * options.spiralLengthFactor / angle) | ||
return pt; | ||
}); | ||
} | ||
return util.mapTimes(count, function (index) { | ||
var angle = index * angleStep; | ||
return { | ||
x: legLength * Math.cos(angle), | ||
y: legLength * Math.sin(angle), | ||
angle: angle, | ||
legLength: legLength, | ||
index: index | ||
}; | ||
}); | ||
} | ||
function generateCircleParams(count, centerPt) { | ||
var circumference = options.circleFootSeparation * (2 + count), | ||
legLength = circumference / twoPi, // = radius from circumference | ||
angleStep = twoPi / count; | ||
function createMarkerElements(spiderParam, marker) { | ||
var parentElem = document.createElement('div'), | ||
markerElem = document.createElement('div'), | ||
lineElem = document.createElement('div'); | ||
return util.mapTimes(count, function (index) { | ||
var angle = index * angleStep; | ||
return { | ||
x: legLength * Math.cos(angle), | ||
y: legLength * Math.sin(angle), | ||
angle: angle, | ||
legLength: legLength, | ||
index: index | ||
}; | ||
}); | ||
} | ||
parentElem.className = 'spidered-marker' + ( options.animate ? ' animate initial ' : ' '); | ||
lineElem.className = 'line-div'; | ||
markerElem.className = 'icon-div'; | ||
function createMarkerElements(spiderParam, marker) { | ||
var parentElem = document.createElement('div'), | ||
markerElem = document.createElement('div'), | ||
lineElem = document.createElement('div'); | ||
parentElem.appendChild(lineElem); | ||
parentElem.appendChild(markerElem); | ||
parentElem.className = 'spidered-marker' + ( options.animate ? ' animate initial ' : ' '); | ||
lineElem.className = 'line-div'; | ||
markerElem.className = 'icon-div'; | ||
parentElem.style['margin-left'] = spiderParam.x + 'px'; | ||
parentElem.style['margin-top'] = spiderParam.y + 'px'; | ||
parentElem.appendChild(lineElem); | ||
parentElem.appendChild(markerElem); | ||
lineElem.style.height = spiderParam.legLength + 'px'; | ||
// lineElem.style.transform = 'rotate(' + (2*Math.PI - spiderParam.angle) +'rad)'; | ||
lineElem.style.transform = 'rotate(' + (spiderParam.angle - Math.PI / 2) + 'rad)'; | ||
parentElem.style['margin-left'] = spiderParam.x + 'px'; | ||
parentElem.style['margin-top'] = spiderParam.y + 'px'; | ||
return { parent: parentElem, line: lineElem, marker: markerElem }; | ||
} | ||
lineElem.style.height = spiderParam.legLength + 'px'; | ||
// lineElem.style.transform = 'rotate(' + (2*Math.PI - spiderParam.angle) +'rad)'; | ||
lineElem.style.transform = 'rotate(' + (spiderParam.angle - Math.PI / 2) + 'rad)'; | ||
// Utility | ||
function eachFn(array, iterator) { | ||
var i = 0; | ||
if (!array || !array.length) { | ||
return []; | ||
return { parent: parentElem, line: lineElem, marker: markerElem }; | ||
} | ||
for (i = 0; i < array.length; i++) { | ||
iterator(array[i], i) | ||
// Utility | ||
function eachFn(array, iterator) { | ||
var i = 0; | ||
if (!array || !array.length) { | ||
return []; | ||
} | ||
for (i = 0; i < array.length; i++) { | ||
iterator(array[i], i) | ||
} | ||
} | ||
} | ||
function eachTimesFn(count, iterator) { | ||
if (!count) { | ||
return []; | ||
function eachTimesFn(count, iterator) { | ||
if (!count) { | ||
return []; | ||
} | ||
for (i = 0; i < count; i++) { | ||
iterator(i); | ||
} | ||
} | ||
for (i = 0; i < count; i++) { | ||
iterator(i); | ||
function mapFn(array, iterator) { | ||
var result = []; | ||
eachFn(array, function (item, i) { | ||
result.push(iterator(item, i)); | ||
}) | ||
return result; | ||
} | ||
} | ||
function mapFn(array, iterator) { | ||
var result = []; | ||
eachFn(array, function (item, i) { | ||
result.push(iterator(item, i)); | ||
}) | ||
return result; | ||
function mapTimesFn(count, iterator) { | ||
var result = []; | ||
eachTimesFn(count, function (i) { | ||
result.push(iterator(i)); | ||
}); | ||
return result; | ||
} | ||
} | ||
function mapTimesFn(count, iterator) { | ||
var result = []; | ||
eachTimesFn(count, function (i) { | ||
result.push(iterator(i)); | ||
}); | ||
return result; | ||
} | ||
} | ||
})); |
{ | ||
"name": "mapboxgl-spiderifier", | ||
"version": "1.0.3", | ||
"description": "Spiderify markers on mapbox-gl using marker overlays. Note it does not create the spiderfication in the canvas but on a overlay on top of the canvas. This uses mapboxgl.Marker to create markers and spider legs.", | ||
"version": "1.0.7", | ||
"description": "Spiderify markers on mapbox-gl using marker overlays.", | ||
"main": "lib/mapboxgl-spiderifier.js", | ||
"style": "lib/mapboxgl-spiderifier.css", | ||
"directories": { | ||
@@ -16,5 +17,8 @@ "doc": "docs" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"keywords": [ | ||
"mapboxgl", | ||
"spiderfy" | ||
], | ||
"author": "bewithjonam", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -21,0 +25,0 @@ "url": "https://github.com/mimio/mapboxgl-spiderifier/issues" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
2467291
0
2792
0
12
7