From 57993503e880e82bf48378a79546030a3e41ff9a Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 16:58:08 +0000 Subject: [PATCH 1/3] Redesign the reader and writer around paper, type and heartbeat ink - Richer ink: iron-gall blue-black at rest, warming through claret to vermilion, cooling to clear blue-black - Words land as wet ink, the caret writes in the current ink - A heartline under the signature draws the whole letter's heart rate - Pause notes sit in the margin beside the line they happened on - Envelope drawn as a real envelope with the seal on the flap - Postscript replaces the summary card; text links replace pill buttons - Writer: same desk and paper, consent as a set sheet, caret follows the heart Co-Authored-By: Claude Opus 5.5 --- src/reader/index.html | 51 ++++--- src/reader/reader.css | 307 +++++++++++++++++++++++++--------------- src/reader/reader.js | 73 +++++++--- src/shared/heartline.js | 70 +++++++++ src/shared/playback.js | 24 ++-- src/writer/index.html | 38 ++--- src/writer/writer.css | 186 ++++++++++++++++-------- src/writer/writer.js | 4 +- test/core.test.js | 6 +- test/dom.test.js | 4 +- 10 files changed, 525 insertions(+), 238 deletions(-) create mode 100644 src/shared/heartline.js diff --git a/src/reader/index.html b/src/reader/index.html index 1f1f68f..ce8dcea 100644 --- a/src/reader/index.html +++ b/src/reader/index.html @@ -5,6 +5,7 @@ + Heartwritten @@ -17,35 +18,53 @@

Heartwritten

-
-

+
+

+
+
+ -

Tap the seal to open it.

-
+

Break the seal to read

+

This letter lives only in its link. Opening it sends nothing anywhere.

-
+
-

-

+
+ +

+

