Skip to content

Commit 7eca02b

Browse files
committed
Fix background in video + make linewidth proportional to density
1 parent e683db1 commit 7eca02b

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

webapp/script.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const map = L.map('map').setView([0, 0], 1);
44

55
// Add OpenStreetMap tile layer with inverted grayscale effect
66
const tileLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
7-
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OSM</a>'
7+
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OSM</a>',
8+
crossOrigin: true
89
}).addTo(map);
910
tileLayer.getContainer().style.filter = 'grayscale(100%) invert(100%)';
1011

@@ -459,6 +460,35 @@ L.Control.MP4Recorder = L.Control.extend({
459460
height: h,
460461
ignoreElements: (element) => {
461462
return element.style.display === 'none';
463+
},
464+
onclone: (clonedDoc) => {
465+
const currentFilter = tileLayer.getContainer().style.filter;
466+
if (currentFilter && currentFilter !== 'none') {
467+
const originalImages = document.querySelectorAll('.leaflet-tile-pane img');
468+
const clonedImages = clonedDoc.querySelectorAll('.leaflet-tile-pane img');
469+
470+
clonedImages.forEach((img, index) => {
471+
const original = originalImages[index];
472+
if (original && original.complete) {
473+
try {
474+
const canvas = document.createElement('canvas');
475+
canvas.width = original.naturalWidth;
476+
canvas.height = original.naturalHeight;
477+
const ctx = canvas.getContext('2d');
478+
ctx.filter = currentFilter;
479+
ctx.drawImage(original, 0, 0);
480+
img.src = canvas.toDataURL();
481+
img.style.filter = 'none';
482+
} catch (e) {
483+
// console.warn('Failed to apply filter to tile', e);
484+
}
485+
}
486+
});
487+
const layers = clonedDoc.querySelectorAll('.leaflet-tile-pane .leaflet-layer');
488+
layers.forEach(layer => {
489+
layer.style.filter = 'none';
490+
});
491+
}
462492
}
463493
});
464494

@@ -505,6 +535,7 @@ L.CanvasEdges = L.Layer.extend({
505535
L.setOptions(this, options);
506536
this.edges = edges;
507537
this.colors = [];
538+
this.densities = [];
508539
},
509540

510541
onAdd: function(map) {
@@ -603,6 +634,11 @@ L.CanvasEdges = L.Layer.extend({
603634
this._redraw();
604635
},
605636

637+
setDensities: function(densities) {
638+
this.densities = densities;
639+
this._redraw();
640+
},
641+
606642
setHighlightedEdge: function(highlightedEdge) {
607643
this.highlightedEdge = highlightedEdge;
608644
this._redraw();
@@ -617,7 +653,7 @@ L.CanvasEdges = L.Layer.extend({
617653
const ctx = this._ctx;
618654
ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
619655
const zoom = this._map.getZoom();
620-
const strokeWidth = 3 + (zoom - baseZoom);
656+
const baseStrokeWidth = 3 + (zoom - baseZoom);
621657

622658
this.edges.forEach((edge, index) => {
623659
if (!edge.geometry || edge.geometry.length === 0) return;
@@ -631,13 +667,21 @@ L.CanvasEdges = L.Layer.extend({
631667
ctx.lineTo(point.x, point.y);
632668
}
633669

634-
ctx.lineWidth = strokeWidth;
670+
// Calculate width based on density
671+
let density = this.densities[index] || 0;
672+
// Scale width: base width + density factor
673+
// Assuming density is roughly 0-1, but can be higher.
674+
// Let's cap the max width increase to avoid huge lines.
675+
const densityFactor = Math.min(density, 2.0);
676+
ctx.lineWidth = Math.max(1, baseStrokeWidth * (0.5 + densityFactor));
677+
635678
ctx.lineCap = 'round';
636679
ctx.lineJoin = 'round';
637680

638681
let color = this.colors[index] || 'rgba(0, 128, 0, 0.69)';
639682
if (this.highlightedEdge && edge.id === this.highlightedEdge) {
640683
color = 'white';
684+
ctx.lineWidth = ctx.lineWidth * 1.5; // Make highlighted edge thicker
641685
}
642686

643687
ctx.strokeStyle = color;
@@ -1071,6 +1115,7 @@ loadDataBtn.addEventListener('click', async function() {
10711115
});
10721116

10731117
canvasEdges.setColors(colors);
1118+
canvasEdges.setDensities(currentDensities);
10741119
}
10751120

10761121
// Set up the time slider based on the density data's maximum time value

0 commit comments

Comments
 (0)