Skip to content

🎥 Make videos programmatically with React

refactor bg drawing

Jonny Burger92075003c77a
diff --git a/packages/web-renderer/src/drawing/draw-background.ts b/packages/web-renderer/src/drawing/draw-background.ts
index 57b08a00c3..9bec00b20b 100644
--- a/packages/web-renderer/src/drawing/draw-background.ts
+++ b/packages/web-renderer/src/drawing/draw-background.ts
@@ -1,10 +1,7 @@
import type {LogLevel} from 'remotion';
import type {InternalState} from '../internal-state';
+import {getBackgroundFill} from './get-background-fill';
import {getBoxBasedOnBackgroundClip} from './get-padding-box';
-import {
- createCanvasGradient,
- parseLinearGradient,
-} from './parse-linear-gradient';
import {precomposeDOMElement} from './precompose';
export const drawBackground = async ({
@@ -91,46 +88,26 @@ export const drawBackground = async ({
contextToDraw.globalCompositeOperation = 'source-in';
}
- if (backgroundImage && backgroundImage !== 'none') {
- const gradientInfo = parseLinearGradient(backgroundImage);
- if (gradientInfo) {
- const gradient = createCanvasGradient({
- ctx: contextToDraw,
- rect: boundingRect,
- gradientInfo,
- offsetLeft,
- offsetTop,
- });
- const originalFillStyle = contextToDraw.fillStyle;
- contextToDraw.fillStyle = gradient;
- contextToDraw.fillRect(
- boundingRect.left - offsetLeft,
- boundingRect.top - offsetTop,
- boundingRect.width,
- boundingRect.height,
- );
- contextToDraw.fillStyle = originalFillStyle;
- return;
- }
- }
+ const backgroundFill = getBackgroundFill({
+ backgroundImage,
+ backgroundColor,
+ contextToDraw,
+ boundingRect,
+ offsetLeft,
+ offsetTop,
+ });
- // Fallback to solid background color if no gradient was drawn
- if (
- backgroundColor &&
- backgroundColor !== 'transparent' &&
- !(
- backgroundColor.startsWith('rgba') &&
- (backgroundColor.endsWith(', 0)') || backgroundColor.endsWith(',0'))
- )
- ) {
- const originalFillStyle = contextToDraw.fillStyle;
- contextToDraw.fillStyle = backgroundColor;
- contextToDraw.fillRect(
- boundingRect.left - offsetLeft,
- boundingRect.top - offsetTop,
- boundingRect.width,
- boundingRect.height,
- );
- contextToDraw.fillStyle = originalFillStyle;
+ if (!backgroundFill) {
+ return;
}
+
+ const originalFillStyle = contextToDraw.fillStyle;
+ contextToDraw.fillStyle = backgroundFill;
+ contextToDraw.fillRect(
+ boundingRect.left - offsetLeft,
+ boundingRect.top - offsetTop,
+ boundingRect.width,
+ boundingRect.height,
+ );
+ contextToDraw.fillStyle = originalFillStyle;
};
diff --git a/packages/web-renderer/src/drawing/get-background-fill.ts b/packages/web-renderer/src/drawing/get-background-fill.ts
new file mode 100644
index 0000000000..d14353e77b
--- /dev/null
+++ b/packages/web-renderer/src/drawing/get-background-fill.ts
@@ -0,0 +1,53 @@
+import {
+ createCanvasGradient,
+ parseLinearGradient,
+} from './parse-linear-gradient';
+
+const isColorTransparent = (color: string) => {
+ return (
+ color === 'transparent' ||
+ (color.startsWith('rgba') &&
+ (color.endsWith(', 0)') || color.endsWith(',0')))
+ );
+};
+
+export const getBackgroundFill = ({
+ backgroundColor,
+ backgroundImage,
+ contextToDraw,
+ boundingRect,
+ offsetLeft,
+ offsetTop,
+}: {
+ backgroundImage: string;
+ backgroundColor: string;
+ contextToDraw: OffscreenCanvasRenderingContext2D;
+ boundingRect: DOMRect;
+ offsetLeft: number;
+ offsetTop: number;
+}): CanvasGradient | string | null => {
+ if (backgroundImage && backgroundImage !== 'none') {
+ const gradientInfo = parseLinearGradient(backgroundImage);
+ if (gradientInfo) {
+ const gradient = createCanvasGradient({
+ ctx: contextToDraw,
+ rect: boundingRect,
+ gradientInfo,
+ offsetLeft,
+ offsetTop,
+ });
+
+ return gradient;
+ }
+ }
+
+ if (
+ backgroundColor &&
+ backgroundColor !== 'transparent' &&
+ !isColorTransparent(backgroundColor)
+ ) {
+ return backgroundColor;
+ }
+
+ return null;
+};