Detect System Theme Preference Change Using JavaScript

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

Land Doesn’t Vote

After nearly every U.S. election a Republican will produce a map of the USA with electoral districts colored to show the winning party. This map is normally accompanied by a claim that the Unied States is a majority Republican country. Democrats will then reply with the traditional response, ‘Land doesn’t vote. People do.’The problem with using a traditional geographic map to visualize election Maps Mania… more →
Posted in: Interactive Maps

Quick Test Post – Sorry!

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

Create a Thumbnail From a Video with ffmpeg

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

The National Fast Food Index

If you buy a MacDonald’s Big Mac in Lee, Massachuesetts you will have to pay a whopping $ 7.89. That is over twice as expensive as buying the very same burger at a MacDonald’s in Eufaula, Oklahoma, where a Big Mac costs just £3.39. The price of a MacDonald’s Big Mac in the towns of Lee & Eufaula, and in the other 13,271 MacDonald’s outlets in the United States can be explored on the Fast Food Maps Mania… more →
Posted in: Interactive Maps

Convert Fahrenheit to Celsius with JavaScript

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

The World’s Largest Polluters

The footprintMap is an interactive mapped visualization of the CO2 footprint of 118 countries around the world. Using the map you can discover the per capita CO2 output of each country and see which countries contribute the most and least to global heating. According to this map Singapore has the highest per capita CO2 footprint of any country. The United States has the 9th highest per capita Maps Mania… more →
Posted in: Interactive Maps

A Bare-Bones Eleventy Template for Glitch

A few weeks ago I blogged about a simple Alpine.js template for Glitch projects. I’m still new to Glitch and wanted to give it a whirl with an Eleventy demo I wanted to share. Glitch has an Eleventy template, but it’s a bit verbose. It sets up a basic blog with sample posts and such, and that’s great to learn, but if you already know Eleventy, you may prefer to start off a bit simpler. With that in mind, I created this repository: https://github.com/cfjedimaster/glitch-eleventy It defines an .eleventy.js file that specifies an input and output directory. It sets up a very basic HTML layout and an empty index page that uses it. I also used Liquid for my demo whereas the Glitch-provided... more →
Posted in: JavaScript
1 50 51 52 53 54 190