-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.js
227 lines (193 loc) · 5.54 KB
/
video.js
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
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame
})();
var video;
var width;
var height;
var canvas;
var images = [];
var ctx;
var result;
var capture;
var loopnum;
var startTime;
var capturing = false;
var msgdiv;
var progress;
var startButton;
var stopButton;
/**
* Set the HTML elements we need.
*/
function initvideo() {
video = document.getElementById('campreview');
canvas = document.createElement('canvas');
ctx = canvas.getContext('2d');
result = document.getElementById('result');
msgdiv = document.getElementById('information');
progress = document.getElementById('progress');
startButton = document.getElementById('startButton');
stopButton = document.getElementById('stopButton');
}
/**
* Capture the next frame of the video.
*/
function nextFrame(){
if(capturing){
var imageData;
ctx.drawImage(video, 0, 0, width, height);
imageData = ctx.getImageData(0, 0, width, height);
function multiply(topValue, bottomValue){
return topValue * bottomValue / 255;
}
pix = imageData.data;
if( filterType == "multiply" ){
// Loop over each pixel and change the color.
for (var i = 0, n = pix.length; i < n; i += 4) {
pix[i ] = multiply(multiplyColor[0], pix[i ]); // red
pix[i+1] = multiply(multiplyColor[1], pix[i+1]); // green
pix[i+2] = multiply(multiplyColor[2], pix[i+2]); // blue
// pix[i+3] is alpha channel (ignored)
}
}
if( filterType == "greyscale" ){
for (var i = 0, n = pix.length; i < n; i += 4) {
var grayscale = pix[i ] * .3 + pix[i+1] * .59 + pix[i+2] * .11;
pix[i ] = grayscale; // red
pix[i+1] = grayscale; // green
pix[i+2] = grayscale; // blue
// alpha
}
}
if( filterType == "blur" ){
JSManipulate.blur.filter(imageData, {amount: 10});
}
if( filterType == "water" ){
JSManipulate.waterripple.filter(imageData);
}
if( filterType == "sparkle" ){
JSManipulate.sparkle.filter(imageData, { amount: 100 ,randomness: 50 });
}
if( filterType == "kaleidoscope" ){
JSManipulate.kaleidoscope.filter(imageData, { angle: 45, rotation: 30, sides: 3, centerX: 0.6, centerY: 0.5 });
}
// Draw the result on the canvas
ctx.putImageData(imageData, 0, 0);
images.push({duration : new Date().getTime() - startTime, datas : imageData});
startTime = new Date().getTime();
requestAnimationFrame(nextFrame);
}else{
requestAnimationFrame(finalizeVideo);
}
}
/**
* Start the encoding of the captured frames.
*/
function finalizeVideo(){
var capture = new Whammy.Video();
setMessage('Encoding video...');
progress.max = images.length;
showProgress(true);
encodeVideo(capture, 0);
}
/**
* Encode the captured frames.
*
* @param capture
* @param currentImage
*/
function encodeVideo(capture, currentImage) {
if (currentImage < images.length) {
ctx.putImageData(images[currentImage].datas, 0, 0);
capture.add(ctx, images[currentImage].duration);
delete images[currentImage];
progress.value = currentImage;
currentImage++;
setTimeout(function() {encodeVideo(capture, currentImage);}, 5);
} else {
var output = capture.compile();
result.src = window.URL.createObjectURL(output);
console.log('the output is' + output);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'savefiles.php', true);
xhr.onload = function(e) { console.log("loaded"); };
xhr.onreadystatechange = function(){
console.log("state: " + xhr.readyState);
};
// Listen to the upload progress.
xhr.send(output);
setMessage('Finished');
images = [];
enableStartButton(true);
}
}
/**
* Initialize the canvas' size with the video's size.
*/
function initSize() {
width = video.clientWidth;
height = video.clientHeight;
canvas.width = width;
canvas.height = height;
}
/**
* Initialize the css style of the buttons and the progress bar
* when capturing.
*/
function initStyle() {
setMessage('Capturing...');
showProgress(false);
enableStartButton(false);
enableStopButton(true);
}
/**
* Start the video capture.
*/
function startCapture() {
initSize();
initStyle();
capturing = true;
startTime = new Date().getTime();
nextFrame();
}
/**
* Stop the video capture.
*/
function stopCapture() {
capturing = false;
enableStopButton(false);
}
/* *************************************************************
* Styles functions
* *************************************************************/
/**
* Enable/Disable the start button.
*/
function enableStartButton(enabled) {
startButton.disabled = !enabled;
}
/**
* Enable/Disable the stop button.
*/
function enableStopButton(enabled) {
stopButton.disabled = !enabled;
}
/**
* Display/Hide the progress bar.
*/
function showProgress(show) {
progress.style.visibility = show ? 'visible' : 'hidden';
}
/**
* Display a message in the msgdiv block.
*/
function setMessage(message) {
msgdiv.innerHTML = message;
}