Improve canvas text#841
Open
jfboeve wants to merge 5 commits into
Open
Conversation
Use bitmap/canvas instead of ImageData
`getImageData` forces the browser to copy the pixels drawn by the canvas to JS world. By using ImageBitmap instead the pixels can be drawn on the GPU and stay there.
|
|
eXecutable
reviewed
Jul 15, 2026
eXecutable
reviewed
Jul 15, 2026
Comment on lines
+68
to
+73
| canvas = stage.platform.createOffscreenCanvas(); | ||
| isOffscreen = | ||
| typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas; | ||
| if (canvas === null) { | ||
| canvas = stage.platform.createCanvas(); | ||
| } |
Contributor
There was a problem hiding this comment.
Suggested change
| canvas = stage.platform.createOffscreenCanvas(); | |
| isOffscreen = | |
| typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas; | |
| if (canvas === null) { | |
| canvas = stage.platform.createCanvas(); | |
| } | |
| canvas = stage.platform.createOffscreenCanvas(); | |
| if (canvas) { | |
| isOffscreen = true; | |
| } else { | |
| isOffscreen = false; | |
| canvas = stage.platform.createCanvas(); | |
| } |
eXecutable
reviewed
Jul 15, 2026
|
|
||
| // If a canvas was provided in the settings, use it. Otherwise, create a new one. | ||
| this.canvas = settings.canvas || this.createCanvas(); | ||
| this.canvas = settings.canvas || document.createElement('canvas'); |
Contributor
There was a problem hiding this comment.
Suggested change
| this.canvas = settings.canvas || document.createElement('canvas'); | |
| this.canvas = settings.canvas || this.createCanvas(); |
eXecutable
reviewed
Jul 15, 2026
|
|
||
| /** | ||
| * Creates a new canvas element. | ||
| * Creates a new canvas. |
Contributor
There was a problem hiding this comment.
Suggested change
| * Creates a new canvas. | |
| * Creates a new canvas element. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request refactors the text rendering pipeline to improve performance and flexibility by allowing the use of
ImageBitmapandHTMLCanvasElementas texture sources, in addition toImageData. The changes optimize how text is rendered and transferred to GPU textures, reduce unnecessary pixel readbacks and copies, and add support for offscreen and onscreen canvas workflows. The most important changes are grouped below:Text rendering pipeline improvements
ImageBitmap(forOffscreenCanvas) or a dedicatedHTMLCanvasElement(for onscreen rendering) as the texture source, instead of always usingImageData. This enables zero-copy transfers to the GPU and avoids unnecessary pixel readbacks. (src/core/text-rendering/CanvasTextRenderer.ts,src/core/text-rendering/TextRenderer.ts) [1] [2] [3]transferToImageBitmap()forOffscreenCanvasand allocating a newHTMLCanvasElementper text node otherwise. (src/core/text-rendering/CanvasTextRenderer.ts) [1] [2] [3]Texture and image source handling
ImageTextureandTextureDatatypes now supportImageBitmapandHTMLCanvasElementas valid texture sources, in addition toImageData. (src/core/textures/ImageTexture.ts,src/core/textures/Texture.ts) [1] [2] [3]HTMLCanvasElementas a valid image source for textures. (src/core/renderers/canvas/CanvasTexture.ts,src/core/renderers/webgl/WebGlCtxTexture.ts,src/core/renderers/webgl/WebGlCtxSubTexture.ts) [1] [2] [3]Platform and canvas management
createCanvas()(for onscreen) andcreateOffscreenCanvas()(for offscreen or worker rendering), and the web platform implements both methods. The renderer now uses these appropriately. (src/core/platforms/Platform.ts,src/core/platforms/web/WebPlatform.ts) [1] [2]These changes collectively make the text rendering pipeline more efficient, flexible, and compatible with modern browser features.