Wrapping Up 2022 / / No Comments I have a tradition here where – usually – I write up a "here’s what I did this year" post. Typically I write this just for myself as I figure it’s a good way to take stock and really appreciate what I’m accomplished. Sometimes it’s just a nice way to say goodbye and look forward to the next year. I feel like every year is incredibly busy as well as exciting, but kind of in the Chinese curse manner. That being said – my kids are healthy and happy, my wife is healthy and happy and I’m well employed. No matter what else happens, that’s a win for the year over all. What I Accomplished This was a huge year for my blog and I’m very... more → Posted in: JavaScript Tagged with: 2022, Wrapping
Detecting Fonts Ready / / No Comments Knowing when resources are loaded is a key part of building functional, elegant websites. We’re used to using the DOMContentLoaded event (commonly referred to as “domready”) but did you know there’s an event that tells you when all fonts have loaded? Let’s learn how to use document.fonts! The document.fonts object features a ready property which is a Promise representing if fonts have been loaded: // Await all fonts being loaded await document.fonts.ready; // Now do something! Maybe add a class to the body document.body.classList.add('fonts-loaded'); Font files can be relatively large so you can never assume they’ve loaded quickly. One simply await from... more → Posted in: JavaScript Tagged with: Detecting, Fonts, Ready
Adding Download Support in an Eleventy Site / / No Comments I was thinking recently about how I would add "Downloads" support to an Eleventy site. By that I mean, a site where you have various resources (PDFs, zip, etc) and want to provide a way to let users download them in a consistent manner, as well as how basic tracking could be done as well. I came up with a few ideas I’d like to share, but as always, please let me know what you’ve done and what you would suggest. Method 1 – Meta Refresh For my first attempt, I imagined a site where I’ve got some files up in S3 (like I do for my images here) and I’d like to take a directory and set them up as resources. So for example, imagine a _data file named downloads.json: {... more → Posted in: JavaScript Tagged with: Adding, download, Eleventy, Site, Support
Download Data as a File with Alpine.js / / No Comments As my readers know, I’ve been updating some of my earlier Vue.js examples to demonstrate how they would work with Alpine.js. Normally I post these "conversions" when I see one of the Vue posts pop up in my stats. Today I noticed this entry was "trending" – Vue Quick Shot – Downloading Data as a File. I thought it would be a great candidate for showing an Alpine version. Let’s take a look. While I won’t repeat everything from the previous post, I’ll quickly cover how it worked. First, it makes use of the download attribute of the anchor tag. This will take a normal link operation and instead ask the browser to download the resource at the... more → Posted in: JavaScript Tagged with: Alpine.js, data, download, file
Detect System Theme Preference Change Using JavaScript / / No Comments JavaScript and CSS allow users to detect the user theme preference with CSS’ prefers-color-scheme media query. It’s standard these days to use that preference to show the dark or light theme on a given website. But what if the user changes their preference while using your app? To detect a system theme preference change using JavaScript, you need to combine matchMedia, prefers-color-scheme, and an event listener: window.matchMedia('(prefers-color-scheme: dark)') .addEventListener('change',({ matches }) ={ if (matches) { console.log("change to dark mode!") } else { console.log("change to light mode!") } }) The change event of the matchMedia API notifies you... more → Posted in: JavaScript Tagged with: Change, Detect, JavaScript, Preference, system, theme, using
Quick Test Post – Sorry! / / No Comments Hey folks, earlier this week I posted about a Pipedream workflow to automatically post new blog entries to Mastodon and Twitter. I discovered an issue with the workflow that ended up being a bug on the Pipedream side. (It happens!) They’ve corrected the issue and I need to test, so I’ve temporarily disabled Mastodon posting and am writing this post just to see if it posts correctly to Twitter. If so, I’ll then restore the Mastodon step (Pipedream makes it easy to disable one part of a workflow) and see what happens when I post again. As I feel guilty "spamming" my subscribers with noise, here’s a quick tip. If you ever need to expose a local web site for external... more → Posted in: JavaScript Tagged with: Post, Quick, Sorry, test
Create a Thumbnail From a Video with ffmpeg / / No Comments Creating a thumbnail to represent a video is a frequent task when presenting media on a website. I previously created a shell script to create a preview video from a larger video, much like many adult sites provide. Let’s view how we can create a preview thumbnail from a video! Developers can use `ffmpeg, an incredible open source media utility, to create a thumbnail. To create a thumbnail from the first frame of a video, execute the following command: ffmpeg -i input.webm -vf "select=eq(n\,34)" -vframes 1 thumbnail.png Providing a video thumbnail is a great tool convert images into video views. You don’t need fancy software and manual labor to create thumbnails — use... more → Posted in: JavaScript Tagged with: create, ffmpeg, from, Thumbnail, video
Convert Fahrenheit to Celsius with JavaScript / / No Comments The United States is one of the last bodies that refuses to implement the Celsius temperature standard. Why? Because we’re arrogant and feel like we don’t need to change. With that said, if you code for users outside the US, it’s important to provide localized weather data to users. Let’s took at how you can convert between Fahrenheit and Celsius. Fahrenheit to Celsius The formula to convert Fahrenheit to Celsius is: °C = 5/9 x (°F - 32) The following function converts Fahrenheit to Celsius: function convertFahrenheitToCelsius(degrees) { return Math.floor(5 / 9 * (degrees - 32)); } Celsius to Fahrenheit The formula to convert Celsius to Fahrenheit is: °F = (°C... more → Posted in: JavaScript Tagged with: Celsius, Convert, Fahrenheit, JavaScript