🎥 Make videos programmatically with React
`@remotion/starburst`: Accept array of colors instead of color1/color2
Jonny Burgerd5fef18651d7
diff --git a/packages/docs/docs/starburst-guide.mdx b/packages/docs/docs/starburst-guide.mdxindex 6745ae1386..9f286bb546 100644--- a/packages/docs/docs/starburst-guide.mdx+++ b/packages/docs/docs/starburst-guide.mdx@@ -16,7 +16,7 @@ import {AbsoluteFill} from 'remotion';const MyComp = () => {return (<AbsoluteFill style={{backgroundColor: 'black'}}>- <Starburst durationInFrames={60} rays={16} color1="#ffdd00" color2="#ff8800" />+ <Starburst durationInFrames={60} rays={16} colors={['#ffdd00', '#ff8800']} /></AbsoluteFill>);};@@ -42,7 +42,7 @@ const MyComp = () => {## Changing the colors-Use [`color1`](/docs/starburst/starburst#color1) and [`color2`](/docs/starburst/starburst#color2) to set the alternating ray colors:+Use the [`colors`](/docs/starburst/starburst#colors) prop to set the ray colors. You can pass any number of colors (minimum 2) and they will be assigned to rays cyclically:```tsx twoslash title="CustomColors.tsx"import {Starburst} from '@remotion/starburst';@@ -52,7 +52,7 @@ import {AbsoluteFill} from 'remotion';const MyComp = () => {return (<AbsoluteFill style={{backgroundColor: 'black'}}>- <Starburst color1="#ff0066" color2="#6600ff" durationInFrames={30} />+ <Starburst colors={['#ff0066', '#6600ff', '#00ccff']} durationInFrames={30} /></AbsoluteFill>);};@@ -95,7 +95,7 @@ const StarburstTransition = () => {<Fill color="#0b84f3" /></TransitionSeries.Sequence><TransitionSeries.Overlay durationInFrames={20}>- <Starburst rays={12} color1="#ffdd00" color2="#ff8800" />+ <Starburst rays={12} colors={['#ffdd00', '#ff8800']} /></TransitionSeries.Overlay><TransitionSeries.Sequence durationInFrames={60}><Fill color="pink" />diff --git a/packages/docs/docs/starburst/starburst.mdx b/packages/docs/docs/starburst/starburst.mdxindex e80a6e0ebc..faf75b58ce 100644--- a/packages/docs/docs/starburst/starburst.mdx+++ b/packages/docs/docs/starburst/starburst.mdx@@ -21,8 +21,7 @@ const MyVideo = () => {<StarburstdurationInFrames={60}rays={16}- color1="#ffdd00"- color2="#ff8800"+ colors={['#ffdd00', '#ff8800', '#ff4400']}rotation={15}/></AbsoluteFill>@@ -44,13 +43,13 @@ If not specified, defaults to the duration of the composition or sequence, readiThe number of rays in the starburst pattern. Must be between `2` and `100`. Default: `12`.-### `color1?`+### `colors?`-The primary color of the starburst rays as a hex string. Default: `"#ffdd00"` (yellow).+An array of hex color strings for the rays. Colors are assigned to rays cyclically. Must contain at least 2 colors. Default: `['#ffdd00', '#ff8800']`.-### `color2?`--The secondary (alternating) color of the starburst rays as a hex string. Default: `"#ff8800"` (orange).+```tsx+<Starburst colors={['#ff0066', '#6600ff', '#00ccff']} />+```### `rotation?`diff --git a/packages/example/src/Starburst/index.tsx b/packages/example/src/Starburst/index.tsxindex a9ea187f6e..8daf462da5 100644--- a/packages/example/src/Starburst/index.tsx+++ b/packages/example/src/Starburst/index.tsx@@ -8,15 +8,13 @@ export const StarburstExample: React.FC = () => {<StarburstdurationInFrames={60}rays={16}- color1="#ffdd00"- color2="#ff8800"+ colors={['#ffdd00', '#ff8800']}/><StarburstdurationInFrames={60}- rays={8}- color1="#ff0066"- color2="#6600ff"- rotation={22.5}+ rays={12}+ colors={['#ff0066', '#6600ff', '#00ccff']}+ rotation={15}/></AbsoluteFill>);diff --git a/packages/starburst/src/Starburst.tsx b/packages/starburst/src/Starburst.tsxindex 79e5f34abc..d97bebf601 100644--- a/packages/starburst/src/Starburst.tsx+++ b/packages/starburst/src/Starburst.tsx@@ -18,12 +18,13 @@ export type StarburstProps = Omit<Omit<AbsoluteFillLayout, 'layout'> & {readonly durationInFrames?: number;readonly rays?: number;- readonly color1?: string;- readonly color2?: string;+ readonly colors?: readonly string[];readonly rotation?: number;readonly smoothness?: number;};+const DEFAULT_COLORS = ['#ffdd00', '#ff8800'] as const;+const hexToRgb = (hex: string): [number, number, number] => {const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);if (!result) {@@ -50,11 +51,13 @@ precision mediump float;#endifuniform float numRays;-uniform vec3 color1;-uniform vec3 color2;uniform float rotationOffset;uniform float smoothEdge;uniform vec2 resolution;+uniform sampler2D colorPalette;+uniform float numColors;++const float Pi = 3.14159265359;void main() {vec2 uv = gl_FragCoord.xy / resolution;@@ -62,12 +65,27 @@ void main() {center.x *= resolution.x / resolution.y;float angle = atan(center.y, center.x) + rotationOffset;- float rayPattern = cos(angle * numRays) * 0.5 + 0.5;+ float normalizedAngle = (angle + Pi) / (2.0 * Pi);+ float sector = normalizedAngle * numRays;+ float rayIndex = mod(floor(sector), numRays);- float edgeSmooth = smoothEdge * 0.5;- float ray = smoothstep(0.5 - edgeSmooth, 0.5 + edgeSmooth, rayPattern);+ float colorIndex = mod(rayIndex, numColors);+ float texCoord = (colorIndex + 0.5) / numColors;+ vec3 col = texture2D(colorPalette, vec2(texCoord, 0.5)).rgb;- vec3 col = mix(color2, color1, ray);+ float fractSector = fract(sector);+ float edgeSmooth = smoothEdge * 0.5;+ float nextColorIndex = mod(rayIndex + 1.0, numColors);+ float nextTexCoord = (nextColorIndex + 0.5) / numColors;+ vec3 nextCol = texture2D(colorPalette, vec2(nextTexCoord, 0.5)).rgb;++ float blend = smoothstep(1.0 - edgeSmooth, 1.0, fractSector);+ col = mix(col, nextCol, blend);+ float blendStart = smoothstep(edgeSmooth, 0.0, fractSector);+ float prevColorIndex = mod(rayIndex - 1.0 + numColors, numColors);+ float prevTexCoord = (prevColorIndex + 0.5) / numColors;+ vec3 prevCol = texture2D(colorPalette, vec2(prevTexCoord, 0.5)).rgb;+ col = mix(col, prevCol, blendStart);gl_FragColor = vec4(col, 1.0);}@@ -77,19 +95,18 @@ type GlContext = {gl: WebGLRenderingContext;resLoc: WebGLUniformLocation;numRaysLoc: WebGLUniformLocation;- color1Loc: WebGLUniformLocation;- color2Loc: WebGLUniformLocation;rotationOffsetLoc: WebGLUniformLocation;smoothEdgeLoc: WebGLUniformLocation;+ numColorsLoc: WebGLUniformLocation;+ colorTexture: WebGLTexture;};const StarburstCanvas: React.FC<{readonly rays: number;- readonly color1: string;- readonly color2: string;+ readonly colors: readonly string[];readonly rotation: number;readonly smoothness: number;-}> = ({rays, color1, color2, rotation, smoothness}) => {+}> = ({rays, colors, rotation, smoothness}) => {const {width, height} = useVideoConfig();const canvasRef = useRef<HTMLCanvasElement>(null);const glRef = useRef<GlContext | null>(null);@@ -139,16 +156,27 @@ const StarburstCanvas: React.FC<{gl.enableVertexAttribArray(pos);gl.vertexAttribPointer(pos, 2, gl.FLOAT, false, 0, 0);+ const colorTexture = gl.createTexture()!;+ gl.activeTexture(gl.TEXTURE0);+ gl.bindTexture(gl.TEXTURE_2D, colorTexture);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);++ const paletteLoc = gl.getUniformLocation(program, 'colorPalette')!;+ gl.uniform1i(paletteLoc, 0);+continueRender(handle);return {gl,resLoc: gl.getUniformLocation(program, 'resolution')!,numRaysLoc: gl.getUniformLocation(program, 'numRays')!,- color1Loc: gl.getUniformLocation(program, 'color1')!,- color2Loc: gl.getUniformLocation(program, 'color2')!,rotationOffsetLoc: gl.getUniformLocation(program, 'rotationOffset')!,smoothEdgeLoc: gl.getUniformLocation(program, 'smoothEdge')!,+ numColorsLoc: gl.getUniformLocation(program, 'numColors')!,+ colorTexture,};},[continueRender, handle, cancelRender],@@ -167,27 +195,47 @@ const StarburstCanvas: React.FC<{gl,resLoc,numRaysLoc,- color1Loc,- color2Loc,rotationOffsetLoc,smoothEdgeLoc,+ numColorsLoc,+ colorTexture,} = ctx;- const rgb1 = hexToRgb(color1);- const rgb2 = hexToRgb(color2);const rotationRad = (rotation * Math.PI) / 180;+ const pixelData = new Uint8Array(colors.length * 4);+ for (let i = 0; i < colors.length; i++) {+ const rgb = hexToRgb(colors[i]);+ pixelData[i * 4] = Math.round(rgb[0] * 255);+ pixelData[i * 4 + 1] = Math.round(rgb[1] * 255);+ pixelData[i * 4 + 2] = Math.round(rgb[2] * 255);+ pixelData[i * 4 + 3] = 255;+ }++ gl.activeTexture(gl.TEXTURE0);+ gl.bindTexture(gl.TEXTURE_2D, colorTexture);+ gl.texImage2D(+ gl.TEXTURE_2D,+ 0,+ gl.RGBA,+ colors.length,+ 1,+ 0,+ gl.RGBA,+ gl.UNSIGNED_BYTE,+ pixelData,+ );+gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);gl.clearColor(0, 0, 0, 0);gl.clear(gl.COLOR_BUFFER_BIT);gl.uniform1f(numRaysLoc, rays);- gl.uniform3f(color1Loc, rgb1[0], rgb1[1], rgb1[2]);- gl.uniform3f(color2Loc, rgb2[0], rgb2[1], rgb2[2]);+ gl.uniform1f(numColorsLoc, colors.length);gl.uniform1f(rotationOffsetLoc, rotationRad);gl.uniform1f(smoothEdgeLoc, smoothness);gl.uniform2f(resLoc, gl.canvas.width, gl.canvas.height);gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);- }, [rays, color1, color2, rotation, smoothness, width, height]);+ }, [rays, colors, rotation, smoothness, width, height]);return (<AbsoluteFill>@@ -261,8 +309,7 @@ const StarburstInner: React.FC<}> = ({rays = 12,- color1 = '#ffdd00',- color2 = '#ff8800',+ colors = DEFAULT_COLORS,rotation = 0,smoothness = 0.1,durationInFrames,@@ -283,6 +330,12 @@ const StarburstInner: React.FC<throw new RangeError(`"rays" must be between 2 and 100, but got ${rays}`);}+ if (!Array.isArray(colors) || colors.length < 2) {+ throw new TypeError(+ `"colors" must be an array with at least 2 colors, but got ${JSON.stringify(colors)}`,+ );+ }+if (typeof rotation !== 'number' || !Number.isFinite(rotation)) {throw new TypeError(`"rotation" must be a finite number, but got ${JSON.stringify(rotation)}`,@@ -311,8 +364,7 @@ const StarburstInner: React.FC<><StarburstCanvasrays={rays}- color1={color1}- color2={color2}+ colors={colors}rotation={rotation}smoothness={smoothness}/>