-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
267 lines (212 loc) · 7.65 KB
/
code.js
File metadata and controls
267 lines (212 loc) · 7.65 KB
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
/**
* Various links
* https://stackoverflow.com/questions/46882550/how-to-save-a-jpg-image-video-captured-with-webcam-in-the-local-hard-drive-with
* http://fabricjs.com/freedrawing
* https://github.com/fabricjs/fabric.js/tree/master/src
*
* Notes:
* - The fabric API seems to do what we want, but the documentation is hard to follow. I mostly had to look through their source to understand what I wanted to do
* - Dimensions can be tricky.
* - I wanted to capture a high res image, but shrink it enough for them to see on a mobile device
* - They should be able to annotate, but annating on a shrunken image should not pixelate when saving a high res image
* - I found the best way to do this is to set the dimensions of the canvas elements to the high res dimensions and use styling (width/height)
* to shrink the size. This retains the resolution, yet makes it small enough to see
* - I also had to make sure the fabric containers match this same idea of a large canvas and smaller viewport. If everything lines up correctly, it
* appears to work!
*/
const MODE_VIDEO = 'VIDEO';
const MODE_CANVAS = 'CANVAS';
let mode = MODE_VIDEO;
let videoSupported = true;
let fabricCanvas;
let thumbnailX;
let thumbnailY;
let thumbnailWidth;
let thumbnailHeight;
//Capture image
function capture() {
setMode(MODE_CANVAS);
initAnnotations();
if(videoSupported) {
loadVideoImage();
} else {
loadTestImage();
}
}
function loadTestImage() {
let canvas = document.getElementById('thumbnail');
let ctx = canvas.getContext('2d'); // get its context
let url = './airplane.jpg';
let image = new Image();
image.src = url;
image.onload = () => {
info('drawing thumbnail');
// let scale = Math.min(canvas.clientWidth / image.width, canvas.clientHeight / image.height);
let scale = Math.min(500 / image.width, 500 / image.height);
thumbnailWidth = image.width * scale;
thumbnailHeight = image.height * scale;
thumbnailX = (canvas.clientWidth / 2) - thumbnailWidth / 2;
thumbnailY = (canvas.clientHeight / 2) - thumbnailHeight / 2;
canvas.width = image.width;
canvas.height = image.height;
let canvasContainer = document.getElementById('canvasContainer');
canvasContainer.style.width = `${thumbnailWidth}px`;
canvasContainer.style.height = `${thumbnailHeight}px`;
fabricCanvas.setDimensions({width: image.width, height: image.height});
fabricCanvas.setDimensions({width: thumbnailWidth, height: thumbnailHeight}, {cssOnly: true});
fabricCanvas.upperCanvasEl.style.zIndex = 2;
ctx.drawImage(image, 0, 0, image.width, image.height);
}
}
function loadVideoImage() {
try {
let vid = document.querySelector('video');
info('drawing thumbnail');
let canvas = document.getElementById('thumbnail');
let ctx = canvas.getContext('2d'); // get its context
canvas.width = vid.videoWidth;
canvas.height = vid.videoHeight;
// info(`canvas: ${canvas.width}, ${canvas.height}`);
// info(`video: ${vid.videoWidth}, ${vid.videoHeight}`);
let scale = Math.min(500 / vid.videoWidth, 500 / vid.videoHeight);
thumbnailWidth = vid.videoWidth * scale;
thumbnailHeight = vid.videoHeight * scale;
thumbnailX = (canvas.width / 2) - thumbnailWidth / 2;
thumbnailY = (canvas.height / 2) - thumbnailHeight / 2;
// info(`scale: ${scale}`);
// info(`new: ${thumbnailWidth}, ${thumbnailHeight}`);
// info(`location: ${thumbnailX}, ${thumbnailY}`);
let canvasContainer = document.getElementById('canvasContainer');
canvasContainer.style.width = `${thumbnailWidth}px`;
canvasContainer.style.height = `${thumbnailHeight}px`;
fabricCanvas.setDimensions({width: vid.videoWidth, height: vid.videoHeight});
fabricCanvas.setDimensions({width: thumbnailWidth, height: thumbnailHeight}, {cssOnly: true});
fabricCanvas.upperCanvasEl.style.zIndex = 2;
ctx.drawImage(vid, 0, 0, vid.videoWidth, vid.videoHeight);
} catch(error) {
info(error);
}
}
//Annotations
function initAnnotations() {
try {
if(fabricCanvas === undefined) {
let canvas = document.getElementById('annotate');
// let canvas = document.getElementById('thumbnail');
fabricCanvas = new fabric.Canvas(canvas, {
isDrawingMode: true
});
fabricCanvas.freeDrawingBrush.width = 10;
}
fabricCanvas.clear();
} catch(error) {
info(error);
}
}
function clearCanvas() {
fabricCanvas.clear();
}
//Misc buttons
function retake() {
setMode(MODE_VIDEO);
let canvas = document.getElementById('thumbnail');
let ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();
}
function download(blob){
// uses the <a download> to download a Blob
let a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'test.jpg';
document.body.appendChild(a);
a.click();
}
function setMode(newMode) {
try {
mode = newMode;
let captureButton = document.getElementById('captureButton');
let retakeButton = document.getElementById('retakeButton');
let saveButton = document.getElementById('saveButton');
captureButton.disabled = mode === MODE_CANVAS;
retakeButton.disabled = mode === MODE_VIDEO;
saveButton.disabled = mode === MODE_VIDEO;
let video = document.getElementById('vid');
let canvasContainer = document.getElementById('canvasContainer');
let annotationButtons = document.getElementById('annotationButtons');
video.style.display = mode === MODE_VIDEO ? 'block' : 'none';
canvasContainer.style.display = mode === MODE_CANVAS ? 'inline-block' : 'none';
annotationButtons.style.display = mode === MODE_CANVAS ? 'inline-block' : 'none';
} catch(error) {
info(error);
}
}
async function save(){
try {
info('saving');
// const canvas = document.createElement('canvas'); // create a canvas
// const ctx = canvas.getContext('2d'); // get its context
// let vid = document.querySelector('video');
// canvas.width = vid.videoWidth; // set its size to the one of the video
// canvas.height = vid.videoHeight;
// ctx.drawImage(vid, 0,0); // the video
// let canvas = document.getElementById('canvas');
let annotateCanvas = document.getElementById('annotate');
let thumbnailCanvas = document.getElementById('thumbnail');
let ctx = thumbnailCanvas.getContext('2d');
ctx.drawImage(annotateCanvas, 0, 0, thumbnailCanvas.width, thumbnailCanvas.height);
let blob = await new Promise((res, rej)=>{
// let canvas = document.getElementById('thumbnail');
// canvas.toBlob(res, 'image/jpeg');
thumbnailCanvas.toBlob(res, 'image/jpeg'); // request a Blob from the canvas
});
info('downloading');
download(blob);
} catch(error) {
info(error);
}
}
//Upload
function upload(input) {
if (input.files) {
let imagesDiv = document.getElementById('images');
for(let i = 0; i < input.files.length; i++) {
let imageDiv = document.createElement('img');
imageDiv.id = `image${i}`;
imageDiv.style = 'max-width: 200px; max-height: 150px;'
imagesDiv.append(imageDiv);
let reader = new FileReader();
reader.onload = function (e) {
imageDiv.src = e.target.result;
};
reader.readAsDataURL(input.files[i]);
}
}
}
function info(value) {
document.getElementById('output').innerHTML += `${value}<br/>`;
}
async function init() {
setMode(MODE_VIDEO);
try {
info('initing video');
let options = {
audio: false,
video: {
facingMode: 'environment',
height: { min: 720, ideal: 1080 },
width: { min: 1280, ideal: 1920 }
}
};
let stream = await navigator.mediaDevices.getUserMedia(options); // request cam
let vid = document.querySelector('video');
vid.srcObject = stream; // don't use createObjectURL(MediaStream)
await vid.play(); // returns a Promise
} catch(error) {
info(error);
videoSupported = false;
}
}
setTimeout(() => {
init();
}, 10);