4
votes

I am trying to make my HTML5 video have top-left and bottom-left rounded corners with transparency, just like how it would act when using border-radius. Unfortunately in Chrome the border-radius does not work for some kind of reason on the HTML video tag, but does in IE10 and Firefox.

After several attempts of trying to achieve that I stumbled upon this answer: https://stackoverflow.com/a/16470150/1115367 But I quickly found out that this will fill in a color in the rounded corners, instead of making it transparent.

How can I make the border radius working on a HTML5 video in Google Chrome without decreasing the video performance?

I'm willing to use javascript / jQuery if necessary.

3

3 Answers

7
votes

To quote another post:

There are some outstanding bugs in WebKit to do with it clipping content in concert with border-radius, like this one or this one specifically about the video element.

If you apply the border-radius to an element that wraps around the video, and adds a -webkit-mask-image, then it can be done in Chrome. Here's a demo that masks a transparent png and clips the corners of the video:

Demo (transparent background): http://jsfiddle.net/pe3QS/24/

Update: I changed the HTML/CSS to only use one wrapping element, and it works on (at least) IE9+, FF and Chrome.


CSS:

div.outer {
    float: left;
    height: 240px;
}
div.outer video {
    width: 320px;
    height: 100%;
    -webkit-mask-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA5JREFUeNpiYGBgAAgwAAAEAAGbA+oJAAAAAElFTkSuQmCC);
    border-radius: 32px 0 32px 0;
}

HTML:

<div class="outer">
    <video src="http://www.w3schools.com/html/movie.ogg" type="video/ogg" controls></video>
</div>
4
votes

You can get around this by using a canvas element. It requires a little more code but this approach also opens up many other possibilities and control.

You can apply the CSS with the Canvas element:

#canvas {
    background:#000;
    border-radius:20px 0 0 20px; /* top left and bottom left as in OP */
}

Then create and load the video manually (alternatively you can load the video with HTML and just hide the original video element):

Live fiddle here

var video = document.createElement('video'),
    url;

/// setup video instance    
video.preload = 'auto';
video.addEventListener('canplaythrough', start, false);

/// check what we can play and borrow some online video link
if (video.canPlayType('video/ogg').length > 0 ) {
    url = 'http://www.w3schools.com/html/movie.ogg';
} else {
    url = 'http://www.w3schools.com/html/movie.mp4';
}

/// start loading video
video.src = url;

/// start the loop
function start(e) {

    /// get canvas and context
    var canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d'),
        w = canvas.width,
        h = canvas.height,
        toggle = false;

    /// start video and loop
    video.play();
    loop();
    
    function loop() {
        
        /// we won't need 60 FPS so reduce to 30 FPS
        toggle = !toggle;
        if (toggle === false) {
            requestAnimationFrame(loop);
            return;
        }
        
        /// draw video frame onto canvas
        ctx.drawImage(video, 0, 0, w, h);
        requestAnimationFrame(loop);
    }
}

This is minimal example of course: you would need to properly check the canPlayType arguments (some browsers say "no" so check for this too or replace no with an empty string to use length as here) for other types as well (ie. webm).

One note though: this won't work with Safari browsers as they do not comply (at current writing) with the Canvas standard. In this case they don't support drawImage with video element as source.

1
votes

We have a video playing with rounded corners and a drop shadow and it's as simple as:

border-radius: 22px; overflow: hidden; -webkit-transform: translateZ(0); box-shadow: 0 19px 51px 0 rgba(0,0,0,0.16), 0 14px 19px 0 rgba(0,0,0,0.07);

The key is the -webkit-transform: translateZ(0). This line of code tells the browser to render on the GPU instead of with the CPU allowing for more process heavy styles like this.

Tested and working as of Safari 11, Chrome 65, Firefox 59, Edge Win 10 & IE 11