|
1 | 1 |
|
2 | | -# Georeferenced Image with Mask |
| 2 | +# Image with Mask |
3 | 3 |
|
4 | | - property="og:description" |
| 4 | +Display an image with a polygon mask to show only a specific area. |
5 | 5 |
|
6 | 6 | <iframe src="/maplibre-gl-layers/demos/image-mask.html" width="100%" style="border:none; height:400px"></iframe> |
7 | 7 |
|
8 | 8 | ```html |
9 | 9 | <!doctype html> |
10 | 10 | <html lang="en"> |
11 | 11 | <head> |
12 | | - <title>Georeferenced Image with Mask</title> |
13 | | - <meta |
14 | | - property="og:description" |
15 | | - content="Display a georeferenced image with a polygon mask to show only a specific area." |
16 | | - /> |
| 12 | + <title>Image with Mask</title> |
| 13 | + <meta property="og:description" content="Display an image with a polygon mask to show only a specific area." /> |
17 | 14 | <meta charset="utf-8" /> |
18 | 15 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
19 | 16 | <link rel="stylesheet" href="https://unpkg.com/maplibre-gl/dist/maplibre-gl.css" /> |
|
27 | 24 | <body> |
28 | 25 | <div id="map"></div> |
29 | 26 | <script> |
30 | | - // const pane = new Tweakpane.Pane() |
| 27 | + const pane = new Tweakpane.Pane({ |
| 28 | + title: 'Image Layer Option' |
| 29 | + }) |
31 | 30 | const map = new maplibregl.Map({ |
32 | 31 | container: 'map', |
33 | 32 | style: 'https://www.naivemap.com/demotiles/style.json', |
|
39 | 38 | padding: { top: 10, bottom: 10, left: 10, right: 10 } |
40 | 39 | } |
41 | 40 | }) |
| 41 | +
|
42 | 42 | map.on('load', () => { |
43 | | - const layer4326 = new ImageLayer('image-layer', { |
| 43 | + const layer = new ImageLayer('image-layer', { |
44 | 44 | url: './images/Terrain_CQ.jpeg', |
45 | 45 | projection: 'EPSG:4326', |
46 | 46 | coordinates: [ |
47 | 47 | [105.29197, 32.20291], |
48 | 48 | [110.19401, 32.20291], |
49 | 49 | [110.19401, 28.16587], |
50 | 50 | [105.29197, 28.16587] |
51 | | - ], |
52 | | - mask: { |
53 | | - data: './data/chongqing.geojson' |
54 | | - } |
| 51 | + ] |
55 | 52 | }) |
56 | 53 |
|
57 | | - map.addLayer(layer4326) |
| 54 | + map.addLayer(layer) |
| 55 | +
|
| 56 | + const PARAMS = { |
| 57 | + opacity: 1.0, |
| 58 | + resampling: 'linear', |
| 59 | + maskType: 'in', |
| 60 | + maskData: './data/chongqing.geojson' |
| 61 | + } |
| 62 | + pane.addInput(PARAMS, 'opacity', { min: 0, max: 1, step: 0.1 }).on('change', (e) => { |
| 63 | + layer.updateImage({ opacity: e.value }) |
| 64 | + }) |
| 65 | + pane |
| 66 | + .addInput(PARAMS, 'resampling', { |
| 67 | + options: { |
| 68 | + linear: 'linear', |
| 69 | + nearest: 'nearest' |
| 70 | + } |
| 71 | + }) |
| 72 | + .on('change', (e) => { |
| 73 | + layer.updateImage({ resampling: e.value }) |
| 74 | + }) |
| 75 | + const MASK_PARAMS = { |
| 76 | + type: 'in', |
| 77 | + data: '' |
| 78 | + } |
| 79 | + const maskPane = pane.addFolder({ title: '遮罩属性' }) |
| 80 | + maskPane |
| 81 | + .addInput(MASK_PARAMS, 'type', { |
| 82 | + options: { |
| 83 | + in: 'in', |
| 84 | + out: 'out' |
| 85 | + } |
| 86 | + }) |
| 87 | + .on('change', (e) => { |
| 88 | + layer.updateImage({ mask: { type: e.value } }) |
| 89 | + }) |
| 90 | + maskPane |
| 91 | + .addInput(MASK_PARAMS, 'data', { |
| 92 | + options: { |
| 93 | + None: '', |
| 94 | + CQ: './data/chongqing.geojson', |
| 95 | + BBOX: { |
| 96 | + coordinates: [ |
| 97 | + [ |
| 98 | + [106.0233, 29.86176], |
| 99 | + [106.0233, 29.37936], |
| 100 | + [108.32478, 29.37936], |
| 101 | + [108.32478, 29.86176], |
| 102 | + [106.0233, 29.86176] |
| 103 | + ] |
| 104 | + ], |
| 105 | + type: 'Polygon' |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | + .on('change', (e) => { |
| 110 | + layer.updateMask({ mask: { data: !!e.value ? e.value : undefined } }) |
| 111 | + }) |
58 | 112 | }) |
59 | 113 | </script> |
60 | 114 | </body> |
|
0 commit comments