forked from alexyak/camera
-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.js
165 lines (137 loc) · 3.6 KB
/
camera.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
'use strict';
Module.register("camera", {
counter: null,
// Default module config.
defaults: {
selfieInterval: 3,
emailConfig: {
service: 'Hotmail',
auth: {
user: '<name@email.com>',
pass: '<password>'
}
},
defaultDisplay: false,
hideMessage: false,
width: 640,
height: 480,
filter: null
},
display: false,
cameraPreview: null,
snapshot: null,
camera: null,
image: null,
processing: false,
message: null,
commands: null,
getScripts: function() {
return ["webcam.js"];
},
start: function() {
if(this.config.hideMessage){
this.message = "";
} else {
this.message = "Say 'SELFIE' to make a selfie or 'HIDE CAMERA' to hide";
}
this.sendSocketNotification('INIT_MAILER', this.config);
console.log(this.config);
this.display = this.config.defaultDisplay;
},
makeSelfie: function(){
var self = this;
var timer = 1;
var interval = setInterval(function() {
if (timer === 4) {
clearInterval(interval);
self.createSnapshot();
}
else {
self.counter.innerHTML = timer;
timer++;
}
}, 1000);
},
createSnapshot: function() {
var self = this;
this.processing = true;
Webcam.snap(function (data_uri, canvas, context) {
var data = data_uri;
if (self.image == null){
self.image = document.createElement("img");
self.image.width = 640;
self.image.height = 480;
self.snapshot.appendChild(self.image);
}
self.image.src = data_uri;
self.cameraPreview.style.display = 'none';
self.sendSocketNotification('SEND_EMAIL', { config: self.config, dataUrl:data_uri } );
self.commands.innerHTML = "Your selfie will be emailed to you";
setTimeout(function(){
self.commands.innerHTML = self.message;
}, 3000);
self.processing = false;
});
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if (this.display) {
// if (this.camera === null) {
this.camera = document.createElement("div");
this.counter = document.createElement("div")
this.counter.style = "text-align: center; padding-bottom: 10px;";
this.counter.className = "large normal";
this.camera.appendChild(this.counter);
this.cameraPreview = document.createElement("div");
this.camera.appendChild(this.cameraPreview);
this.snapshot = document.createElement("div");
this.camera.appendChild(this.snapshot)
this.commands = document.createElement("div");
this.commands.innerHTML = this.message;
this.commands.className = "small light dimmed";
this.commands.style = "padding-top: 10px;"
this.camera.appendChild(this.commands);
wrapper.appendChild(this.camera);
Webcam.set({
width: this.config.width,
height: this.config.height,
image_format: 'jpeg',
jpeg_quality: 90,
constraints: {
mandatory: {
minWidth: 640,
minHeight: 480
},
optional: [
{ minFrameRate: 60 }
]
},
filter: this.config.filter
});
Webcam.attach(this.cameraPreview);
}
else {
if (this.camera != null){
Webcam.reset();
this.camera.style = "visibility:hidden;";
}
}
return wrapper;
},
notificationReceived: function(notification, payload, sender) {
if (notification === "SHOW_CAMERA" && this.display === false){
this.display = true;
this.updateDom(500);
}
if (notification === "HIDE_CAMERA" && this.display == true){
this.display = false;
this.updateDom(500);
}
if (notification === "SELFIE"){
if (!this.processing && this.display){
this.makeSelfie();
}
}
},
});