Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Created February 25, 2020 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/55c103178f4c5febe859b972ee8b5df2 to your computer and use it in GitHub Desktop.
Save andersevenrud/55c103178f4c5febe859b972ee8b5df2 to your computer and use it in GitHub Desktop.
OS.js Webcam application example (localhost or https only)
//
// NOTE: THIS ONLY WORKS ON "localhost" OR A HOST WITH "https:"
//
import osjs from 'osjs';
import {name as applicationName} from './metadata.json';
const createVideoElement = () => {
const video = document.createElement('video');
video.style.width = '100%';
video.style.height = '100%';
video.style.backgroundColor = '#000000';
video.autoplay = true;
return video;
};
const register = (core, args, options, metadata) => {
const proc = core.make('osjs/application', {args, options, metadata});
const video = createVideoElement();
const errorDialog = message => core
.make('osjs/dialog', 'alert', {
title: 'An error occured while opening device',
message
}, () => {});
const requestCamera = () => {
if (!navigator || !navigator.mediaDevices) {
errorDialog('No media device support (or not loaded via https)');
} else {
navigator
.mediaDevices
.getUserMedia({video: true})
.then(stream => (video.srcObject = stream))
.catch(errorDialog);
}
};
proc.createWindow({
id: 'WebcamTestWindow',
title: metadata.title.en_EN,
position: 'center',
dimension: {width: 640, height: 640}
})
.on('destroy', () => proc.destroy())
.on('render', requestCamera)
.render($content => $content.appendChild(video));
return proc;
};
osjs.register(applicationName, register);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment