Video in SVG and HTML

One film plays in a circular SVG window and an HTML player, on a page that rearranges itself with CSS media queries.

What you are seeing

A two part page. A tall panel on the left shows a still of the rodents from Big Buck Bunny with a large white circle holding the words watch the trailer. Clicking it plays the trailer inside the circle. To the right is a short article about the technique with a framed video player on a green textured background. On small screens the panel moves above the article.

How it works

An SVG document in an iframe shows a film still with a soft circle over it.

Clicking plays a hidden video whose frames are painted into an SVG image clipped to that circle.

The article holds an ordinary HTML video in a frame whose texture is an SVG turbulence filter used as a CSS background.

Media queries move the SVG panel from the left edge to the top of the page on narrower screens.

History

Published on October 4, 2009. It combined the SVG Tiny 1.2 video element with HTML video, web fonts and early media queries.

Source

/video2/video.html
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<title>HTML5 and SVG video, combined with CSS3 media queries</title>
		<meta name="description" content="The Big Buck Bunny trailer plays in an SVG circle and an HTML video, in a page that changes layout with CSS media queries." />
    <!--
      /* ============================================================================
       * Author: Erik Dahlström
       * License terms: see svg-wow.org
       * CC0 http://creativecommons.org/publicdomain/zero/1.0/
       * Restored: the object elements are iframes, the fonts are system fonts,
       * the grass photo is a procedural SVG, and the filler text is new.
       * =========================================================================== */
    -->
	<style>
		html, body { margin: 0; background: #fff; color: #1b1b1b; }
		#pusher { display: none }
		.caption { font-size: 12px; font-family: system-ui, sans-serif; margin-top: 10px; margin-bottom: 0px; text-align: center; color: #fff; max-width: 540px; }
		.caption a { color: #fff; }
		h1,h3 { font-family: "Arial Black", Impact, system-ui, sans-serif; font-variant: small-caps }
		p { line-height: 1.55; }
		#side { border: 0; display: block; }
		#htmlvideo video { display: block; max-width: 100%; height: auto; background: #000; }
		#content { font-family: Georgia, "Times New Roman", serif; box-sizing: border-box; }
		.back { font-family: system-ui, sans-serif; font-size: 14px; }
		@media all and (min-width: 1024px) {
			#htmlvideo { float:right; padding: 25px; margin-left:10px; background: url(video-background.svg); max-width: 540px; }
			#content { position: relative; margin-left: 320px; padding-right: 24px; }
			#side { width: 300px; height: 100%; position: fixed; left:0px; top:0px }
		}
		@media all and (max-width: 1023.98px) {
			#htmlvideo { display: block; padding: 16px; margin: 0 0 12px; background: url(video-background.svg); box-sizing: border-box; }
			#content { padding: 0 16px 24px; }
			#side { width: 100%; height: 360px; margin-bottom: 10px; }
			.caption { max-width: none; }
		}
	</style>
	</head>
	<body>
		<iframe id="side" src="video.svg" title="Big Buck Bunny trailer in an SVG circle. Click to play or pause."></iframe>
		<div id="content">
			<h1>Video in two places</h1>
			<p class="back"><a href="/gallery/video/">Back to the gallery page</a></p>
			<p>This page shows one film in two ways. On the left, or at the top on a small screen, an SVG document clips the video to a circle. In the article, a plain HTML video player sits in a frame whose texture is itself an SVG file, used as a CSS background image.</p>
			<span id="htmlvideo">
				<video id="articleVideo" poster="bbb-frame.jpg" controls="controls" preload="metadata" playsinline="playsinline" width="540" height="300">
					<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>
				<div class="caption">Big Buck Bunny, (c) 2008 Blender Foundation, <a href="https://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>, <a href="https://peach.blender.org/">peach.blender.org</a></div>
			</span>
			<div id="pusher"></div>
			<h3>Resize the window</h3>
			<p>Two media queries decide the layout. At 1024 pixels and wider, the SVG panel is fixed to the left edge and the article flows beside it, with the player floated to the right. Below that width the panel moves above the article and the player takes the full column.</p>

			<h3>Why the circle still works</h3>
			<p>In 2009 this demo used the video element from SVG Tiny 1.2, which only Opera shipped in full. Browsers later dropped the idea. The restored panel keeps an HTML video out of sight, copies each frame into an SVG image, and applies the original clip path to that image. The circular window and the click to play behavior are the same in all current browsers.</p>

			<h3>What changed</h3>
			<p>The headings and body text use system fonts in place of the original web fonts. The grass photograph behind the player is replaced by an SVG turbulence filter. The placeholder Latin is replaced by this description. Only one player has sound at a time: starting either one pauses the other.</p>
		</div>
		<script>
			var articleVideo = document.getElementById('articleVideo');
			function pauseArticleVideo() {
				articleVideo.pause();
			}
			articleVideo.addEventListener('play', function () {
				var side = document.getElementById('side');
				try {
					if (side.contentWindow && side.contentWindow.pauseVideo) {
						side.contentWindow.pauseVideo();
					}
				} catch (e) {}
			});
		</script>
	</body>
</html>
/video2/video.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 -->
<!-- whose frames are painted into an SVG image with the same clip circle.  -->
<!-- ======================================================================= -->
<svg id="root" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" onclick="togglePlay()" onkeydown="if (event.key === ' ' || event.key === 'Enter') { event.preventDefault(); togglePlay(); }" tabindex="0" role="button" aria-label="Play or pause the Big Buck Bunny trailer in the circle">
	<title>Big Buck Bunny in an SVG circle</title>
	<desc>A still from Big Buck Bunny fills the panel. A soft white circle holds the words watch the trailer. Clicking plays the trailer clipped to the circle, and clicking again pauses it.</desc>
	<defs>
		<style>
			#root { cursor: pointer; outline: none; }
			#root:focus-visible #ring { stroke: #ffd24a; stroke-width: 4; }
		</style>
		<clipPath id="cp1">
			<circle id="c" cx="50%" cy="70%" r="35%"/>
		</clipPath>
		<filter id="f1">
			<feGaussianBlur stdDeviation="4"/>
		</filter>
		<radialGradient id="rg1">
			<stop stop-color="white" stop-opacity="1"/>
			<stop stop-color="white" stop-opacity="0.6" offset="1"/>
		</radialGradient>
		<radialGradient id="rg2">
			<stop stop-color="black" stop-opacity="1" offset="0.9"/>
			<stop stop-color="black" stop-opacity="0.1" offset="1"/>
		</radialGradient>
	</defs>
	<image xlink:href="bigbuckbunny_rodents.jpg" width="100%" height="100%" preserveAspectRatio="xMidYMid slice"/>
	<circle id="ring" cx="50%" cy="70%" r="37%" fill="url(#rg1)" stroke="none" stroke-width="1"/>
	<circle cx="50%" cy="70%" r="35%" fill="white"/>
	<image id="trailerLogo" xlink:href="bigbuckbunny_watchtrailer.gif" x="20%" y="55%" width="60%" height="30%" clip-path="url(#cp1)"/>
	<!-- WebKit ignores clip paths on a video in a foreignObject, so the video
	     stays out of sight and its frames are painted into this clipped image. -->
	<image id="videoImage" clip-path="url(#cp1)" visibility="hidden" preserveAspectRatio="xMidYMid slice" 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>
	<g id="playIcon" opacity="0" pointer-events="none">
		<circle cx="50%" cy="70%" r="9%" fill="black" fill-opacity="0.55"/>
		<svg x="50%" y="70%" width="1" height="1" overflow="visible"><path d="M -14 -20 L 24 0 L -14 20 Z" fill="white"/></svg>
		<animate attributeName="opacity" to="1" dur="0.5s" begin="root.mouseover" fill="freeze"/>
		<animate attributeName="opacity" to="0" dur="0.5s" begin="root.mouseout" fill="freeze"/>
	</g>
	<text x="50%" y="98%" text-anchor="middle" font-family="Verdana, Arial, sans-serif" font-size="10" fill="white" stroke="black" stroke-width="2.5" paint-order="stroke">Big Buck Bunny, (c) 2008 Blender Foundation, CC BY 3.0</text>
	<script><![CDATA[
		var video = document.getElementById('video');
		var frame = document.getElementById('videoImage');
		var canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
		var ctx = canvas.getContext('2d');
		var painted = -1;
		canvas.width = 720;
		canvas.height = 400;

		// 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 () {
			decoding = false;
			// A frame that finishes decoding after the end must not reopen it.
			if (video.ended) {
				return;
			}
			frame.setAttribute('href', loader.src);
			frame.setAttribute('visibility', 'visible');
			logo.setAttribute('visibility', 'hidden');
		};
		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);
			}
		}
		var logo = document.getElementById('trailerLogo');

		// Size the foreignObject to the clip circle. A percentage radius in SVG
		// is relative to the normalized diagonal of the viewport.
		function placeVideo() {
			var box = document.documentElement.getBoundingClientRect();
			var r = 0.35 * Math.sqrt((box.width * box.width + box.height * box.height) / 2);
			frame.setAttribute('x', box.width * 0.5 - r);
			frame.setAttribute('y', box.height * 0.7 - r);
			frame.setAttribute('width', 2 * r);
			frame.setAttribute('height', 2 * r);
		}

		function togglePlay() {
			if (video.paused || video.ended) {
				var result = video.play();
				if (result && result.catch) {
					result.catch(function () {});
				}
			} else {
				video.pause();
			}
		}

		function pauseVideo() {
			video.pause();
		}

		video.addEventListener('play', function () {
			requestAnimationFrame(paint);
			// Only one of the two players on the page should sound at a time.
			try {
				if (window.parent !== window && window.parent.pauseArticleVideo) {
					window.parent.pauseArticleVideo();
				}
			} catch (e) {}
		});

		video.addEventListener('ended', function () {
			frame.setAttribute('visibility', 'hidden');
			logo.setAttribute('visibility', 'visible');
		});

		window.addEventListener('resize', placeVideo);
		placeVideo();
	]]></script>
