<div class="s-prose s-prose--article">
<p class="lede">Video is a bigger part of the web experience than ever before. With native browser support for <span class="caps">HTML5</span> video elements freeing us from the tyranny of plugins, and the availability of faster <strong>internet</strong> connections to the workplace, home and mobile networks, it’s now pretty straightforward to publish video in a way that can be consumed in all sorts of ways on all sorts of different web devices.</p>
<p>I recently worked on a project where the client had shot some dedicated video shorts to publish on their site. They also had some <strong>five-second</strong> motion graphics produced to top and tail the videos with context and branding. This pretty common requirement is a great idea on the web, where a user might land at your video having followed a link and be viewing a page without much context.</p>
<blockquote>
<p>[I]t appears probable that the progenitors of man, either the males or females or both sexes, before acquiring the power of expressing their mutual love in articulate language, endeavoured to charm each other with musical notes and rhythm.</p>
<footer>—Charles <span class="caps">DARWIN</span>, <cite>The Descent of Man, and Selection in Relation to Sex</cite>, 1871</footer>
</blockquote>
<p>Known as <em>bumpers</em>, these short introduction clips help brand a video and make it look a lot more professional.</p>
<figure>
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>
</figure>
<figure>
<div class="s-gallery">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
</div>
<figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>
</figure>
<figure>
<div class="s-gallery">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
</div>
<figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>
</figure>
<figure>
<div class="s-gallery">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
</div>
<figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>
</figure>
<h2>Heading 2</h2>
<p>The simplest way to add bumpers to a video would be to edit them on to the start and end of the video file itself. Cooking the bumpers into the video file is easy, but should you ever want to update them it can become a real headache. If the branding needs updating, for example, you’d need to re-edit and re-encode all your videos. Not a fun task.</p>
<p>Save the document by pressing <kbd><kbd>Ctrl</kbd> + <kbd>S</kbd></kbd></p>
<p>There are many, many options for recording your screen, including QuickTime Player on Mac OS X (<kbd><kbd><samp>File</samp></kbd> → <kbd><samp>New Screen Recording</samp></kbd></kbd>), <a href="http://www.gifgrabber.com/">GifGrabber</a>, or <a href="http://giffingtool.com">Giffing Tool</a> on Windows.</p>
<p>Just <samp>sample</samp> text.</p>
<h2>Heading 2…</h2>
<h3>…followed by a heading 3</h3>
<p>What if the bumpers could be added dynamically? That would enable you to use the same bumper for multiple videos (decreasing download time for users who might watch more than one) and to update the bumpers whenever you wanted. You could change them seasonally, update them for special promotions, run different advertising slots, perform multivariate testing, or even target different bumpers to different users.</p>
<p><a href="#">View an example</a></p>
<blockquote>
<p>The responsive projects I’ve worked on have had a lot of success combining design and development into one hybrid phase, bringing the two teams into one highly collaborative group.</p>
<ul>
<li>A list inside a blockquote</li>
<li>Is a very fine thing</li>
</ul>
<p>A lot of success combining design and development into one hybrid phase, bringing the two teams into one highly collaborative group.</p>
<ol>
<li>How about an ordered list</li>
<li>Inside a blockquote, too?</li>
</ol>
</blockquote>
<h3>Heading 3</h3>
<p>The trade-off, of course, is that if you dynamically add your bumpers, there’s a chance that a user in a given circumstance might not see the bumper. For example, if the main video feature was uploaded to YouTube, you’d have no way to control the playback. As always, you need to weigh up the pros and cons and make your choice.</p>
<h4>Heading 4</h4>
<p>If you wanted to dynamically add bumpers to your <abbr title="Hyper Text Markup Language 5">HTML5</abbr> video, how would you go about it? That was the question I found myself needing to answer for this particular client project.</p>
<h5>Heading 5</h5>
<p>My initial thought was to treat it just like an image slideshow. If I were building a slideshow that moved between images, I’d use <abbr title="Cascading Style Sheets">CSS</abbr> absolute positioning with <code>z-index</code> to stack the images up on top of each other in a pile, with the first image on top. To transition to the second image, I’d use JavaScript to fade the top image out, revealing the second image beneath it.</p>
<figure>
<div class="s-embed s-embed--widescreen">
<iframe src="https://www.youtube.com/embed/QILiHiTD3uc" allowfullscreen></iframe>
</div>
<figcaption>Example of responsive video embed.</figcaption>
</figure>
<p>Now that video is just a native object in the <span class="caps">DOM</span>, just like an image, why not do the same? Stack the videos up with the opening bumper on top, listen for the video’s <code>onended</code> event, and fade it out to reveal the main feature behind. Good idea, right?</p>
<table>
<thead>
<tr>
<th>The Very Best <code>Eggnog</code></th>
<th>Serves 12</th>
<th>Serves 24</th>
</tr>
</thead>
<tbody>
<tr>
<td>Milk</td>
<td>1 quart</td>
<td>2 quart</td>
</tr>
<tr>
<td>Cinnamon Sticks</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>Vanilla Bean, Split</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>Cloves</td>
<td>5</td>
<td>10</td>
</tr>
<tr>
<td>Mace</td>
<td>10</td>
<td>20</td>
</tr>
<tr>
<td>Egg Yolks</td>
<td>12</td>
<td>24</td>
</tr>
<tr>
<td>Cups Sugar</td>
<td>1 ½ cups</td>
<td>3 cups</td>
</tr>
<tr>
<td>Dark Rum</td>
<td>1 ½ cups</td>
<td>3 cups</td>
</tr>
<tr>
<td>Brandy</td>
<td>1 ½ cups</td>
<td>3 cups</td>
</tr>
<tr>
<td>Vanilla</td>
<td>1 tbsp</td>
<td>2 tbsp</td>
</tr>
<tr>
<td>
<ul>
<li>Light Cream</li>
<li>Double Cream</li>
</ul>
</td>
<td>1 quart</td>
<td>2 quart</td>
</tr>
</tbody>
</table>
<p>Remember that this is the web. It’s never going to be that easy. The problem here is that many non-desktop devices use native, dedicated video players. Think about watching a video on a mobile phone – when you play the video, the phone often goes full-screen in its native player, leaving the web page behind. There’s no opportunity to fade or switch <code>z-index</code>, as the video isn’t being viewed in the page. Your page is left powerless. Powerless!</p>
<img src="https://media.24ways.org/2012/mclellan/media-player.jpg" alt="iOS full-screen media player" />
<p>So what can we do? What can we control?</p>
<hr />
<p>Those of us with particularly long memories might recall a time before <abbr title="Cascading Style Sheets">CSS</abbr>, when we’d have to use JavaScript to perform image rollovers. As <abbr title="Cascading Style Sheets">CSS</abbr> background images weren’t a practical reality, we would use lots of <code><img></code> elements, and perform a rollover by modifying the <code>src</code> attribute of the image.</p>
<figure class="pull-right">
<img src="https://media.24ways.org/2013/baxter/indexcards.jpg" alt="Index cards">
<figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>
</figure>
<p>Turns out, this old trick of modifying the source can help us out with video, too. In most cases, modifying the <code>src</code> attribute of a <code><video></code> element, or perhaps more likely the <code>src</code> attribute of a <code>source</code> element, will swap from one video to another.</p>
<h2>Swappin’ it</h2>
<p>Let’s take a deliberately simple example of a super-basic video tag:</p>
<pre><code class="language-html"><video src="mycat.webm" controls>no fallback coz i is lame, innit.</video>
</code></pre>
<p>We could very simply write a script to find all video tags and give them a new <code>src</code> to show our bumper.</p>
<pre><code class="language-html"><script>
var videos, i, l;
videos = document.getElementsByTagName('video');
for(i=0, l=videos.length; i<l; i++) {
videos[i].setAttribute('src', 'bumper-in.webm');
}
</script>
</code></pre>
<p><a href="https://media.24ways.org/2012/mclellan/examples/1.html">View the example</a> in a browser with WebM support. You’ll see that the video is swapped out for the opening bumper. Great!</p>
<h2>Beefing it up</h2>
<p>Of course, we can’t just publish video in one format. In practical use, you need a <code><video></code> element with multiple <code><source></code> elements containing your different source formats.</p>
<pre><code class="language-html"><video controls>
<source src="mycat.mp4" type="video/mp4" />
<source src="mycat.webm" type="video/webm" />
<source src="mycat.ogv" type="video/ogg" />
</video>
</code></pre>
<p>This time, our script needs to loop through the sources, not the videos. We’ll use a regular expression replacement to swap out the file name while maintaining the correct file extension.</p>
<pre><code class="language-html"><script>
var sources, i, l, orig;
sources = document.getElementsByTagName('source');
for(i=0, l=sources.length; i<l; i++) {
orig = sources[i].getAttribute('src');
sources[i].setAttribute('src', orig.replace(/(w+).(w+)/, 'bumper-in.$2'));
// Reload the video
sources[i].parentNode.load();
}
</script>
</code></pre>
<p>The difference this time is that when changing the <code>src</code> of a <code><source></code> we need to call the <code>.load()</code> method on the video to get it to acknowledge the change.</p>
<p><a href="https://media.24ways.org/2012/mclellan/examples/2.html">See the code in action</a>, this time in a wider range of browsers.</p>
<h2>But, my video!</h2>
<ul>
<li>Store the original <code>src</code> in a <code>data-</code> attribute so we can access it later</li>
<li>Add an event listener so we can detect the end of the bumper playing, and load the original video back in</li>
</ul>
<p>I guess we should get the original video playing again. Keeping the same markup, we need to modify the script to do two things:</p>
<ul>
<li>Store the original <code>src</code> in a <code>data-</code> attribute so we can access it later</li>
<li>Add an event listener so we can detect the end of the bumper playing, and load the original video back in</li>
</ul>
<p>As we need to loop through the videos this time to add the event listener, I’ve moved the <code>.load()</code> call into that loop. It’s a bit more efficient to call it only once after modifying all the video’s sources.</p>
<ul>
<li><strong>Raspberry Pi</strong> any of the following models :
<ul>
<li>Zero (will need straight male header pins soldered2 and Micro USB OTG adaptor),
<ul>
<li>A+, B+, 2 or 3</li>
</ul>
</li>
</ul>
</li>
<li><strong>Micro SD card</strong> at least 4Gb Class 10 speed3</li>
<li><strong>Micro USB power supply</strong> at least 2A</li>
<li><strong>USB Wifi dongle</strong> (unless you have a Pi 3 - that has wifi built in).</li>
</ul>
<pre><code class="language-html"><script>
var videos, sources, i, l, orig;
sources = document.getElementsByTagName('source');
for(i=0, l=sources.length; i<l; i++) {
orig = sources[i].getAttribute('src');
sources[i].setAttribute('data-orig', orig);
sources[i].setAttribute('src', orig.replace(/(w+).(w+)/, 'bumper-in.$2'));
}
videos = document.getElementsByTagName('video');
for(i=0, l=videos.length; i<l; i++) {
videos[i].load();
videos[i].addEventListener('ended', function(){
sources = this.getElementsByTagName('source');
for(i=0, l=sources.length; i<l; i++) {
orig = sources[i].getAttribute('data-orig');
if (orig) {
sources[i].setAttribute('src', orig);
}
sources[i].setAttribute('data-orig','');
}
this.load();
this.play();
});
}
</script>
</code></pre>
<p>Again, <a href="https://media.24ways.org/2012/mclellan/examples/3.html">view the example</a> to see the bumper play, followed by our spectacular main feature. (That’s my cat, Widget. His interests include sleeping and internet marketing.)</p>
<h2>Tidying things up</h2>
<p>The final thing to do is add our closing bumper after the main video has played. This involves the following changes:</p>
<ol>
<li>We need to keep track of whether the <code>src</code> has been changed, so we only play the video if it’s changed. I’ve added the <code>modified</code> variable to track this, and it stops us getting into a situation where the video just loops forever.</li>
<li>Add an <code>else</code> to the event listener, for when the <code>orig</code> is false (so the main feature has been playing) to load in the end bumper. We also check that we’re not already playing the end bumper. Because looping.</li>
</ol>
<pre><code class="language-html"><script>
var videos, sources, i, l, orig, current, modified;
sources = document.getElementsByTagName('source');
for(i=0, l=sources.length; i<l; i++) {
orig = sources[i].getAttribute('src');
sources[i].setAttribute('data-orig', orig);
sources[i].setAttribute('src', orig.replace(/(w+).(w+)/, 'bumper-in.$2'));
}
videos = document.getElementsByTagName('video');
for(i=0, l=videos.length; i<l; i++) {
videos[i].load();
modified = false;
videos[i].addEventListener('ended', function(){
sources = this.getElementsByTagName('source');
for(i=0, l=sources.length; i<l; i++) {
orig = sources[i].getAttribute('data-orig');
if (orig) {
sources[i].setAttribute('src', orig);
modified = true;
} else {
current = sources[i].getAttribute('src');
if (current.indexOf('bumper-out')==-1) {
sources[i].setAttribute('src', current.replace(/([w]+).(w+)/, 'bumper-out.$2'));
modified = true;
} else {
this.pause();
modified = false;
}
}
sources[i].setAttribute('data-orig','');
}
if (modified) {
this.load();
this.play();
}
});
}
</script>
</code></pre>
<p>Yo ho ho, that’s a lot of JavaScript. <a href="https://media.24ways.org/2012/mclellan/examples/4.html">See it in action</a> – you should get a bumper, the cat video, and an end bumper.</p>
<p>Of course, this code works fine for demonstrating the principle, but it’s very procedural. Nothing wrong with that, but to do something similar in production, you’d probably want to make the code more modular to ease maintainability. Besides, you may want to use a framework, rather than basic JavaScript.</p>
<h2>The end credits</h2>
<p>One really important principle here is that of <strong>progressive enhancement</strong>. If the browser doesn’t support JavaScript, the user won’t see your bumper, but they will get the main video. If the browser supports JavaScript but doesn’t allow you to modify the <code>src</code> (as was the case with older versions of iOS), the user won’t see your bumper, but they will get the main video.</p>
<p>At Twitter, <em>every commit</em> gets a code review. We do a lot of reviewing, so small efficiency and effectiveness improvements make a big difference. Over time we’ve learnt a few things:</p>
<ul>
<li>Don’t review for <em>more than hour</em><sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup></li>
<li>Keep reviews <em>smaller than ~400 lines</em><sup class="footnote-ref"><a href="#fn2" id="fnref2">[2]</a></sup></li>
</ul>
<p>If a search engine or social media bot grabs your page and looks for content, they won’t see your bumper, but they will get the main video – which is absolutely what you want.</p>
<p>This means that if the bumper is absolutely crucial, you may still need to cook it into the video. However, for many applications, running it dynamically can work quite well.</p>
<p>As always, it comes down to three things:</p>
<ol>
<li>Measure your audience: know how people access your site</li>
<li>Test the solution: make sure it works for your audience</li>
<li>Plan for failure: it’s the web and that’s how things work 'round these parts</li>
</ol>
<p>But most of all <strong>play around with it, have fun and build something awesome</strong>.</p>
<hr class="footnotes-sep" />
<section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item">
<p>Dunsmore et al. 2000. Object-Oriented Inspection in the Face of Delocalisation. Beverly, MA: SmartBear Software. <a href="#fnref1" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn2" class="footnote-item">
<p>Cohen, Jason. 2006. Best Kept Secrets of Peer Code Review. <em>Proceedings of the 22nd ICSE 2000</em>: 467-476. <a href="#fnref2" class="footnote-backref">↩︎</a></p>
</li>
</ol>
</section>
</div>
<div class="s-prose{% for mod in mods %} s-prose--{{ mod }}{% endfor %}">
{{ content | markdown | safe }}
</div>
{
"site": {
"title": "24 ways",
"handle": "24ways",
"description": "24 ways is the advent calendar for web geeks. Each day throughout December we publish a daily dose of web design and development goodness to bring you all a little Christmas cheer.",
"url": "https://24ways.org",
"feed": "https://feeds.feedburner.com/24ways",
"theme_color": "#f04"
},
"theme": {
"year": [
348,
344,
340,
336,
332,
328,
324,
320,
316,
312,
308,
304,
300,
296,
292,
288
],
"day": [
360,
353,
346,
339,
332,
325,
318,
311,
304,
297,
290,
283,
276,
269,
262,
255,
248,
241,
234,
227,
220,
213,
206,
199
]
},
"content": "<p class=\"lede\">Video is a bigger part of the web experience than ever before. With native browser support for <span class=\"caps\">HTML5</span> video elements freeing us from the tyranny of plugins, and the availability of faster <strong>internet</strong> connections to the workplace, home and mobile networks, it’s now pretty straightforward to publish video in a way that can be consumed in all sorts of ways on all sorts of different web devices.</p>\n\nI recently worked on a project where the client had shot some dedicated video shorts to publish on their site. They also had some <strong>five-second</strong> motion graphics produced to top and tail the videos with context and branding. This pretty common requirement is a great idea on the web, where a user might land at your video having followed a link and be viewing a page without much context.\n\n<blockquote>\n<p>[I]t appears probable that the progenitors of man, either the males or females or both sexes, before acquiring the power of expressing their mutual love in articulate language, endeavoured to charm each other with musical notes and rhythm.</p>\n<footer>—Charles <span class=\"caps\">DARWIN</span>, <cite>The Descent of Man, and Selection in Relation to Sex</cite>, 1871</footer>\n</blockquote>\n\nKnown as _bumpers_, these short introduction clips help brand a video and make it look a lot more professional.\n\n<figure>\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>\n</figure>\n\n<figure>\n <div class=\"s-gallery\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n </div>\n <figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>\n</figure>\n\n<figure>\n <div class=\"s-gallery\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n </div>\n <figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>\n</figure>\n\n<figure>\n <div class=\"s-gallery\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n </div>\n <figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>\n</figure>\n\n## Heading 2\nThe simplest way to add bumpers to a video would be to edit them on to the start and end of the video file itself. Cooking the bumpers into the video file is easy, but should you ever want to update them it can become a real headache. If the branding needs updating, for example, you'd need to re-edit and re-encode all your videos. Not a fun task.\n\nSave the document by pressing <kbd><kbd>Ctrl</kbd> + <kbd>S</kbd></kbd>\n\nThere are many, many options for recording your screen, including QuickTime Player on Mac OS X (<kbd><kbd><samp>File</samp></kbd> → <kbd><samp>New Screen Recording</samp></kbd></kbd>), <a href=\"http://www.gifgrabber.com/\">GifGrabber</a>, or <a href=\"http://giffingtool.com\">Giffing Tool</a> on Windows.\n\nJust <samp>sample</samp> text.\n\n## Heading 2…\n### …followed by a heading 3\nWhat if the bumpers could be added dynamically? That would enable you to use the same bumper for multiple videos (decreasing download time for users who might watch more than one) and to update the bumpers whenever you wanted. You could change them seasonally, update them for special promotions, run different advertising slots, perform multivariate testing, or even target different bumpers to different users.\n\n[View an example](#)\n\n> The responsive projects I've worked on have had a lot of success combining design and development into one hybrid phase, bringing the two teams into one highly collaborative group.\n>\n> * A list inside a blockquote\n> * Is a very fine thing\n>\n> A lot of success combining design and development into one hybrid phase, bringing the two teams into one highly collaborative group.\n>\n> 1. How about an ordered list\n> 2. Inside a blockquote, too?\n\n### Heading 3\nThe trade-off, of course, is that if you dynamically add your bumpers, there's a chance that a user in a given circumstance might not see the bumper. For example, if the main video feature was uploaded to YouTube, you'd have no way to control the playback. As always, you need to weigh up the pros and cons and make your choice.\n\n#### Heading 4\nIf you wanted to dynamically add bumpers to your HTML5 video, how would you go about it? That was the question I found myself needing to answer for this particular client project.\n\n##### Heading 5\nMy initial thought was to treat it just like an image slideshow. If I were building a slideshow that moved between images, I'd use CSS absolute positioning with `z-index` to stack the images up on top of each other in a pile, with the first image on top. To transition to the second image, I'd use JavaScript to fade the top image out, revealing the second image beneath it.\n\n<figure>\n <div class=\"s-embed s-embed--widescreen\">\n <iframe src=\"https://www.youtube.com/embed/QILiHiTD3uc\" allowfullscreen></iframe>\n </div>\n <figcaption>Example of responsive video embed.</figcaption>\n</figure>\n\nNow that video is just a native object in the <span class=\"caps\">DOM</span>, just like an image, why not do the same? Stack the videos up with the opening bumper on top, listen for the video's `onended` event, and fade it out to reveal the main feature behind. Good idea, right?\n\n| The Very Best `Eggnog` | Serves 12 | Serves 24 |\n|--------------------------------|-----------------|-----------|\n| Milk | 1 quart | 2 quart |\n| Cinnamon Sticks | 1 | 2 |\n| Vanilla Bean, Split | 1 | 2 |\n| Cloves | 5 | 10 |\n| Mace | 10 | 20 |\n| Egg Yolks | 12 | 24 |\n| Cups Sugar | 1 ½ cups | 3 cups |\n| Dark Rum | 1 ½ cups | 3 cups |\n| Brandy | 1 ½ cups | 3 cups |\n| Vanilla | 1 tbsp | 2 tbsp |\n| <ul><li>Light Cream</li><li>Double Cream</li></ul> | 1 quart | 2 quart |\n\nRemember that this is the web. It's never going to be that easy. The problem here is that many non-desktop devices use native, dedicated video players. Think about watching a video on a mobile phone -- when you play the video, the phone often goes full-screen in its native player, leaving the web page behind. There's no opportunity to fade or switch `z-index`, as the video isn't being viewed in the page. Your page is left powerless. Powerless!\n\n<img src=\"https://media.24ways.org/2012/mclellan/media-player.jpg\" alt=\"iOS full-screen media player\" />\n\nSo what can we do? What can we control?\n\n***\n\nThose of us with particularly long memories might recall a time before CSS, when we'd have to use JavaScript to perform image rollovers. As CSS background images weren't a practical reality, we would use lots of `<img>` elements, and perform a rollover by modifying the `src` attribute of the image.\n\n<figure class=\"pull-right\">\n <img src=\"https://media.24ways.org/2013/baxter/indexcards.jpg\" alt=\"Index cards\">\n <figcaption>Index cards represent each feature the rental property software would launch with.</figcaption>\n</figure>\n\nTurns out, this old trick of modifying the source can help us out with video, too. In most cases, modifying the `src` attribute of a `<video>` element, or perhaps more likely the `src` attribute of a `source` element, will swap from one video to another.\n\n## Swappin' it\nLet's take a deliberately simple example of a super-basic video tag:\n\n```html\n<video src=\"mycat.webm\" controls>no fallback coz i is lame, innit.</video>\n```\n\nWe could very simply write a script to find all video tags and give them a new `src` to show our bumper.\n\n```html\n<script>\n var videos, i, l;\n videos = document.getElementsByTagName('video');\n\n for(i=0, l=videos.length; i<l; i++) {\n videos[i].setAttribute('src', 'bumper-in.webm');\n }\n</script>\n```\n\n[View the example](https://media.24ways.org/2012/mclellan/examples/1.html) in a browser with WebM support. You'll see that the video is swapped out for the opening bumper. Great!\n\n## Beefing it up\nOf course, we can't just publish video in one format. In practical use, you need a `<video>` element with multiple `<source>` elements containing your different source formats.\n\n```html\n<video controls>\n <source src=\"mycat.mp4\" type=\"video/mp4\" />\n <source src=\"mycat.webm\" type=\"video/webm\" />\n <source src=\"mycat.ogv\" type=\"video/ogg\" />\n</video>\n```\n\nThis time, our script needs to loop through the sources, not the videos. We'll use a regular expression replacement to swap out the file name while maintaining the correct file extension.\n\n```html\n<script>\n var sources, i, l, orig;\n sources = document.getElementsByTagName('source');\n\n for(i=0, l=sources.length; i<l; i++) {\n orig = sources[i].getAttribute('src');\n sources[i].setAttribute('src', orig.replace(/(w+).(w+)/, 'bumper-in.$2'));\n\n // Reload the video\n sources[i].parentNode.load();\n }\n</script>\n```\n\nThe difference this time is that when changing the `src` of a `<source>` we need to call the `.load()` method on the video to get it to acknowledge the change.\n\n[See the code in action](https://media.24ways.org/2012/mclellan/examples/2.html), this time in a wider range of browsers.\n\n## But, my video!\n* Store the original `src` in a `data-` attribute so we can access it later\n* Add an event listener so we can detect the end of the bumper playing, and load the original video back in\n\nI guess we should get the original video playing again. Keeping the same markup, we need to modify the script to do two things:\n\n* Store the original `src` in a `data-` attribute so we can access it later\n* Add an event listener so we can detect the end of the bumper playing, and load the original video back in\n\nAs we need to loop through the videos this time to add the event listener, I've moved the `.load()` call into that loop. It's a bit more efficient to call it only once after modifying all the video's sources.\n\n* **Raspberry Pi** any of the following models :\n * Zero (will need straight male header pins soldered2 and Micro USB OTG adaptor),\n * A+, B+, 2 or 3\n* **Micro SD card** at least 4Gb Class 10 speed3\n* **Micro USB power supply** at least 2A\n* **USB Wifi dongle** (unless you have a Pi 3 - that has wifi built in).\n\n```html\n<script>\n var videos, sources, i, l, orig;\n sources = document.getElementsByTagName('source');\n\n for(i=0, l=sources.length; i<l; i++) {\n orig = sources[i].getAttribute('src');\n sources[i].setAttribute('data-orig', orig);\n sources[i].setAttribute('src', orig.replace(/(w+).(w+)/, 'bumper-in.$2'));\n }\n\n videos = document.getElementsByTagName('video');\n\n for(i=0, l=videos.length; i<l; i++) {\n videos[i].load();\n videos[i].addEventListener('ended', function(){\n sources = this.getElementsByTagName('source');\n for(i=0, l=sources.length; i<l; i++) {\n orig = sources[i].getAttribute('data-orig');\n if (orig) {\n sources[i].setAttribute('src', orig);\n }\n sources[i].setAttribute('data-orig','');\n }\n this.load();\n this.play();\n });\n }\n</script>\n```\n\nAgain, [view the example](https://media.24ways.org/2012/mclellan/examples/3.html) to see the bumper play, followed by our spectacular main feature. (That's my cat, Widget. His interests include sleeping and internet marketing.)\n\n## Tidying things up\nThe final thing to do is add our closing bumper after the main video has played. This involves the following changes:\n\n1. We need to keep track of whether the `src` has been changed, so we only play the video if it's changed. I've added the `modified` variable to track this, and it stops us getting into a situation where the video just loops forever.\n2. Add an `else` to the event listener, for when the `orig` is false (so the main feature has been playing) to load in the end bumper. We also check that we're not already playing the end bumper. Because looping.\n\n```html\n<script>\n var videos, sources, i, l, orig, current, modified;\n sources = document.getElementsByTagName('source');\n\n for(i=0, l=sources.length; i<l; i++) {\n orig = sources[i].getAttribute('src');\n sources[i].setAttribute('data-orig', orig);\n sources[i].setAttribute('src', orig.replace(/(w+).(w+)/, 'bumper-in.$2'));\n }\n\n videos = document.getElementsByTagName('video');\n\n for(i=0, l=videos.length; i<l; i++) {\n videos[i].load();\n modified = false;\n\n videos[i].addEventListener('ended', function(){\n sources = this.getElementsByTagName('source');\n\n for(i=0, l=sources.length; i<l; i++) {\n orig = sources[i].getAttribute('data-orig');\n if (orig) {\n sources[i].setAttribute('src', orig);\n modified = true;\n } else {\n current = sources[i].getAttribute('src');\n if (current.indexOf('bumper-out')==-1) {\n sources[i].setAttribute('src', current.replace(/([w]+).(w+)/, 'bumper-out.$2'));\n modified = true;\n } else {\n this.pause();\n modified = false;\n }\n }\n sources[i].setAttribute('data-orig','');\n }\n\n if (modified) {\n this.load();\n this.play();\n }\n });\n }\n</script>\n```\n\nYo ho ho, that's a lot of JavaScript. [See it in action](https://media.24ways.org/2012/mclellan/examples/4.html) -- you should get a bumper, the cat video, and an end bumper.\n\nOf course, this code works fine for demonstrating the principle, but it's very procedural. Nothing wrong with that, but to do something similar in production, you'd probably want to make the code more modular to ease maintainability. Besides, you may want to use a framework, rather than basic JavaScript.\n\n## The end credits\nOne really important principle here is that of **progressive enhancement**. If the browser doesn't support JavaScript, the user won't see your bumper, but they will get the main video. If the browser supports JavaScript but doesn't allow you to modify the `src` (as was the case with older versions of iOS), the user won't see your bumper, but they will get the main video.\n\nAt Twitter, *every commit* gets a code review. We do a lot of reviewing, so small efficiency and effectiveness improvements make a big difference. Over time we've learnt a few things:\n\n* Don't review for *more than hour*[^1]\n* Keep reviews *smaller than ~400 lines*[^2]\n\n[^1]: Dunsmore et al. 2000. Object-Oriented Inspection in the Face of Delocalisation. Beverly, MA: SmartBear Software.\n\n[^2]: Cohen, Jason. 2006. Best Kept Secrets of Peer Code Review. *Proceedings of the 22nd ICSE 2000*: 467-476.\n\nIf a search engine or social media bot grabs your page and looks for content, they won't see your bumper, but they will get the main video -- which is absolutely what you want.\n\nThis means that if the bumper is absolutely crucial, you may still need to cook it into the video. However, for many applications, running it dynamically can work quite well.\n\nAs always, it comes down to three things:\n\n1. Measure your audience: know how people access your site\n2. Test the solution: make sure it works for your audience\n3. Plan for failure: it's the web and that's how things work 'round these parts\n\nBut most of all **play around with it, have fun and build something awesome**.\n\n*[CSS]: Cascading Style Sheets\n*[HTML5]: Hyper Text Markup Language 5\n",
"mods": [
"article"
]
}
.s-prose {
@apply --typeset-prose;
max-width: 75ch;
> * {
margin-bottom: 0.75em;
}
/* Text */
strong,
dt {
font-family: map(fonts, family-sans);
}
pre,
code,
samp {
font-variant: lining-nums tabular-nums;
hanging-punctuation: none;
hyphens: none;
}
pre,
code {
margin: -0.125em 0.125em;
padding: 0.0625em 0.125em;
background-color: color(white a(50%));
}
/* Grouping content */
pre {
margin: 1em -0.5em;
padding: 0.25em 0.5em;
code {
margin: 0;
background-color: transparent;
padding: 0;
}
}
> ol,
> ul {
list-style-position: inside;
@media (--from-large-screen) {
list-style-position: outside;
}
}
li {
margin-bottom: 0.25em;
}
> ul > li {
list-style-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='0.75em' height='0.75em' viewBox='0 0 2 2'%3E%3Cpolygon fill='%233F3F46' fill-opacity='.75' points='0 1 1 0 2 1 1 2'/%3E%3C/svg%3E ");
}
ul li {
list-style-type: circle;
}
ol li {
list-style-type: decimal;
}
li li,
dd {
margin-left: 2em;
}
dt {
font-weight: 700;
}
.caps {
font-variant: lining-nums small-caps;
}
.caption {
@apply --typeset-caption;
opacity: 0.9;
}
.lede {
@apply --typeset-lede;
position: relative;
top: calc(map(spaces, xsmall) * -1);
}
}
.s-prose--article {
@apply --typescale-prose;
> *:first-child {
margin-top: 0;
}
pre,
figure,
table {
margin-top: map(spaces, large);
margin-bottom: map(spaces, large);
}
/* Sections */
h2,
h3,
h4,
h5 {
@apply --typeset-heading;
margin-top: map(spaces, large);
margin-bottom: map(spaces, small);
}
h2 {
margin-top: map(spaces, xlarge);
font-size: 1.5em;
font-weight: 700;
}
h2,
h3 {
color: $color-day--dark;
color: var(--color-day--dark, $color-day--dark);
}
h3 {
font-size: 1.25em;
opacity: 0.9;
}
h2 + h3 {
margin-top: 0;
}
h4 {
font-size: 1.125em;
}
h5 {
font-size: 0.875em;
letter-spacing: 0.05em;
text-transform: uppercase;
opacity: 0.9;
}
hr {
text-align: center;
margin: map(spaces, large) 0 map(spaces, xlarge);
&::after {
content: '\25C6 \25C6 \25C6';
height: 0;
font-family: sans-serif;
letter-spacing: map(spaces, medium);
color: color($color-text a(25%));
}
}
/* Grouping content */
blockquote {
border-left: 0.25rem solid $prose-color--rule;
padding-left: map(spaces, medium);
margin-bottom: map(spaces, large);
> p,
> ul,
> ol {
margin-bottom: 0.75em;
font-size: 1.125em;
line-height: 1.25;
list-style-position: inside;
}
> footer,
> cite { /* ! Legacy */
@apply --typeset-caption;
}
}
figure,
p.image { /* ! Legacy */
margin-left: calc(map(spaces, large) * -1);
margin-right: calc(map(spaces, large) * -1);
max-width: none;
@media (--from-large-screen) {
margin-left: 0;
margin-right: 0;
}
@media (--from-max-screen) {
margin-right: -20%;
}
}
figcaption,
p.image span.caption { /* ! Legacy */
@apply --typeset-caption;
display: block;
border-bottom: 1px solid $prose-color--rule;
margin-right: map(spaces, large);
margin-left: map(spaces, large);
padding: map(spaces, medium) 0;
@media (--from-large-screen) {
margin: 0;
}
}
/* Embedded content */
img,
video {
display: block;
max-width: 100%;
}
/* Key press within user input sequence. */
kbd > kbd {
font-size: 90%;
background-color: white;
margin: 0 0.125rem;
border: 1px solid $prose-color--shadow;
border-radius: map(borders, radius-default);
padding: map(spaces, xsmall) map(spaces, small);
box-shadow: 0 1px 0 $prose-color--shadow;
}
/* Menu selection within user input sequence. */
kbd > samp {
font-family: inherit;
font-size: 1em;
background-color: white;
margin: -0.375rem -0.625rem; /* -6px -10px */
border-radius: 0;
padding: 0.375rem 0.625rem; /* 6px 10px */
box-shadow: none;
}
/* Links */
a {
text-decoration: underline;
text-decoration-color: var(--color-day--dark-alpha, $color-day--dark-alpha);
&:hover {
text-decoration-color: var(--color-day, $color-day);
}
@media print {
border-bottom: 1px dotted #999;
&::after {
font-size: 0.75em;
content: ' [' attr(href) ']';
}
}
}
/* Tables */
table {
@apply --typeset-ui;
ol,
ul {
margin-left: map(spaces, large);
}
}
caption {
@apply --typeset-caption;
color: map(colors, neutral, base);
caption-side: bottom;
text-align: left;
margin: map(spaces, medium) 0;
}
td,
th {
border-bottom: 1px solid $prose-color--rule;
padding: map(spaces, small) map(spaces, large) map(spaces, small) map(spaces, small);
hyphens: none;
}
th {
line-height: 1.25;
background-color: $prose-color--background;
code {
background: none;
}
}
.pull-right {
@media (--from-large-screen) {
width: 40%;
float: right;
margin-top: 0;
margin-left: map(spaces, large);
}
}
.footnotes {
@apply --typeset-caption;
li {
margin-bottom: map(spaces, small);
}
a:last-of-type {
/* Chrome doesn’t seem to use fallback glyph in font stack */
font-family: sans-serif;
}
}
}
There are no notes for this item.