-
Notifications
You must be signed in to change notification settings - Fork 36
/
camera_css_wrap.html
127 lines (109 loc) · 3.82 KB
/
camera_css_wrap.html
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
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Camera with mediaDevice</title>
<style type="text/css">
#rotate_video
{
animation-duration:4s;
animation-iteration-count:infinite;
animation-timing-function:linear;
animation-name:rotate360;
}
#shake_video
{
animation-duration:0.5s;
animation-iteration-count:infinite;
animation-timing-function:ease-in-out;
animation-name:shake;
}
@keyframes rotate360
{
0%{transform:rotate(0deg);}
100%{transform:rotate(360deg);}
}
@keyframes shake
{
0%{transform:rotate(-20deg);}
50%{transform:rotate(20deg);}
100%{transform:rotate(-20deg);}
}
</style>
</head>
<body>
Camera with mediaDevice.getUserMedia()<br />
<button onclick="startVideo()">Start</button>
<button onclick="stopVideo()">Stop</button>
<br />
<!-- 通常 -->
<video id="local_video" autoplay style="width: 320px; height: 240px; border: 1px solid black;"></video>
<!-- 左右反転 -->
<video id="flip_video" autoplay style="transform: scaleX(-1); width: 320px; height: 240px; border: 1px solid black;"></video>
<!-- 角丸 -->
<video id="round_video" autoplay style="border-radius: 80px 80px 80px 80px; width: 320px; height: 240px; border: 1px solid black;"></video>
<!-- セピア -->
<video id="filter_video" autoplay style="filter: sepia(100%); -webkit-filter: sepia(100%); width: 320px; height: 240px; border: 1px solid black;"></video>
<br />
<!-- 回転アニメーション -->
<video id="rotate_video" autoplay style="width: 320px; height: 240px; border: 1px solid black;"></video>
<!-- 振動アニメーション -->
<video id="shake_video" autoplay style="width: 320px; height: 240px; border: 1px solid black;"></video>
</body>
<script type="text/javascript">
// --- prefix -----
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
let localVideo = document.getElementById('local_video');
let localStream;
// start local video
function startVideo() {
getDeviceStream({video: true, audio: false})
.then(function (stream) { // success
localStream = stream;
localVideo.src = window.URL.createObjectURL(localStream);
document.getElementById('flip_video').src = localVideo.src;
document.getElementById('round_video').src = localVideo.src;
document.getElementById('filter_video').src = localVideo.src;
document.getElementById('rotate_video').src = localVideo.src;
document.getElementById('shake_video').src = localVideo.src;
}).catch(function (error) { // error
console.error('mediaDevice.getUserMedia() error:', error);
return;
});
}
// stop local video
function stopVideo() {
stopStream(localStream);
localStream = null;
localVideo.pause();
window.URL.revokeObjectURL(localVideo.src);
localVideo.src = '';
document.getElementById('flip_video').src = '';
document.getElementById('round_video').src = '';
document.getElementById('filter_video').src = '';
document.getElementById('rotate_video').src = '';
document.getElementById('shake_video').src = '';
}
function getDeviceStream(option) {
if ('getUserMedia' in navigator.mediaDevices) {
console.log('navigator.mediaDevices.getUserMadia');
return navigator.mediaDevices.getUserMedia(option);
}
else {
console.log('wrap navigator.getUserMadia with Promise');
return new Promise(function(resolve, reject){
navigator.getUserMedia(option,
resolve,
reject
);
});
}
}
function stopStream(stream) {
for (track of stream.getTracks()) {
track.stop();
}
}
</script>
</html>