forked from jywarren/image-sequencer
-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathSetInputStep.js
138 lines (105 loc) · 4.75 KB
/
SetInputStep.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
// TODO: potentially move this into ImportImage module
function setInputStepInit() {
return function setInputStep(options) {
var dropzone = $(options.dropZoneSelector);
var fileInput = $(options.fileInputSelector);
var takePhoto = $(options.takePhotoSelector);
var onLoad = options.onLoad;
var onTakePhoto = options.onTakePhoto;
var reader = new FileReader();
function handleFile(e) {
e.preventDefault();
e.stopPropagation(); // stops the browser from redirecting.
if (e.target && e.target.files) var file = e.target.files[0];
else var file = e.dataTransfer.files[0];
if(!file) return;
var reader = new FileReader();
reader.onload = onLoad;
reader.readAsDataURL(file);
}
function runVideo(){
/* event handler for Take-Photo */
document.getElementById('video').style.display = 'inline';
document.getElementById('capture').style.display = 'inline';
document.getElementById('close').style.display = 'inline';
fileInput.css('display', 'none');
takePhoto.css('display', 'none');
document.getElementById('dropzone-text').style.display = 'none';
var video = document.getElementById('video');
canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
vendorUrl = window.URL || window.webkitURL;
const constraints = { audio: false, video: true};
function handleSuccess(stream) {
window.stream = stream; // make stream available to browser console
video.srcObject = stream;
video.onloadedmetadata = function(e) {
video.play();
};
document.getElementById('capture').addEventListener('click', function(){
context.drawImage(video, 0, 0, 400, 300);
options.onTakePhoto(canvas.toDataURL());
setTimeout(stopStream(stream),1); // wait for 1 second before closing webcam so that image loads properly
});
document.getElementById('close').addEventListener('click', function () {
stopStream(stream);
});
}
function handleError(error) {
console.log('navigator.getUserMedia error: ', error);
// when user dismissed the camera access (includes closing of prompt which requests for camera access)
if(error.message == 'Permission denied' || error.message == 'NotAllowedError' || error.message == 'PermissionDismissedError'){
document.getElementById('capture').addEventListener('click', function(e) {
alert('Enable camera access in order to take picture');
});
}
// when user don't have webcam to use.
if(error.message == 'NotFoundError' || error.message == 'DevicesNotFoundError'){
alert('You do not have appropriate devices to use this Functionality');
}
// when webcam is already used by some other application
if(error.message == 'NotReadableError' || error.message == 'TrackStartError' || error.message == 'Concurrent mic process limit'){
alert('Your webcam is already in use by some other application');
}
// when some of the requested constraints can't be satisfied like high frame rate or high resolution
if(error.message == 'OverconstrainedError' || error.message == 'ConstraintNotSatisfiedError'){
console.log('Requested Constraints can not be satisfied ', error);
}
}
navigator.mediaDevices.getUserMedia(constraints).then(handleSuccess).catch(handleError);
document.getElementById('close').addEventListener('click', function() {
video.style.display = 'none';
});
function stopStream(stream) {
stream.getVideoTracks().forEach(function (track) {
track.stop();
});
document.getElementById('video').style.display = 'none';
document.getElementById('capture').style.display = 'none';
document.getElementById('close').style.display = 'none';
fileInput.css('display', 'block');
takePhoto.css('display', 'block');
document.getElementById('dropzone-text').style.display = 'block';
}
}
fileInput.on('change', handleFile);
takePhoto.on('click', runVideo);
dropzone[0].addEventListener('drop', handleFile, false);
dropzone.on('dragover', function onDragover(e) {
dropzone.addClass('hover');
e.preventDefault();
e.stopPropagation();
});
dropzone.on('dragenter', function onDragEnter(e) {
dropzone.addClass('hover');
});
dropzone.on('dragleave', function onDragLeave(e) {
dropzone.removeClass('hover');
});
dropzone.on('drop', function onDrop(e) {
dropzone.removeClass('hover');
e.preventDefault();
});
};
}
module.exports = setInputStepInit;