xaviergonz / js-angusj-clipper

Polygon and line clipping and offsetting library (Javascript) - a port of Angus Johnson's clipper
MIT License
162 stars 19 forks source link

Is it available on WebWorker? Does not seem to work #7

Closed FishOrBear closed 2 years ago

FishOrBear commented 4 years ago

/web/wasm/clipper.js /web/wasm/clipper-wasm.js

add line:

let document = {};

fix

xaviergonz commented 4 years ago

Did you try the universal version or the web one? If you tried the web version, could you try the universal?

FishOrBear commented 4 years ago

I use this library in WebWorker in webpage, this is my current solution because Document is accessed in the code.

import * as clipperLib from "js-angusj-clipper/web";

export let clipperCpp: { lib?: clipperLib.ClipperLibWrapper; } = {};
export function InitClipperCpp(): Promise<void>
{
    if (clipperCpp.lib) return;
    if (!globalThis.document)
        globalThis.document = {} as any;
    return new Promise((res, rej) =>
    {
        clipperLib.loadNativeClipperLibInstanceAsync(
            // let it autodetect which one to use, but also available WasmOnly and AsmJsOnly
            clipperLib.NativeClipperLibRequestedFormat.WasmWithAsmJsFallback
        ).then(c =>
        {
            clipperCpp.lib = c;
            res();
        });
    });
}

image

xaviergonz commented 4 years ago

I think if you try the /universal one it should work without any workaround. The web version was only done for somebody saying that the universal version didn't work for angular in server side rendering mode: https://github.com/xaviergonz/js-angusj-clipper/issues/2

xaviergonz commented 4 years ago

Just to clarify, to use universal it would mean removing /web from the import

import * as clipperLib from "js-angusj-clipper"; // instead of js-angusj-clipper/web
xaviergonz commented 2 years ago

Since v1.2.0 it now uses emscripten module mode, which does not try to use process/document (AFAIK). Feel free to give it a try

FishOrBear commented 2 years ago

Such code is already available.

export let clipperCpp: { lib?: clipperLib.ClipperLibWrapper; } = {};
export function InitClipperCpp(): Promise<void>
{
    if (clipperCpp.lib) return;
    return new Promise((res, rej) =>
    {
        clipperLib.loadNativeClipperLibInstanceAsync(
            // let it autodetect which one to use, but also available WasmOnly and AsmJsOnly
            clipperLib.NativeClipperLibRequestedFormat.WasmWithAsmJsFallback
        ).then(c =>
        {
            clipperCpp.lib = c;
            res();
            console.log("载入成功!");
        });
    });
}

However, this code cannot be used, and an error will occur. in web,webpack

import * as clipperLib from "js-angusj-clipper";

Error:

Module not found: Can't resolve 'path' in 'E:\WebProject\node_modules\js-angusj-clipper\universal\wasm'
node_modules/js-angusj-clipper/universal/wasm/clipper-wasm.js