<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nate  Mowbray</title>
    <description>The latest articles on Forem by Nate  Mowbray (@diffidentdude).</description>
    <link>https://forem.com/diffidentdude</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1070445%2F51092421-1345-491a-9946-cb5d14942959.jpeg</url>
      <title>Forem: Nate  Mowbray</title>
      <link>https://forem.com/diffidentdude</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/diffidentdude"/>
    <language>en</language>
    <item>
      <title>Analysis: The YouTube video glow</title>
      <dc:creator>Nate  Mowbray</dc:creator>
      <pubDate>Tue, 25 Apr 2023 02:11:57 +0000</pubDate>
      <link>https://forem.com/cultureamp/the-youtube-glowy-effect-295i</link>
      <guid>https://forem.com/cultureamp/the-youtube-glowy-effect-295i</guid>
      <description>&lt;p&gt;Hi! , I'm Nate, a Software Engineer at Culture Amp. This content isn't directly related to the work that we do at Culture Amp, it's more something on the side that I found interesting from a frontend perspective and that I hope you do too.&lt;/p&gt;

&lt;p&gt;Anyway, roll the intro!&lt;/p&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;I noticed the other day while watching a YouTube video, around the edge of the video there was a subtle glowing effect that matched the content of the video.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d-GXw5OB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2lft5qigaf2fv06uipf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d-GXw5OB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2lft5qigaf2fv06uipf.png" alt="Image A YouTube video showing it's glowing border. The glowing border is indicated by the words &amp;quot;Dat Glow&amp;quot;" width="800" height="509"&gt;&lt;/a&gt;&lt;br&gt;
It's probably inspired by the recentish trend to put LED light strips on the back of your TV. The fancy version of this syncs the light colours to what's playing on your TV.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IR00QUBo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tom3yidgkplq58pyqhvp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IR00QUBo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tom3yidgkplq58pyqhvp.png" alt="Image A TV with Multicoloured wall lighting behind it" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In it's simplest form, it's supposed to reduce eye strain and help improve the perceived contrast of the screen. In the more complex form it could improve the immersion of the video by allowing it to escape it's box. Or it just looks pretty because colours.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Investigate&lt;/li&gt;
&lt;li&gt;How it's done&lt;/li&gt;
&lt;li&gt;Example&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Investigate &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Back on YouTube, I was wondering how they achieved &lt;em&gt;their&lt;/em&gt; glowy effect. &lt;strong&gt;Let’s dig in and see&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Start by moving the video out of the way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jw5VuTX7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9enm6ai3a6wxz1zn34g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jw5VuTX7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9enm6ai3a6wxz1zn34g.gif" alt="Image When you move the YouTube video out of the way, you can see a full frame blurred image underneath" width="610" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we do this we can see that under the video is what &lt;em&gt;appears&lt;/em&gt; to be a &lt;strong&gt;blurred&lt;/strong&gt; version of the current place in the video.&lt;/p&gt;

&lt;p&gt;It sits under the video, with the blurred edges peeking out giving this pretty soft glow effect.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When you blur an image, depending on the method, it becomes larger due to the mixing of the pixels at the edges with the surrounding canvas and the overall softening effect.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/diffidentDude/embed/JjmWvdw?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;

&lt;p&gt;An inspection of the DOM shows some &lt;strong&gt;canvas&lt;/strong&gt; elements being actually responsible for displaying the blurred content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"cinematics-container"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"style-scope ytd-watch-flexy"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"cinematics"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"style-scope ytd-watch-flexy"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"position: absolute; inset: 0px; pointer-events: none; transform: scale(1.5, 2);"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;canvas&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"110"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"75"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"position: absolute; width: 100%; height: 100%;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;canvas&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"110"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"75"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"position: absolute; width: 100%; height: 100%; opacity: 1;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we unroll the elements, we can see the arrangement. The previous frame, the next one and the actual video.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GQy4Z8CH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2v01zri4j3fbliyn26f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GQy4Z8CH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2v01zri4j3fbliyn26f.png" alt="Image The YouTube video from earlier, with the two canvas elements unrolled so that they can be viewed separately" width="800" height="1466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A couple of observations so far.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The blurred “frame” doesn’t update at the same rate as the video, every now and then it’s a little out of sync. This isn’t a problem for the effect as it’s in the background anyway.&lt;/li&gt;
&lt;li&gt;The image alternates between the two canvas elements, swapping back and forth with a simple crossfade between them every 10ish seconds.&lt;/li&gt;
&lt;li&gt;Because it’s canvas, this must be some kind of programatic rendering with javascript.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I still have questions though. &lt;strong&gt;Let’s dig deeper&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How it's done &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;After digging though the code a bit, I found the javascript driving the canvas and this very quickly revealed a couple of things.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's interesting to note that for this simple effect, there are no libraries in use here. This is possibly to keep the frontend lightweight.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is an matrix of images used to create the blurred backgrounds. In the case of long video’s there are several matrixes of 10x10 images like the below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Sr-jDMd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3707a6ytywywf217py4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Sr-jDMd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3707a6ytywywf217py4.png" alt="Image A matrix of YouTube video thumbnails arranged into a 10x10 grid" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each time the video playback changes, an event is fired and the background images are updated by looking them up in the matrix and selecting which frame to crop out of it.&lt;/p&gt;

&lt;p&gt;The cropped image is rendered onto the canvas, then a blur is applied. Animations are used to alternate between in incoming canvas with the new background image and the previous canvas with the old background image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Neat&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I built a simple version of it using one of the matrixes to show how the effect can be achieved.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/diffidentDude/embed/bGmBJKK?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you're curious as to the video I was watching while writing this, I've added it below.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/R_g4nT4a28U"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
