-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
255 lines (211 loc) · 6.93 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html>
<head>
<title>v2c(video2canvas) example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.2/gh-fork-ribbon.min.css" />
<style>
h1 {
margin: 0 0 .5em 2.5em;
}
.controll-button {
display: flex;
}
.button {
padding: 5px 10px;
background-color: rgba(105, 105, 105, 1);
color: #fff;
border: 1px solid rgba(192, 192, 192, .1);
font-size: 1em;
border-radius: 3px;
margin-right: .5em;
outline: 0;
}
.init {
background-color: rgba(0, 0, 0, 1);
}
.deactivate:hover {
background-color: rgba(105, 105, 105, .8);
}
.active, .deactivate:active {
padding-top: 6px;
padding-bottom: 4px;
background-color: rgba(105, 105, 105, .8);
box-shadow: inset 1px 1px 2px rgba(0, 0, 0, 0.5);
}
.range {
display: flex;
}
#long-side-size {
margin: 0 1em;
}
.range-desc {
margin: 22px 0;
}
.effect h3 {
margin: 0;
}
.github-fork-ribbon:before {
background-color: #696969;
}
</style>
</head>
<body>
<a class="github-fork-ribbon" href="https://github.com/shimabox/v2c" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
<h1>v2c(video2canvas) demo.</h1>
<div id="v2c-wrapper"></div>
<h3>Controll</h3>
<div class="controll-button">
<input type="button" id="start" value="Start" class="button init deactivate">
<input type="button" id="stop" value="Stop" class="button deactivate">
<input type="button" id="capture" value="Capture" class="button deactivate">
<input type="button" id="switch" value="SwitchCamera" class="button deactivate">
</div>
<div class="range">
<h4>Canvas long side</h4>
<input id="long-side-size" type="range" step="5" min="50" max="" value="">
<p class="range-desc"><span class="range-val"></span><span>px</span></p>
</div>
<div class="effect">
<h3>Effect</h3>
<select id="effect-list" name="effect-list">
<option value="none" selected>none</option>
<option value="grayscale">grayscale</option>
<option value="invert">invert</option>
<option value="threshold">threshold</option>
<option value="bayerDither">bayerDither</option>
<option value="sepia">sepia</option>
<option value="heatmap">heatmap</option>
<option value="mosaic">mosaic</option>
</select>
</div>
<script src="js/vendor/stats/stats.min.js"></script>
<script src="js/v2c.js"></script>
<script src="js/effect.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
'use strict';
const longSideSize = 360;
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const captureButton = document.getElementById('capture');
const switchButton = document.getElementById('switch');
const sizeSlider = document.getElementById('long-side-size');
const rangeVal = document.querySelector('.range-val');
const effectList = document.getElementById('effect-list');
sizeSlider.disabled = true;
effectList.disabled = true;
// Callback function after initialization.
const callbackOnAfterInit = function(v2c) {
const canvas = v2c.getCanvas();
const ctx = canvas.getContext('2d');
const w = canvas.width;
const h = canvas.height;
const message = 'Click Start Button.';
canvas.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
const _c = document.createElement('canvas');
const _ctx = _c.getContext('2d');
_c.width = w;
_c.height = h;
_ctx.font = "2em 'sans-serif'";
_ctx.fillStyle = 'rgba(255, 255, 255, 1)';
const metrics = _ctx.measureText(message);
const textWidth = metrics.width;
const x = (w / 2) - (textWidth / 2);
const y = h / 2;
_ctx.fillText(message, x, y);
ctx.save();
if (v2c.useFrontCamera()) {
ctx.scale(-1, 1);
ctx.drawImage(_c, -w, 0, w, h);
} else {
ctx.drawImage(_c, 0, 0, w, h);
}
ctx.restore();
}
const option = {
'longSideSize': longSideSize,
'callbackOnAfterInit': callbackOnAfterInit
};
const v2c = new V2C('#v2c-wrapper', option);
startButton.addEventListener('click', (e) => {
const targetClassList = e.target.classList;
if (targetClassList.contains('deactivate')) {
targetClassList.remove('deactivate');
targetClassList.add('active');
}
sizeSlider.disabled = false;
effectList.disabled = false;
v2c.start((canvas, t) => {
applyEffect(canvas, t);
_stats();
});
});
stopButton.addEventListener('click', () => {
const targetClassList = startButton.classList;
if (targetClassList.contains('active')) {
targetClassList.remove('active');
targetClassList.add('deactivate');
}
sizeSlider.disabled = true;
effectList.disabled = true;
v2c.stop();
});
captureButton.addEventListener('click', () => {
v2c.capture();
});
switchButton.addEventListener('click', () => {
v2c.switchCamera();
});
sizeSlider.max = getMeasurementSize();
sizeSlider.value = longSideSize;
rangeVal.innerHTML = longSideSize;
sizeSlider.addEventListener('input', (e) => {
rangeVal.innerHTML = e.target.value;
});
sizeSlider.addEventListener('change', (e) => {
v2c.changeLongSideSize(e.target.value);
});
let effectType = 'none';
effectList.addEventListener('change', (e) => {
effectType = e.target.value;
});
function getMeasurementSize() {
let _o = window.orientation;
let _orientation = _o === undefined ? 90 : _o;
if (_orientation === 0 || _orientation === 180) {
return window.innerHeight;
}
return window.innerWidth;
}
function applyEffect(canvas, t) {
if (effectType === 'none') { return; }
const _effect = effect(effectType);
_effect(canvas, t);
}
/*
|--------------------------------------------------------------------------
| Stats
|--------------------------------------------------------------------------
*/
function _stats() {
body.dispatchEvent(event);
}
const body = document.querySelector('body');
// Create the event.
const event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('draw_loop', true, true);
const stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
body.appendChild(stats.domElement);
// Update stats on every iteration.
document.addEventListener('draw_loop', function(event) {
stats.update();
}, false);
});
</script>
</body>
</html>