Skip to content

🎥 Make videos programmatically with React

skia zoom blur

Jonny Burger6447f13e6add
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index d8ffefb71c..58cd2861fc 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -56,6 +56,7 @@ import EllipseTest from './Shapes/EllipseTest';
import RectTest from './Shapes/RectTest';
import StarTest from './Shapes/StarTest';
import TriangleTest from './Shapes/TriangleTest';
+import {RuntimeShaderZoomBlur} from './Skia/Blur';
import {RuntimeShaderDemo} from './Skia/Shader';
import {SkipZeroFrame} from './SkipZeroFrame';
import {BaseSpring, SpringWithDuration} from './Spring/base-spring';
@@ -1206,6 +1207,14 @@ export const Index: React.FC = () => {
width={1080}
durationInFrames={120}
/>
+ <Composition
+ id="skia-zoomblur"
+ component={RuntimeShaderZoomBlur}
+ fps={30}
+ height={1080}
+ width={1080}
+ durationInFrames={120}
+ />
</Folder>
</>
);
diff --git a/packages/example/src/Skia/Blur.tsx b/packages/example/src/Skia/Blur.tsx
new file mode 100644
index 0000000000..873f434f9c
--- /dev/null
+++ b/packages/example/src/Skia/Blur.tsx
@@ -0,0 +1,149 @@
+import {
+ Canvas,
+ Fill,
+ ImageShader,
+ Shader,
+ Skia,
+ useImage,
+} from '@shopify/react-native-skia';
+import React from 'react';
+import {staticFile, useCurrentFrame} from 'remotion';
+
+import type {Transition} from './Base';
+
+type Value = string | number;
+type Values = Value[];
+
+export const glsl = (source: TemplateStringsArray, ...values: Values) => {
+ const processed = source.flatMap((s, i) => [s, values[i]]).filter(Boolean);
+ return processed.join('');
+};
+
+export const frag = (source: TemplateStringsArray, ...values: Values) => {
+ const code = glsl(source, ...values);
+ const rt = Skia.RuntimeEffect.Make(code);
+ if (rt === null) {
+ throw new Error("Couln't Compile Shader");
+ }
+ return rt;
+};
+
+export const transition = (t: Transition) => {
+ return frag`
+ uniform shader image1;
+ uniform shader image2;
+
+ uniform float progress;
+ uniform float2 resolution;
+
+ half4 getFromColor(float2 uv) {
+ return image1.eval(uv * resolution);
+ }
+
+ half4 getToColor(float2 uv) {
+ return image2.eval(uv * resolution);
+ }
+
+ ${t}
+
+ half4 main(vec2 xy) {
+ vec2 uv = xy / resolution;
+ return transition(uv);
+ }
+ `;
+};
+
+export const cube: Transition = glsl`
+// GL Transitions format
+uniform float strength;
+
+// Constants
+const float PI = 3.141592653589793;
+
+// Easing functions
+float Linear_ease(in float begin, in float change, in float duration, in float time) {
+ return change * time / duration + begin;
+}
+
+float Exponential_easeInOut(in float begin, in float change, in float duration, in float time) {
+ if (time == 0.0)
+ return begin;
+ else if (time == duration)
+ return begin + change;
+ time /= (duration / 2.0);
+ if (time < 1.0)
+ return change / 2.0 * pow(2.0, 10.0 * (time - 1.0)) + begin;
+ return change / 2.0 * (-pow(2.0, -10.0 * (time - 1.0)) + 2.0) + begin;
+}
+
+float Sinusoidal_easeInOut(in float begin, in float change, in float duration, in float time) {
+ return -change / 2.0 * (cos(PI * time / duration) - 1.0) + begin;
+}
+
+// Random function
+float random(vec3 xyz, in vec3 scale, in float seed) {
+ return fract(sin(dot(xyz + seed, scale)) * 43758.5453 + seed);
+}
+
+// Mix textures with progress
+vec4 transition(vec2 uv) {
+ float p = progress * 0.7; // Adjusted progress
+ vec2 center = vec2(Linear_ease(0.5, 0.0, 1.0, p), 0.5);
+ float dissolve = Exponential_easeInOut(0.0, 1.0, 1.0, p);
+
+
+ float currentStrength = Sinusoidal_easeInOut(0.0, strength, 0.5, p);
+
+ vec3 color = vec3(0.0);
+ float total = 0.0;
+ vec2 toCenter = center - uv;
+
+ float offset = random(vec3(uv, 0), vec3(12.9898, 78.233, 151.7182), 0.0) * 0.5;
+
+ for (float t = 0.0; t <= 20.0; t++) {
+ float percent = (t + offset) / 20.0;
+ float weight = 1.0 * (percent - percent * percent);
+ vec2 samplePoint = uv + toCenter * percent * currentStrength;
+ color += mix(getFromColor(samplePoint).rgb, getToColor(samplePoint).rgb, dissolve) * weight;
+ total += weight;
+ }
+
+ return vec4(color / total, 1.0);
+}
+`;
+
+export const RuntimeShaderZoomBlur = () => {
+ React.useState(0);
+
+ const image = useImage(staticFile('nested/mp4.png'));
+ const prog = useCurrentFrame();
+ if (!image) {
+ return null;
+ }
+
+ return (
+ <Canvas style={{width: 1080, height: 1080}}>
+ <Fill>
+ <Shader
+ source={transition(cube)}
+ uniforms={{
+ progress: prog / 30,
+ resolution: [1080, 1080],
+ strength: 0.5,
+ }}
+ >
+ <ImageShader
+ image={image}
+ fit="cover"
+ rect={{x: 0, y: 0, width: 1080, height: 1080}}
+ />
+ <ImageShader
+ image={image}
+ fit="cover"
+ rect={{x: 0, y: 0, width: 1080, height: 1080}}
+ />
+ </Shader>
+ </Fill>
+ </Canvas>
+ );
+};