-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
576 lines (502 loc) · 20.3 KB
/
main.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// http://mdn-samples.mozilla.org/s/webrtc-capturestill/
(function() {
// The width and height of the captured photo. We will set the
// width to the value defined here, but the height will be
// calculated based on the aspect ratio of the input stream.
const MESSAGE_PREFIX = "#MyVoteMatters because";
const MESSAGE_SUFFIX = "(share yours at myvotematters.today)";
const MIN_WIDTH = 300;
const MIN_WIDTH_RATIO = 0.5;
var MARGIN = 30;
var TEXT_HEIGHT = 40;
var TEXT_PADDING = 10;
var HORIZ_INC = 2*MARGIN;
var VERT_INC = 2*MARGIN + TEXT_HEIGHT;
const RED = "#ff3333";
const BLUE = "#0066cc";
var CAMERA_STARTED = false;
var width = 0; // We will scale the photo width to this
var height = 0; // This will be computed based on the input stream
// The various HTML elements we need to configure or control. These
// will be set by the startup() function.
var video = null;
var wrapper = null;
var canvas = null;
var context = null;
var photo = null;
var startbutton = null;
var camerabutton = null;
function startCamera() {
$("#camerabutton").addClass("no-display");
$("#streaming").removeClass("no-display");
connectCamera().then(function() {
video.addEventListener('canplay', function(ev){
(function loop() {
addTextToImage();
setTimeout(loop, 1000 / 60);
})();
resizeCanvas();
video.play();
}, false);
video.play();
}, createNoCameraUI);
}
function connectCamera() {
// FIXME: Replace with MediaDevices.getUserMedia.
var getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
return new Promise(function(resolve, reject) {
if (!getUserMedia) {
reject();
return;
}
CAMERA_STARTED = true;
getUserMedia = getUserMedia.bind(navigator);
getUserMedia({ video: true, audio: false },
function(stream) {
if (navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL.createObjectURL(stream);
}
resolve();
}, reject
);
});
}
function startup() {
video = document.getElementById('video');
wrapper = document.getElementById('video-wrapper');
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
photo = document.getElementById('photo');
startbutton = document.getElementById('startbutton');
camerabutton = document.getElementById('camerabutton');
// wait for Montserrat to be loaded
// video wrapper is hidden at first to prevent weird flashing on page load
// in theory document.fonts.ready works in FF and Chrome, but it doesn't, so use a hacky timeout instead
setTimeout(function() {
$("#video-wrapper").removeClass("no-display");
resizeCanvas();
createBackgroundSelfies();
}, 100);
// Event listeners
camerabutton.addEventListener('click', function(ev){
ga('send', 'event', 'camera', 'click');
startCamera();
ev.preventDefault();
}, false);
startbutton.addEventListener('click', function(ev){
ga('send', 'event', 'start', 'click');
takepicture();
ev.preventDefault();
}, false);
canvas.addEventListener('click', function(ev){
ga('send', 'event', 'start', 'click');
takepicture();
}, false);
$('#controls').on('click', '.cancel-button', function(ev) {
ga('send', 'event', 'cancel', 'click');
untakepicture();
});
$('#controls').on('click', '.twitter-share-button', function(ev) {
ga('send', 'event', 'share', 'click', 'twitter');
postToTwitter();
});
$('#controls').on('click', '.fb-share-button', function(ev) {
ga('send', 'event', 'share', 'click', 'facebook');
postToFacebook();
});
$('#controls').on('click', '.download-button', function(ev) {
ga('send', 'event', 'share', 'click', 'download');
downloadImage(this);
});
$(window).on('orientationchange', function(ev) {
setTimeout(resizeCanvas, 300); // FIXME can this be lower?
});
$(window).on('resize', function(ev) {
resizeCanvas();
});
if (window.OAuth) {
initOAuth();
return;
}
document.querySelector('#oauth').addEventListener('load', initOAuth);
}
function initOAuth() {
// Initialize OAuth
// http://blog.devteaminc.co/posting-a-canvas-image-to-twitter-using-oauth/
var OAuthKey = "WDBN6HtSl2OSBHDCMdhaT_tMBRE";
OAuth.initialize(OAuthKey);
}
function drawPlaceholder() {
context.beginPath();
context.fillStyle = "white";
context.rect(0, 0, width + 2*INSIDE_MARGIN, height + 2*INSIDE_MARGIN + 20*TEXT_HEIGHT + 2*TEXT_PADDING);
context.fill();
context.fillStyle = BLUE;
context.textAlign = "center";
context.font = (TEXT_HEIGHT/2) + "px Montserrat, Helvetica Neue, Helvetica, Arial, sans-serif";
context.fillText("Share why YOUR vote matters", canvas.width/2, canvas.height - INSIDE_MARGIN - TEXT_HEIGHT + TEXT_PADDING*2);
context.fillStyle = "black";
context.beginPath();
context.fillStyle = RED;
context.rect(INSIDE_MARGIN, INSIDE_MARGIN, width, height);
context.fill();
}
function resizeCanvas() {
width = Math.min(MIN_WIDTH, Math.round(screen.width * MIN_WIDTH_RATIO));
MARGIN = width / 15; // of blue wrapper
INSIDE_MARGIN = width / 20; // of white polaroid
TEXT_HEIGHT = 2 * MARGIN;
TEXT_PADDING = TEXT_HEIGHT / 4;
HORIZ_INC = 2*INSIDE_MARGIN;
VERT_INC = 2*INSIDE_MARGIN + TEXT_HEIGHT + 2*TEXT_PADDING;
height = video.videoHeight / (video.videoWidth/width);
// Firefox currently has a bug where the height can't be read from
// the video, so we will make assumptions if this happens.
if (isNaN(height)) {
height = width / (4/3);
}
canvas.setAttribute('width', width + HORIZ_INC);
canvas.setAttribute('height', height + VERT_INC);
canvas.style.top = MARGIN + "px";
wrapper.style.width = (width + HORIZ_INC + 2*MARGIN) + "px";
wrapper.style.height = (height + VERT_INC + 2*MARGIN) + "px";
$("input").css("width", width + HORIZ_INC + 2*MARGIN);
if (!CAMERA_STARTED) {
drawPlaceholder();
}
}
function createNoCameraUI() {
ga('send', 'event', 'no-camera', 'error');
// Show an error message explaining that you
// need to upload a photo instead
$("#canvas").addClass("no-display");
wrapper.classList.add('camera-failure');
wrapper.classList.add('fgwhite');
var textNode = document.createTextNode('Oops! It looks like your camera won\'t work here, but you can upload a photo instead by pressing the button below.');
wrapper.appendChild(textNode);
var controls = document.querySelector('#controls');
var upload = document.createElement('input');
upload.type ='file';
upload.style.display = 'none';
upload.setAttribute('capture', 'camera');
upload.setAttribute('accept', 'image/*');
document.querySelector('#streaming .text').textContent = 'Upload photo';
// FIXME: This is a dirty hack, making an Image quack like a <video> element.
// Namely, the takepicture() function calls video.pause(),
// which calls upload.click()
video = document.createElement('img');
video.play = function() {};
video.pause = function() {
upload.click();
};
upload.addEventListener('change', function(event) {
// on upload click we should ...
// 1) get rid of the error message
// 2) Show the canvas again
event.preventDefault();
wrapper.classList.remove('camera-failure');
try { // might have already removed this node
wrapper.removeChild(textNode);
} catch (err) {
}
$("#canvas").removeClass("no-display");
var imageFile = event.target.files[0];
var image = document.createElement('img');
image.addEventListener('load', function() {
computeRotation(image).then(function(degrees) {
video = rotateImage(image, degrees);
video.play = function() {};
video.pause = function() {
upload.click();
};
video.videoWidth = video.width;
video.videoHeight = video.height;
(function loop() {
addTextToImage();
setTimeout(loop, 1000 / 60);
})();
resizeCanvas();
});
});
image.src = URL.createObjectURL(imageFile);
});
controls.appendChild(upload);
function rotateImage(rotationImage, degrees) {
var rotationCanvas = document.createElement('canvas');
var rotationContext = rotationCanvas.getContext('2d');
var sideways = Math.abs(degrees) == 90;
var finalWidth = sideways ? rotationImage.naturalHeight : rotationImage.naturalWidth;
var finalHeight = sideways ? rotationImage.naturalWidth : rotationImage.naturalHeight;
rotationCanvas.width = finalWidth;
rotationCanvas.height = finalHeight;
rotationContext.save();
rotationContext.translate(finalWidth / 2, finalHeight / 2);
rotationContext.rotate(degrees * Math.PI / 180);
rotationContext.drawImage(rotationImage, - (rotationImage.naturalWidth / 2), - (rotationImage.naturalHeight / 2));
rotationContext.restore();
return rotationCanvas;
}
function computeRotation(image) {
return new Promise(function(resolve) {
EXIF.getData(image, function() {
var degrees = 0;
switch(EXIF.getTag(this, 'Orientation')) {
case 8:
degrees = -90;
break;
case 3:
degrees = 180;
break;
case 6:
degrees = 90;
}
resolve(degrees);
});
});
}
}
// Capture a photo by fetching the current contents of the video
// and drawing it into a canvas, then converting that to a PNG
// format data URL. By drawing it on an offscreen canvas and then
// drawing that to the screen, we can change its size and/or apply
// other changes before drawing it.
function polaroid(canvas, context, width, height, fs) {
context.beginPath();
context.fillStyle = "white";
context.rect(0, 0, width + 2*INSIDE_MARGIN, height + 2*INSIDE_MARGIN + 20*TEXT_HEIGHT + 2*TEXT_PADDING);
context.fill();
context.fillStyle = BLUE;
context.textAlign = "center";
context.font = fs + "px Montserrat";
context.fillText(MESSAGE_PREFIX, canvas.width/2, canvas.height - INSIDE_MARGIN - TEXT_HEIGHT + TEXT_PADDING);
context.fillStyle = "black";
}
function takepicture() {
video.pause();
$('#streaming').addClass('no-display');
$(".input-wrapper").removeClass("no-display");
$('#share-photo').removeClass('no-display');
}
function untakepicture() {
clearMessage();
video.play();
$('#streaming').removeClass('no-display');
$('#share-photo').addClass('no-display');
$('.result').addClass('no-display');
}
function getImageData() {
var canvas = document.getElementById('canvas');
var data = canvas.toDataURL('image/png');
var file = dataURItoBlob(data);
return file;
}
function getMessage(fix) {
var message = $(".input-message").val();
if (fix) {
message = MESSAGE_PREFIX + " " + message + " " + MESSAGE_SUFFIX;
}
return message;
}
function clearMessage() {
$(".input-message").val("");
}
function postToTwitter() {
var file = getImageData();
var message = getMessage(true);
OAuth.popup("twitter").then(function(result) {
var data = new FormData();
data.append('status', message);
data.append('media[]', file, 'pic.png');
return result.post('/1.1/statuses/update_with_media.json', {
data: data,
cache: false,
processData: false,
contentType: false
});
}).done(function(data){
var url = data.entities.media[0].display_url;
ga('send', 'event', 'share', 'success', 'twitter');
onShareSuccess('twitter', "http://" + url);
}).fail(function(e){
ga('send', 'event', 'share', 'error', 'twitter');
onShareError('twitter', e);
});
}
function postToFacebook() {
var file = getImageData();
var message = getMessage(true);
OAuth.popup("facebook").then(function(result) {
var data = new FormData();
data.append('caption', message);
data.append('source', file);
return result.post('/me/photos', {
data: data,
cache: false,
processData: false,
contentType: false
});
}).done(function(data){
var url = "https://www.facebook.com/photo.php?fbid=" + data.id;
ga('send', 'event', 'share', 'success', 'facebook');
onShareSuccess('facebook', url);
}).fail(function(e){
ga('send', 'event', 'share', 'error', 'facebook');
onShareError('facebook', e);
});
}
function downloadImage(link) {
var file = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); // here is the most important part because if you dont replace you will get a DOM 18 exception.
link.href=file;
link.download = "my_vote_matters.png";
}
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
function onShareSuccess(service, url) {
// Hide the share buttons,
// show the result field
ga('send', 'exception', {'exDescription': 'successful ' + service + ' share', 'exFatal': false});
$("#share-photo").addClass("no-display");
$(".result").removeClass("no-display");
$(".result-text").html("Success! View your post <a target=\"_blank\" href=\"" + url + "\">here.</a>");
}
function onShareError(service, err) {
console.log(err);
ga('send', 'exception', {'exDescription': '[' + service + '] ' + err, 'exFatal': false});
// Hide the share buttons,
// show the result field
$("#share-photo").addClass("no-display");
$(".result").removeClass("no-display");
$(".result-text").html("Sorry, something went wrong.");
}
function addTextToImage() {
polaroid(canvas, context, width, height, TEXT_HEIGHT/2);
context.drawImage(video, INSIDE_MARGIN, INSIDE_MARGIN, width, height);
var message = getMessage();
// limit to the length that will fit in a tweet
var len = MESSAGE_PREFIX.length + MESSAGE_SUFFIX.length + 2;
message = message.substr(0, len);
drawMessage(canvas, context, message);
}
function drawMessage(canvas, context, message) {
// autosize font while word-wrapping
context.textAlign = "center";
var div = 2;
context.font = (TEXT_HEIGHT/div) + "px Montserrat";
var lines = getLines(context, message, width);
while (!textFits(context, lines, div - 1, width)) {
div++;
context.font = (TEXT_HEIGHT/div) + "px Montserrat";
lines = getLines(context, message, canvas.width);
}
for (var i = 0; i < lines.length; i++) {
var ypos = canvas.height - TEXT_PADDING*1.5;
if (lines.length > 1) {
ypos += (i - lines.length/2) * (TEXT_HEIGHT/(lines.length * div) + TEXT_PADDING/(div/2));
}
context.fillText(lines[i], canvas.width/2, ypos);
}
}
function textFits(ctx, lines, maxLines, maxWidth) {
if (lines.length > maxLines) return false;
for (var i = 0; i < lines.length; i++) {
if (ctx.measureText(lines[i]) > maxWidth) {
return false;
}
}
return true;
}
function getLines(ctx, text, maxWidth) {
var words = text.split(" ");
var lines = [];
var currentLine = words[0];
for (var i = 1; i < words.length; i++) {
var word = words[i];
var width = ctx.measureText(currentLine + " " + word).width;
if (width < maxWidth) {
currentLine += " " + word;
} else {
lines.push(currentLine);
currentLine = word;
}
}
lines.push(currentLine);
return lines;
}
function createBackgroundSelfies() {
var images = [
{"filename": "Caroline Kelly_CB.JPG", "caption": "I have never been the type of person to stay quiet about what I think is right!" },
{"filename": "Carrie & Margaret.jpg", "caption": "of my daughter, and the world she will inherit." },
{"filename": "Erin Rowley_CB.JPG", "caption": "women are now voting in a greater percentage than men. Yet as a woman it takes more than that to make my rights equal" },
{"filename": "Jane Ruby.png", "caption": "empowered women deserve to be heard and change happens both individually and collectively." },
{"filename": "Julia Viani_CB.JPG", "caption": "every vote counts, and I want to elect someone who will protect my rights & create a safe nation." },
{"filename": "Julie Fraga & Lucy.jpg", "caption": "helping to elect the first female president models to my daughter that women can rise to leadership positions." },
{"filename": "Katie & Lucy_CB (color).jpg", "caption": "I have two daughters & I want them to grow up in a country that view females not as sexual objects but as individuals" },
{"filename": "Monica & Francisca_CB.jpg", "caption": "I can make a difference for my children by ensuring a better future." },
{"filename": "Ray & Jill_CB.jpg", "caption": "we believe in unity rather than division, celebrating differences rather than condemning them & love rather than hate" },
{"filename": "bob-villaflor.jpg", "caption": "my non-gender conforming child will be able to grow up in a world free of discrimination." },
{"filename": "christy-johnston.JPG", "caption": "women's rights are human rights. As a mother to a daughter, it's important we continue to move forward, not backward" },
{"filename": "graham-campbell.jpg", "caption": "as a DC resident this is the only vote I have that matters nationally. It's important that I stand up & be counted." },
{"filename": "jay-johnston.JPG", "caption": "foreign governments are attempting to influence our election." },
{"filename": "maceo-marquez.jpg", "caption": "we need a qualified, sensible leader to deal with emerging challenges to the economy." },
{"filename": "phyllis-anderson.jpg", "caption": "she's by far the most experienced candidate & has the best temperament to serve as President. That matters." }
];
var SELFIE_COUNT = images.length-1;
const SELFIE_COL_COUNT = 4;
const SELFIE_ROW_COUNT = 2;
photoIndices = createArrayFromKnuthShuffle();
var root = document.querySelector('#photos>div');
photoIndices.slice(0, SELFIE_ROW_COUNT * SELFIE_COL_COUNT).forEach(function(index) {
var img = document.createElement('img');
console.log(index);
img.src = 'img/samples/' + images[index].filename;
$(img).on('load', (function (img, text) { return function() {
var cv = document.createElement('canvas');
cv.setAttribute("width", "245");
cv.setAttribute("height", "236");
var ctx = cv.getContext('2d');
polaroid(cv, ctx, 245, 236, 10);
ctx.drawImage(img, INSIDE_MARGIN, INSIDE_MARGIN, 245 - 2*INSIDE_MARGIN, 236 - 2*INSIDE_MARGIN - TEXT_HEIGHT);
drawMessage(cv, ctx, text);
root.appendChild(cv);
}})(img, images[index].caption));
});
function createArrayFromKnuthShuffle() {
var array = Array.apply(null, Array(SELFIE_COUNT));
array = array.map(function(_, i) { return i + 1; })
var currentIndex = SELFIE_COUNT;
var temp;
var randomIndex;
while (currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temp = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temp;
}
return array;
}
}
// Set up our event listener to run the startup process
// once loading is complete.
window.addEventListener('DOMContentLoaded', startup, false);
})();