Is that possible to make <video> mirrored?

Is that possible to make a video inside tag mirrored horizontally or vertically?

52677 次浏览

You can do it using a CSS3 3D transformation.

#videoElement
{
transform: rotateY(180deg);
-webkit-transform:rotateY(180deg); /* Safari and Chrome */
-moz-transform:rotateY(180deg); /* Firefox */
}

This will rotate it 180 degrees around its Y axis (so you're now looking at it from behind) which gives the same appearance as being mirrored.

Example at http://jsfiddle.net/DuT9U/1/

You can use CSS3 scaleX or scaleY set to -1 to respectively flip the video horizontally or vertically.

Using JavaScript, if video is the video element, to mirror (flip horizontally) you can use

video.style.cssText = "-moz-transform: scale(-1, 1); \
-webkit-transform: scale(-1, 1); -o-transform: scale(-1, 1); \
transform: scale(-1, 1); filter: FlipH;";

To flip vertically you can use

video.style.cssText = "-moz-transform: scale(1, -1); \
-webkit-transform: scale(1, -1); -o-transform: scale(1, -1); \
transform: scale(1, -1); filter: FlipV;";

By any chance if somebody wants a working example, here is the code (with mirrored/rotated). Refer the video element #videoElement under style tag:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>


<style>
#container {
margin: 0px auto;
width: 500px;
height: 375px;
border: 10px #333 solid;
}
#videoElement {
width: 500px;
height: 375px;
background-color: #666;
/*Mirror code starts*/
transform: rotateY(180deg);
-webkit-transform:rotateY(180deg); /* Safari and Chrome */
-moz-transform:rotateY(180deg); /* Firefox */
/*Mirror code ends*/
}


</style>
</head>


<body>
<div id="container">
<video autoplay="true" id="videoElement">


</video>
</div>
<script>
var video = document.querySelector("#videoElement");


navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;


if (navigator.getUserMedia) {
navigator.getUserMedia({video: true}, handleVideo, videoError);
}


function handleVideo(stream) {
video.src = window.URL.createObjectURL(stream);
}


function videoError(e) {
// do something
}
</script>
</body>
</html>

to prevent rotating controls

video {
transform: scale(-1,1);
}
video::-webkit-media-controls-panel {
transform: scale(-1,1);
}