|
8 | 8 | <style> |
9 | 9 | body { margin: 0; padding: 0; } |
10 | 10 | html, body, #map { height: 100%; } |
| 11 | + #controls { position: absolute; top: 0; left: 0; } |
11 | 12 | </style> |
12 | 13 | </head> |
13 | 14 |
|
14 | 15 | <body> |
15 | 16 | <div id='map'></div> |
| 17 | +<div id='controls'> |
| 18 | + <input id="slider" type="range" list="colors" min="0" step="1"> |
| 19 | + <datalist id="colors"></datalist> |
| 20 | +</div> |
16 | 21 |
|
17 | 22 | <script src='../dist/mapbox-gl-dev.js'></script> |
18 | 23 | <script src='../debug/access_token_generated.js'></script> |
19 | 24 | <script> |
20 | 25 |
|
21 | 26 | var map = window.map = new mapboxgl.Map({ |
22 | 27 | container: 'map', |
23 | | - center: [-74.5, 40], |
24 | | - zoom: 2, |
25 | | - style: { |
26 | | - version: 8, |
27 | | - sources: {}, |
28 | | - layers: [] |
29 | | - }, |
| 28 | + center: [0, 0], |
| 29 | + zoom: 0, |
| 30 | + style: 'mapbox://styles/mapbox/light-v10', |
30 | 31 | hash: true |
31 | 32 | }); |
32 | 33 |
|
| 34 | +const tileSize = 256; |
| 35 | +const colors = ['#74a9cf', '#3690c0', '#0570b0', '#045a8d']; |
| 36 | +let currentColor = colors[0]; |
| 37 | + |
33 | 38 | map.on('load', () => { |
34 | 39 | map.addSource('custom-source', { |
35 | 40 | type: 'custom', |
36 | | - tileSize: 256, |
37 | | - attribution: 'Map tiles by <a target="_top" rel="noopener" href="http://stamen.com">Stamen Design</a>, under <a target="_top" rel="noopener" href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a target="_top" rel="noopener" href="http://openstreetmap.org">OpenStreetMap</a>, under <a target="_top" rel="noopener" href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>', |
38 | | - async loadTile(tile, {signal}) { |
39 | | - const tilesUrl = 'https://stamen-tiles.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.jpg'; |
40 | | - const url = tilesUrl |
41 | | - .replace('{z}', String(tile.z)) |
42 | | - .replace('{x}', String(tile.x)) |
43 | | - .replace('{y}', String(tile.y)); |
| 41 | + tileSize, |
| 42 | + async loadTile({z, x, y}) { |
| 43 | + const tileSize = 256; |
| 44 | + const offscreenCanvas = new OffscreenCanvas(tileSize, tileSize); |
| 45 | + const context = offscreenCanvas.getContext('2d'); |
| 46 | + context.fillStyle = 'red'; |
| 47 | + context.fillRect(0, 0, tileSize, tileSize); |
| 48 | + |
| 49 | + context.font = '18px serif'; |
| 50 | + context.fillStyle = 'white'; |
| 51 | + context.textAlign = 'center'; |
| 52 | + context.fillText(`${z}/${x}/${y}`, tileSize / 2, tileSize / 2, tileSize); |
44 | 53 |
|
45 | | - const response = await fetch(url, {signal}); |
46 | | - const data = await response.arrayBuffer(); |
| 54 | + const imageData = context.getImageData(0, 0, tileSize, tileSize); |
| 55 | + return imageData; |
| 56 | + }, |
| 57 | + prepareTile({z, x, y}) { |
| 58 | + const tileSize = 256; |
| 59 | + const offscreenCanvas = new OffscreenCanvas(tileSize, tileSize); |
| 60 | + const context = offscreenCanvas.getContext('2d'); |
| 61 | + context.fillStyle = currentColor; |
| 62 | + context.fillRect(0, 0, tileSize, tileSize); |
47 | 63 |
|
48 | | - const blob = new window.Blob([new Uint8Array(data)], {type: 'image/png'}); |
49 | | - const imageBitmap = await window.createImageBitmap(blob); |
| 64 | + context.font = '18px serif'; |
| 65 | + context.fillStyle = 'white'; |
| 66 | + context.textAlign = 'center'; |
| 67 | + context.fillText(`${z}/${x}/${y}`, tileSize / 2, tileSize / 2, tileSize); |
50 | 68 |
|
51 | | - return imageBitmap; |
| 69 | + const imageData = context.getImageData(0, 0, tileSize, tileSize); |
| 70 | + return imageData; |
| 71 | + }, |
| 72 | + hasTile({z, x, y}) { |
| 73 | + return (x + y) % 2 === 0; |
52 | 74 | } |
53 | 75 | }); |
54 | 76 |
|
55 | 77 | map.addLayer({ |
56 | 78 | id: 'custom-source', |
57 | 79 | type: 'raster', |
58 | | - source: 'custom-source' |
| 80 | + source: 'custom-source', |
| 81 | + paint: { |
| 82 | + 'raster-opacity': 0.75, |
| 83 | + 'raster-fade-duration': 0 |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + const slider = document.getElementById('slider'); |
| 88 | + slider.value = 0; |
| 89 | + slider.max = colors.length - 1; |
| 90 | + slider.addEventListener('input', (e) => { |
| 91 | + currentColor = colors[e.target.value]; |
| 92 | + map.triggerRepaint(); |
59 | 93 | }); |
60 | 94 | }); |
61 | 95 |
|
|
0 commit comments