Skip to content

Commit e565653

Browse files
authored
Merge pull request #1365 from RobertOrthofer/master
implement basic support for usage in worker
2 parents 0b7d5e7 + 2ef903f commit e565653

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed

package-lock.json

Lines changed: 3 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/apply.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ License: https://raw.githubusercontent.com/openlayers/ol-mapbox-style/master/LIC
77
import {derefLayers} from '@maplibre/maplibre-gl-style-spec';
88
import Map from 'ol/Map.js';
99
import View from 'ol/View.js';
10+
import {createMockDiv} from 'ol/dom.js';
1011
import {getCenter, getTopLeft} from 'ol/extent.js';
1112
import GeoJSON from 'ol/format/GeoJSON.js';
1213
import MVT from 'ol/format/MVT.js';
14+
import {WORKER_OFFSCREEN_CANVAS} from 'ol/has.js';
1315
import LayerGroup from 'ol/layer/Group.js';
1416
import ImageLayer from 'ol/layer/Image.js';
1517
import Layer from 'ol/layer/Layer.js';
@@ -409,7 +411,11 @@ export function applyStyle(
409411
options.accessToken,
410412
styleUrl || location.href,
411413
);
412-
spriteScale = window.devicePixelRatio >= 1.5 ? 0.5 : 1;
414+
spriteScale = WORKER_OFFSCREEN_CANVAS
415+
? 1
416+
: window.devicePixelRatio >= 1.5
417+
? 0.5
418+
: 1;
413419
const sizeFactor = spriteScale == 0.5 ? '@2x' : '';
414420

415421
Promise.all(
@@ -632,7 +638,9 @@ function getBackgroundColor(glLayer, resolution, options, functionCache) {
632638
* @return {Layer} OpenLayers layer.
633639
*/
634640
function setupBackgroundLayer(glLayer, options, functionCache) {
635-
const div = document.createElement('div');
641+
const div = WORKER_OFFSCREEN_CANVAS
642+
? createMockDiv()
643+
: document.createElement('div');
636644
div.className = 'ol-mapbox-style-background';
637645
div.style.position = 'absolute';
638646
div.style.width = '100%';
@@ -1165,15 +1173,28 @@ export function apply(mapOrGroupOrElement, style, options = {}) {
11651173
let promise;
11661174
/** @type {Map|LayerGroup} */
11671175
let mapOrGroup;
1168-
if (
1169-
typeof mapOrGroupOrElement === 'string' ||
1170-
mapOrGroupOrElement instanceof HTMLElement
1171-
) {
1172-
mapOrGroup = new Map({
1173-
target: mapOrGroupOrElement,
1174-
});
1175-
} else {
1176+
1177+
if (WORKER_OFFSCREEN_CANVAS) {
1178+
if (
1179+
!(mapOrGroupOrElement instanceof Map) &&
1180+
!(mapOrGroupOrElement instanceof LayerGroup)
1181+
) {
1182+
throw new Error(
1183+
'ol-mapbox-style in a web worker requires a Map or a LayerGroup as first argument',
1184+
);
1185+
}
11761186
mapOrGroup = mapOrGroupOrElement;
1187+
} else {
1188+
if (
1189+
typeof mapOrGroupOrElement === 'string' ||
1190+
mapOrGroupOrElement instanceof HTMLElement
1191+
) {
1192+
mapOrGroup = new Map({
1193+
target: mapOrGroupOrElement,
1194+
});
1195+
} else {
1196+
mapOrGroup = mapOrGroupOrElement;
1197+
}
11771198
}
11781199

11791200
if (typeof style === 'string') {

src/text.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mb2css from 'mapbox-to-css-font';
2+
import {WORKER_OFFSCREEN_CANVAS} from 'ol/has.js';
23
import {checkedFonts} from 'ol/render/canvas.js';
34
import {createCanvas} from './util.js';
45

@@ -149,6 +150,10 @@ export function getFonts(
149150
fonts,
150151
templateUrl = 'https://cdn.jsdelivr.net/npm/@fontsource/{font-family}/{fontweight}{-fontstyle}.css',
151152
) {
153+
if (WORKER_OFFSCREEN_CANVAS) {
154+
//FIXME Font handling in web workers
155+
return fonts;
156+
}
152157
let fontDescriptions;
153158
for (let i = 0, ii = fonts.length; i < ii; ++i) {
154159
const font = fonts[i];

0 commit comments

Comments
 (0)