Skip to content

Commit

Permalink
Allow rendering in WebWorkers
Browse files Browse the repository at this point in the history
  • Loading branch information
FranCarrascosaMS committed Oct 3, 2023
1 parent 55521de commit 18ae87c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/troika-2d/src/HitTestContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* whether an Object2DFacade is under the mouse cursor, by passing this as the context
* to its render() method.
*/

const hitTestContext = document.createElement('canvas').getContext('2d')
const canvas = typeof document === 'undefined' ? new OffscreenCanvas() : document.createElement('canvas')
const hitTestContext = canvas.getContext('2d')
hitTestContext.save()

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/troika-3d/src/facade/WorldTextureProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function makeWorldTextureProvider(WrappedFacadeClass) {
}
if (newWorldConfig) {
this.worldTexture.dispose()
const canvas = document.createElement('canvas')
const canvas = typeof document === 'undefined' ? new OffscreenCanvas() : document.createElement('canvas')
canvas.width = newWorldConfig.width
canvas.height = newWorldConfig.height
this.worldTexture = new CanvasTexture(canvas)
Expand Down
2 changes: 1 addition & 1 deletion packages/troika-animation/src/Interpolators.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const colorValueToNumber = (function() {

// 2D canvas for evaluating string values
if (!colorCanvas) {
colorCanvas = document.createElement('canvas')
colorCanvas = typeof document === 'undefined' ? new OffscreenCanvas() : document.createElement('canvas')
colorCanvasCtx = colorCanvas.getContext('2d')
}

Expand Down
2 changes: 1 addition & 1 deletion packages/troika-three-text/src/TextBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function getTextRenderInfo(args, callback) {
const glyphsPerRow = (textureWidth / sdfGlyphSize * 4)
let atlas = atlases[sdfGlyphSize]
if (!atlas) {
const canvas = document.createElement('canvas')
const canvas = typeof document === 'undefined' ? new OffscreenCanvas() : document.createElement('canvas')
canvas.width = textureWidth
canvas.height = sdfGlyphSize * 256 / glyphsPerRow // start tall enough to fit 256 glyphs
atlas = atlases[sdfGlyphSize] = {
Expand Down

0 comments on commit 18ae87c

Please sign in to comment.