</svg>
/video2/video-background.svg
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 400 400">
  <title>Filtered green texture</title>
  <desc>An original procedural meadow texture made with an SVG turbulence filter. It replaces the grass photo used in 2009.</desc>
  <filter id="meadow" x="0" y="0" width="100%" height="100%">
    <feTurbulence type="fractalNoise" baseFrequency="0.018 0.22" numOctaves="4" seed="12" stitchTiles="stitch"/>
    <feColorMatrix values="0 0 0 0 0.13  0.30 0.16 0 0 0.20  0 0 0 0 0.07  0 0 0 0 1"/>
  </filter>
  <rect width="400" height="400" fill="#2f4f1c"/>
  <rect width="400" height="400" filter="url(#meadow)"/>
</svg>
/video2/video-about.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Video: about this demo</title>
  <meta name="description" content="About the 2009 SVG Wow! video demo by Erik Dahlström: a circular SVG video window, an HTML video and a layout driven by CSS media queries.">
  <style>
    html, body { margin: 0; background: #fff; color: #1b1b1b; font: 16px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif; }
    main { max-width: 760px; margin: 0 auto; padding: 20px 16px 32px; }
    h1 { font-size: 1.6rem; margin: 0 0 4px; }
    img { display: block; width: 100%; height: auto; border: 1px solid #ccc; }
    .launch { display: inline-block; margin: 12px 0; padding: 9px 16px; background: #2f4f1c; color: #fff; border-radius: 5px; text-decoration: none; }
    .launch:focus-visible { outline: 3px solid #d98a00; outline-offset: 2px; }
    .meta { color: #555; margin: 0 0 12px; }
    small { color: #555; }
  </style>
</head>
<body>
  <main>
    <h1>Video</h1>
    <p class="meta">By Erik Dahlström, October 4, 2009. <a href="/gallery/video/">Gallery page</a></p>
    <a href="video.html"><img src="video_poster.jpg" width="1280" height="800" alt="The restored page: a video clipped to a circle on the left and an article with a framed video player on the right."></a>
    <a class="launch" href="video.html">Open the demo</a>
    <p>The demo shows these features working together:</p>
    <ul>
      <li>A video inside an SVG document, clipped to a circle</li>
      <li>An external SVG file embedded in an HTML page</li>
      <li>An HTML video player</li>
      <li>An SVG file used as a CSS background image</li>
      <li>CSS media queries that change the layout with the window size</li>
    </ul>
    <p>The SVG video element it first used has since left browsers, so the restored panel paints frames from a hidden HTML video into a clipped SVG image. System fonts replace the original web fonts.</p>
    <p><small>Film: Big Buck Bunny, (c) 2008 Blender Foundation, <a href="https://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>, <a href="https://peach.blender.org/">peach.blender.org</a>.</small></p>
  </main>
</body>
</html>