diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-10-24 16:05:36 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-10-24 16:05:36 -0700 |
commit | 7806cb2d725ab1aa4f66db86bc376a027dda6df5 (patch) | |
tree | 18b318131c3cfc854b6623715bbd3ec273d4cbd9 /modern/src/map/mapUtil.js | |
parent | 326f64d9a6c3ce9fe50b8658d54efc3275d30d7f (diff) | |
download | trackermap-web-7806cb2d725ab1aa4f66db86bc376a027dda6df5.tar.gz trackermap-web-7806cb2d725ab1aa4f66db86bc376a027dda6df5.tar.bz2 trackermap-web-7806cb2d725ab1aa4f66db86bc376a027dda6df5.zip |
Extract map unit functions
Diffstat (limited to 'modern/src/map/mapUtil.js')
-rw-r--r-- | modern/src/map/mapUtil.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/modern/src/map/mapUtil.js b/modern/src/map/mapUtil.js new file mode 100644 index 00000000..614234b5 --- /dev/null +++ b/modern/src/map/mapUtil.js @@ -0,0 +1,52 @@ +export const loadImage = (url) => { + return new Promise(imageLoaded => { + const image = new Image(); + image.onload = () => imageLoaded(image); + image.src = url; + }); +}; + +export const loadIcon = async (key, background, url) => { + const image = await loadImage(url); + const pixelRatio = window.devicePixelRatio; + + const canvas = document.createElement('canvas'); + canvas.width = background.width * pixelRatio; + canvas.height = background.height * pixelRatio; + canvas.style.width = `${background.width}px`; + canvas.style.height = `${background.height}px`; + + const context = canvas.getContext('2d'); + context.drawImage(background, 0, 0, canvas.width, canvas.height); + + const iconRatio = 0.5; + const imageWidth = canvas.width * iconRatio; + const imageHeight = canvas.height * iconRatio; + context.drawImage(image, (canvas.width - imageWidth) / 2, (canvas.height - imageHeight) / 2, imageWidth, imageHeight); + + return context.getImageData(0, 0, canvas.width, canvas.height); +}; + +export const calculateBounds = features => { + if (features && features.length) { + const first = features[0].geometry.coordinates; + const bounds = [[...first], [...first]]; + for (let feature of features) { + const longitude = feature.geometry.coordinates[0] + const latitude = feature.geometry.coordinates[1] + if (longitude < bounds[0][0]) { + bounds[0][0] = longitude; + } else if (longitude > bounds[1][0]) { + bounds[1][0] = longitude; + } + if (latitude < bounds[0][1]) { + bounds[0][1] = latitude; + } else if (latitude > bounds[1][1]) { + bounds[1][1] = latitude; + } + } + return bounds; + } else { + return null; + } +} |