-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathraster-blend-modes.html
More file actions
358 lines (320 loc) · 12.7 KB
/
raster-blend-modes.html
File metadata and controls
358 lines (320 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<html>
<head>
<title>Raster Blend Modes Test - Mapbox GL JS</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../dist/mapbox-gl.css" />
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#controls {
position: fixed;
top: 10px;
left: 10px;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
z-index: 1;
max-width: 350px;
}
#controls h3 {
margin: 0 0 15px 0;
}
.control-group {
margin-bottom: 20px;
}
.control-group label {
display: block;
font-weight: bold;
margin-bottom: 8px;
}
.button-group {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
padding: 10px;
border: 2px solid #2196F3;
background: white;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
transition: all 0.2s;
}
button:hover {
background: #e3f2fd;
}
button.active {
background: #2196F3;
color: white;
}
.slider-group {
margin-top: 10px;
}
.slider-group input {
width: 100%;
margin-top: 5px;
}
.value-display {
text-align: right;
color: #666;
font-size: 14px;
}
#info {
margin-top: 15px;
padding: 15px;
background: #f5f5f5;
border-radius: 4px;
font-size: 13px;
}
#info .current {
font-weight: bold;
color: #2196F3;
margin-bottom: 8px;
}
#info .expected {
color: #666;
line-height: 1.6;
}
.debug {
margin-top: 10px;
padding: 10px;
background: #fff3cd;
border-radius: 4px;
font-size: 11px;
font-family: monospace;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="controls">
<h3>Raster Blend Modes</h3>
<p style="margin: 0 0 15px 0; font-size: 12px; color: #666;">
WebGL blend functions with neutral color blending. Zero performance overhead.<br>
<strong>Layers:</strong> Satellite (base) + OSM overlay (top, with blend mode applied)
</p>
<div class="control-group">
<label>Blend Mode:</label>
<div class="button-group">
<button id="btn-normal" class="active">Normal</button>
<button id="btn-multiply">Multiply</button>
<button id="btn-screen">Screen</button>
<button id="btn-darken">Darken</button>
<button id="btn-lighten">Lighten</button>
</div>
</div>
<div class="control-group">
<label>OSM Layer Opacity:</label>
<input type="range" id="opacity-slider" min="0" max="1" step="0.1" value="0.7">
<div class="value-display">Current: <span id="opacity-value">0.7</span></div>
<p style="margin: 5px 0 0 0; font-size: 11px; color: #999;">
Controls opacity of the OSM overlay layer (top layer)
</p>
</div>
<div id="info">
<div class="current">Current: <span id="current-mode">normal</span> @ <span id="current-opacity">0.7</span></div>
<div class="expected" id="expected-text">
Standard alpha blending. OSM layer semi-transparent over satellite imagery.
</div>
</div>
<div class="debug">
<strong>Debug Info:</strong><br>
Shader premultiply: <span id="debug-premult">?</span><br>
Actual blend mode: <span id="debug-blend">?</span><br>
Actual opacity: <span id="debug-opacity">?</span>
</div>
<div class="debug" id="validation-warning" style="display: none; background: #ffebee; border: 1px solid #f44336;">
<strong style="color: #f44336;">⚠️ Compatibility Warning:</strong><br>
<span id="warning-text"></span>
</div>
<div class="debug" style="background: #e3f2fd; border: 1px solid #2196F3;">
<strong style="color: #2196F3;">📍 Map Position:</strong><br>
Center: <span id="map-center">-122.2129, 37.5047</span><br>
Zoom: <span id="map-zoom">13.68</span><br>
<small style="color: #666;">Move the map to find interesting areas!</small>
</div>
</div>
<script src="../dist/mapbox-gl-dev.js"></script>
<script type="module">
import {getAccessToken} from './access_token_generated.js';
mapboxgl.accessToken = getAccessToken();
const expectedText = {
'normal': 'Standard alpha blending (NO blend mode set). Uses only raster-opacity for transparency. This is the default behavior when raster-blend-mode is not specified.',
'multiply': 'MULTIPLY (✓ Excellent): Darkens the image. Uses neutral color blending toward white. Note: Middle opacity values (0.3-0.7) appear BRIGHTER than expected due to blending toward white before multiplication. This is expected behavior, not a bug.',
'screen': 'SCREEN (✓ Excellent): Lightens the image. Uses neutral color blending toward black. Note: Similar non-linear brightness behavior as multiply - middle opacity values show reduced lightening effect.',
'darken': 'DARKEN (⚠️ Limited): Shows darker pixels (MIN operation). WARNING: Limited opacity support - opacity only affects alpha channel, not RGB blending. Use MULTIPLY instead for opacity-controlled darkening.',
'lighten': 'LIGHTEN (✓ Excellent): Shows lighter pixels (MAX operation). Uses neutral color blending toward black. Provides most intuitive opacity behavior of all blend modes.'
};
const map = new mapboxgl.Map({
container: 'map',
style: {
version: 8,
sources: {
'satellite': {
type: 'raster',
url: 'mapbox://mapbox.satellite',
tileSize: 256
},
'osm': {
type: 'raster',
tiles: ['https://tile.openstreetmap.org/{z}/{x}/{y}.png'],
tileSize: 256,
attribution: '© OpenStreetMap'
}
},
layers: [
{
id: 'background',
type: 'background',
paint: {
'background-color': '#000'
}
},
{
id: 'satellite',
type: 'raster',
source: 'satellite'
},
{
id: 'osm-overlay',
type: 'raster',
source: 'osm',
paint: {
'raster-opacity': 0.7
// No raster-blend-mode set = standard alpha blending
}
}
]
},
center: [-122.2129, 37.5047], // San Francisco Bay Area
zoom: 13.68
});
// Add navigation controls
map.addControl(new mapboxgl.NavigationControl(), 'top-right');
let currentMode = 'normal';
let currentOpacity = 0.7;
function updateDebugInfo() {
// Check if layer exists before accessing it
if (!map.getLayer('osm-overlay')) {
return;
}
const blendMode = map.getPaintProperty('osm-overlay', 'raster-blend-mode');
const opacity = map.getPaintProperty('osm-overlay', 'raster-opacity');
const isPremult = !blendMode ? 1.0 : 0.0;
document.getElementById('debug-blend').textContent = blendMode || '(not set)';
document.getElementById('debug-opacity').textContent = opacity;
document.getElementById('debug-premult').textContent = isPremult;
// Show validation warning for darken with opacity !== 0 or 1
const warningDiv = document.getElementById('validation-warning');
const warningText = document.getElementById('warning-text');
if (blendMode === 'darken' && opacity !== 0 && opacity !== 1) {
warningText.textContent = `⚠️ DARKEN has limited opacity support. RGB blending remains at full strength (only alpha changes). For opacity-controlled darkening, use MULTIPLY instead. See console for details.`;
warningDiv.style.display = 'block';
} else {
warningDiv.style.display = 'none';
}
}
function setBlendMode(mode) {
if (!map.getLayer('osm-overlay')) {
console.warn('Layer not ready yet');
return;
}
currentMode = mode;
// For 'normal', remove the blend mode property entirely (uses only opacity)
if (mode === 'normal') {
// Remove the property to use standard alpha blending
map.setPaintProperty('osm-overlay', 'raster-blend-mode', undefined);
} else {
map.setPaintProperty('osm-overlay', 'raster-blend-mode', mode);
}
// Update UI
document.querySelectorAll('.button-group button').forEach(btn => {
btn.classList.remove('active');
});
document.getElementById(`btn-${mode}`).classList.add('active');
document.getElementById('current-mode').textContent = mode;
document.getElementById('expected-text').textContent = expectedText[mode];
updateDebugInfo();
console.log(`Blend mode changed to: ${mode} @ opacity ${currentOpacity}`);
}
function setOpacity(value) {
if (!map.getLayer('osm-overlay')) {
console.warn('Layer not ready yet');
return;
}
currentOpacity = value;
map.setPaintProperty('osm-overlay', 'raster-opacity', parseFloat(value));
document.getElementById('opacity-value').textContent = value;
document.getElementById('current-opacity').textContent = value;
updateDebugInfo();
}
// Blend mode buttons
document.getElementById('btn-normal').addEventListener('click', () => setBlendMode('normal'));
document.getElementById('btn-multiply').addEventListener('click', () => setBlendMode('multiply'));
document.getElementById('btn-screen').addEventListener('click', () => setBlendMode('screen'));
document.getElementById('btn-darken').addEventListener('click', () => setBlendMode('darken'));
document.getElementById('btn-lighten').addEventListener('click', () => setBlendMode('lighten'));
// Opacity slider
document.getElementById('opacity-slider').addEventListener('input', (e) => {
setOpacity(e.target.value);
});
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.key === '1') setBlendMode('normal');
if (e.key === '2') setBlendMode('multiply');
if (e.key === '3') setBlendMode('screen');
if (e.key === '4') setBlendMode('darken');
if (e.key === '5') setBlendMode('lighten');
});
map.on('load', () => {
console.log('Map loaded successfully');
updateDebugInfo();
console.log('═══════════════════════════════════════════════════════');
console.log(' Mapbox GL JS - Raster Blend Modes Test');
console.log('═══════════════════════════════════════════════════════');
console.log('Controls:');
console.log(' • Click buttons to change blend mode');
console.log(' • Use slider to change opacity');
console.log(' • Keyboard: 1=Normal, 2=Multiply, 3=Screen, 4=Darken, 5=Lighten');
console.log('');
console.log('Expected Behavior:');
console.log(' • MULTIPLY/SCREEN: Non-linear brightness at middle opacity');
console.log(' (This is expected - blends toward neutral color)');
console.log(' • LIGHTEN: Excellent opacity support, most intuitive');
console.log(' • DARKEN: Limited opacity support (use multiply instead)');
console.log('');
console.log('Implementation: Neutral color blending (zero overhead)');
console.log(' multiply → white, screen → black, lighten → black');
console.log(' darken → no neutral (direct output)');
console.log('═══════════════════════════════════════════════════════');
});
map.on('idle', () => {
updateDebugInfo();
});
// Update map position display
function updateMapPosition() {
const center = map.getCenter();
const zoom = map.getZoom();
document.getElementById('map-center').textContent =
`${center.lng.toFixed(4)}, ${center.lat.toFixed(4)}`;
document.getElementById('map-zoom').textContent = zoom.toFixed(2);
}
map.on('move', updateMapPosition);
map.on('zoom', updateMapPosition);
map.on('load', updateMapPosition);
</script>
</body>
</html>