openlayers / ol-vite Goto Github PK
View Code? Open in Web Editor NEWUsing OpenLayers with Vite
Using OpenLayers with Vite
I have built a build environment that allows me to easily start "OpenLayers" with a combination of "OpenLayers" and "Vite."
This starter supports TypeScript. Please check it out!
Starting from the template in this repository, I tried using a RasterSource
with a custom “pixel” operation
, but the code fails with a weird ReferenceError
.
Here is the relevant part of my code (the full diff is available here):
// compute an array of colors for every possible byte value
const colors = [];
for (let i = 0 ; i < 256 ; i++) {
colors[i] = [i, 256 - i, i, 255];
}
const rasterSource = new RasterSource({
sources: [rawLayer],
operationType: 'pixel',
operation: (pixels) => {
const pixel = pixels[0];
return colors[pixel[2]]; // apply the color from the look up table
},
});
In the browser, it fails with error “ReferenceError: colors is not defined.” This is weird because the variable colors
is defined before the rasterSource
definition.
If I open the browser debugger I see the chunk produced by Vite:
const __minion__ = (function createMinion(operation) {
let workerHasImageData = true;
try {
new ImageData(10, 10);
} catch (_) {
workerHasImageData = false;
}
function newWorkerImageData(data, width, height) {
if (workerHasImageData) {
return new ImageData(data, width, height);
} else {
return { data, width, height };
}
}
return function(data) {
const buffers = data["buffers"];
const meta = data["meta"];
const imageOps = data["imageOps"];
const width = data["width"];
const height = data["height"];
const numBuffers = buffers.length;
const numBytes = buffers[0].byteLength;
if (imageOps) {
const images = new Array(numBuffers);
for (let b = 0; b < numBuffers; ++b) {
images[b] = newWorkerImageData(
new Uint8ClampedArray(buffers[b]),
width,
height
);
}
const output2 = operation(images, meta).data;
return output2.buffer;
}
const output = new Uint8ClampedArray(numBytes);
const arrays = new Array(numBuffers);
const pixels = new Array(numBuffers);
for (let b = 0; b < numBuffers; ++b) {
arrays[b] = new Uint8ClampedArray(buffers[b]);
pixels[b] = [0, 0, 0, 0];
}
for (let i = 0; i < numBytes; i += 4) {
for (let j = 0; j < numBuffers; ++j) {
const array = arrays[j];
pixels[j][0] = array[i];
pixels[j][1] = array[i + 1];
pixels[j][2] = array[i + 2];
pixels[j][3] = array[i + 3];
}
const pixel = operation(pixels, meta);
output[i] = pixel[0];
output[i + 1] = pixel[1];
output[i + 2] = pixel[2];
output[i + 3] = pixel[3];
}
return output.buffer;
};
})((pixels) => {
const pixel = pixels[0];
return colors[pixel[2]];
});self.addEventListener("message", function(event) { const buffer = __minion__(event.data); self.postMessage({buffer: buffer, meta: event.data.meta}, [buffer]);});
I am not sure what it means precisely, but indeed, that code contains the definition of the function operation
without the definition of the variable colors
, hence the ReferenceError.
Is there something special I should do to use Vite with openlayers without running into that type of errors?
You can reproduce the issue by cloning my branch and running npm run start
and then opening your browser.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.