Git Product home page Git Product logo

ol-vite's Issues

Unable to define a pixel operation on a raster image

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.