Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Gradient-to-gradient morphing; props panel always reflects current values
nyblnet753919eedb6f
diff --git a/CLAUDE.md b/CLAUDE.mdindex a035a6a7..9c0b7b40 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -13,7 +13,9 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.Reveal sections. Elements carry `data-el-id` (editing) and `data-flip-id` (morph).- `src/present.ts` — Reveal.js overlay; slides with `transition:'morph'` use GSAP Flip:matched `data-flip-id` elements animate geometry via Flip, style props (fill/color)- tween straight from the model values. Elements carry `fx` (enter stagger — equal+ tween straight from the model values — including gradients (stop colors + line+ coords tween; solid⇄gradient fabricates/collapses a temp gradient; from-colors+ are the other side sampled at matching stop positions). Elements carry `fx` (enter stagger — equal`order` = simultaneous, countUp, ken-burns (`fx.ken` dir drift/out/in + zoom %/secs;out/in are one-shot settles per slide entry), `loop` dash-march/motion-path), `link`(click → slide id) and `group`; slides can set `hover:'focus-group'` (dim otherdiff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.tsindex d64430ce..fb0008ad 100644--- a/slides/src/editor/panels.ts+++ b/slides/src/editor/panels.ts@@ -14,12 +14,22 @@ export class PropsPanel {private host: HTMLElement,private store: Store,) {- store.on('selection', () => this.rebuild())- store.on('current', () => this.rebuild())+ // Selection/slide switches always rebuild — the user acted outside the+ // panel, so whatever input was focused is obsolete. Doc mutations respect+ // a focused input (skip + mark stale) and catch up when focus leaves.+ store.on('selection', () => this.rebuild(true))+ store.on('current', () => this.rebuild(true))store.on('doc', () => this.rebuild())+ this.host.addEventListener('focusout', () => {+ setTimeout(() => {+ if (this.stale && !this.host.matches(':focus-within')) this.rebuild()+ }, 0)+ })this.rebuild()}+ private stale = false+/** checkpoint once per burst of continuous input, commit on change. */private edit(mutate: () => void, final: boolean) {if (!this.burst) {@@ -31,8 +41,12 @@ export class PropsPanel {if (final) this.burst = false}- private rebuild() {- if (this.host.matches(':focus-within')) return // don't rip inputs out from under the user+ private rebuild(force = false) {+ if (!force && this.host.matches(':focus-within')) {+ this.stale = true // don't rip inputs out from under the user; catch up on focusout+ return+ }+ this.stale = falsethis.burst = falsethis.host.innerHTML = ''const els = this.store.selectedElementsdiff --git a/slides/src/main.ts b/slides/src/main.tsindex 6864a3fd..f70081b1 100644--- a/slides/src/main.ts+++ b/slides/src/main.ts@@ -2,6 +2,7 @@// mutation — the captured copy is what gets re-serialized on save.import './styles.css'+import { gsap } from 'gsap'import { capturePristine, readEmbeddedDoc, serializeFile } from './save'import { parseDoc, starterDoc } from './model'import { injectFonts } from './fonts'@@ -55,4 +56,6 @@ if (location.hash === '#present') {get selection() {return store.selection.slice()},+ /** animation engine, exposed for scripting/diagnostics */+ gsap,}diff --git a/slides/src/present.ts b/slides/src/present.tsindex 75f2e40b..70563554 100644--- a/slides/src/present.ts+++ b/slides/src/present.ts@@ -8,8 +8,8 @@ import 'reveal.js/dist/reveal.css'import { gsap } from 'gsap'import { Flip } from 'gsap/Flip'import { MotionPathPlugin } from 'gsap/MotionPathPlugin'-import type { BentoDoc, Slide, SlideElement } from './model'-import { applyElementFrame, renderSlide } from './render'+import type { BentoDoc, GradientFill, ShapeElement, Slide, SlideElement } from './model'+import { applyElementFrame, gradientLineCoords, renderSlide } from './render'gsap.registerPlugin(Flip, MotionPathPlugin)@@ -512,15 +512,9 @@ function runMorph(if (a.opacity !== b.opacity) {gsap.fromTo(to, { opacity: a.opacity }, { opacity: b.opacity, duration: MORPH_DURATION, ease: MORPH_EASE })}- if (a.type === 'shape' && b.type === 'shape' && a.fill !== b.fill) {- const target = to.querySelector('rect,ellipse,polygon,line,path')- if (target) {- gsap.fromTo(- target,- { attr: { fill: a.fill } },- { attr: { fill: b.fill }, duration: MORPH_DURATION, ease: MORPH_EASE },- )- }+ if (a.type === 'shape' && b.type === 'shape') {+ const target = to.querySelector<SVGElement>('rect,ellipse,polygon,line,path')+ if (target) morphShapeFill(target, a, b)}if (a.type === 'text' && b.type === 'text' && a.color !== b.color) {const inner = to.querySelector<HTMLElement>('.bento-text-inner')@@ -530,3 +524,117 @@ function runMorph(}}}++// --- fill morphing (solid ⇄ solid, solid ⇄ gradient, gradient ⇄ gradient) ----++const SVG_NS = 'http://www.w3.org/2000/svg'+let morphGradSeq = 0++/** Any solid CSS color we author (#hex / rgb / rgba) → [r, g, b, a]. */+function colorParts(v: string): [number, number, number, number] {+ const m = v?.match(/rgba?\(([^)]+)\)/)+ if (m) {+ const p = m[1].split(/[\s,/]+/).map(Number)+ return [p[0] || 0, p[1] || 0, p[2] || 0, Number.isFinite(p[3]) ? p[3] : 1]+ }+ let hex = (v ?? '').trim()+ if (/^#[0-9a-fA-F]{3}$/.test(hex)) hex = '#' + [...hex.slice(1)].map((c) => c + c).join('')+ if (/^#[0-9a-fA-F]{6,8}$/.test(hex)) {+ return [+ parseInt(hex.slice(1, 3), 16),+ parseInt(hex.slice(3, 5), 16),+ parseInt(hex.slice(5, 7), 16),+ hex.length === 9 ? parseInt(hex.slice(7, 9), 16) / 255 : 1,+ ]+ }+ return [0, 0, 0, v === 'transparent' || v === 'none' ? 0 : 1]+}++const rgbaStr = (c: [number, number, number, number]) =>+ `rgba(${Math.round(c[0])}, ${Math.round(c[1])}, ${Math.round(c[2])}, ${Math.round(c[3] * 1000) / 1000})`++/** Color of a gradient evaluated at position t (piecewise-linear between stops). */+function sampleGradient(stops: GradientFill['stops'], t: number): string {+ const s = [...stops].sort((x, y) => x.at - y.at)+ if (t <= s[0].at) return rgbaStr(colorParts(s[0].color))+ for (let i = 0; i < s.length - 1; i++) {+ const a = s[i]+ const b = s[i + 1]+ if (t <= b.at) {+ const f = b.at === a.at ? 0 : (t - a.at) / (b.at - a.at)+ const ca = colorParts(a.color)+ const cb = colorParts(b.color)+ return rgbaStr([0, 1, 2, 3].map((k) => ca[k] + (cb[k] - ca[k]) * f) as [number, number, number, number])+ }+ }+ return rgbaStr(colorParts(s[s.length - 1].color))+}++/**+ * Tween a shape's fill from element a's to element b's. Solids tween the fill+ * attribute; when a gradient is involved the tween runs on the <stop> nodes+ * (colors sampled from the other side at matching positions) and on the+ * gradient line, so angle changes sweep too. A solid destination gets a+ * temporary gradient that collapses to the flat color and is then removed.+ */+function morphShapeFill(target: SVGElement, a: ShapeElement, b: ShapeElement) {+ if (a.fill === b.fill && JSON.stringify(a.fillGradient) === JSON.stringify(b.fillGradient)) return+ const ag = a.fillGradient?.stops.length ? a.fillGradient : undefined+ const bg = b.fillGradient?.stops.length ? b.fillGradient : undefined+ if (!ag && !bg) {+ if (a.fill !== b.fill) {+ gsap.fromTo(target, { attr: { fill: a.fill } }, { attr: { fill: b.fill }, duration: MORPH_DURATION, ease: MORPH_EASE })+ }+ return+ }+ const svg = target.ownerSVGElement+ if (!svg) return++ let lin = svg.querySelector('linearGradient')+ if (!lin) {+ // destination is solid — fabricate a gradient shaped like the source so+ // there is something to tween through, then collapse it to b.fill+ lin = document.createElementNS(SVG_NS, 'linearGradient')+ lin.id = `bento-morph-grad-${morphGradSeq++}`+ for (const s of ag!.stops) {+ const stop = document.createElementNS(SVG_NS, 'stop')+ stop.setAttribute('offset', String(s.at))+ lin.appendChild(stop)+ }+ const defs = document.createElementNS(SVG_NS, 'defs')+ defs.appendChild(lin)+ svg.appendChild(defs)+ target.setAttribute('fill', `url(#${lin.id})`)+ }++ const stops = [...lin.querySelectorAll('stop')]+ // per rendered stop: where it sits, what it starts as, what it ends as+ const finals = bg ? bg.stops : ag!.stops.map((s) => ({ at: s.at, color: b.fill }))+ stops.forEach((node, i) => {+ const at = finals[i]?.at ?? 1+ const fromColor = ag ? sampleGradient(ag.stops, at) : rgbaStr(colorParts(a.fill))+ const toColor = finals[i]?.color ?? b.fill+ gsap.fromTo(+ node,+ { attr: { 'stop-color': fromColor } },+ {+ attr: { 'stop-color': toColor },+ duration: MORPH_DURATION,+ ease: MORPH_EASE,+ ...(i === 0 && !bg+ ? {+ // solid destination: swap the temp gradient back to a flat fill+ onComplete: () => {+ target.setAttribute('fill', b.fill)+ lin!.parentElement?.remove()+ },+ }+ : {}),+ },+ )+ })++ const fromLine = gradientLineCoords((ag ?? bg)!.angle)+ const toLine = gradientLineCoords((bg ?? ag)!.angle)+ gsap.fromTo(lin, { attr: fromLine }, { attr: toLine, duration: MORPH_DURATION, ease: MORPH_EASE })+}diff --git a/slides/src/render.ts b/slides/src/render.tsindex fefad3a9..629b3717 100644--- a/slides/src/render.ts+++ b/slides/src/render.ts@@ -73,20 +73,26 @@ export function applyElementFrame(node: HTMLElement, el: SlideElement) {// url(#…) references resolve document-wide.let gradSeq = 0+/** Gradient line endpoints (objectBoundingBox units) for a CSS-convention+ * angle: 0deg points up, 90deg points right. Shared with morph tweening. */+export function gradientLineCoords(angle: number) {+ const rad = ((angle ?? 180) * Math.PI) / 180+ const dx = Math.sin(rad) / 2+ const dy = -Math.cos(rad) / 2+ return { x1: 0.5 - dx, y1: 0.5 - dy, x2: 0.5 + dx, y2: 0.5 + dy }+}+/** Materialize a GradientFill as a <defs> gradient; returns its url() ref. */function gradientRef(svg: SVGSVGElement, g: NonNullable<ShapeElement['fillGradient']>): string {const id = `bento-grad-${gradSeq++}`const defs = document.createElementNS(SVG_NS, 'defs')const lin = document.createElementNS(SVG_NS, 'linearGradient')lin.setAttribute('id', id)- // CSS angle convention: 0deg points up, 90deg points right- const rad = ((g.angle ?? 180) * Math.PI) / 180- const dx = Math.sin(rad) / 2- const dy = -Math.cos(rad) / 2- lin.setAttribute('x1', String(0.5 - dx))- lin.setAttribute('y1', String(0.5 - dy))- lin.setAttribute('x2', String(0.5 + dx))- lin.setAttribute('y2', String(0.5 + dy))+ const { x1, y1, x2, y2 } = gradientLineCoords(g.angle)+ lin.setAttribute('x1', String(x1))+ lin.setAttribute('y1', String(y1))+ lin.setAttribute('x2', String(x2))+ lin.setAttribute('y2', String(y2))for (const s of g.stops) {const stop = document.createElementNS(SVG_NS, 'stop')stop.setAttribute('offset', String(Math.min(Math.max(s.at, 0), 1)))