Skip to content

Instantly share code, notes, and snippets.

@KoGor
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KoGor/1bafcec2048fb820ee2f to your computer and use it in GitHub Desktop.
Save KoGor/1bafcec2048fb820ee2f to your computer and use it in GitHub Desktop.
Web maps and animated SVG patterns

Animated SVG pattern on Leaflet map (zoom available). Animation made with help of D3.js. Blur filter added for revealing level of hardware acceleration in different browsers. As we can see perfomance degradation quite unsignificant. Works great in Firefox and webkit based browsers.

var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
stamenUrl = 'http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png',
attrib = '&copy; Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> | <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
main = L.tileLayer(stamenUrl, {maxZoom: 18, attribution: attrib}),
map = new L.Map('map', {
layers: [main],
center: [55.7501, 37.6687],
zoom: 11
});
//Initialize SVG layer in Leaflet (works for leaflet-0.7.3)
map._initPathRoot()
var svg = d3.select(".leaflet-overlay-pane").select("svg"),
// add "leaflet-zoom-hide" class to SVG container for turning off zoom
defs = svg.append("defs"),
filterBlur = defs.append("filter").attr("id", "blur");
//Blur filter params
filterBlur.append("feGaussianBlur").attr("stdDeviation", 2);
//Leaflet.draw stuff
var drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var drawControl = new L.Control.Draw({
draw: {
position: 'topleft',
polygon: {
title: 'Draw a sexy polygon!',
allowIntersection: false,
drawError: {
color: '#0099FF',
timeout: 1000
},
shapeOptions: {
color: '#0099FF',
weight: 10,
opacity: 0.8,
fill: false
},
showArea: true
},
rectangle: {
shapeOptions: {
color: '#0099FF',
weight: 10,
opacity: 0.8,
fill: false
}
},
polyline: false,
marker: false,
circle: false
},
edit: {
featureGroup: drawnItems
}
});
map.addControl(drawControl);
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
drawnItems.addLayer(layer);
});
//Some extra controls
var patternDropdown = L.Control.extend({
options: {
position: 'topright'
},
onAdd: function (map) {
// create the control container with a particular class name
var container = L.DomUtil.create('div', 'dropdown-control');
// ... initialize other DOM elements, add listeners, etc.
container.innerHTML = '<label>pattern: <select><option selected>none</option><option>diagonalHatch</option><option>diagonalHash</option></select></label>';
L.DomEvent
.on(container.firstChild.firstElementChild, 'change', function(e) {
if (this.value != 'none') {
drawControl.setDrawingOptions({
polygon: {
shapeOptions: {
color: '#0099FF',
weight: 10,
opacity: 0.8,
fill: true,
fillOpacity: 0.5,
fillColor: 'url(#'+ this.value + ')'
},
},
rectangle: {
shapeOptions: {
color: '#0099FF',
weight: 10,
opacity: 0.8,
fill: true,
fillOpacity: 0.5,
fillColor: 'url(#'+ this.value + ')'
}
}
})
} else {
drawControl.setDrawingOptions({
polygon: {
shapeOptions: {
color: '#0099FF',
fill: false
},
showArea: true
},
rectangle: {
shapeOptions: {
color: '#0099FF',
weight: 10,
opacity: 0.8,
fill: false
}
}
});
};
});
return container;
}
});
map.addControl(new patternDropdown());
var blurControl = L.Control.extend({
options: {
position: 'topright'
},
onAdd: function (map) {
// create the control container with a particular class name
var container = L.DomUtil.create('div', 'checkbox-control');
// ... initialize other DOM elements, add listeners, etc.
container.innerHTML = '<label><input type="checkbox" name="blur" value="clear">BLUR</label>';
L.DomEvent
.on(container.firstChild.firstElementChild, 'change', function(e) {
if (this.value == 'clear') {
svg.selectAll("g").selectAll('path').attr("filter", "url(#blur)");
this.value = 'blured';
} else {
svg.selectAll("g").selectAll('path').attr("filter", "none");
this.value = 'clear';
};
});
return container;
}
});
map.addControl(new blurControl());
var animateControl = L.Control.extend({
options: {
position: 'topright'
},
onAdd: function (map) {
// create the control container with a particular class name
var container = L.DomUtil.create('div', 'checkbox-control');
// ... initialize other DOM elements, add listeners, etc.
container.innerHTML = '<label><input type="checkbox" name="animate" value="still">Animate pattern</label>';
L.DomEvent
.on(container.firstChild.firstElementChild, 'change', function(e) {
if (this.value == 'still') {
this.value = 'animating';
var activePattern = d3.select("select").property("value");
if (activePattern == "diagonalHatch") {
d3.select('#diagonalHatch').call(drop, 250, this.value);
} else if (activePattern == "diagonalHash") {
d3.select('#diagonalHash').call(spin, 10000, this.value);
};
} else {
this.value = 'still';
};
});
return container;
}
});
map.addControl(new animateControl());
function drop(pattern, duration, status) {
if (status == 'animating') {
pattern.transition()
.duration(duration)
.attr('patternTransform','rotate(55 0 0)translate(0 30)')
.ease("linear")
.each("end", function() {
d3.select(this).attr('patternTransform','rotate(55 0 0) translate(0 0)');
drop(pattern, duration, d3.select('input[name=animate]').attr("value"));
});
};
};
function spin(pattern, duration, status) {
if (status == 'animating') {
pattern.transition()
.duration(duration)
.attr('patternTransform','rotate(360 0 0)')
.ease("linear")
.each("end", function() {
d3.select(this).attr('patternTransform','rotate(0 0 0)');
spin(pattern, duration, d3.select('input[name=animate]').attr("value"));
});
};
};
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8 />
<title>Dynamic pattern animation</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.css' rel='stylesheet' />
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-draw/v0.2.2/leaflet.draw.js'></script>
<style>
.checkbox-control {
background-color: #fff;
box-shadow: rgba(0, 0, 0, 0.65) 0px 1px 5px 0px;
}
.checkbox-control > label {
background-color: #fff;
border-radius: 4px;
cursor: pointer;
display: block;
padding: 4px;
}
.checkbox-control input {
vertical-align: bottom;
}
.dropdown-control {
box-shadow: rgba(0, 0, 0, 0.65) 0px 1px 5px 0px;
background-color: #fff;
}
.dropdown-control > label {
background-color: #fff;
border-radius: 4px;
padding: 4px;
display: block;
}
</style>
</head>
<body>
<div id="map" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></div>
<script src="dpa.js"></script>
<!--SVG filters for map container -->
<svg>
<defs>
<pattern id="diagonalHatch" width="40" height="40" patternTransform="rotate(55 0 0)translate(0 0)" patternUnits="userSpaceOnUse">
<line x1="0" y1="0" x2="0" y2="40" stroke-dasharray="8, 5, 4, 8, 5" style="stroke:#0099FF; stroke-width:6 " />
</pattern>
<pattern id="diagonalHash" width="30" height="30" patternTransform="rotate(0 0 0)" patternUnits="userSpaceOnUse">
<g>
<line x1="0" y1="0" x2="0" y2="30" style="stroke:#0099FF; stroke-width:6" />
<line x1="0" y1="0" x2="30" y2="0" style="stroke:#0099FF; stroke-width:6" />
</g>
</pattern>
</defs>
</svg>
</body>
</html>
The MIT License (MIT)
Copyright (c) 2014 [KoGor](https://github.com/KoGor)
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment