forked from brianchitester/facequalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacequalm.js
97 lines (85 loc) · 2.42 KB
/
facequalm.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
Images = new Mongo.Collection("images");
ImagesToMatch = new Mongo.Collection("imagesToMatch");
if (Meteor.isClient) {
Template.body.helpers({
storedImages: function() {
return Images.find({});
},
imagesToMatch: function() {
return ImagesToMatch.find({});
}
});
var context, canvas;
Template.camera.onRendered(function(){
canvas = $("#canvas")[0];
context = canvas.getContext("2d");
var video = $("#video")[0];
var videoObj = {"video": true};
var errBack = function(error) {
console.log("Video capture error: ", error.code);
};
if(navigator.getUserMedia) { // Standard
navigator.getUserMedia(videoObj, function(stream) {
video.src = stream;
video.play();
}, errBack);
} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
navigator.webkitGetUserMedia(videoObj, function(stream){
video.src = window.webkitURL.createObjectURL(stream);
video.play();
}, errBack);
}
else if(navigator.mozGetUserMedia) { // Firefox-prefixed
navigator.mozGetUserMedia(videoObj, function(stream){
video.src = window.URL.createObjectURL(stream);
video.play();
}, errBack);
}
})
Template.camera.events({
'click button' : function() {
context.drawImage(video, 0, 0, 320, 240);
// This is the data URL:
Images.insert({
imageSource: canvas.toDataURL('image/png'),
numVotes: 0,
createdAt: new Date()
});
console.log(canvas.toDataURL('image/png'))
}
});
Template.storedImage.events({
'click button' : function(e) {
Images.update({
_id: $(e.currentTarget).data().id
},
{
$inc: { numVotes: 1 }
});
}
});
Template.newGame.events({
'click button': function() {
Meteor.call("clearImages");
window.alert("New game has begun; you have EXACTLY one minute to take a selfie and vote on other peoples.");
$.getJSON("http://uifaces.com/api/v1/random", function(data) {
Meteor.call("clearImagesToMatch");
ImagesToMatch.insert({
url: data.image_urls.epic
});
});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
return Meteor.methods({
clearImages: function() {
return Images.remove({});
},
clearImagesToMatch: function() {
return ImagesToMatch.remove({});
}
})
});
}