commit e1e4e9557d0c90794bb2609eb81682fabe40975e
parent b1825da5c7cd207cc09628794bead4facd774409
Author: Michael Neuper <michael@michaelneuper.com>
Date: Wed, 28 Oct 2026 10:32:29 +0200
lazily load videos on posts
Diffstat:
6 files changed, 62 insertions(+), 14 deletions(-)
diff --git a/README.md b/README.md
@@ -62,7 +62,7 @@ draft: false # true keeps it off the site
---
```
-Maths goes between `$...$` or `$$...$$` and is rendered with KaTeX when the site is built. Videos and other files a post links to go in `public/posts/<slug>/`.
+Maths goes between `$...$` or `$$...$$` and is rendered with KaTeX when the site is built. Videos and other files a post links to go in `public/posts/<slug>/`. A clip that should play like a GIF is written as `<video loop muted playsinline controls preload="none" width="…" height="…">`, with the video's own size, and plays only while it's on screen.
## Deploying
diff --git a/src/components/post/OrgBody.astro b/src/components/post/OrgBody.astro
@@ -1,6 +1,7 @@
---
// A post's text. Its headings get stars that fold them, as in Org
-// (src/scripts/post/folding.js), and M-x imenu jumps between them.
+// (src/scripts/post/folding.js), M-x imenu jumps between them, and its clips
+// play only while they're on screen (src/scripts/post/videos.js).
import "../../styles/org.css";
import "../../styles/post/org.css";
---
@@ -11,7 +12,9 @@ import "../../styles/post/org.css";
<script>
import { initFolding } from "../../scripts/post/folding.js";
import { initImenu } from "../../scripts/post/imenu.js";
+ import { initVideos } from "../../scripts/post/videos.js";
initFolding();
initImenu();
+ initVideos();
</script>
diff --git a/src/content/posts/efficient-latex-editing-with-emacs/index.md b/src/content/posts/efficient-latex-editing-with-emacs/index.md
@@ -34,7 +34,7 @@ Since I prefer to stay inside emacs, I set my pdf viewer to `pdf-tools` by addin
Here's what it looks like:
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2804" height="1832">
<source src="preview.webm" type="video/webm">
<source src="preview.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -60,7 +60,7 @@ This is also enabled by default in Doom Emacs.
This is easier to show than to explain:
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2812" height="1832">
<source src="tex-folding.webm" type="video/webm">
<source src="tex-folding.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -84,7 +84,7 @@ _CDLaTeX_ also has a ton of features, but here are the ones I use the most:
Abbreviation expansion is triggered with <kbd>TAB</kbd>, for example:
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2812" height="1832">
<source src="abbreviations.webm" type="video/webm">
<source src="abbreviations.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -101,7 +101,7 @@ For example, typing `` `a`` inserts `\(\alpha\)`.
You can wait a second after pressing `` ` `` and a list of all the available macros will pop up.
The macros can also be customised with the `cdlatex-math-symbol-alist` variable.
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2812" height="1832">
<source src="math-symbols.webm" type="video/webm">
<source src="math-symbols.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -112,7 +112,7 @@ The macros can also be customised with the `cdlatex-math-symbol-alist` variable.
Putting accents on mathematical characters and/or changing the font of a character uses key combinations with the quote character `'` as a prefix.
The accent or font change is applied to the character or LaTeX macro **before** point.
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2812" height="1832">
<source src="accents.webm" type="video/webm">
<source src="accents.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -138,7 +138,7 @@ So it should look something like this if you're using [cdlatex](#cdlatex) as wel
When you build your LaTeX project for the first time using _LatexMk_, press <kbd>C-c C-c</kbd> and select the `LatexMk` option.
After that, you can build it with `C-c C-a`.
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2812" height="1832">
<source src="latexmk.webm" type="video/webm">
<source src="latexmk.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -186,7 +186,7 @@ Here are some the notable features of Xenops:
_Xenops_ replaces the `preview-latex` functionality in _AUCTeX_, which does not seem to support rendering images as SVGs.
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2812" height="1832">
<source src="xenops-render.webm" type="video/webm">
<source src="xenops-render.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -196,7 +196,7 @@ _Xenops_ replaces the `preview-latex` functionality in _AUCTeX_, which does not
Simply run `M-x xenops-image-handle-paste` to paste an image from the clipboard into your document.
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2940" height="1912">
<source src="xenops-paste.webm" type="video/webm">
<source src="xenops-paste.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -211,7 +211,7 @@ You can install _YASnippet_ by enabling `:editor snippets` in your `doom` block.
Some useful snippets can be found [here](https://github.com/doomemacs/snippets).
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2818" height="1832">
<source src="snippets.webm" type="video/webm">
<source src="snippets.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -233,7 +233,7 @@ It can do symbolic differentiation, linear algebra, taylor series expansions, am
You can start calc in embedded mode by running `M-x calc-embedded` and perform operations on LaTeX equations.
Here's an example of me finding the taylor series expansion of _e<sup>x</sup>_ to 6 terms:
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2804" height="1832">
<source src="gnu-calc.webm" type="video/webm">
<source src="gnu-calc.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
@@ -246,7 +246,7 @@ This should also be installed by default in Doom Emacs.
Here's an example of me using it:
-<video autoplay loop muted playsinline width="100%">
+<video loop muted playsinline controls preload="none" width="2804" height="1832">
<source src="evil-tex.webm" type="video/webm">
<source src="evil-tex.mp4" type="video/mp4">
Your browser does not support the video tag or the file format of this video.
diff --git a/src/scripts/post/folding.js b/src/scripts/post/folding.js
@@ -2,7 +2,8 @@
// heading at its level or above. Clicking its stars folds that away. With the
// stars focused, TAB does the same and S-TAB cycles the whole buffer
// (overview, contents, show all), as in the posts list on the home page.
-// Folding toggles a class rather than moving anything, so videos keep playing.
+// Folding toggles a class rather than moving anything, so a folded clip pauses
+// where it was and picks up from there when unfolded (src/scripts/post/videos.js).
import { $, $$ } from "../util.js";
import { defcommand, message } from "../emacs/index.js";
diff --git a/src/scripts/post/videos.js b/src/scripts/post/videos.js
@@ -0,0 +1,39 @@
+// A post's clips: looping, muted videos that play like GIFs. Their markup has
+// preload="none" and controls, so without JavaScript nothing downloads until a
+// reader presses play. Here each clip starts loading a screen before it comes
+// into view and plays only while it's on screen, so a post with a dozen clips
+// doesn't fetch them all on load. Under reduced motion the clips keep their
+// controls and only load far enough to show their first frame.
+
+import { $$, reducedMotion } from "../util.js";
+
+export function initVideos() {
+ const clips = $$(".post-body video[loop][muted]");
+ if (!clips.length) return;
+ const still = reducedMotion.matches;
+
+ const near = new IntersectionObserver((entries) => {
+ for (const { target, isIntersecting } of entries) {
+ if (!isIntersecting) continue;
+ near.unobserve(target);
+ // Raising preload resumes the fetch. load() would too, but it would
+ // cancel a play() that came first for a clip already on screen.
+ target.preload = still ? "metadata" : "auto";
+ }
+ }, { rootMargin: "100% 0px" });
+
+ const visible = new IntersectionObserver((entries) => {
+ for (const { target, isIntersecting } of entries) {
+ // play() rejects if the browser blocks it; the clip just stays still.
+ if (isIntersecting) target.play().catch(() => {});
+ else target.pause();
+ }
+ });
+
+ for (const clip of clips) {
+ near.observe(clip);
+ if (still) continue;
+ clip.controls = false;
+ visible.observe(clip);
+ }
+}
diff --git a/src/styles/post/org.css b/src/styles/post/org.css
@@ -301,6 +301,11 @@
margin: 1.4em 0;
}
+/* Clips load late (src/scripts/post/videos.js), so show where one will be. */
+.post-body video {
+ background: var(--hl);
+}
+
.post-body hr {
margin: 2em 0;
border: 0;