forked from electron/electron-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
82 lines (77 loc) · 2.38 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
<html>
<head>
<title>Test blob</title>
<script>
var $ = require('jquery');
$( document ).ready(function() {
var vid = $('.vid')[0];
$('.start').on('click', function() {
// Start the video
console.log('Starting...');
navigator.mediaDevices.getUserMedia({video:{width:{min:1280}}, audio:false})
.then(function(stream) {
console.log('Got local media');
vid.autoplay = true;
vid.src = window.URL.createObjectURL(stream);
$(vid).on('play', function() {
console.log('Playing: '+vid.videoWidth+'/'+vid.videoHeight);
// Do canvas ops
doCanvasOp();
});
});
});
function doCanvasOp() {
// Get frame as canvas
var canvas = document.createElement('canvas');
canvas.width = vid.videoWidth;
canvas.height = vid.videoHeight;
var ctx = canvas.getContext('2d');
ctx.drawImage(vid, 0, 0);
// Get a small crop
var canvasCrop = document.createElement('canvas');
canvasCrop.width = 150;
canvasCrop.height = 150;
var ctxCrop = canvasCrop.getContext('2d');
ctxCrop.drawImage(canvas, 0, 0, 150, 150, 0, 0, 150, 150);
// Convert to blob
canvasCrop.toBlob(function(blob) {
console.log('Blob size: '+blob.size);
if(!blob.size) {
// Error!
// Try again just for fun
canvasCrop.toBlob(function(blob2) {
// Display sample and stop
$('.sample').empty();
$('.sample').append(canvasCrop);
alert('Woops! First try:'+blob.size+' Second try:'+blob2.size);
}, 'image/jpeg');
return;
}
// Do it again
doCanvasOp();
},
'image/jpeg');
}
});
</script>
<style>
.button.start {
margin-bottom: 40px;
display: inline-block;
padding: 10px 20px;
border: 1px solid grey;
cursor: pointer;
}
video.vid {
width: 600px;
}
</style>
</head>
<body>
<div>
<div class="button start">Start</div>
</div>
<div class="sample"></div>
<video class="vid"></video>
</body>
</html>