There's an example on Openlayers website:
https://openlayers.org/en/latest/examples/offscreen-canvas.html
It works fine but when I try to add an additional tile layer (XYZ or WMS) to the map, the rendering process gets drastically slower.
Did anyone solve the problem?
Just tried to add an OSM layer to the map along with the layer from the example which resulted in significantly slowing render.
From the way the code is using
frameState
it might be expecting the worker to do all the rendering and adding another layer directly would conflict with that assumption. If you must add directly to the map specifyingtransition: 0
for the OSM might help.You could also try adding the OSM to the worker - it will not need
stylefunction
but I think everything else is applicableSome additional work would be needed as this would need a custom Tile class - Image cannot be used in a worker so it would need to be loaded in the same way as sprites.
UPDATE
Ideally OSM could take a tileLoadFunction which loaded the image as a worker compatible imageBitmap in the same way as sprite images:
Unfortunately the code in ImageTile will still throw an error because it initially tries to create an empty Image without checking if that is supported, so unless that is fixed a custom class will be needed - for example you could copy all the code from https://github.com/openlayers/openlayers/blob/main/src/ol/ImageTile.js change the class name to ImageBitmapTile and edit any occurrences of
new Image()
tonew Observable()
(imported fromol/Observable.js
- a simple generic object which accepts listeners)OSM does not accept a custom tile class so you would need use a TileImage source and specify the OSM url and maxZoom:
I finally got it working with an OSM source simply by defining a dummy
self.Image
at the start of the worker. The tileLoadFunction must also check that the image returned is the one requested otherwise it picks up images for other tiles, andrenderDeclutter
should only be called for vector layers