-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathwebcampersistence.tsx
More file actions
76 lines (72 loc) · 1.7 KB
/
webcampersistence.tsx
File metadata and controls
76 lines (72 loc) · 1.7 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
import React, { useEffect, useContext } from "react";
import { Uniform, LinearCopy, Node, Shaders, GLSL } from "gl-react";
import { Surface } from "gl-react-dom";
import { Video, VideoContext } from "./video";
const shaders = Shaders.create({
Persistence: {
frag: GLSL`
precision highp float;
varying vec2 uv;
uniform sampler2D t, back;
uniform float persistence;
void main () {
gl_FragColor = vec4(mix(
texture2D(t, uv),
texture2D(back, uv+vec2(0.0, 0.005)),
persistence
).rgb, 1.0);
}`,
},
});
export const Persistence = ({
children: t,
persistence,
}: {
children: any;
persistence: number;
}) => (
<Node
shader={shaders.Persistence}
backbuffering
uniforms={{ t, back: Uniform.Backbuffer, persistence }}
/>
);
const WebCamSource = () => {
const videoRef = useContext(VideoContext);
useEffect(() => {
let stream: MediaStream | null = null;
if (videoRef?.current && navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ video: true }).then((s) => {
stream = s;
if (videoRef.current) {
videoRef.current.srcObject = stream;
}
}).catch(() => {});
}
return () => {
if (stream) {
stream.getTracks().forEach((track) => track.stop());
}
};
}, [videoRef]);
return null;
};
export default function WebcamPersistence({
persistence = 0.8,
}: {
persistence?: number;
}) {
return (
<Surface width={400} height={300}>
<LinearCopy>
<Persistence persistence={persistence}>
{(redraw: any) => (
<Video onFrame={redraw} autoPlay>
<WebCamSource />
</Video>
)}
</Persistence>
</LinearCopy>
</Surface>
);
}