Skip to content

🎥 Make videos programmatically with React

Refactor starburst origin to UV coordinate

Cursor Agent74455d74df43
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index c08b312841..e7aefa995a 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -2287,19 +2287,10 @@ export const effectsStarburstDemo: DemoType = {
optional: 'no',
},
{
- name: 'originOffsetX',
- type: 'numeric',
- default: 0,
- min: -1,
- max: 1,
- step: 0.01,
- optional: 'no',
- },
- {
- name: 'originOffsetY',
- type: 'numeric',
- default: 0,
- min: -1,
+ name: 'origin',
+ type: 'uv-coordinate',
+ default: [0.5, 0.5],
+ min: 0,
max: 1,
step: 0.01,
optional: 'no',
diff --git a/packages/docs/components/effects/effects-starburst-preview.tsx b/packages/docs/components/effects/effects-starburst-preview.tsx
index f180e28727..775d865d9f 100644
--- a/packages/docs/components/effects/effects-starburst-preview.tsx
+++ b/packages/docs/components/effects/effects-starburst-preview.tsx
@@ -12,9 +12,8 @@ export const EffectsStarburstPreview: React.FC<{
readonly rays: number;
readonly rotation: number;
readonly smoothness: number;
- readonly originOffsetX: number;
- readonly originOffsetY: number;
-}> = ({rays, rotation, smoothness, originOffsetX, originOffsetY}) => {
+ readonly origin: readonly [number, number];
+}> = ({rays, rotation, smoothness, origin}) => {
return (
<CanvasImage
src={EFFECTS_PREVIEW_IMAGE_SRC}
@@ -28,8 +27,7 @@ export const EffectsStarburstPreview: React.FC<{
colors: ['#ffdd00', '#ff8800'],
rotation,
smoothness,
- originOffsetX,
- originOffsetY,
+ origin,
}),
]}
/>
diff --git a/packages/docs/docs/starburst/starburst-effect.mdx b/packages/docs/docs/starburst/starburst-effect.mdx
index 1477d16af1..7bc6913168 100644
--- a/packages/docs/docs/starburst/starburst-effect.mdx
+++ b/packages/docs/docs/starburst/starburst-effect.mdx
@@ -39,6 +39,7 @@ export const MyComp: React.FC = () => {
colors: ['#ffdd00', '#ff8800', '#ff4400'],
rotation: 15,
smoothness: 0.1,
+ origin: [0.5, 0.5],
}),
]}
/>
@@ -67,13 +68,9 @@ Rotates the starburst pattern in degrees. Defaults to `0`.
Controls the softness of the ray edges. `0` gives hard edges, `1` gives very soft edges. Default: `0`.
-### `originOffsetX?`
+### `origin?`
-Shifts the origin of the starburst pattern horizontally. `-1` moves the origin to the left edge, `1` to the right edge. Default: `0` (centered).
-
-### `originOffsetY?`
-
-Shifts the origin of the starburst pattern vertically. `-1` moves the origin to the top edge, `1` to the bottom edge. Default: `0` (centered).
+UV coordinate of the starburst origin. `[0, 0]` is the top-left corner, `[1, 1]` is the bottom-right corner. Default: `[0.5, 0.5]`.
### `disabled?`
diff --git a/packages/starburst/src/index.ts b/packages/starburst/src/index.ts
index 488b151157..aec08ebab7 100644
--- a/packages/starburst/src/index.ts
+++ b/packages/starburst/src/index.ts
@@ -6,6 +6,7 @@ export {
starburst,
starburstEffectSchema,
type StarburstEffectParams,
+ type StarburstOrigin,
} from './starburst-effect';
export const StarburstInternals = {
diff --git a/packages/starburst/src/starburst-effect.ts b/packages/starburst/src/starburst-effect.ts
index 229145065c..ad711cf02d 100644
--- a/packages/starburst/src/starburst-effect.ts
+++ b/packages/starburst/src/starburst-effect.ts
@@ -4,6 +4,8 @@ import {hexToRgb} from './hex-to-rgb';
const {createEffect, createWebGL2ContextError} = Internals;
+const DEFAULT_ORIGIN = [0.5, 0.5] as const;
+
export const starburstEffectSchema = {
rays: {
type: 'number',
@@ -29,31 +31,24 @@ export const starburstEffectSchema = {
default: 0,
description: 'Edge Smoothness',
},
- originOffsetX: {
- type: 'number',
- min: -1,
- max: 1,
- step: 0.01,
- default: 0,
- description: 'Origin Offset X',
- },
- originOffsetY: {
- type: 'number',
- min: -1,
+ origin: {
+ type: 'uv-coordinate',
+ min: 0,
max: 1,
step: 0.01,
- default: 0,
- description: 'Origin Offset Y',
+ default: DEFAULT_ORIGIN,
+ description: 'Origin',
},
} as const satisfies SequenceSchema;
+export type StarburstOrigin = readonly [number, number];
+
export type StarburstEffectParams = {
readonly rays: number;
readonly colors: readonly string[];
readonly rotation?: number;
readonly smoothness?: number;
- readonly originOffsetX?: number;
- readonly originOffsetY?: number;
+ readonly origin?: StarburstOrigin;
};
type StarburstResolved = {
@@ -61,8 +56,7 @@ type StarburstResolved = {
colors: readonly string[];
rotation: number;
smoothness: number;
- originOffsetX: number;
- originOffsetY: number;
+ origin: StarburstOrigin;
};
const resolve = (p: StarburstEffectParams): StarburstResolved => ({
@@ -70,8 +64,7 @@ const resolve = (p: StarburstEffectParams): StarburstResolved => ({
colors: p.colors,
rotation: p.rotation ?? 0,
smoothness: p.smoothness ?? 0,
- originOffsetX: p.originOffsetX ?? 0,
- originOffsetY: p.originOffsetY ?? 0,
+ origin: (p.origin ?? DEFAULT_ORIGIN) as StarburstOrigin,
});
const validateStarburstEffectParams = (params: StarburstEffectParams): void => {
@@ -120,20 +113,18 @@ const validateStarburstEffectParams = (params: StarburstEffectParams): void => {
}
if (
- typeof r.originOffsetX !== 'number' ||
- !Number.isFinite(r.originOffsetX)
+ !Array.isArray(r.origin) ||
+ r.origin.length !== 2 ||
+ r.origin.some((coordinate) => {
+ return typeof coordinate !== 'number' || !Number.isFinite(coordinate);
+ })
) {
- throw new TypeError(
- `"originOffsetX" must be a finite number, but got ${JSON.stringify(params.originOffsetX)}`,
- );
+ throw new TypeError('"origin" must be a [number, number] tuple');
}
- if (
- typeof r.originOffsetY !== 'number' ||
- !Number.isFinite(r.originOffsetY)
- ) {
- throw new TypeError(
- `"originOffsetY" must be a finite number, but got ${JSON.stringify(params.originOffsetY)}`,
+ if (r.origin.some((coordinate) => coordinate < 0 || coordinate > 1)) {
+ throw new RangeError(
+ `"origin" must contain coordinates between 0 and 1, but got ${JSON.stringify(r.origin)}`,
);
}
@@ -161,7 +152,7 @@ uniform float rotationOffset;
uniform float smoothEdge;
uniform vec2 resolution;
uniform float numColors;
-uniform vec2 originOffset;
+uniform vec2 origin;
in vec2 vUv;
out vec4 fragColor;
@@ -170,7 +161,7 @@ const float Pi = 3.14159265359;
void main() {
vec2 uv = vUv;
- vec2 center = uv - 0.5 - originOffset;
+ vec2 center = uv - origin;
center.x *= resolution.x / resolution.y;
float angle = atan(center.y, center.x) + rotationOffset;
@@ -212,7 +203,7 @@ type StarburstGlState = {
uSmoothEdge: WebGLUniformLocation | null;
uResolution: WebGLUniformLocation | null;
uNumColors: WebGLUniformLocation | null;
- uOriginOffset: WebGLUniformLocation | null;
+ uOrigin: WebGLUniformLocation | null;
cachedPaletteKey: string;
palettePixelData: Uint8Array;
};
@@ -267,7 +258,7 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
backend: 'webgl2',
calculateKey: (params) => {
const r = resolve(params);
- return `starburst-${r.rays}-${r.colors.join('|')}-${r.rotation}-${r.smoothness}-${r.originOffsetX}-${r.originOffsetY}`;
+ return `starburst-${r.rays}-${r.colors.join('|')}-${r.rotation}-${r.smoothness}-${r.origin.join(':')}`;
},
setup: (target) => {
const gl = target.getContext('webgl2', {
@@ -339,7 +330,7 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
uSmoothEdge: gl.getUniformLocation(program, 'smoothEdge'),
uResolution: gl.getUniformLocation(program, 'resolution'),
uNumColors: gl.getUniformLocation(program, 'numColors'),
- uOriginOffset: gl.getUniformLocation(program, 'originOffset'),
+ uOrigin: gl.getUniformLocation(program, 'origin'),
cachedPaletteKey: '',
palettePixelData: new Uint8Array(0),
};
@@ -357,7 +348,7 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
uSmoothEdge,
uResolution,
uNumColors,
- uOriginOffset,
+ uOrigin,
} = state;
const rotationRad = (r.rotation * Math.PI) / 180;
@@ -409,8 +400,7 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
if (uNumColors) gl.uniform1f(uNumColors, r.colors.length);
if (uRotationOffset) gl.uniform1f(uRotationOffset, rotationRad);
if (uSmoothEdge) gl.uniform1f(uSmoothEdge, r.smoothness);
- if (uOriginOffset)
- gl.uniform2f(uOriginOffset, r.originOffsetX, r.originOffsetY);
+ if (uOrigin) gl.uniform2f(uOrigin, r.origin[0], 1 - r.origin[1]);
if (uResolution) gl.uniform2f(uResolution, width, height);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
diff --git a/packages/starburst/src/test/starburst-effect-params.test.ts b/packages/starburst/src/test/starburst-effect-params.test.ts
index 4f924b8549..f675ee4c10 100644
--- a/packages/starburst/src/test/starburst-effect-params.test.ts
+++ b/packages/starburst/src/test/starburst-effect-params.test.ts
@@ -35,6 +35,24 @@ test('starburst() does not expose the removed vignette control', () => {
).toBe(false);
});
+test('starburst() exposes origin as a UV coordinate control', () => {
+ const {schema} = starburst({
+ rays: 12,
+ colors: ['#ff0000', '#00ff00'],
+ }).definition;
+
+ expect('originOffsetX' in schema).toBe(false);
+ expect('originOffsetY' in schema).toBe(false);
+ expect(schema.origin).toEqual({
+ type: 'uv-coordinate',
+ min: 0,
+ max: 1,
+ step: 0.01,
+ default: [0.5, 0.5],
+ description: 'Origin',
+ });
+});
+
test('starburst() parameters produce distinct effect keys', () => {
const defaultStarburst = starburst({
rays: 12,
@@ -54,6 +72,11 @@ test('starburst() parameters produce distinct effect keys', () => {
colors: ['#ff0000', '#00ff00'],
smoothness: 0.5,
});
+ const shiftedOrigin = starburst({
+ rays: 12,
+ colors: ['#ff0000', '#00ff00'],
+ origin: [0.25, 0.75],
+ });
expect(
new Set([
@@ -61,6 +84,35 @@ test('starburst() parameters produce distinct effect keys', () => {
moreRays.effectKey,
rotated.effectKey,
smoother.effectKey,
+ shiftedOrigin.effectKey,
]).size,
- ).toBe(4);
+ ).toBe(5);
+});
+
+test('starburst() defaults origin to the center UV coordinate', () => {
+ expect(starburst({rays: 12, colors: ['#ff0000', '#00ff00']}).effectKey).toBe(
+ starburst({
+ rays: 12,
+ colors: ['#ff0000', '#00ff00'],
+ origin: [0.5, 0.5],
+ }).effectKey,
+ );
+});
+
+test('starburst() validates origin as a UV coordinate', () => {
+ expect(() =>
+ starburst({
+ rays: 12,
+ colors: ['#ff0000', '#00ff00'],
+ origin: [0.5, Number.NaN],
+ }),
+ ).toThrow('"origin" must be a [number, number] tuple');
+
+ expect(() =>
+ starburst({
+ rays: 12,
+ colors: ['#ff0000', '#00ff00'],
+ origin: [1.1, 0.5],
+ }),
+ ).toThrow('"origin" must contain coordinates between 0 and 1');
});