-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathtransitions.tsx
More file actions
145 lines (138 loc) · 3.17 KB
/
transitions.tsx
File metadata and controls
145 lines (138 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from "react";
import { Shaders, Node, GLSL, LinearCopy } from "gl-react";
import { Surface } from "gl-react-dom";
import { useTimeLoop } from "../hooks/useTimeLoop";
const transitionShaders = Shaders.create({
fade: {
frag: GLSL`
precision highp float;
varying vec2 uv;
uniform sampler2D from, to;
uniform float progress;
void main() {
gl_FragColor = mix(texture2D(from, uv), texture2D(to, uv), progress);
}`,
},
circleOpen: {
frag: GLSL`
precision highp float;
varying vec2 uv;
uniform sampler2D from, to;
uniform float progress;
void main() {
float dist = distance(uv, vec2(0.5));
float radius = progress * 0.75;
gl_FragColor = dist < radius
? texture2D(to, uv)
: texture2D(from, uv);
}`,
},
directionalWipe: {
frag: GLSL`
precision highp float;
varying vec2 uv;
uniform sampler2D from, to;
uniform float progress;
void main() {
float m = step(progress, uv.x);
gl_FragColor = mix(texture2D(to, uv), texture2D(from, uv), m);
}`,
},
dissolve: {
frag: GLSL`
precision highp float;
varying vec2 uv;
uniform sampler2D from, to;
uniform float progress;
float rand(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898,78.233))) * 43758.5453);
}
void main() {
float r = rand(uv);
gl_FragColor = r < progress
? texture2D(to, uv)
: texture2D(from, uv);
}`,
},
squeeze: {
frag: GLSL`
precision highp float;
varying vec2 uv;
uniform sampler2D from, to;
uniform float progress;
void main() {
float p = smoothstep(0.0, 1.0, progress);
if (uv.y < 0.5 - p * 0.5 || uv.y > 0.5 + p * 0.5) {
float squeezeY = (uv.y - 0.5) / (1.0 - p) + 0.5;
gl_FragColor = texture2D(from, vec2(uv.x, squeezeY));
} else {
float revealY = (uv.y - (0.5 - p * 0.5)) / p;
gl_FragColor = texture2D(to, vec2(uv.x, revealY));
}
}`,
},
});
const transitionList = [
"fade",
"circleOpen",
"directionalWipe",
"dissolve",
"squeeze",
] as const;
const images =
"wxqlQkh,G2Whuq3,0bUSEBX,giP58XN,iKdXwVm,IvpoR40,zJIxPEo,CKlmtPs,fnMylHI,vGXYiYy,MnOB9Le,YqsZKgc,0BJobQo,Otbz312"
.split(",")
.map((id) => `https://i.imgur.com/${id}.jpg`);
function GLTransition({
from,
to,
progress,
transition,
}: {
from: string;
to: string;
progress: number;
transition: keyof typeof transitionShaders;
}) {
return (
<Node
shader={transitionShaders[transition]}
uniforms={{ from, to, progress }}
/>
);
}
function Slideshow({
slides,
delay,
duration,
}: {
slides: string[];
delay: number;
duration: number;
}) {
const { time } = useTimeLoop();
const total = delay + duration;
const index = Math.floor(time / total);
const from = slides[index % slides.length];
const to = slides[(index + 1) % slides.length];
const transition =
transitionList[index % transitionList.length];
const progress = (time - index * total - delay) / duration;
return progress > 0 ? (
<GLTransition
from={from}
to={to}
progress={Math.min(progress, 1)}
transition={transition}
/>
) : (
<LinearCopy>{from}</LinearCopy>
);
}
export default function Transitions() {
return (
<Surface width={600} height={400}>
<Slideshow slides={images} delay={2000} duration={1500} />
</Surface>
);
}