by Zarema Khalilova
const constraints = {video: true, audio: true}
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {...})
.catch(error => {...})
stream => {
const $video = document.querySelector('video')
$video.srcObject = stream
$video.onloadedmetadata = () => $video.play()
}
Choose this slide to see demo
stream => {
const $video = document.createElement('video')
$video.srcObject = stream
$video.onloadedmetadata = () => {
$video.play()
...
...
const $canvas = document.querySelector('canvas')
$canvas.width = $video.videoWidth
$canvas.height = $video.videoHeight
...
...
const context = $canvas.getContext('2d')
const draw = () => {
context.drawImage($video, 0, 0)
requestAnimationFrame(draw)
}
...
...
requestAnimationFrame(draw)
}
}
Choose this slide to see demo
Choose this slide to see demo
Choose this slide to see demo
drawImage
calls is higher than the frame rate of the videolet frameDrawed = true
const runDraw = () => {
if (!frameDrawed) return
frameDrawed = false
requestAnimationFrame(draw)
}
const streamTrack = stream.getVideoTracks()[0]
const {frameRate} = streamTrack.getSettings()
const delay = 1000 / frameRate
let frameInterval = setInterval(runDraw, delay)
const draw = () => {
...
requestAnimationFrame(draw)frameDrawed = true
}
const constraints = {video: true, audio: true}
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {...})
.catch(error => {...})
const supportedConstraints =
navigator.mediaDevices.getSupportedConstraints()
const constraints = {
video: {
height: 200,
aspectRatio: 1,
},
}
navigator.mediaDevices.getUserMedia(constraints)
Choose this slide to see demo
const videoTrack = stream.getVideoTracks()[0]
const capabilities = videoTrack.getCapabilities()
if (Object.keys(capabilities).length) {
// You can configure stream
}
if (capabilities.torch) {
const $torch = document.querySelector('button.torch')
$torch.onclick = () => {
const torch = (videoTrack.getSettings()).torch
videoTrack.applyConstraints({
advanced: [{torch: !currentTorch}]
})
if (capabilities.zoom) {
const $zoom = document.querySelector('input[type=range]')
$zoom.min = capabilities.zoom.min
$zoom.max = capabilities.zoom.max
$zoom.step = capabilities.zoom.step
...
...
$zoom.value = (videoTrack.getSettings()).zoom
$zoom.onchange = (e) => videoTrack.applyConstraints({
advanced: [{zoom: e.target.value}]
})
Choose this slide to see demo
const blobToImage = blob => {
const $img = document.createElement('img')
$img.src = URL.createObjectURL(blob)
$img.onload = () => URL.revokeObjectURL($img.src)
document.querySelector('.output').append($img)
}
canvas
& drawImage
Grab video frame
drawImage
stream => {
... // Get video element and put stream to that
... // Create canvas and context
context.drawImage($video, 0, 0)
...
drawImage
...
$canvas.toBlob(blob => blobToImage(blob))
}
ImageCapture
& grabFrame
Grab stream frame
grabFrame
stream => {
const streamTrack = stream.getVideoTracks()[0]
const imageCapture = new ImageCapture(streamTrack)
...
grabFrame
...
imageCapture.grabFrame()
.then(imageBitmap => {
... // Create canvas and context
context.drawImage(imageBitmap, 0, 0)
$canvas.toBlob(blob => blobToImage(blob))
}
}
ImageCapture
& takePhoto
Get full resolution photo from stream
takePhoto
...
imageCapture.takePhoto()
.then(blob => blobToImage(blob))
}
Choose this slide to see demo
Choose this slide to see demo
stream => {
... // Create video and put stream to video
let chunks = []
const mediaRecorder = new MediaRecorder(stream)
...
...
$startButton.onclick = () => mediaRecorder.start()
$stopButton.onclick = () => mediaRecorder.stop()
...
...
mediaRecorder.ondataavailable = (e) => chunks.push(e.data)
mediaRecorder.onstop = () =>
blobToVideo(new Blob(chunks, {type: 'video/webm'}))
}
Choose this slide to see demo
Just use CSS Filters for video
video[data-filter=grayscale] {
filter: grayscale(100%);
}
To take photo with filter use drawImage
way with tip:
context.filter = window
.getComputedStyle($video, null)
.getPropertyValue('filter')
context.drawImage($video, 0, 0)
Please allow this page to access your camera.
Your browser does not support the Camera API.
Choose this slide to see demo
<script src=".../aframe.min.js"></script>
<script src=".../aframe-ar.js"></script>
<body style='margin : 0px; overflow: hidden;'>
<a-scene embedded artoolkit='sourceType: webcam;'>
<a-box position='0 0 0.5' material='opacity: 0.7;'></a-box>
<a-marker-camera preset='hiro'></a-marker-camera>
</a-scene>
</body>