Skip to content

Instantly share code, notes, and snippets.

@atomkirk
Created March 13, 2020 04:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomkirk/4dcf7b2947fbaff8b0b15a93e3d49c6b to your computer and use it in GitHub Desktop.
Save atomkirk/4dcf7b2947fbaff8b0b15a93e3d49c6b to your computer and use it in GitHub Desktop.
iOS Safari Camera API
<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>
@Antoine-lb
Copy link

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 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment