Skip to content

Commit 7407ef1

Browse files
authored
Make GLText work in workers (cruise-automation#622)
This is half-reasonable, half-hack: * Preferring `OffscreenCanvas`, and detecting it at runtime is reasonable. * Hacking `document` for TinySDF is a bit crazy. I've raised an issue upstream, hopefully this won't live long. Test plan: Linked against a local webviz and opened a bag containing a strange character with the worker flag on.
1 parent 05c5ab9 commit 7407ef1

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/regl-worldview/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "regl-worldview",
3-
"version": "0.16.1",
3+
"version": "0.16.2",
44
"description": "A reusable component for rendering 2D and 3D views using regl",
55
"license": "Apache-2.0",
66
"repository": "cruise-automation/webviz/tree/master/packages/regl-worldview",

packages/regl-worldview/src/commands/GLText.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import { createInstancedGetChildrenForHitmap } from "../utils/getChildrenForHitm
1111
import Command, { type CommonCommandProps } from "./Command";
1212
import { isColorDark, type TextMarker } from "./Text";
1313

14+
// HACK: TinySDF doesn't agree with workers. Until support is added, hack this to make it work.
15+
// TODO(steel): Upstream the fix in memoizedCreateCanvas.
16+
if (!self.document) {
17+
// $FlowFixMe: Flow doesn't know about OffscreenCanvas.
18+
self.document = { createElement: () => new OffscreenCanvas(0, 0) };
19+
}
20+
1421
// The GLText command renders text from a Signed Distance Field texture.
1522
// There are many external resources about SDFs and text rendering in WebGL, including:
1623
// https://steamcdn-a.akamaihd.net/apps/valve/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf
@@ -80,7 +87,8 @@ const BG_COLOR_LIGHT = Object.freeze({ r: 1, g: 1, b: 1, a: 1 });
8087
const BG_COLOR_DARK = Object.freeze({ r: 0, g: 0, b: 0, a: 1 });
8188

8289
const memoizedCreateCanvas = memoizeOne((font) => {
83-
const canvas = document.createElement("canvas");
90+
// $FlowFixMe: Flow doesn't know about OffscreenCanvas.
91+
const canvas: HTMLCanvasElement = self.OffscreenCanvas ? new OffscreenCanvas(0, 0) : document.createElement("canvas");
8492
const ctx = canvas.getContext("2d");
8593
ctx.font = font;
8694
return ctx;

0 commit comments

Comments
 (0)