fabricjs / fabric.js

Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser
http://fabricjs.com
Other
28.66k stars 3.48k forks source link

feat(): web-worker/offscreen support #7451

Open ajil-paul opened 2 years ago

ajil-paul commented 2 years ago

I am using webpack for a React.js application. I need to do a heavy work with fabric. So I tried to use web worker for that purpose. I used worker-loader plugin for running web-worker. And my woker is working fine. This is my worker file

const fabric = require('fabric')
self.onmessage = async ({ data: { canvasInfo } }) => {

    const canvas = new fabric.StaticCanvas('canvas', {
      width:1024,
      height: 1024,
      selection: false,
    })

    // Doing heavy work with fabric canvas fn()

    self.postMessage({
      message:"success"
    })
}

I am getting the following error.

Uncaught TypeError: jsdom.JSDOM is not a constructor
    at Object../node_modules/fabric/dist/fabric.js (fabric.js:25)
    at __webpack_require__ (bootstrap:21)
    at web.worker.js:5
    at web.worker.bundle.worker.js:140722
    at web.worker.bundle.worker.js:140724
asturur commented 2 years ago

I'm not sure if we discussed this somewhere else, but we need to change how we detect node if we want to have a chance to have fabricJS working in a web worker. First of all it needs to use the offscreencanvas, and on top of that we need to stop using window === undefined to detect node.

ajil-paul commented 2 years ago

Yes, I have discussed the same thing in stackoverflow. I am still stuck on the issue.

kingschnulli commented 2 years ago

this should be addressed with #7596 as well as other node detection related issues

ShaMan123 commented 2 years ago

see #8208