This repository was archived by the owner on Nov 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvasToVideo.js
281 lines (186 loc) · 6.02 KB
/
CanvasToVideo.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*!
* CanvasToVideo v0.1.0beta
* https://github.com/neogeek/CanvasToVideo
*
* Copyright (c) 2014 Scott Doxey
*/
(function () {
'use strict';
/**
* Sends a PUT request to a URL with the specified data and fires a callback upon completion.
*
* putRequest('capture.php', [], function() { alert('Done!'); });
*
* @param {String} url The URL for the PUT request.
* @param {Object} data The data to send.
* @param {Function} [callback] The callback function to call once the request has completed.
* @return {void}
* @api private
*/
function putRequest(url, data, callback) {
var http = new window.XMLHttpRequest();
http.open('put', url);
if (Object.prototype.toString.call(data) === '[object Array]') {
data = JSON.stringify(data);
}
if (String(typeof callback) === 'function') {
http.addEventListener('readystatechange', function () {
if (this.readyState === 4 && this.status === 204) {
callback.call(this);
}
});
}
http.send(data);
}
/**
* Creates a new CanvasToVideo object.
*
* var ctv = new CanvasToVideo('capture.php', document.querySelector('canvas'));
*
* @param {String} url The URL for all PUT requests.
* @param {Object} canvas The canvas to capture frames from.
* @return {Object} New CanvasToVideo object.
* @api public
*/
var CanvasToVideo = function (url, canvas) {
this.url = url;
this.canvas = canvas;
this.imageType = 'image/png';
this.imageQuality = 100;
this.cache = [];
this.cacheEnabled = false;
try {
this.canvas.getContext('2d');
} catch (e) {
this.canvas = null;
throw new Error('Element passed to CanvasToVideo was not a valid canvas element.');
}
};
/**
* Method called after canvas drawing has occured to capture frame and either send to server or store in local temporary cache.
*
* function draw () {
* // draw objects to canvas
* ctv.capture();
* requestAnimationFrame(draw);
* }
* requestAnimationFrame(draw);
*
* @return {void}
* @api public
*/
CanvasToVideo.prototype.capture = function () {
var data;
if (this.canvas) {
data = this.canvas.toDataURL(this.imageType, this.imageQuality).replace(/^[\W\w]*,/, '');
if (this.cacheEnabled) {
this.cache.push(data);
} else {
putRequest(this.url, [data]);
}
}
};
/**
* Called once animation is complete to send all cached images to the server through the specified URL.
*
* ctv.flush();
*
* @return {void}
* @api public
*/
CanvasToVideo.prototype.flush = function () {
if (this.cacheEnabled) {
putRequest(this.url, this.cache, function () {
this.cache = [];
});
}
};
/**
* Sets a new canvas tag once the CanvasToVideo object has already been created.
*
* ctv.setCanvas(document.querySelector('.stage'));
*
* @param {Object} canvas The canvas to capture frames from.
* @return {void}
* @api public
*/
CanvasToVideo.prototype.setCanvas = function (canvas) {
this.canvas = canvas;
try {
this.canvas.getContext('2d');
} catch (e) {
this.canvas = null;
throw new Error('Element passed to CanvasToVideo.setCanvas was not a valid canvas element.');
}
};
/**
* Sets if caching is enabled or not.
*
* ctv.setCacheEnabled(true); // Caching enabled.
* ctv.setCacheEnabled(false); // Caching disabled.
*
* @param {Boolean} enabled Boolean flag for enabling cache.
* @return {void}
* @api public
*/
CanvasToVideo.prototype.setCacheEnabled = function (enabled) {
if (enabled) {
this.cacheEnabled = true;
} else {
this.cacheEnabled = false;
}
};
/**
* Sets a new URL for sending all PUT requests.
*
* ctv.setEndpoint('different_capture_url.php');
*
* @param {String} url The new URL for all PUT requests.
* @return {void}
* @api public
*/
CanvasToVideo.prototype.setEndpoint = function (url) {
this.url = url;
};
/**
* Sets the image type saved from the canvas. Can be either image/png or image/jpeg.
*
* ctv.setImageType('image/png');
* ctv.setImageType('image/jpeg');
*
* @param {String} type The type of image to be captured from the canvas.
* @return {void}
* @api public
*/
CanvasToVideo.prototype.setImageType = function (type) {
if (type.match(/image\/(png|jpeg)/)) {
this.imageType = type;
} else {
throw new Error(type + ' is not a valid image type. The valid types are: image/png and image/jpeg.');
}
};
/**
* Sets the image quality saved from the canvas. Must be an integer between 0 and 100.
*
* ctv.setImageQuality(60);
*
* @param {Integer} type Number between 0 and 100 specifying the quality.
* @return {void}
* @api public
*/
CanvasToVideo.prototype.setImageQuality = function (quality) {
if (String(typeof quality) === 'number') {
this.imageQuality = quality;
} else {
throw new Error(quality + ' is not a valid number. Must be a number between 0 (lowest quality) and 100 (highest quality).');
}
};
/*!
* AMD Support
*/
if (String(typeof window.define) === 'function' && window.define.hasOwnProperty('amd')) {
window.define([], function () { return CanvasToVideo; });
} else {
window.CanvasToVideo = CanvasToVideo;
}
}());