Move map markers into separate svg
And improve navigation performance
This commit is contained in:
1 parent
6dc97db7da
commit
25d2935042
4 files changed
+28
-18
No files matched your search
@@ -9,6 +9,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="mapContainer">
|
<div id="mapContainer">
|
||||||
|
<div id="mapNavigator">
|
||||||
<svg id="map" xmlns="http://www.w3.org/2000/svg" width="1000" height="1000" viewBox="0 0 10000 10000">
|
<svg id="map" xmlns="http://www.w3.org/2000/svg" width="1000" height="1000" viewBox="0 0 10000 10000">
|
||||||
<style>
|
<style>
|
||||||
.sign{
|
.sign{
|
||||||
@@ -178,6 +179,9 @@
|
|||||||
</g>
|
</g>
|
||||||
</defs>
|
</defs>
|
||||||
</svg>
|
</svg>
|
||||||
|
<svg id="mapMarkers" xmlns="http://www.w3.org/2000/svg" width="1000" height="1000" viewBox="0 0 10000 10000">
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="topleft">
|
<div id="topleft">
|
||||||
<div id="headerbar" class="panel orange">
|
<div id="headerbar" class="panel orange">
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ const scaleCullThresholds = [
|
|||||||
// CODE
|
// CODE
|
||||||
const map = document.getElementById('map');
|
const map = document.getElementById('map');
|
||||||
const map_container = document.getElementById('mapContainer');
|
const map_container = document.getElementById('mapContainer');
|
||||||
|
const map_navigator = document.getElementById('mapNavigator');
|
||||||
const zoomLevelDisplay = document.getElementById('zoomLevelDisplay');
|
const zoomLevelDisplay = document.getElementById('zoomLevelDisplay');
|
||||||
const styleRoot = document.querySelector(':root');
|
const styleRoot = document.querySelector(':root');
|
||||||
const svgns = map.getAttribute('xmlns');
|
const svgns = map.getAttribute('xmlns');
|
||||||
@@ -52,9 +53,10 @@ const map_rails = document.createElementNS(svgns, 'g');
|
|||||||
map_rails.setAttribute('id', 'map_rails');
|
map_rails.setAttribute('id', 'map_rails');
|
||||||
map.appendChild(map_rails);
|
map.appendChild(map_rails);
|
||||||
|
|
||||||
const map_markers = document.createElementNS(svgns, 'g');
|
//const map_markers = document.createElementNS(svgns, 'g');
|
||||||
map_markers.setAttribute('id', 'map_markers');
|
//map_markers.setAttribute('id', 'map_markers');
|
||||||
map.appendChild(map_markers);
|
//map.appendChild(map_markers);
|
||||||
|
const map_markers = document.getElementById('mapMarkers');
|
||||||
|
|
||||||
let mapData = [];
|
let mapData = [];
|
||||||
let trackNameCounting = {};
|
let trackNameCounting = {};
|
||||||
@@ -80,7 +82,6 @@ async function loadTrackData(file){
|
|||||||
function mapScrollSetup(){
|
function mapScrollSetup(){
|
||||||
let mapNav = {x:currentMapNav.x, y:currentMapNav.y, scale:currentMapNav.scale};
|
let mapNav = {x:currentMapNav.x, y:currentMapNav.y, scale:currentMapNav.scale};
|
||||||
let prevMouse = {x:mapNav.x, y:mapNav.y, scale:mapNav.scale};
|
let prevMouse = {x:mapNav.x, y:mapNav.y, scale:mapNav.scale};
|
||||||
let scrollDat = {ticking:false};
|
|
||||||
const mouseDownHandler = e => {
|
const mouseDownHandler = e => {
|
||||||
map_container.style.cursor = 'grabbing';
|
map_container.style.cursor = 'grabbing';
|
||||||
prevMouse.x = e.clientX;
|
prevMouse.x = e.clientX;
|
||||||
@@ -93,8 +94,6 @@ function mapScrollSetup(){
|
|||||||
mapNav.x += prevMouse.x-e.clientX;
|
mapNav.x += prevMouse.x-e.clientX;
|
||||||
mapNav.y += prevMouse.y-e.clientY;
|
mapNav.y += prevMouse.y-e.clientY;
|
||||||
|
|
||||||
updateMapview(mapNav);
|
|
||||||
|
|
||||||
prevMouse.x = e.clientX;
|
prevMouse.x = e.clientX;
|
||||||
prevMouse.y = e.clientY;
|
prevMouse.y = e.clientY;
|
||||||
};
|
};
|
||||||
@@ -104,8 +103,6 @@ function mapScrollSetup(){
|
|||||||
document.removeEventListener('mouseup', mouseUpHandler);
|
document.removeEventListener('mouseup', mouseUpHandler);
|
||||||
};
|
};
|
||||||
const zoomHandler = e => {
|
const zoomHandler = e => {
|
||||||
if(!scrollDat.ticking){
|
|
||||||
scrollDat.ticking = true;
|
|
||||||
let rect = map_container.getBoundingClientRect();
|
let rect = map_container.getBoundingClientRect();
|
||||||
let cursorLocal = {
|
let cursorLocal = {
|
||||||
x: -e.clientX - rect.left,
|
x: -e.clientX - rect.left,
|
||||||
@@ -120,22 +117,28 @@ function mapScrollSetup(){
|
|||||||
mapNav.x = cursorLocal.x - scaleMult * (cursorLocal.x-mapNav.x);
|
mapNav.x = cursorLocal.x - scaleMult * (cursorLocal.x-mapNav.x);
|
||||||
mapNav.y = cursorLocal.y - scaleMult * (cursorLocal.y-mapNav.y);
|
mapNav.y = cursorLocal.y - scaleMult * (cursorLocal.y-mapNav.y);
|
||||||
mapNav.scale *= scaleMult;
|
mapNav.scale *= scaleMult;
|
||||||
updateMapview(mapNav);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
scrollDat.ticking = false;
|
|
||||||
}, 20);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let prevMapNav = {};
|
||||||
|
const navUpdate = () => {
|
||||||
|
if(prevMapNav.x != currentMapNav.x || prevMapNav.y != currentMapNav.y || prevMapNav.scale != currentMapNav.scale){
|
||||||
|
updateMapview(mapNav);
|
||||||
|
prevMapNav.x = currentMapNav.x;
|
||||||
|
prevMapNav.y = currentMapNav.y;
|
||||||
|
prevMapNav.scale = currentMapNav.scale;
|
||||||
|
}
|
||||||
|
requestAnimationFrame(navUpdate);
|
||||||
|
}
|
||||||
|
|
||||||
map_container.addEventListener('mousedown', mouseDownHandler);
|
map_container.addEventListener('mousedown', mouseDownHandler);
|
||||||
map_container.addEventListener('wheel', zoomHandler);
|
map_container.addEventListener('wheel', zoomHandler);
|
||||||
|
navUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Readjust map view to focus on position */
|
/** Readjust map view to focus on position */
|
||||||
function updateMapview(navData = {x:0, y:0, scale:1}){
|
function updateMapview(navData = {x:0, y:0, scale:1}){
|
||||||
currentMapNav = navData;
|
currentMapNav = navData;
|
||||||
map.style.transform = `translate(${-navData.x}px, ${-navData.y}px) scale(${navData.scale})`;
|
map_navigator.style.transform = `translate(${-navData.x}px, ${-navData.y}px) scale(${navData.scale})`;
|
||||||
if(navData.scale !== previousMapScale){
|
if(navData.scale !== previousMapScale){
|
||||||
zoomLevelDisplay.innerHTML = `Zoom: ${Math.round(navData.scale*100)/100}x`;
|
zoomLevelDisplay.innerHTML = `Zoom: ${Math.round(navData.scale*100)/100}x`;
|
||||||
let dirtyScale = previousMapScale < 0;
|
let dirtyScale = previousMapScale < 0;
|
||||||
@@ -179,6 +182,7 @@ function establishDimensions(){
|
|||||||
let width = maxX-minX;
|
let width = maxX-minX;
|
||||||
let height = maxY-minY;
|
let height = maxY-minY;
|
||||||
map.setAttribute('viewBox', `${minX-padding} ${minY-padding} ${width+2*padding} ${height+2*padding}`);
|
map.setAttribute('viewBox', `${minX-padding} ${minY-padding} ${width+2*padding} ${height+2*padding}`);
|
||||||
|
map_markers.setAttribute('viewBox', map.getAttribute('viewBox'));
|
||||||
updateMapview({
|
updateMapview({
|
||||||
x: map.clientWidth/2-map_container.clientWidth/2,
|
x: map.clientWidth/2-map_container.clientWidth/2,
|
||||||
y: map.clientHeight/2-map_container.clientHeight/2,
|
y: map.clientHeight/2-map_container.clientHeight/2,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ let settingEntries = [
|
|||||||
id: 'toggle_signage',
|
id: 'toggle_signage',
|
||||||
state: true,
|
state: true,
|
||||||
func: state =>{
|
func: state =>{
|
||||||
document.getElementById('map_markers').style.display = state ? 'unset' : 'none';
|
document.getElementById('mapMarkers').style.display = state ? 'unset' : 'none';
|
||||||
},
|
},
|
||||||
children: [
|
children: [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -100,10 +100,12 @@ a:visited{
|
|||||||
text-align: right;
|
text-align: right;
|
||||||
}
|
}
|
||||||
|
|
||||||
#map{
|
#mapNavigator{
|
||||||
transition: transform 100ms ease-out;
|
transition: transform 100ms ease-out;
|
||||||
transform-origin: 0% 0%;
|
transform-origin: 0% 0%;
|
||||||
position: relative;
|
>*{
|
||||||
|
position:absolute;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#mapContainer{
|
#mapContainer{
|
||||||
|
|||||||
Reference in new issue
Block a user