-
Notifications
You must be signed in to change notification settings - Fork 38
Improve canvas text #841
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
Open
jfboeve
wants to merge
8
commits into
main
Choose a base branch
from
fix/improve-canvas-text
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Improve canvas text #841
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a559cfd
Support OffscreenCanvas for canvas text rendering
jorge-graca-sky 4a71316
Improve canvas text (#831)
jfboeve 7b765f8
added createOffscreenCanvas fn
jfboeve 6def141
updated createOffscreenCanvas
jfboeve 81f138a
fix change
jfboeve df859fa
use this.createCanvas
jfboeve 925a634
adjust type comment
jfboeve 39e6123
improve if canvas is offscreen check
jfboeve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,12 +35,16 @@ import { normalizeCanvasColor } from '../lib/colorCache.js'; | |||||||||||||||||||||||||||
| const type = 'canvas' as const; | ||||||||||||||||||||||||||||
| const font: FontHandler = CanvasFontHandler; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| let stage: Stage | null = null; | ||||||||||||||||||||||||||||
| let canvas: HTMLCanvasElement | OffscreenCanvas | null = null; | ||||||||||||||||||||||||||||
| let context: | ||||||||||||||||||||||||||||
| | CanvasRenderingContext2D | ||||||||||||||||||||||||||||
| | OffscreenCanvasRenderingContext2D | ||||||||||||||||||||||||||||
| | null = null; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Whether the drawing canvas is an OffscreenCanvas (supports transferToImageBitmap) | ||||||||||||||||||||||||||||
| let isOffscreen = false; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Separate canvas and context for text measurements | ||||||||||||||||||||||||||||
| let measureCanvas: HTMLCanvasElement | OffscreenCanvas | null = null; | ||||||||||||||||||||||||||||
| let measureContext: | ||||||||||||||||||||||||||||
|
|
@@ -56,26 +60,32 @@ const renderInfoCache = new Map<string, CanvasRenderInfo>(); | |||||||||||||||||||||||||||
| const _richTextResult = new ParseResult(); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Initialize the Text Renderer | ||||||||||||||||||||||||||||
| const init = (stage: Stage): void => { | ||||||||||||||||||||||||||||
| const init = (_stage: Stage): void => { | ||||||||||||||||||||||||||||
| stage = _stage; | ||||||||||||||||||||||||||||
| const dpr = stage.options.devicePhysicalPixelRatio; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Drawing canvas and context | ||||||||||||||||||||||||||||
| canvas = stage.platform.createCanvas() as HTMLCanvasElement | OffscreenCanvas; | ||||||||||||||||||||||||||||
| context = canvas.getContext('2d', { willReadFrequently: true }) as | ||||||||||||||||||||||||||||
| canvas = stage.platform.createOffscreenCanvas(); | ||||||||||||||||||||||||||||
| if (canvas !== null) { | ||||||||||||||||||||||||||||
| isOffscreen = true; | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| isOffscreen = false; | ||||||||||||||||||||||||||||
| canvas = stage.platform.createCanvas(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+68
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied this, it looks a lot neater! Thanks! |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| context = canvas.getContext('2d') as | ||||||||||||||||||||||||||||
| | CanvasRenderingContext2D | ||||||||||||||||||||||||||||
| | OffscreenCanvasRenderingContext2D; | ||||||||||||||||||||||||||||
| assertTruthy(context, '.getContext(2d) failed'); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| context.setTransform(dpr, 0, 0, dpr, 0, 0); | ||||||||||||||||||||||||||||
| context.textRendering = 'optimizeSpeed'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Separate measuring canvas and context | ||||||||||||||||||||||||||||
| measureCanvas = stage.platform.createCanvas() as | ||||||||||||||||||||||||||||
| | HTMLCanvasElement | ||||||||||||||||||||||||||||
| | OffscreenCanvas; | ||||||||||||||||||||||||||||
| measureContext = measureCanvas.getContext('2d') as | ||||||||||||||||||||||||||||
| | CanvasRenderingContext2D | ||||||||||||||||||||||||||||
| | OffscreenCanvasRenderingContext2D; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| measureCanvas = stage.platform.createCanvas(); | ||||||||||||||||||||||||||||
| assertTruthy(measureCanvas, 'createCanvas returned null'); | ||||||||||||||||||||||||||||
| measureContext = measureCanvas.getContext('2d'); | ||||||||||||||||||||||||||||
| assertTruthy(measureContext, '.getContext(2d) failed'); | ||||||||||||||||||||||||||||
| measureContext.setTransform(dpr, 0, 0, dpr, 0, 0); | ||||||||||||||||||||||||||||
| measureContext.textRendering = 'optimizeSpeed'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -165,8 +175,21 @@ const renderText = (props: CoreTextNodeProps): TextRenderInfo => { | |||||||||||||||||||||||||||
| const canvasW = Math.ceil(effectiveWidth); | ||||||||||||||||||||||||||||
| const canvasH = Math.ceil(effectiveHeight); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| canvas.width = canvasW; | ||||||||||||||||||||||||||||
| canvas.height = canvasH; | ||||||||||||||||||||||||||||
| // For OffscreenCanvas we reuse the shared canvas and snapshot via | ||||||||||||||||||||||||||||
| // transferToImageBitmap at the end. For HTMLCanvasElement we allocate a | ||||||||||||||||||||||||||||
| // fresh canvas per text node so it can be passed directly as the texture | ||||||||||||||||||||||||||||
| // source — no pixel readback or intermediate copy needed. | ||||||||||||||||||||||||||||
| let drawCanvas: HTMLCanvasElement | OffscreenCanvas; | ||||||||||||||||||||||||||||
| if (isOffscreen) { | ||||||||||||||||||||||||||||
| drawCanvas = canvas; | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| assertTruthy(stage, 'Stage is not available'); | ||||||||||||||||||||||||||||
| drawCanvas = stage.platform.createCanvas(); | ||||||||||||||||||||||||||||
| context = drawCanvas.getContext('2d') as CanvasRenderingContext2D; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| drawCanvas.width = canvasW; | ||||||||||||||||||||||||||||
| drawCanvas.height = canvasH; | ||||||||||||||||||||||||||||
| context.fillStyle = 'white'; | ||||||||||||||||||||||||||||
| context.font = baseFont; | ||||||||||||||||||||||||||||
| context.textBaseline = 'hanging'; | ||||||||||||||||||||||||||||
|
|
@@ -357,10 +380,26 @@ const renderText = (props: CoreTextNodeProps): TextRenderInfo => { | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Extract image data | ||||||||||||||||||||||||||||
| let imageData: ImageData | null = null; | ||||||||||||||||||||||||||||
| if (canvas.width > 0 && canvas.height > 0) { | ||||||||||||||||||||||||||||
| imageData = context.getImageData(0, 0, canvasW, canvasH); | ||||||||||||||||||||||||||||
| // Capture the rendered text as a texture source. | ||||||||||||||||||||||||||||
| // OffscreenCanvas: transferToImageBitmap detaches the backing bitmap | ||||||||||||||||||||||||||||
| // synchronously (zero-copy). The shared canvas gets a fresh empty bitmap. | ||||||||||||||||||||||||||||
| // HTMLCanvasElement: drawCanvas was allocated specifically for this text | ||||||||||||||||||||||||||||
| // node, so it can be passed directly — no copy needed. | ||||||||||||||||||||||||||||
| let imageData: ImageBitmap | HTMLCanvasElement | null = null; | ||||||||||||||||||||||||||||
| if (drawCanvas.width > 0 && drawCanvas.height > 0) { | ||||||||||||||||||||||||||||
| if (isOffscreen) { | ||||||||||||||||||||||||||||
| assertTruthy( | ||||||||||||||||||||||||||||
| drawCanvas instanceof OffscreenCanvas, | ||||||||||||||||||||||||||||
| 'drawCanvas is not an OffscreenCanvas', | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| imageData = drawCanvas.transferToImageBitmap(); | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| assertTruthy( | ||||||||||||||||||||||||||||
| drawCanvas instanceof HTMLCanvasElement, | ||||||||||||||||||||||||||||
| 'drawCanvas is not an HTMLCanvasElement', | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| imageData = drawCanvas; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| const renderInfo = { | ||||||||||||||||||||||||||||
| type, | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.