ai / offscreen-canvas

Polyfill for OffscreenCanvas to move Three.js/WebGL/2D canvas to Web Worker
MIT License
332 stars 13 forks source link

Receiving messages from the Worker is not possible. #2

Closed WaldemarLehner closed 5 years ago

WaldemarLehner commented 5 years ago

Hello. I'm trying to get the messages from the Web Worker. This works, when canvas.transferControlToOffscreen is not supported but does not work when it is supported. In the picture below you can see Mozilla Firefox (66.0.5) on the left and Chrome (74.0.3729.157) on the right.

image

This is my worker file:

//This is the Webworker / Script (on envs that dont support canvas webworkers) that interfaces with the THREE Context.
let insideWorker = require("offscreen-canvas/inside-worker");

const worker = insideWorker(msg => {
    if(msg.data.canvas){
        worker.post("canvas found");
    }else{
        worker.post("canvas not found");
    }
    if(worker.isWorker){
        worker.post("this is a worker");
    }else{
        worker.post("this is not a worker");
    }
});

And this is the file where I create the Worker.

function canvasWorker(url,callback){
    //Override callback for now.
    callback = console.info;

    let createWorker = require("offscreen-canvas/create-worker.js");

    const canvas = document.querySelector("canvas.canvas");

    const worker = createWorker(canvas, url, callback);

    return worker;

}

Now I'm wondering if that is a bug with the polyfill or with my code?

ai commented 5 years ago

Hi. I will try to look during this week

ai commented 5 years ago

I updated tests and everything work b64cc89

Can you show how do you use canvasWorker function?

WaldemarLehner commented 5 years ago

Yup! canvasWorker is part of module being called from this file

I use laravel mix with the project. The index.js i mentioned is used as an entry point for laravel, as seen here.

ai commented 5 years ago

And where do you try to send messages?

WaldemarLehner commented 5 years ago

I think I found the root of the issue. It seems to be an issue with laravel-mix and/or webpack and the fact that im splitting the webworker into a vendor, manifest, and main file. Removing mix.extract() from the webpack-mix.js fixed the issue for me.

Yeah. I feel pretty stupid in retrospect. 😂