UNPKG

leaflet-canvaslayer-field

Version:

A set of layers using canvas to draw ASCIIGrid or GeoTIFF files. This includes a basic raster layer (*ScalaField*) and an animated layer for vector fields, such as wind or currents (*VectorFieldAnim*)

102 lines (88 loc) 3.7 kB
<!DOCTYPE html> <html> <head> <title>VectorFieldAnim / Style dynamic</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <link rel="stylesheet" href="//unpkg.com/leaflet@1.2.0/dist/leaflet.css" /> <link rel="stylesheet" href="examples.css" /> <link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet"> </head> <body> <h1 class="title mapTitle">VectorFieldAnim dynamic styles</h1> <div id="map"></div> <div id="floating-panel"> <ul> <li> <label>Width</label> <input id="width" type="range" min="0" max="5" step="0.2" value="0.8"> </li> <li> <label>Color</label> <input id="color" type="color" value="#ffffff"> </li> <li> <label>Velocity</label> <input id="velocityScale" type="range" min="1000" max="10000" value="5000" style="transform: rotateY(180deg);"> </li> <li> <label>Opacity</label> <input id="opacity" type="range" min="0" max="1" value="1" step="0.1"> </li> </ul> </div> <!-- CDN --> <script src="//d3js.org/d3.v4.min.js"></script> <script src="//npmcdn.com/leaflet@1.2.0/dist/leaflet.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/chroma-js/1.3.0/chroma.min.js"></script> <!-- Plugin --> <script src="dist/leaflet.canvaslayer.field.js"></script> <script> let map = L.map('map'); /* Basemap */ let url = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_nolabels/{z}/{x}/{y}.png'; L.tileLayer(url, { attribution: 'OSM & Carto', subdomains: 'abcd', maxZoom: 19 }).addTo(map); /* Default vectorfield animation, from two ASCIIGrid files with u|v current velocity */ let layer; d3.text('data/Bay_U.asc', function (u) { d3.text('data/Bay_V.asc', function (v) { let vf = L.VectorField.fromASCIIGrids(u, v); layer = L.canvasLayer.vectorFieldAnim(vf).addTo(map); map.fitBounds(layer.getBounds()); layer.on('click', function (e) { if (e.value !== null) { let vector = e.value; let v = vector.magnitude().toFixed(2); let d = vector.directionTo().toFixed(0); let html = (`<span class="popupText">${v} m/s to ${d}&deg</span>`); let popup = L.popup() .setLatLng(e.latlng) .setContent(html) .openOn(map); } }); }); }); /* Basic style adjustments with controls */ let width = document.getElementById('width'); width.addEventListener('input', function () { layer.options.width = width.value; }); let color = document.getElementById('color'); color.addEventListener('input', function () { layer.options.color = color.value; }); let velocityScale = document.getElementById('velocityScale'); velocityScale.addEventListener('input', function () { layer.options.velocityScale = 1 / velocityScale.value; }); let opacity = document.getElementById('opacity'); opacity.addEventListener('input', function () { layer.setOpacity(opacity.value); }); </script> </body> </html>