Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OffscreenCanvas when in worker contexts #35

Open
MatthewSteel opened this issue Apr 15, 2021 · 6 comments
Open

OffscreenCanvas when in worker contexts #35

MatthewSteel opened this issue Apr 15, 2021 · 6 comments

Comments

@MatthewSteel
Copy link

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.

@mourner
Copy link
Member

mourner commented Apr 16, 2021

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);
}
}

@MatthewSteel
Copy link
Author

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

@mourner
Copy link
Member

mourner commented Apr 16, 2021

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

@mourner
Copy link
Member

mourner commented Sep 22, 2021

Added as a part of ff5cef8

@mourner mourner closed this as completed Sep 22, 2021
@mourner mourner reopened this Sep 24, 2021
@mourner
Copy link
Member

mourner commented Sep 24, 2021

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.

@morandd
Copy link

morandd commented Sep 23, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants