Video overlay
An SVG caption panel with a gradient, filter shadows and SMIL motion fades in over a playing video and slides away.
What you are seeing
The Big Buck Bunny trailer plays in a wide frame on a black stage, with a credit line above it. A red gradient panel at the lower left shows a small film icon, the title Big Buck Bunny and a two line description. It fades in when the video starts and slides out to the left after ten seconds. Round white play and pause buttons sit at the lower right.
How it works
A hidden HTML video supplies the picture and sound, and each frame is copied through a canvas into an SVG image.
When the video starts, a SMIL animation fades in the caption group.
Ten seconds later a second animation fades the group out while an animateTransform moves it off to the left.
Pausing the video also pauses the SVG document timeline, so the caption holds its position.
History
Published on October 4, 2009 for browsers with the SVG Tiny 1.2 video element. The original film is not used here because its public domain status could not be confirmed.
Source
/video/videoOverlays.svg
<?xml version="1.0" encoding="UTF-8"?>
<!-- ======================================================================= -->
<!-- Erik Dahlström -->
<!-- License terms: see svg-wow.org -->
<!-- CC0 http://creativecommons.org/publicdomain/zero/1.0/ -->
<!-- Restored: the removed SVG Tiny video element is now a hidden HTML video -->
<!-- painted into an SVG image, and the film is the Big Buck Bunny trailer. -->
<!-- ======================================================================= -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 700 512" width="100%" height="100%" style="background:#000">
<title>Video overlay</title>
<desc>The Big Buck Bunny trailer plays with an SVG caption panel sliding over it. A red gradient panel with a film icon, a title and a short description fades in when the video starts and slides out to the left after ten seconds. Round play and pause buttons sit at the lower right. Space also plays and pauses.</desc>
<defs>
<style>
text { font-family: Verdana, "DejaVu Sans", Arial, sans-serif; fill: white; }
#header { font-size: 24px; font-weight: bold; }
#subheader { font-size: 14px; }
#credit, #credit a { font-size: 12px; fill: #d8d8d8; }
#credit a { text-decoration: underline; }
#controls circle { cursor: pointer; }
#controls circle:focus { outline: none; stroke: #ffd24a; stroke-width: 3; }
#startHint { font-size: 18px; font-weight: bold; }
@media (max-width: 520px) { #credit, #credit a { font-size: 19px; } #startHint { font-size: 24px; } #subheader { font-size: 17px; } }
#videoImage { cursor: pointer; }
</style>
<linearGradient id="overlaygrad" x2="1" y1="-0.03">
<stop offset="0%" stop-color="#5f070e"/>
<stop offset="70%" stop-color="#ff2700"/>
<stop offset="85%" stop-color="#ff2700" stop-opacity="0.7"/>
<stop offset="100%" stop-color="#ff2700" stop-opacity="0"/>
</linearGradient>
<filter id="ds1" height="1.3">
<feGaussianBlur in="SourceAlpha" stdDeviation="4"/>
<feOffset dx="2" dy="2"/>
<feBlend in="SourceGraphic" mode="normal"/>
</filter>
<filter id="ds2" height="1.3" width="1.3">
<feGaussianBlur in="SourceAlpha" stdDeviation="4"/>
<feOffset dx="2" dy="2"/>
<feBlend in="SourceGraphic" mode="normal"/>
</filter>
</defs>
<!-- WebKit paints a video in a foreignObject at the wrong place and size, so
the video stays out of sight and its frames are painted into this image. -->
<image id="videoImage" x="0" y="61" width="700" height="389" href="bbb-frame.jpg" role="img" aria-label="Big Buck Bunny trailer"/>
<foreignObject id="videoFrame" x="0" y="0" width="2" height="2" opacity="0.01" pointer-events="none">
<video xmlns="http://www.w3.org/1999/xhtml" id="video" width="2" height="2" preload="auto" playsinline="playsinline" aria-hidden="true">
<source src="../res/2.0/videos/bigbuckbunny_trailer_480p.webm" type="video/webm"/>
<source src="../res/2.0/videos/bigbuckbunny_trailer_480p.mp4" type="video/mp4"/>
</video>
</foreignObject>
<text id="credit" x="350" y="24" text-anchor="middle"><tspan x="350">Big Buck Bunny, (c) 2008 Blender Foundation</tspan><tspan x="350" dy="1.3em"><a xlink:href="https://creativecommons.org/licenses/by/3.0/" target="_blank">CC BY 3.0</a>, <a xlink:href="https://peach.blender.org/" target="_blank">peach.blender.org</a></tspan></text>
<text id="startHint" x="350" y="258" text-anchor="middle" filter="url(#ds1)">Press play to start the video and sound</text>
<g id="overlay" opacity="0">
<rect id="figure-rect" x="-20" y="380" height="120" width="75%" fill="url(#overlaygrad)"/>
<g id="media-icon" transform="translate(20,400)" filter="url(#ds2)">
<rect width="32" height="26" y="3" rx="3" fill="#f4f4f4"/>
<path d="M 12 9 L 23 16 L 12 23 Z" fill="#a01a10"/>
</g>
<text id="subheader" x="23" y="456"><tspan x="23">A very large rabbit, three rude rodents and one</tspan><tspan x="23" dy="1.3em">carefully planned payback, in a short trailer.</tspan></text>
<text id="header" x="60" y="430">Big Buck Bunny</text>
<animate id="overlayIn" attributeName="opacity" to="1" dur="1s" begin="indefinite" fill="freeze"/>
<animate id="master" attributeName="opacity" to="0" dur="1s" begin="overlayIn.begin+10s" fill="freeze"/>
<animateTransform attributeName="transform" type="translate" to="-500 0" dur="3s" begin="master.begin" fill="freeze"/>
</g>
<g id="controls" transform="translate(550 420)">
<rect fill-opacity="0.7" rx="10" width="86" height="46"/>
<circle id="play" fill="white" r="20" cx="22" cy="23" role="button" tabindex="0" aria-label="Play"/>
<circle id="pause" fill="white" r="20" cx="64" cy="23" role="button" tabindex="0" aria-label="Pause"/>
<path d="M 15 12 L 33 23 L 15 34 Z" fill="#222" pointer-events="none"/>
<path d="M 56 12 H 62 V 34 H 56 Z M 66 12 H 72 V 34 H 66 Z" fill="#222" pointer-events="none"/>
</g>
<script><![CDATA[
(function () {
var svg = document.documentElement;
var video = document.getElementById('video');
var hint = document.getElementById('startHint');
var image = document.getElementById('videoImage');
var canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
var ctx = canvas.getContext('2d');
var painted = -1;
var overlayStarted = false;
canvas.width = 720;
canvas.height = 400;
function setImage(url) {
image.setAttribute('href', url);
}
// Each frame is decoded off screen first. Firefox never shows an SVG
// image whose source is replaced again before it has finished loading.
var loader = new Image();
var decoding = false;
loader.onload = function () {
setImage(loader.src);
decoding = false;
};
loader.onerror = function () {
decoding = false;
};
function paint() {
if (!decoding && video.readyState >= 2 && video.currentTime !== painted) {
painted = video.currentTime;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
decoding = true;
loader.src = canvas.toDataURL('image/jpeg', 0.8);
}
if (!video.paused && !video.ended) {
requestAnimationFrame(paint);
}
}
// The SVG document timeline follows the video, so the panel holds when
// the video is paused. The panel is replaced to restart its animations.
function restartOverlay() {
var old = document.getElementById('overlay');
var fresh = old.cloneNode(true);
old.parentNode.replaceChild(fresh, old);
document.getElementById('overlayIn').beginElement();
}
function playVideo() {
var result = video.play();
if (result && result.catch) {
result.catch(function () {});
}
}
function toggle() {
if (video.paused || video.ended) {
playVideo();
} else {
video.pause();
}
}
video.addEventListener('play', function () {
hint.setAttribute('display', 'none');
svg.unpauseAnimations();
requestAnimationFrame(paint);
if (!overlayStarted) {
overlayStarted = true;
restartOverlay();
}
});
video.addEventListener('pause', function () {
svg.pauseAnimations();
});
video.addEventListener('ended', function () {
overlayStarted = false;
hint.textContent = 'Press play to watch again';
hint.setAttribute('display', 'inline');
});
function bind(id, action) {
var el = document.getElementById(id);
el.addEventListener('click', function (e) { e.stopPropagation(); action(); });
el.addEventListener('keydown', function (e) {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
e.stopPropagation();
action();
}
});
}
bind('play', playVideo);
bind('pause', function () { video.pause(); });
image.addEventListener('click', toggle);
document.addEventListener('keydown', function (e) {
if (e.key === ' ') {
e.preventDefault();
toggle();
}
});
})();
]]></script>
</svg>
/video/videoOverlays.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Video overlay</title>
<meta name="description" content="An SVG caption panel animated over a playing video. Original demo by Erik Dahlström, 2009.">
<style>
html, body { margin: 0; background: #111; color: #eee; font: 16px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif; }
main { max-width: 900px; margin: 0 auto; padding: 16px; }
h1 { font-size: 1.5rem; margin: 0 0 4px; }
p { margin: 0 0 12px; }
a { color: #ffb08a; }
iframe { display: block; width: 100%; aspect-ratio: 700 / 512; border: 0; background: #000; }
</style>
</head>
<body>
<main>
<h1><svg width="26" height="22" viewBox="0 0 32 26" aria-hidden="true"><rect width="32" height="26" rx="3" fill="#f4f4f4"/><path d="M 12 6 L 23 13 L 12 20 Z" fill="#a01a10"/></svg> Video overlay</h1>
<p>By Erik Dahlström, 2009. <a href="/gallery/video-overlay/">Gallery page</a></p>
<iframe src="videoOverlays.svg" title="Video overlay demo. Use the play and pause buttons, or press space after clicking inside."></iframe>
</main>
</body>
</html>