-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
@remotion/web-renderer: Add support for text shadows
#6607
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
Merged
+212
−59
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
59 changes: 59 additions & 0 deletions
59
packages/web-renderer/src/drawing/text/parse-text-shadow.ts
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| export interface TextShadow { | ||
| offsetX: number; | ||
| offsetY: number; | ||
| blurRadius: number; | ||
| color: string; | ||
| } | ||
|
|
||
| export const parseTextShadow = (textShadowValue: string): TextShadow[] => { | ||
| if (!textShadowValue || textShadowValue === 'none') { | ||
| return []; | ||
| } | ||
|
|
||
| const shadows: TextShadow[] = []; | ||
|
|
||
| // Split by comma, but respect rgba() colors | ||
| const shadowStrings = textShadowValue.split(/,(?![^(]*\))/); | ||
|
|
||
| for (const shadowStr of shadowStrings) { | ||
| const trimmed = shadowStr.trim(); | ||
| if (!trimmed || trimmed === 'none') { | ||
| continue; | ||
| } | ||
|
|
||
| const shadow: TextShadow = { | ||
| offsetX: 0, | ||
| offsetY: 0, | ||
| blurRadius: 0, | ||
| color: 'rgba(0, 0, 0, 0.5)', | ||
| }; | ||
|
|
||
| let remaining = trimmed; | ||
|
|
||
| // Extract color (can be rgb(), rgba(), hsl(), hsla(), hex, or named color) | ||
| const colorMatch = remaining.match( | ||
| /(rgba?\([^)]+\)|hsla?\([^)]+\)|#[0-9a-f]{3,8}|[a-z]+)/i, | ||
| ); | ||
| if (colorMatch) { | ||
| shadow.color = colorMatch[0]; | ||
| remaining = remaining.replace(colorMatch[0], '').trim(); | ||
| } | ||
|
|
||
| // Parse remaining numeric values (offset-x offset-y blur-radius) | ||
| const numbers = remaining.match(/[+-]?\d*\.?\d+(?:px|em|rem|%)?/gi) || []; | ||
| const values = numbers.map((n) => parseFloat(n) || 0); | ||
|
|
||
| if (values.length >= 2) { | ||
| shadow.offsetX = values[0]; | ||
| shadow.offsetY = values[1]; | ||
|
|
||
| if (values.length >= 3) { | ||
| shadow.blurRadius = Math.max(0, values[2]); | ||
| } | ||
| } | ||
|
|
||
| shadows.push(shadow); | ||
| } | ||
|
|
||
| return shadows; | ||
| }; | ||
JonnyBurger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
Binary file added
BIN
+25.3 KB
...r/src/test/__screenshots__/text-shadow.test.tsx/text-shadow-chromium-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24 KB
...er/src/test/__screenshots__/text-shadow.test.tsx/text-shadow-firefox-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+21.8 KB
...rer/src/test/__screenshots__/text-shadow.test.tsx/text-shadow-webkit-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions
85
packages/web-renderer/src/test/fixtures/text/text-shadow.tsx
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import React from 'react'; | ||
| import {AbsoluteFill} from 'remotion'; | ||
|
|
||
| const Component: React.FC = () => { | ||
| return ( | ||
| <AbsoluteFill | ||
| style={{ | ||
| backgroundColor: 'white', | ||
| padding: 20, | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| gap: 20, | ||
| }} | ||
| > | ||
| {/* Simple text shadow */} | ||
| <div | ||
| style={{ | ||
| fontSize: 30, | ||
| fontWeight: 'bold', | ||
| textShadow: '2px 2px 4px rgba(0, 0, 0, 0.5)', | ||
| }} | ||
| > | ||
| Shadow | ||
| </div> | ||
|
|
||
| {/* Colored text shadow */} | ||
| <div | ||
| style={{ | ||
| fontSize: 30, | ||
| fontWeight: 'bold', | ||
| color: 'blue', | ||
| textShadow: '3px 3px 0px red', | ||
| }} | ||
| > | ||
| Color | ||
| </div> | ||
|
|
||
| {/* Multiple text shadows */} | ||
| <div | ||
| style={{ | ||
| fontSize: 30, | ||
| fontWeight: 'bold', | ||
| textShadow: | ||
| '1px 1px 2px red, 0 0 10px blue, 0 0 20px rgba(0, 0, 255, 0.3)', | ||
| }} | ||
| > | ||
| Multi | ||
| </div> | ||
|
|
||
| {/* Text shadow with no blur */} | ||
| <div | ||
| style={{ | ||
| fontSize: 30, | ||
| fontWeight: 'bold', | ||
| textShadow: '2px 2px 0 black', | ||
| }} | ||
| > | ||
| Hard | ||
| </div> | ||
|
|
||
| {/* Text shadow with glow effect */} | ||
| <div | ||
| style={{ | ||
| fontSize: 30, | ||
| fontWeight: 'bold', | ||
| color: 'white', | ||
| backgroundColor: '#333', | ||
| padding: 10, | ||
| textShadow: '0 0 10px white, 0 0 20px white', | ||
| }} | ||
| > | ||
| Glow | ||
| </div> | ||
| </AbsoluteFill> | ||
| ); | ||
| }; | ||
|
|
||
| export const textShadow = { | ||
| component: Component, | ||
| id: 'text-shadow', | ||
| width: 300, | ||
| height: 400, | ||
| fps: 25, | ||
| durationInFrames: 1, | ||
| } as const; |
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import {test} from 'vitest'; | ||
| import {renderStillOnWeb} from '../render-still-on-web'; | ||
| import '../symbol-dispose'; | ||
| import {textShadow} from './fixtures/text/text-shadow'; | ||
| import {testImage} from './utils'; | ||
|
|
||
| test('should render text-shadow', async () => { | ||
| const {blob} = await renderStillOnWeb({ | ||
| licenseKey: 'free-license', | ||
| composition: textShadow, | ||
| frame: 0, | ||
| inputProps: {}, | ||
| imageFormat: 'png', | ||
| }); | ||
|
|
||
| await testImage({blob, testId: 'text-shadow', threshold: 0.01}); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heads up: each shadow pass calls
fillText, which draws both the shadow and the text body. So for N shadows the text body is composited N+1 times. For opaque text this is visually correct, but for semi-transparentwebkitTextFillColorthe text body would appear more opaque than CSStext-shadowrenders it.The
box-shadowimplementation works around this withdestination-outcompositing on an offscreen canvas. A similar technique could be applied here for pixel-perfect fidelity, but it's a reasonable trade-off for v1.