Created
March 13, 2020 04:49
-
-
Save atomkirk/4dcf7b2947fbaff8b0b15a93e3d49c6b to your computer and use it in GitHub Desktop.
iOS Safari Camera API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<video id="player" autoplay muted playsinline> </video> | |
<button id="capture">Capture</button> | |
<canvas id="canvas" width=320 height=240></canvas> | |
<script> | |
const player = document.getElementById('player'); | |
const canvas = document.getElementById('canvas'); | |
const context = canvas.getContext('2d'); | |
const captureButton = document.getElementById('capture'); | |
const constraints = { | |
video: { | |
facingMode: { | |
exact: 'environment' | |
} | |
} | |
}; | |
captureButton.addEventListener('click', () => { | |
// Draw the video frame to the canvas. | |
context.drawImage(player, 0, 0, canvas.width, canvas.height); | |
}); | |
// Attach the video stream to the video element and autoplay. | |
navigator.mediaDevices.getUserMedia(constraints) | |
.then((stream) => { | |
player.srcObject = stream; | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
3 years later and it still very relevant, many snippets on the internet don't work properly on both iOS and Android, this one does, and with very little code. Thanks 🚀