-
Notifications
You must be signed in to change notification settings - Fork 42
/
interactioncam.js
executable file
·216 lines (193 loc) · 5.84 KB
/
interactioncam.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
(function() {
var API_KEY = 'eb18642b5b220484864483b8e21386c3';
// ^ get your own at https://imgur.com/register/api_anon
// as it is limited to 50 uploads an hour!
var video = document.querySelector('#video'),
cover = document.querySelector('#cover'),
canvas = document.querySelector('#canvas'),
vidcontainer = document.querySelector('#videocontainer'),
resetbutton = document.querySelector('#resetbutton'),
startbutton = document.querySelector('#startbutton'),
uploadbutton = document.querySelector('#uploadbutton'),
urlfield = document.querySelector('#uploaded input'),
urllink = document.querySelector('#uploaded a');
var ctx = canvas.getContext('2d'),
streaming = false,
width = 600,
height = 450,
state = 'intro';
var audio = document.querySelectorAll('audio'),
sounds = {
shutter: audio[0],
rip: audio[1],
takeoff: audio[2]
};
/* BRANDING */
var img = new Image(),
imgwidth = 150,
imgheight = 150;
img.src = 'mozcamp.png';
if (location.hostname.indexOf('localhost')!== -1) {
document.querySelector('#imgurform').style.display = 'none';
}
setstate(state);
function init() {
navigator.getMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.getMedia(
{
video: true,
audio: false
},
function(stream) {
if (navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL ? vendorURL.createObjectURL(stream) : stream;
}
video.play();
video.style.width = width + 'px';
video.style.height = height + 'px';
},
function(err) {
console.log("An error occured! " + err);
}
);
}
function takepicture() {
sounds.shutter.play();
ctx.save();
ctx.translate(width, 0);
ctx.scale(-1, 1);
ctx.drawImage(video, 0, 0, width, finalheight);
ctx.restore();
ctx.scale(1, 1);
ctx.drawImage(img, 590 - imgwidth, 440 - imgheight, imgwidth, imgheight);
}
function reshoot() {
if (state === 'reviewing') {
sounds.rip.play();
}
if (state === 'reviewing' || state === 'uploaded') {
canvas.width = width;
canvas.height = finalheight;
ctx.drawImage(img, 590 - imgwidth, 440 - imgheight, imgwidth, imgheight);
setstate('playing');
}
}
function initiateupload() {
if (state === 'reviewing') {
sounds.takeoff.play();
setstate('uploading');
upload();
}
}
function upload() {
var head = /^data:image\/(png|jpeg);base64,/,
data = '',
fd = new FormData(),
xhr = new XMLHttpRequest();
setstate('uploading');
data = canvas.toDataURL('image/jpeg', 0.9).replace(head, '');
if (location.hostname.indexOf('localhost')!== -1) {
fd.append('contents', data);
xhr.open('POST', 'copy.php');
xhr.addEventListener('error', function(ev) {
console.log('Upload Error!');
}, false);
xhr.addEventListener('load', function(ev) {
setstate('uploaded');
}, false);
xhr.send(fd);
} else {
fd.append('image', data);
fd.append('key', API_KEY);
xhr.open('POST', 'http://api.imgur.com/2/upload.json');
xhr.addEventListener('error', function(ev) {
console.log('Upload Error!');
}, false);
xhr.addEventListener('load', function(ev) {
try {
var links = JSON.parse(xhr.responseText).upload.links;
store(links.imgur_page.replace(/.*\/+/,''));
urlfield.value = links.imgur_page;
urllink.href = links.imgur_page;
setstate('uploaded');
} catch(e) {
console.log('Upload Error!' + e);
}
}, false);
xhr.send(fd);
}
}
function setstate(newstate) {
state = newstate;
document.body.className = newstate;
}
function store(name) {
if (localStorage.interactionphotos === undefined) {
localStorage.interactionphotos = '';
}
localStorage.interactionphotos += ' '+ name;
}
/* Event Handlers */
video.addEventListener('play', function(ev){
if (!streaming) {
console.log(video.clientHeight);
finalheight = video.clientHeight / (video.clientWidth/width);
video.setAttribute('width', width);
video.setAttribute('height', finalheight);
canvas.width = width;
canvas.height = finalheight;
ctx.drawImage(img, 590 - imgwidth, 440 - imgheight, imgwidth, imgheight);
streaming = true;
vidcontainer.classname = 'playing';
}
}, false);
document.addEventListener('keydown', function(ev) {
if (ev.which === 32 || ev.which === 37 || ev.which === 39) {
ev.preventDefault();
}
if (ev.which === 32) {
if (state === 'intro') {
setstate('playing');
init();
} else {
setstate('reviewing');
takepicture();
}
}
if (ev.which === 37) {
reshoot();
}
if (ev.which === 39) {
initiateupload();
}
},false);
video.addEventListener('click', function(ev){
setstate('reviewing');
takepicture();
}, false);
resetbutton.addEventListener('click', function(ev){
if (state === 'reviewing') {
setstate('playing');
}
ev.preventDefault();
}, false);
startbutton.addEventListener('click', function(ev){
if (state === 'uploaded') {
setstate('playing');
}
ev.preventDefault();
}, false);
uploadbutton.addEventListener('click', function(ev){
if (state === 'reviewing') {
setstate('uploading');
upload();
}
ev.preventDefault();
}, false);
})();