- +
+
+
+
+
-
diff --git a/src/reader/reader.css b/src/reader/reader.css index f2a4d9a..ecfe63b 100644 --- a/src/reader/reader.css +++ b/src/reader/reader.css @@ -1,29 +1,59 @@ +/* Heartwritten reader. + One sheet of paper on a desk. The ink is the only colour that moves. */ :root { - --paper: #fbf6ec; - --paper-edge: #efe5d2; - --desk: #e9dfcc; - --ink: #1f2942; - --muted: #6d6556; - --wax: #a3232a; - --glow: rgba(232, 176, 60, 0.32); - --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif; - --hand: "Snell Roundhand", "Segoe Script", "Bradley Hand", "Apple Chancery", cursive; + --desk: #e5dfd4; + --paper: #f7f3ea; + --paper-shade: #ede6d8; + --ink: #1c2234; + --muted: #776f5f; + --on-desk: #2a2a2e; + --on-desk-muted: #7a7366; + --hair: rgba(28, 34, 52, 0.14); + --stamp: #a8381e; + --glow: rgba(214, 158, 52, 0.26); + --serif: "Iowan Old Style", Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif; + --tooth: url("data:image/svg+xml;utf8,"); color-scheme: light dark; } @media (prefers-color-scheme: dark) { - :root { --desk: #1c1a17; --muted: #a49a88; } + :root { + --desk: #161412; + --paper: #efe9dd; + --paper-shade: #e4dccc; + --on-desk: #ebe3d4; + --on-desk-muted: #9b9282; + } } + * { box-sizing: border-box; } html, body { margin: 0; min-height: 100%; } body { - background: radial-gradient(120% 90% at 50% 0%, color-mix(in srgb, var(--desk) 70%, #fff 30%), var(--desk)); - color: var(--ink); - font: 18px/1.65 var(--serif); + background: var(--desk); + color: var(--on-desk); + font: 18px/1.6 var(--serif); + font-variant-numeric: oldstyle-nums proportional-nums; -webkit-font-smoothing: antialiased; + text-rendering: optimizeLegibility; min-height: 100vh; } -main { max-width: 860px; margin: 0 auto; padding: 48px 16px 64px; } -button { font: inherit; cursor: pointer; } +main { max-width: 900px; margin: 0 auto; padding: 56px 24px 72px; } +button { font: inherit; color: inherit; cursor: pointer; background: none; border: 0; padding: 0; } +button:focus-visible { outline: 1.5px solid currentColor; outline-offset: 4px; } + +.smallcaps { + font-variant-caps: all-small-caps; + letter-spacing: 0.09em; + font-size: 17px; + margin: 0; +} +.fine { color: var(--on-desk-muted); font-size: 13px; line-height: 1.5; margin: 0; } +.sample-note { + margin: 0; + color: var(--stamp); + font-variant-caps: all-small-caps; + letter-spacing: 0.09em; + font-size: 15px; +} .view { display: none; } body[data-view="empty"] .view-empty, @@ -31,122 +61,175 @@ body[data-view="envelope"] .view-envelope, body[data-view="breaking"] .view-envelope, body[data-view="letter"] .view-letter { display: block; } -.wordmark { font-style: italic; font-weight: 400; font-size: 44px; margin: 18vh 0 8px; text-align: center; } -.view-empty p { text-align: center; color: var(--muted); max-width: 34em; margin: 0 auto; } +/* Nothing to open */ +.view-empty { padding-top: 22vh; max-width: 30em; } +.wordmark { font-weight: 400; font-style: italic; font-size: 40px; margin: 0 0 12px; letter-spacing: -0.01em; } +.view-empty p { color: var(--on-desk-muted); margin: 0; } + +/* The envelope */ +.view-envelope { max-width: 600px; margin: 4vh auto 0; } +.address { margin: 0 0 28px; } +.address .smallcaps { color: var(--on-desk-muted); } +.to { font-weight: 400; font-style: italic; font-size: 46px; line-height: 1.1; margin: 6px 0 4px; letter-spacing: -0.01em; } +.from { margin: 0; color: var(--on-desk-muted); } -/* Envelope */ .envelope { - margin: 8vh auto 0; max-width: 520px; padding: 48px 32px 40px; text-align: center; - background: linear-gradient(160deg, var(--paper), var(--paper-edge)); - border-radius: 6px; box-shadow: 0 1px 0 #fff inset, 0 30px 60px -30px rgba(40, 25, 5, 0.45), 0 2px 6px rgba(40, 25, 5, 0.12); - position: relative; color: #1f2942; -} -.envelope::before { /* the flap */ - content: ""; position: absolute; inset: 0 0 auto 0; height: 46%; - background: linear-gradient(to bottom right, transparent 49.5%, rgba(120, 90, 40, 0.10) 50%, transparent 50.5%), - linear-gradient(to bottom left, transparent 49.5%, rgba(120, 90, 40, 0.10) 50%, transparent 50.5%); - pointer-events: none; -} -.eyebrow { font-size: 13px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); margin: 0 0 8px; } -.envelope .eyebrow { color: #7d7262; } -.to { font-weight: 400; font-style: italic; font-size: 38px; margin: 0; } -.from { margin: 4px 0 26px; color: #6d6556; } -.seal-button { background: none; border: 0; padding: 0; border-radius: 50%; display: inline-block; } -.seal-button:focus-visible { outline: 3px solid #c99a3b; outline-offset: 6px; } -.seal { display: block; width: 168px; height: 168px; animation: heartbeat var(--beat, 0.9s) ease-in-out infinite; transform-origin: 50% 52%; } -.seal svg { width: 100%; height: 100%; } -.hint { color: #7d7262; font-size: 15px; margin: 14px 0 0; } -.fine { color: var(--muted); font-size: 13px; text-align: center; margin-top: 28px; } -.badge { display: inline-block; margin: 16px auto 0; font-size: 12px; letter-spacing: 0.04em; padding: 4px 10px; border-radius: 99px; background: #f1e0b6; color: #5b4310; } + position: relative; + aspect-ratio: 1.6; + background: var(--tooth), var(--paper); + box-shadow: 0 0 0 1px rgba(60, 45, 20, 0.07), 0 24px 44px -30px rgba(40, 28, 10, 0.55); +} +.flap { position: absolute; inset: 0; width: 100%; height: 100%; } +.flap-top { fill: var(--paper-shade); stroke: rgba(80, 60, 30, 0.16); stroke-width: 1; vector-effect: non-scaling-stroke; } +.flap-side { fill: none; stroke: rgba(80, 60, 30, 0.11); stroke-width: 1; vector-effect: non-scaling-stroke; } +.seal-button { + position: absolute; left: 50%; top: 58%; transform: translate(-50%, -50%); + border-radius: 50%; +} +.seal-button:focus-visible { outline: 1.5px solid var(--ink); outline-offset: 6px; } +.seal { + display: block; width: 148px; height: 148px; + animation: heartbeat var(--beat, 0.9s) ease-in-out infinite; + transform-origin: 50% 52%; + filter: drop-shadow(0 6px 6px rgba(60, 10, 10, 0.18)); +} +.seal svg { width: 100%; height: 100%; display: block; } +.hint { margin: 22px 0 0; color: var(--on-desk-muted); text-align: center; } +.view-envelope .sample-note { text-align: center; margin-top: 6px; } +.view-envelope .fine { text-align: center; margin-top: 40px; } + @keyframes heartbeat { 0%, 100% { transform: scale(1); } - 12% { transform: scale(1.045); } - 24% { transform: scale(0.995); } - 36% { transform: scale(1.025); } - 50% { transform: scale(1); } + 12% { transform: scale(1.025); } + 26% { transform: scale(1); } + 38% { transform: scale(1.014); } + 52% { transform: scale(1); } } -body[data-view="breaking"] .seal { animation: crack 0.9s cubic-bezier(.5,0,.2,1) forwards; } -body[data-view="breaking"] .envelope { animation: lift 0.9s ease forwards; } +body[data-view="breaking"] .seal { animation: crack 0.9s cubic-bezier(.5, 0, .2, 1) forwards; } +body[data-view="breaking"] .flap-top { animation: open 0.9s ease forwards; transform-origin: 50% 0; } +body[data-view="breaking"] .view-envelope { animation: away 0.9s ease forwards; } @keyframes crack { - 0% { transform: scale(1) rotate(0); filter: none; } - 30% { transform: scale(1.08) rotate(-4deg); } - 100% { transform: scale(0.6) rotate(18deg) translateY(40px); opacity: 0; filter: blur(2px); } + 0% { transform: scale(1) rotate(0); } + 30% { transform: scale(1.06) rotate(-3deg); } + 100% { transform: scale(0.7) rotate(14deg) translateY(30px); opacity: 0; } } -@keyframes lift { to { transform: translateY(-12px); opacity: 0; } } +@keyframes open { to { transform: scaleY(-1); } } +@keyframes away { 70% { opacity: 1; } to { opacity: 0; transform: translateY(-8px); } } -/* Letter */ -.desk { display: grid; grid-template-columns: 92px 1fr; gap: 20px; align-items: start; } -.margin { position: sticky; top: 32px; text-align: center; color: var(--muted); font-size: 14px; } -.heart { color: var(--ink); display: inline-block; transition: color 1.2s ease; transform-origin: 50% 60%; } -.heart.beat { animation: thump 0.42s ease-out; } -@keyframes thump { 0% { transform: scale(1); } 18% { transform: scale(1.28); } 38% { transform: scale(0.96); } 60% { transform: scale(1.1); } 100% { transform: scale(1); } } -.bpm { margin: 2px 0 18px; font-variant-numeric: tabular-nums; } -.bpm span { font-size: 22px; color: var(--ink); display: block; line-height: 1.1; } -@media (prefers-color-scheme: dark) { .bpm span { color: #eadfca; } } -.bpm small { letter-spacing: 0.12em; text-transform: uppercase; font-size: 11px; } -.pauses { list-style: none; padding: 0; margin: 0; font-style: italic; font-size: 13px; line-height: 1.4; } -.pauses li { margin-bottom: 8px; animation: fadein 1.2s ease; } -.took-back { font-style: italic; font-size: 13px; line-height: 1.4; } -@keyframes fadein { from { opacity: 0; transform: translateY(4px); } } +/* The letter */ +.sheet { display: grid; grid-template-columns: 112px minmax(0, 680px); gap: 28px; justify-content: center; align-items: start; } +.margin { position: relative; align-self: stretch; color: var(--on-desk-muted); } +.pulse { position: sticky; top: 40px; text-align: right; padding-top: 64px; } +.heart { + display: inline-block; width: 9px; height: 9px; border-radius: 50%; + background: currentColor; color: var(--ink); + transition: color 1.2s ease; +} +@media (prefers-color-scheme: dark) { .heart { color: #d8cdb8; } } +.heart.beat { animation: thump 0.38s ease-out; } +@keyframes thump { 0% { transform: scale(1); } 20% { transform: scale(1.7); } 45% { transform: scale(0.9); } 70% { transform: scale(1.25); } 100% { transform: scale(1); } } +.bpm { margin: 6px 0 0; line-height: 1; } +.bpm span { display: block; font-size: 34px; color: var(--on-desk); letter-spacing: -0.01em; } +.bpm small { font-variant-caps: all-small-caps; letter-spacing: 0.09em; font-size: 14px; } +.pauses { list-style: none; padding: 0; margin: 0; } +.pauses li { + position: absolute; right: 0; max-width: 112px; text-align: right; + font-style: italic; font-size: 14px; line-height: 1.35; + animation: fadein 1s ease; +} +.pauses li::after { content: ""; display: inline-block; width: 14px; height: 1px; background: currentColor; opacity: 0.5; vertical-align: 0.3em; margin-left: 6px; } +.took-back { position: absolute; bottom: 0; right: 0; margin: 0; text-align: right; font-style: italic; font-size: 14px; line-height: 1.35; max-width: 112px; } +@keyframes fadein { from { opacity: 0; } } .paper { --breath: 0; - background: linear-gradient(170deg, var(--paper), var(--paper-edge)); - color: #1f2942; - padding: 44px 48px 40px; border-radius: 4px; min-height: 60vh; - box-shadow: 0 1px 0 #fff inset, 0 calc(26px + var(--breath) * 6px) calc(56px + var(--breath) * 10px) -30px rgba(40, 25, 5, 0.42), 0 2px 6px rgba(40, 25, 5, 0.1); - transform: translateY(calc(var(--breath) * -2px)) scale(calc(1 + var(--breath) * 0.004)); - transition: transform 0.25s linear, box-shadow 0.25s linear; - animation: unfold 0.9s cubic-bezier(.2,.7,.2,1); -} -@keyframes unfold { from { transform: translateY(24px) scaleY(0.92); opacity: 0; } } -.salutation { font-style: italic; font-size: 22px; margin: 0 0 14px; } -.body-text { font-size: 20px; line-height: 1.75; white-space: pre-wrap; overflow-wrap: anywhere; min-height: 3em; } + position: relative; + background: var(--tooth), var(--paper); + color: var(--ink); + padding: 56px 72px 56px; + min-height: 72vh; + box-shadow: 0 0 0 1px rgba(60, 45, 20, 0.07), 0 30px 50px -36px rgba(40, 28, 10, 0.6); + transform: translateY(calc(var(--breath) * -2px)); + transition: transform 0.3s linear; + animation: unfold 0.8s cubic-bezier(.2, .7, .2, 1); +} +@keyframes unfold { from { transform: translateY(16px); opacity: 0; } } +.letterhead { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; margin-bottom: 40px; color: var(--muted); } +.letterhead .smallcaps { margin-left: auto; } +.body-text { + font-size: 21px; line-height: 1.62; + white-space: pre-wrap; overflow-wrap: anywhere; + min-height: 4em; + hanging-punctuation: first; +} .body-text span { transition: color 0.6s ease; } -.body-text .smiled { background: linear-gradient(transparent 62%, var(--glow) 62%, var(--glow) 92%, transparent 92%); border-radius: 2px; } -.caret { display: inline-block; width: 2px; height: 1.1em; background: #1f2942; vertical-align: -0.18em; margin-left: 1px; animation: blink 1.1s steps(1) infinite; } +.body-text .wet { animation: ink 0.55s ease-out; } +@keyframes ink { from { opacity: 0; filter: blur(1.4px); } 60% { filter: blur(0.2px); } } +.body-text .smiled { background: var(--glow); border-radius: 2px; box-decoration-break: clone; -webkit-box-decoration-break: clone; } +.pz-mark { display: inline; } +.caret { + display: inline-block; width: 1.5px; height: 1.05em; margin-left: 1px; + background: var(--caret, var(--ink)); vertical-align: -0.16em; + animation: blink 1.1s steps(1) infinite; +} .finished .caret { display: none; } @keyframes blink { 50% { opacity: 0; } } -.signature { font-family: var(--hand); font-size: 30px; margin: 24px 0 0; text-align: right; min-height: 1.2em; } +.signature { font-style: italic; font-size: 30px; line-height: 1.2; margin: 28px 0 0; min-height: 1.2em; } -.controls { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin: 22px 0 0 112px; } -.controls button, .summary-actions button { - background: transparent; border: 1px solid color-mix(in srgb, var(--muted) 45%, transparent); color: var(--muted); - border-radius: 99px; padding: 6px 14px; font-size: 14px; -} -.controls button[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); } -@media (prefers-color-scheme: dark) { .controls button[aria-pressed="true"] { background: #eadfca; color: #1c1a17; border-color: #eadfca; } } -.controls button:focus-visible, .summary-actions button:focus-visible { outline: 2px solid #c99a3b; outline-offset: 2px; } -.finished .controls { display: none; } +.heartline-wrap { margin: 18px 0 0; } +.heartline-slot { height: 44px; } +.heartline { display: block; width: 100%; height: 44px; overflow: visible; } +.heartline-wrap figcaption { display: flex; justify-content: space-between; color: var(--muted); margin-top: 6px; } +.heartline-wrap .smallcaps { font-size: 14px; } +.range { font-size: 14px; font-style: italic; } -.summary { margin: 28px 0 0 112px; padding: 28px 32px; border-radius: 6px; background: color-mix(in srgb, var(--paper) 70%, transparent); color: #1f2942; animation: fadein 1s ease; } -.summary h2 { font-weight: 400; font-style: italic; font-size: 24px; margin: 0 0 10px; } -.summary p { margin: 8px 0; } -.summary .sum-lead { color: #6d6556; } -.summary .q span { color: #7d7262; font-size: 14px; white-space: nowrap; margin-left: 6px; } -.summary q { font-style: italic; } -.legend { font-size: 13px; color: #7d7262; display: flex; gap: 14px; flex-wrap: wrap; } -.ink::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; } -.ink.calm::before { background: rgb(31, 41, 66); } -.ink.warm::before { background: rgb(168, 38, 44); } -.ink.glow::before { background: rgba(232, 176, 60, 0.6); } -.summary-actions { display: flex; gap: 8px; margin-top: 16px; } -.summary-actions button { color: #6d6556; } -.summary .fine { text-align: left; margin-top: 18px; color: #7d7262; } +/* Postscript */ +.postscript { margin-top: 44px; padding-top: 28px; border-top: 1px solid var(--hair); animation: fadein 1s ease; } +.postscript h2 { color: var(--muted); font-weight: 400; margin-bottom: 14px; } +.postscript p { margin: 0 0 8px; font-size: 18px; } +.postscript .lead { color: var(--muted); } +.postscript q { font-style: italic; } +.postscript q::before { content: "“"; } .postscript q::after { content: "”"; } +.postscript .bpm-tag { color: var(--muted); font-size: 15px; margin-left: 6px; white-space: nowrap; } +.scale { display: flex; align-items: center; gap: 10px; margin: 20px 0 22px; color: var(--muted); font-size: 14px; font-style: italic; } +.scale i { flex: 0 1 200px; height: 3px; border-radius: 2px; background: linear-gradient(90deg, rgb(32, 72, 150), rgb(28, 34, 52) 45%, rgb(112, 28, 48) 72%, rgb(184, 52, 28)); } +.links { display: flex; gap: 28px; } +.links button, .controls button { + font-variant-caps: all-small-caps; letter-spacing: 0.09em; font-size: 17px; + text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 5px; + text-decoration-color: transparent; transition: text-decoration-color 0.2s; +} +.links button { color: var(--ink); } +.links button:hover, .controls button:hover, .controls button[aria-pressed="true"] { text-decoration-color: currentColor; } +.postscript .fine { color: var(--muted); margin-top: 22px; } -@media (max-width: 640px) { - main { padding: 24px 16px 48px; } - .desk { grid-template-columns: 1fr; } - .margin { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 10px; justify-content: flex-start; padding: 8px 0; background: var(--desk); } - .bpm { margin: 0; display: flex; gap: 4px; align-items: baseline; } - .bpm span { display: inline; } +.controls { display: flex; flex-wrap: wrap; gap: 8px 28px; max-width: 680px; margin: 22px auto 0; padding-left: 140px; color: var(--on-desk-muted); } +.controls button[aria-pressed="true"] { color: var(--on-desk); } +.finished .controls { display: none; } + +/* Phones: the screen becomes the sheet. */ +@media (max-width: 760px) { + main { padding: 0 0 48px; } + .view-empty { padding: 24vh 20px 0; } + .view-envelope { padding: 40px 20px 0; margin-top: 0; } + .to { font-size: 38px; } + .seal { width: 112px; height: 112px; } + .sheet { display: block; } + .margin { position: sticky; top: 0; z-index: 3; background: var(--tooth), var(--paper); color: var(--muted); border-bottom: 1px solid var(--hair); } + .pulse { position: static; display: flex; align-items: center; gap: 10px; padding: 10px 20px; text-align: left; } + .bpm { margin: 0; display: flex; align-items: baseline; gap: 5px; } + .bpm span { display: inline; font-size: 20px; color: var(--ink); } + .heart { color: var(--ink); } .pauses, .took-back { display: none; } - .paper { padding: 28px 22px; } - .body-text { font-size: 18px; } - .controls, .summary { margin-left: 0; } - .to { font-size: 30px; } + .paper { padding: 28px 20px 40px; min-height: 0; box-shadow: none; animation: none; transform: none; } + .letterhead { margin-bottom: 26px; } + .body-text { font-size: 19px; } + .controls { padding: 0 20px; margin-top: 18px; gap: 6px 22px; } } @media (prefers-reduced-motion: reduce) { - .seal, .heart.beat, .paper { animation: none !important; } + .seal, .heart.beat, .paper, .body-text .wet { animation: none !important; } .paper { transform: none; transition: none; } } +@media (prefers-color-scheme: dark) and (min-width: 761px) { + .heart { box-shadow: 0 0 0 3px var(--paper); } +} diff --git a/src/reader/reader.js b/src/reader/reader.js index d5c4906..27e26dc 100644 --- a/src/reader/reader.js +++ b/src/reader/reader.js @@ -6,6 +6,7 @@ import { decodeLetter, extractToken, finalText } from '../shared/letter-format.j import { Player, bpmAt, inkFor, sampleAt } from '../shared/playback.js'; import { summarize, median, formatDuration, excerpt } from '../shared/summary.js'; import { sealSvg } from '../shared/seal.js'; +import { heartlineSvg, heartlineAt } from '../shared/heartline.js'; const $ = (id) => document.getElementById(id); @@ -63,7 +64,7 @@ function showEnvelope(letter) { // --------------------------------------------------------------------------- // Replay -const state = { speed: 1, sound: false, audio: null, raf: 0, player: null, startedAt: 0, at: 0, paused: false }; +const state = { lastRun: -1, speed: 1, sound: false, audio: null, raf: 0, player: null, startedAt: 0, at: 0, paused: false }; function openLetter(letter) { document.body.dataset.view = 'breaking'; @@ -80,10 +81,14 @@ function startReplay(letter) { state.at = 0; state.last = performance.now(); state.nextBeat = 0; - $('salutation').textContent = letter.to ? `To ${letter.to},` : ''; $('signature').textContent = ''; $('letter-day').textContent = dayLabel(letter.day); $('replay-sim').hidden = !letter.sim; + $('heartline').innerHTML = heartlineSvg(letter, { id: 'hl' }); + $('heartline-who').textContent = letter.from ? `${letter.from}’s heart while writing` : 'Their heart while writing'; + const s = summarize(letter); + $('heartline-range').textContent = s.low > 0 && s.high > 0 ? `${s.low} to ${s.high} beats a minute` : ''; + state.lastRun = -1; render(true); cancelAnimationFrame(state.raf); state.raf = requestAnimationFrame(frame); @@ -119,6 +124,8 @@ function pulse(now) { } const br = sampleAt(letter.vit.br, letter.vit.hz, t) / 100; $('paper').style.setProperty('--breath', br.toFixed(3)); + const reveal = document.getElementById('hl-reveal'); + if (reveal) reveal.setAttribute('width', String(640 * (state.player.done ? 1 : heartlineAt(letter, t)))); } function lastOpAt() { @@ -130,42 +137,75 @@ function lastOpAt() { function render() { const { player, letter, baseline } = state; const chars = player.chars; + // Long pauses are noted in the margin, beside the line they happened on. + const notes = state.pauses.filter((pz) => pz.at <= state.at); + const marks = new Map(); + notes.forEach((pz, k) => { + const at = Math.min(chars.length, Math.max(0, pz.pos)); + if (!marks.has(at)) marks.set(at, k); + }); + const newest = chars.reduce((m, c) => Math.max(m, c.t), -1); + const fresh = newest > state.lastRun; let html = ''; let i = 0; + let caret = ''; const smileAt = (t) => letter.share.smiles && sampleAt(letter.vit.sm, letter.vit.hz, t) >= 6; while (i < chars.length) { + if (marks.has(i)) html += ``; const t = chars[i].t; let j = i; let run = ''; - while (j < chars.length && chars[j].t === t) { run += chars[j].ch; j++; } - const bpm = bpmAt(letter, t); - const cls = smileAt(t) ? ' class="smiled"' : ''; + while (j < chars.length && chars[j].t === t && (j === i || !marks.has(j))) { run += chars[j].ch; j++; } + const ink = inkFor(bpmAt(letter, t), baseline); + const cls = [smileAt(t) ? 'smiled' : '', fresh && t === newest && !state.player.done ? 'wet' : ''].filter(Boolean).join(' '); run = esc(run).replace(/\n/g, '
'); - html += `${run}`; + html += `${run}`; + if (t === newest) caret = ink; i = j; } + if (marks.has(chars.length)) html += ``; + state.lastRun = newest; $('body-text').innerHTML = html + ''; - // Long pauses are noted in the margin, in order. - const notes = state.pauses.filter((pz) => pz.at <= state.at); - $('pauses').innerHTML = notes.map((pz) => `
  • paused ${formatDuration(pz.ms)}
  • `).join(''); + if (caret) $('body-text').style.setProperty('--caret', caret); + $('pauses').innerHTML = notes.map((pz, k) => `
  • paused ${formatDuration(pz.ms)}
  • `).join(''); + placeNotes(); $('took-back').textContent = letter.share.edits && player.tookBack ? `${player.tookBack} character${player.tookBack === 1 ? '' : 's'} taken back` : ''; } +// Lines the margin notes up with the words they belong to. +function placeNotes() { + const paper = $('paper'); + let floor = 0; + for (const li of document.querySelectorAll('#pauses li')) { + const mark = paper.querySelector(`.pz-mark[data-k="${li.dataset.k}"]`); + if (!mark) { li.hidden = true; continue; } + const top = Math.max(floor, mark.offsetTop + 4); + li.style.top = `${top}px`; + floor = top + 40; + } +} + function finish() { const { letter } = state; render(); - $('signature').textContent = letter.from ? `— ${letter.from}` : ''; + $('signature').textContent = letter.from || ''; + const reveal = document.getElementById('hl-reveal'); + if (reveal) reveal.setAttribute('width', '640'); document.body.classList.add('finished'); const s = summarize(letter); + const base = state.baseline; + const who = letter.from ? esc(letter.from) : 'They'; const rows = []; - rows.push(`

    Written in ${formatDuration(s.writtenMs)}, ${s.words} words.

    `); - if (s.low > 0 && s.high > 0) rows.push(`

    ${esc(letter.from || 'Their')}${letter.from ? '’s' : ''} heart ran between ${s.low} and ${s.high} beats a minute while writing.

    `); - if (s.quickest) rows.push(`

    Quickest at ${esc(excerpt(s.quickest.text))} ${s.quickest.bpm} bpm

    `); - if (s.steadiest) rows.push(`

    Steadiest at ${esc(excerpt(s.steadiest.text))} ${s.steadiest.bpm} bpm

    `); - if (s.smiles) rows.push(`

    ${letter.from ? esc(letter.from) : 'They'} smiled ${s.smiles === 1 ? 'once' : `${s.smiles} times`}; the words glow where.

    `); - if (s.tookBack) rows.push(`

    ${s.tookBack} characters were written and taken back.

    `); + const lead = [`Written in ${formatDuration(s.writtenMs)}`, `${s.words} words`]; + rows.push(`

    ${lead.join(', ')}.

    `); + if (s.quickest) rows.push(`

    Quickest at ${esc(excerpt(s.quickest.text))}${s.quickest.bpm} a minute

    `); + if (s.steadiest) rows.push(`

    Steadiest at ${esc(excerpt(s.steadiest.text))}${s.steadiest.bpm} a minute

    `); + const extra = []; + if (s.smiles) extra.push(`${who} smiled ${s.smiles === 1 ? 'once' : s.smiles === 2 ? 'twice' : `${s.smiles} times`}, and the words glow where.`); + if (s.tookBack) extra.push(`${s.tookBack} characters were written and taken back.`); + if (extra.length) rows.push(`

    ${extra.join(' ')}

    `); $('summary-body').innerHTML = rows.join(''); $('summary').hidden = false; $('copy-text').onclick = async () => { @@ -210,6 +250,7 @@ function wireControls() { startReplay(state.letter); }); window.addEventListener('hashchange', () => location.reload()); + window.addEventListener('resize', () => { if (state.player) placeNotes(); }); } if (typeof document !== 'undefined' && !globalThis.__HW_NO_AUTOBOOT__) { diff --git a/src/shared/heartline.js b/src/shared/heartline.js new file mode 100644 index 0000000..72a8a00 --- /dev/null +++ b/src/shared/heartline.js @@ -0,0 +1,70 @@ +// heartline.js +// The whole letter's heart rate drawn as one thin line of ink, the way a pen +// would trace it under a signature. Each stretch takes the ink colour the +// heart gave the words written then. Returns an SVG string. + +import { inkFor } from './playback.js'; +import { median } from './summary.js'; + +function smooth(xs, r) { + return xs.map((_, i) => { + let s = 0; let n = 0; + for (let k = i - r; k <= i + r; k++) if (xs[k] > 0) { s += xs[k]; n++; } + return n ? s / n : 0; + }); +} + +/** + * @param {object} letter decoded letter + * @param {object} [o] + * @param {number} [o.width=640] + * @param {number} [o.height=56] + * @param {string} [o.id='hl'] prefix for ids inside the SVG + * @param {number} [o.stroke=1.6] + */ +export function heartlineSvg(letter, { width = 640, height = 56, id = 'hl', stroke = 1.6 } = {}) { + const raw = letter.vit.bpm || []; + const base = median(raw); + const pts = smooth(raw, Math.max(1, Math.round((letter.vit.hz || 4) * 1.5))); + const live = pts.filter((v) => v > 0); + if (live.length < 4) return ''; + let lo = Math.min(...live); let hi = Math.max(...live); + if (hi - lo < 8) { const m = (hi + lo) / 2; lo = m - 4; hi = m + 4; } + const pad = stroke * 2; + const n = pts.length; + const x = (i) => (n > 1 ? (i / (n - 1)) * width : 0); + const y = (v) => pad + (1 - (v - lo) / (hi - lo)) * (height - pad * 2); + + // Group into runs of about one colour so the SVG stays small. + const step = Math.max(1, Math.floor(n / 160)); + const segs = []; + let cur = null; + for (let i = 0; i < n; i += step) { + const v = pts[i]; + if (!(v > 0)) { cur = null; continue; } + const colour = inkFor(raw[i] > 0 ? raw[i] : v, base); + const p = `${x(i).toFixed(1)} ${y(v).toFixed(1)}`; + if (cur && cur.colour === colour) cur.d.push(p); + else { + const d = cur ? [cur.d[cur.d.length - 1], p] : [p]; + cur = { colour, d }; + segs.push(cur); + } + } + const paths = segs + .filter((s) => s.d.length > 1) + .map((s) => ``) + .join(''); + return `` + + `` + + `${paths}` + + ''; +} + +/** Fraction of the heartline that covers the writer's time t (ms). */ +export function heartlineAt(letter, t) { + const n = (letter.vit.bpm || []).length; + if (n < 2) return 1; + const i = (t / 1000) * (letter.vit.hz || 4); + return Math.max(0, Math.min(1, i / (n - 1))); +} diff --git a/src/shared/playback.js b/src/shared/playback.js index d5fd830..fbe7e0f 100644 --- a/src/shared/playback.js +++ b/src/shared/playback.js @@ -122,17 +122,23 @@ function splitUnits(s, t) { /** * Ink colour for a heartbeat relative to the writer's own resting level. - * Calm is deep ink blue, a quickened heart turns the ink warm red. + * At rest the ink is iron-gall blue-black. A quicker heart warms it through + * claret to vermilion; a slower one cools it to a clear blue-black. */ +export const INK = { + calm: [28, 34, 52], + warm: [[112, 28, 48], [184, 52, 28]], + cool: [32, 72, 150], +}; + export function inkFor(bpm, baseline) { - if (!(bpm > 0) || !(baseline > 0)) return 'rgb(31, 41, 66)'; - const d = Math.max(-1, Math.min(1, (bpm - baseline) / 18)); - const calm = [31, 41, 66]; - const warm = [168, 38, 44]; - const cool = [24, 54, 92]; - const [a, b, f] = d >= 0 ? [calm, warm, d] : [calm, cool, -d]; - const mix = a.map((x, k) => Math.round(x + (b[k] - x) * f)); - return `rgb(${mix[0]}, ${mix[1]}, ${mix[2]})`; + const rgb = (c) => `rgb(${c[0]}, ${c[1]}, ${c[2]})`; + if (!(bpm > 0) || !(baseline > 0)) return rgb(INK.calm); + const d = Math.max(-1, Math.min(1, (bpm - baseline) / 16)); + const mix = (a, b, f) => a.map((x, k) => Math.round(x + (b[k] - x) * f)); + if (d < 0) return rgb(mix(INK.calm, INK.cool, -d)); + if (d <= 0.5) return rgb(mix(INK.calm, INK.warm[0], d / 0.5)); + return rgb(mix(INK.warm[0], INK.warm[1], (d - 0.5) / 0.5)); } /** Final characters of the letter, each with the time it was typed. */ diff --git a/src/writer/index.html b/src/writer/index.html index d7d49e9..9ec151a 100644 --- a/src/writer/index.html +++ b/src/writer/index.html @@ -3,6 +3,7 @@ + Heartwritten @@ -11,17 +12,22 @@