-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat: Added <amp-blurhash> component to render BlurHash placeholders for AMP pages #40359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <!doctype html> | ||
| <html ⚡> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>amp-blurhash Example</title> | ||
| <link rel="canonical" href="amp-blurhash.html" /> | ||
| <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" /> | ||
| <style amp-boilerplate> | ||
| body { | ||
| -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both; | ||
| -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both; | ||
| -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both; | ||
| animation: -amp-start 8s steps(1, end) 0s 1 normal both; | ||
| } | ||
|
|
||
| @-webkit-keyframes -amp-start { | ||
| from { | ||
| visibility: hidden; | ||
| } | ||
| to { | ||
| visibility: visible; | ||
| } | ||
| } | ||
|
|
||
| @-moz-keyframes -amp-start { | ||
| from { | ||
| visibility: hidden; | ||
| } | ||
| to { | ||
| visibility: visible; | ||
| } | ||
| } | ||
|
|
||
| @-ms-keyframes -amp-start { | ||
| from { | ||
| visibility: hidden; | ||
| } | ||
| to { | ||
| visibility: visible; | ||
| } | ||
| } | ||
|
|
||
| @keyframes -amp-start { | ||
| from { | ||
| visibility: hidden; | ||
| } | ||
| to { | ||
| visibility: visible; | ||
| } | ||
| } | ||
| </style> | ||
| <noscript> | ||
| <style amp-boilerplate> | ||
| body { | ||
| -webkit-animation: none; | ||
| -moz-animation: none; | ||
| -ms-animation: none; | ||
| animation: none; | ||
| } | ||
| </style> | ||
| </noscript> | ||
|
|
||
| <script async src="https://cdn.ampproject.org/v0.js"></script> | ||
|
|
||
| <!-- Include the custom element --> | ||
| <script async custom-element="amp-blurhash" src="/dist/v0/amp-blurhash-0.1.js"></script> | ||
| </head> | ||
| <body> | ||
| <h1>amp-blurhash demo</h1> | ||
|
|
||
| <!-- Replace with any valid blurhash string --> | ||
| <amp-blurhash | ||
| layout="fixed" | ||
| width="320" | ||
| height="200" | ||
| hash="LEHV6nWB2yk8pyo0adR*.7kCMdnj" | ||
| > | ||
| </amp-blurhash> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .i-amphtml-blurhash-canvas { | ||
| object-fit: cover; | ||
| width: 100%; | ||
| height: 100%; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,164 @@ | ||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Copyright 2025 The AMP Project Authors. | ||||||||||||||||||||||||
| * Licensed under the Apache License, Version 2.0. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * <amp-blurhash> – lightweight placeholder renderer. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * Attributes: | ||||||||||||||||||||||||
| * hash – required • BlurHash string to decode | ||||||||||||||||||||||||
| * width – required • original media pixel width | ||||||||||||||||||||||||
| * height – required • original media pixel height | ||||||||||||||||||||||||
| * punch – optional • contrast boost (default 1.0) | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * Example: | ||||||||||||||||||||||||
| * <amp-blurhash hash="LEHV6nWB2yk8pyo0adR*.7kCMdnj" | ||||||||||||||||||||||||
| * width="320" height="213" layout="fixed"></amp-blurhash> | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import {decode} from './blurhash-decode'; // 25‑line helper (embedded below) | ||||||||||||||||||||||||
| import {AmpElement} from '#core/dom/amp-element'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export class AmpBlurhash extends AmpElement { | ||||||||||||||||||||||||
| /** @override */ | ||||||||||||||||||||||||
| static ['layoutSizeDefined']() { | ||||||||||||||||||||||||
| // We have intrinsic size; no crawl‑up | ||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** @override */ | ||||||||||||||||||||||||
| buildCallback() { | ||||||||||||||||||||||||
| // Parse attributes. | ||||||||||||||||||||||||
| const hash = this.element.getAttribute('hash'); | ||||||||||||||||||||||||
| const w = parseInt(this.element.getAttribute('width'), 10); | ||||||||||||||||||||||||
| const h = parseInt(this.element.getAttribute('height'), 10); | ||||||||||||||||||||||||
| const punch = | ||||||||||||||||||||||||
| parseFloat(this.element.getAttribute('punch')) || /*default*/ 1; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Basic validation – AMP validator also checks, but we fail early here | ||||||||||||||||||||||||
| if (!hash || !w || !h) { | ||||||||||||||||||||||||
| this.user().error('Missing required attributes on <amp-blurhash>.'); | ||||||||||||||||||||||||
|
Comment on lines
+38
to
+39
|
||||||||||||||||||||||||
| if (!hash || !w || !h) { | |
| this.user().error('Missing required attributes on <amp-blurhash>.'); | |
| const missingAttributes = []; | |
| if (!hash) missingAttributes.push('hash'); | |
| if (!w) missingAttributes.push('width'); | |
| if (!h) missingAttributes.push('height'); | |
| if (missingAttributes.length > 0) { | |
| this.user().error( | |
| `Missing required attribute(s) on <amp-blurhash>: ${missingAttributes.join(', ')}.` | |
| ); |
Copilot
AI
Jul 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string replacement logic appears to handle URL-safe base64 variants, but BlurHash uses its own base83 encoding, not base64. Using atob() on a BlurHash string will likely fail. BlurHash requires a custom base83 decoder.
| const bytes = atob(str.replace(/#/g, '+').replace(/_/g, '/')); | |
| const blurhash = new Uint8Array(bytes.length); | |
| for (let i = 0; i < blurhash.length; ++i) blurhash[i] = bytes.charCodeAt(i); | |
| const blurhash = base83Decode(str); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| describes.realWin('amp-blurhash', { | ||
| amp: true, | ||
| extensions: ['amp-blurhash'], | ||
| }, env => { | ||
| it('renders a canvas with decoded pixels', async () => { | ||
| const el = env.win.document.createElement('amp-blurhash'); | ||
| el.setAttribute('hash', 'LEHV6nWB2yk8pyo0adR*.7kCMdnj'); | ||
| el.setAttribute('width', '16'); | ||
| el.setAttribute('height', '16'); | ||
| el.setAttribute('layout', 'fixed'); | ||
| env.win.document.body.appendChild(el); | ||
| await el.whenBuilt(); | ||
| const canvas = el.querySelector('canvas'); | ||
| expect(canvas).to.exist; | ||
| expect(canvas.width).to.equal(16); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3884,6 +3884,23 @@ attr_lists: { | |||||||||||
| "{{\\^" # Section delimiters are disallowed. | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| # <amp-blurhash> | ||||||||||||
| tag: "AMP-BLURHASH" | ||||||||||||
| spec_name: "amp-blurhash" | ||||||||||||
| spec_url: "https://amp.dev/documentation/components/amp-blurhash" | ||||||||||||
| html_format: AMP | ||||||||||||
| requires_extension: "amp-blurhash" | ||||||||||||
| attr_list: { | ||||||||||||
| attrs: { | ||||||||||||
| name: "hash" | ||||||||||||
| mandatory: true | ||||||||||||
| value_regex: "[A-Za-z0-9+/]{6,}" | ||||||||||||
|
||||||||||||
| value_regex: "[A-Za-z0-9+/]{6,}" | |
| # BlurHash strings must follow the format: 4 + 2 * X * Y characters, | |
| # where X and Y are the number of components in the horizontal and vertical directions. | |
| # This regex validates the length and ensures only valid base64 characters are used. | |
| value_regex: "[A-Za-z0-9+/]{6,4096}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import statement references './blurhash-decode' but the decode function is actually defined within the same file. This import will fail at runtime. Either remove this import statement or move the decode function to a separate module.