File: camera.js

package info (click to toggle)
openboard 1.7.3%2Bdfsg-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 57,404 kB
  • sloc: cpp: 76,443; javascript: 10,089; xml: 234; ansic: 38; makefile: 23; sh: 8
file content (477 lines) | stat: -rw-r--r-- 13,247 bytes parent folder | download
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
// size options
const sizeOptions = [
  { 'label': "S", 'size': 320 },
  { 'label': "M", 'size': 640 },
  { 'label': "L", 'size': 960 },
  { 'label': "XL", 'size': 1280 }
];

async function init() {
  if (!"mediaDevices" in navigator ||
      !"getUserMedia" in navigator.mediaDevices) {
    handleError("Camera API is not available in your browser");
    return;
  }

  // get page elements
  const video         = document.getElementById("video");
  const buttons       = document.getElementById("buttons");
  const buttonsLeft   = document.getElementById("buttons-left");
  const snapshot      = document.getElementById("snapshot");
  const rotleft       = document.getElementById("rotleft");
  const rotright      = document.getElementById("rotright");
  const canvas        = document.getElementById("canvas");
  const nocamera      = document.getElementById("nocamera");
  const selectCamera  = document.getElementById("selectCamera");
  const videoSelect   = document.getElementById("videoSelect");
  const sizeSelect    = document.getElementById("sizeSelect");
  const sizeIcon      = document.getElementById("sizeIcon");
  const play          = document.getElementById("play");
  const pause         = document.getElementById("pause");
  const glass         = document.getElementById("glass");

  // current camera;
  let camera;

  // current camera label
  let cameraLabel;

  // camera status
  let playing = true;

  // current video stream
  let videoStream;

  // orientation
  let orientation = parseInt(await sankore.async.preference("orientation", "0"));

  // size of video in widget
  let videoSize;

  // snapshot size
  let size;

  // cropper
  let cropper;

  let cropOptions = {
    viewMode: 0,
    guides: false,
    center: false,
    background: false,
    rotatable: false,
    scalable: false,
    zoomable: false,
    autoCropArea: 0.99,
    toggleDragModeOnDblclick: false,
    ready() {
      resizeCropper(videoSize);
      showCropper(true);
    },
    cropend() {
      updateSizes();
    }
  };

  // translate tooltips
  translate();

  // setup size options
  for (const sizeOption of sizeOptions) {
    const input = document.createElement('input');
    input.setAttribute("type", "radio");
    input.setAttribute("name", "size");
    input.setAttribute("id", sizeOption.label);
    input.value = sizeOption.size;

    const label = document.createElement('label');
    label.appendChild(input);
    const name = document.createTextNode(sizeOption.label);
    label.appendChild(name);

    const div = document.createElement('div');
    div.onclick = () => {
      setSize(sizeOption.label);
    };
    div.appendChild(label);

    sizeSelect.appendChild(div);
  }

  let sizeId = await sankore.async.preference("size", "M")
  setSize(sizeId);

  // connect hover slots
  window.widget.onenter.connect(() => {
    buttons.style.display = "flex";
    buttonsLeft.style.display = "flex";
    if (videoStream) {
      (playing ? pause : play).style.display = "block";
    }

    showCropper(true);
  });

  window.widget.onleave.connect(() => {
    buttons.style.display = "none";
    buttonsLeft.style.display = "none";
    pause.style.display = "none";
    play.style.display = "none";
    showCropper(false);
  });

  // connect event handler
  // rescan camera list
  selectCamera.onmouseenter = () => {
    getDevices().then(gotDevices);
  }

  // resize widget to video aspect ratio
  video.onresize = () => {
    if (!cropper) {
      cropper = new Cropper(glass, cropOptions);
    }

    let videoSize = layoutVideo();
    sankore.resize(videoSize.width, videoSize.height);
  };

  // adjust video size when widget is resized
  window.onresize = layoutVideo;

  // rotate
  rotleft.onclick = () => {
    orientation = (orientation + 3) % 4;
    sankore.setPreference("orientation", orientation);
    let videoSize = layoutVideo();
    sankore.resize(videoSize.width, videoSize.height);
  };

  rotright.onclick = () => {
    orientation = (orientation + 1) % 4;
    sankore.setPreference("orientation", orientation);
    let videoSize = layoutVideo();
    sankore.resize(videoSize.width, videoSize.height);
  };

  // play/pause
  play.onclick = () => {
    video.play();
    playing = true;
    play.style.display = "none";
    pause.style.display = "block";
  };

  pause.onclick = () => {
    video.pause();
    playing = false;
    pause.style.display = "none";
    play.style.display = "block";
  };

  // take snapshot
  snapshot.onclick = (event) => {
    event.cancelBubble = true;

    let clipBox = getClipBox();

    // compute cropping factors
    let cleft = clipBox.left / videoSize.width;
    let cwidth = clipBox.width / videoSize.width;
    let ctop = clipBox.top / videoSize.height;
    let cheight = clipBox.height / videoSize.height;

    let vleft;
    let vwidth;
    let vtop;
    let vheight;

    // cropped video coordinates depending on orientation
    switch (orientation) {
      case 0:
        vleft = cleft * video.videoWidth;
        vwidth = cwidth * video.videoWidth;
        vtop = ctop * video.videoHeight;
        vheight = cheight * video.videoHeight;
        break;
      case 1:
        vleft = ctop * video.videoWidth;
        vwidth = cheight * video.videoWidth;
        vtop = (1 - cleft - cwidth) * video.videoHeight;
        vheight = cwidth * video.videoHeight;
        break;
      case 2:
        vleft = (1 - cleft - cwidth) * video.videoWidth;
        vwidth = cwidth * video.videoWidth;
        vtop = (1 - ctop - cheight) * video.videoHeight;
        vheight = cheight * video.videoHeight;
        break;
      case 3:
        vleft = (1 - ctop - cheight) * video.videoWidth;
        vwidth = cheight * video.videoWidth;
        vtop = cleft * video.videoHeight;
        vheight = cwidth * video.videoHeight;
        break;
    }

    const aspectRatio = clipBox.width / clipBox.height;

    if (aspectRatio < 1) {
      canvas.height = size.value;
      canvas.width = canvas.height * aspectRatio;
    } else {
      canvas.width = size.value;
      canvas.height = canvas.width / aspectRatio;
    }

    let ctx = canvas.getContext("2d");
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.translate(canvas.width / 2, canvas.height / 2);
    ctx.rotate(orientation * Math.PI / 2);

    if (orientation % 2) {
      ctx.translate(-canvas.height / 2, -canvas.width / 2);
      ctx.drawImage(video, vleft, vtop, vwidth, vheight, 0, 0, canvas.height, canvas.width);
    } else {
      ctx.translate(-canvas.width / 2, -canvas.height / 2);
      ctx.drawImage(video, vleft, vtop, vwidth, vheight, 0, 0, canvas.width, canvas.height);
    }

    let data = canvas.toDataURL("image/jpeg");
    sankore.addObject(data, 0, 0, 0, 0, false);
  };

  // layout video
  function layoutVideo() {
    if (video.videoHeight === 0 || video.videoWidth === 0) {
      return;
    }

    let longWindow = Math.max(window.innerHeight, window.innerWidth);
    let width;
    let height;

    video.style.transform = "rotate(" + 90 * orientation + "deg)";

    if (orientation % 2 === 0) {
      // for landscape:
      // use longest side for width
      // adapt the height according to aspect ratio
      width = longWindow;
      height = width * video.videoHeight / video.videoWidth;

      video.style.width = width + "px";
      video.style.height = height + "px";
      video.style.top = "0px";
      video.style.left = "0px";
    } else {
      // for portrait:
      // use longest side for height
      // adapt width according to aspect ratio
      height = longWindow;
      width = height * video.videoHeight / video.videoWidth;

      video.style.width = height + "px";
      video.style.height = width + "px";
      let offset = (height - width) / 2;
      video.style.top = offset + "px";
      video.style.left = -offset + "px";
    }

    glass.style.width = width + "px";
    glass.style.height = height + "px";

    if (videoSize === undefined || Math.abs(width - videoSize.width) > 2 || Math.abs(height - videoSize.height) > 2) {
      videoSize = {
        'width': width,
        'height': height
      };

      // delay until cropper is ready
      setTimeout(() => {
        resizeCropper(videoSize);
        updateSizes();
      }, 100);
    }

    return videoSize;
  };

  // stop video stream
  function stopVideoStream() {
    if (videoStream) {
      videoStream.getTracks().forEach((track) => {
        track.stop();
      });
    }
  }

  // request list of media devices
  function getDevices() {
    return navigator.mediaDevices.enumerateDevices();
  }

  // extract video devices and build selection options
  function gotDevices(deviceInfos) {
    // delete list
    while (videoSelect.hasChildNodes()) {
      videoSelect.removeChild(videoSelect.firstChild);
    }
    // populate list
    for (const deviceInfo of deviceInfos) {
      if (deviceInfo.kind === 'videoinput') {
        const input = document.createElement('input');
        input.setAttribute("type", "radio");
        input.setAttribute("name", "source");
        input.setAttribute("id", deviceInfo.deviceId);
        input.value = deviceInfo.deviceId;

        const label = document.createElement('label');
        label.appendChild(input);
        const text = deviceInfo.label || `Camera ${videoSelect.length + 1}`;
        const name = document.createTextNode(text);
        label.appendChild(name);

        const div = document.createElement('div');
        div.onclick = () => {
          setCamera(deviceInfo.deviceId);
        };
        div.appendChild(label);

        videoSelect.appendChild(div);
      }
    }

    // select previously selected video source
    let selected = undefined;

    if (cameraLabel) {
      selected = [...videoSelect.children].find(entry => entry.firstChild.textContent === cameraLabel);
    }

    if (selected) {
      selected.firstChild.firstChild.checked = true;
    } else {
      // camera no longer available
      handleError();
    }
  }

  // request stream for selected device
  function getStream() {
    stopVideoStream();
    const videoSource = camera;
    const constraints = {
      video: {
        deviceId: videoSource ? {exact: videoSource} : undefined,
        width: { ideal: 1920 },
        height: { ideal: 1080 }
      }
    };
    return navigator.mediaDevices.getUserMedia(constraints).
      then(gotStream).catch(handleError);
  }

  // assign stream to video element
  function gotStream(stream) {
    videoStream = stream;
    cameraLabel = stream.getVideoTracks()[0].label;
    let selected = [...videoSelect.children].find(entry => entry.firstChild.textContent === cameraLabel);
    if (selected) {
      selected.firstChild.firstChild.checked = true;
    }

    video.srcObject = stream;
    video.style.display = "unset";
    nocamera.style.display = "none";
  }

  // display "no camera" symbol
  function handleError(error) {
    videoStream = undefined;
    camera = undefined;
    cameraLabel = undefined;
    video.style.display = "none";
    pause.style.display = "none";
    play.style.display = "none";
    nocamera.style.display = "flex";
    showCropper(false);
  }

  // change size
  function setSize(id) {
    size = document.getElementById(id);
    size.checked = true;
    sizeIcon.textContent = id;
    sankore.setPreference("size", id);
  }

  // select camera
  function setCamera(id) {
    const input = document.getElementById(id);

    if (input) {
      input.checked = true;
      camera = input.id;
      getStream();
    }
  }

  // show or hide cropper
  function showCropper(show) {
    const cropperContainer = document.querySelector(".cropper-container");

    if (cropperContainer) {
      if (show && videoStream) {
        cropperContainer.style.display = "unset";
      } else {
        cropperContainer.style.display = "none";
      }
    }
  }

  // resize cropper
  function resizeCropper(size) {
    if (cropper) {
      cropper.setCropBoxData({
        left: 2,
        top: 2,
        width: size.width - 4,
        height: size.height - 4
      });
    }
  }

  // get effective size of crop box
  function getClipBox() {
    let cropBox = cropper.getCropBoxData();
    let clipBox = { ...cropBox };

    // round if close to border
    if (cropBox.left < 4) clipBox.left = 0;
    if (cropBox.top < 4) clipBox.top = 0;
    if (videoSize.width - cropBox.left - cropBox.width < 4) clipBox.width = videoSize.width - clipBox.left;
    if (videoSize.height - cropBox.top - cropBox.height < 4) clipBox.height = videoSize.height - clipBox.top;

    return clipBox;
  }

  // update pixel sizes in size selection menu
  function updateSizes() {
    const clipBox = getClipBox();
    const longSide = Math.max(clipBox.width, clipBox.height);

    for (entry of sizeSelect.children) {
      const pixels = entry.firstChild.firstChild.value;
      const factor = pixels / longSide;
      const x = Math.round(clipBox.width * factor);
      const y = Math.round(clipBox.height * factor);
      const label = entry.firstChild.firstChild.id + " (" + x + "x" + y + ")";
      entry.firstChild.childNodes[1].textContent = label;
    }
  }

  // initialize
  getDevices().then(gotDevices).then(getStream);
}