Skip to content

OffscreenCanvas when in worker contexts #35

Closed

Description

@MatthewSteel

Hi! I'm trying to use tiny-sdf in a web-worker, and document.createElement("canvas") isn't working because document isn't found on the global object.

I'm happy to make a PR that does something like

if (self.OffscreenCanvas) {
  this.canvas = new OffscreenCanvas(size, size);
} else {
  this.canvas = document.createElement("canvas");
  this.canvas.width = this.canvas.height = size;
}

or that reversed, or using some different kind of logical test. Looks like your eslint config doesn't love OffscreenCanvas, so I might need some help convincing it too.

Not super urgent, I think I can hack around this until it's fixed here.

Activity

  1. mourner commented on Apr 16, 2021

    @mourner
    Member

    Alternatively you could just extend the TinySDF class and override the _createCanvas method. This is what I'm doing in tests to switch to Node's canvas:

    tiny-sdf/test/test.js

    Lines 13 to 17 in 88d5b01

    class MockTinySDF extends TinySDF {
    _createCanvas(size) {
    return nodeCanvas.createCanvas(size, size);
    }
    }

  2. MatthewSteel commented on Apr 16, 2021

    @MatthewSteel
    Author

    Ah, that's much better than the hack I had in mind, thanks for the tip!

  3. mourner commented on Apr 16, 2021

    @mourner
    Member

    @MatthewSteel you're welcome! This will only work on the master branch for now though — I need to release a new version.

  4. mourner commented on Sep 22, 2021

    @mourner
    Member

    Added as a part of ff5cef8

  5. mourner commented on Sep 24, 2021

    @mourner
    Member

    Had to remove it again because apparently OffscreenCanvas doesn't respect the willReadFrequently context option on current Chrome 93 (which appears to be fixed on 94 beta), causing performance issues. Will readd it in the future when there are no performance issues across current browsers.

  6. morandd commented on Sep 23, 2022

    @morandd

    It seems Chrome added willReadFrequently support back in R99?
    https://chromestatus.com/feature/6051647656558592

  7. added a commit that references this issue on Apr 7, 2026
    05d208a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions