Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Editable animated diagrams: path shapes, groups, loop fx, hover focus

nyblnet20031a84164a
diff --git a/slides/src/model.ts b/slides/src/model.ts
index 1591919e..ea409089 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -20,15 +20,21 @@ export interface ElementBase {
fx?: {
/** entrance animation when the slide is shown */
enter?: 'fade-up' | 'fade'
- /** stagger position within the slide's entrance sequence */
+ /** stagger step within the entrance sequence; equal values enter together */
order?: number
/** animate numeric parts of the text from 0 to their final value */
countUp?: boolean
/** continuous ambient motion (slow zoom, for full-bleed photos) */
ambient?: 'kenburns'
+ /** continuous looping animation */
+ loop?:
+ | { type: 'dash-march'; distance?: number; duration?: number }
+ | { type: 'motion-path'; path: string; duration: number; delay?: number }
}
/** while presenting, clicking this element jumps to the slide with this id */
link?: string
+ /** semantic group tag — hover focus and multi-element behaviours target it */
+ group?: string
}
export interface TextElement extends ElementBase {
@@ -46,7 +52,7 @@ export interface TextElement extends ElementBase {
letterSpacing?: number
}
-export type ShapeKind = 'rect' | 'ellipse' | 'triangle' | 'arrow' | 'line'
+export type ShapeKind = 'rect' | 'ellipse' | 'triangle' | 'arrow' | 'line' | 'path'
export interface ShapeElement extends ElementBase {
type: 'shape'
@@ -58,6 +64,10 @@ export interface ShapeElement extends ElementBase {
radius: number
/** dash length in px; 0/undefined = solid stroke */
strokeDash?: number
+ /** path only: SVG path data in the coordinate space given by pathBox */
+ d?: string
+ /** path only: [x, y, w, h] viewBox the path was authored in */
+ pathBox?: [number, number, number, number]
}
export interface ImageElement extends ElementBase {
@@ -89,6 +99,8 @@ export interface Slide {
transition: TransitionKind
elements: SlideElement[]
notes: string
+ /** present-mode hover behaviour: focus the hovered element's group */
+ hover?: { type: 'focus-group'; dim?: number }
}
export interface BentoDoc {
diff --git a/slides/src/present.ts b/slides/src/present.ts
index 55758c18..5534b74e 100644
--- a/slides/src/present.ts
+++ b/slides/src/present.ts
@@ -7,10 +7,11 @@ import RevealNotes from 'reveal.js/plugin/notes/notes'
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 { renderSlide } from './render'
-gsap.registerPlugin(Flip)
+gsap.registerPlugin(Flip, MotionPathPlugin)
const MORPH_DURATION = 0.65
const MORPH_EASE = 'power2.inOut'
@@ -27,6 +28,9 @@ export function startPresentation(
const overlay = document.createElement('div')
overlay.className = 'bento-present-overlay'
overlay.style.setProperty('--bento-accent', doc.theme.accent)
+ // Reveal ignores key events originating from form fields. If focus is still
+ // on an editor input (title, notes…) when the show starts, arrows go dead.
+ ;(document.activeElement as HTMLElement | null)?.blur?.()
const revealEl = document.createElement('div')
revealEl.className = 'reveal'
@@ -87,13 +91,27 @@ export function startPresentation(
onExit(last)
}
- // Esc exits the show (capture phase so Reveal's own Esc handling never sees it).
+ // Capture-phase keys: Esc exits; arrows navigate unconditionally. Reveal
+ // drops key events when focus sits in odd places (a leftover form field, a
+ // host-embedded frame) — present mode has no fields, so arrows are always
+ // navigation. Handled here exclusively (stopPropagation avoids double-steps).
const onKeydown = (ev: KeyboardEvent) => {
if (ev.key === 'Escape') {
if (deck.isOverview()) return // let Reveal close its overview first
ev.preventDefault()
ev.stopPropagation()
exit()
+ return
+ }
+ const key = ev.key || ({ 37: 'ArrowLeft', 39: 'ArrowRight', 33: 'PageUp', 34: 'PageDown' } as Record<number, string>)[ev.keyCode]
+ if (key === 'ArrowRight' || key === 'PageDown') {
+ ev.preventDefault()
+ ev.stopPropagation()
+ deck.next()
+ } else if (key === 'ArrowLeft' || key === 'PageUp') {
+ ev.preventDefault()
+ ev.stopPropagation()
+ deck.prev()
}
}
document.addEventListener('keydown', onKeydown, true)
@@ -115,6 +133,7 @@ export function startPresentation(
else runEnterFx(doc.slides[toIdx], to)
runAmbientFx(doc.slides[toIdx], to)
restartSvgAnimations(to)
+ wireHoverFocus(doc.slides[toIdx], to)
}) as any)
// Clicking an element with a link jumps to its target slide.
@@ -141,6 +160,7 @@ export function startPresentation(
runEnterFx(doc.slides[startIndex], first)
runAmbientFx(doc.slides[startIndex], first)
restartSvgAnimations(first)
+ wireHoverFocus(doc.slides[startIndex], first)
}
})
@@ -164,8 +184,11 @@ function runEnterFx(slide: Slide, section: HTMLElement) {
const entering = fxNodes(slide, section)
.filter(([el]) => el.fx!.enter || el.fx!.countUp)
.sort((a, b) => (a[0].fx!.order ?? 0) - (b[0].fx!.order ?? 0))
+ // Delay derives from fx.order when set (equal order ⇒ elements enter
+ // together — how a diagram reveals band-by-band), else from list position.
entering.forEach(([el, node], i) => {
const fx = el.fx!
+ const step = fx.order ?? i
if (fx.enter) {
gsap.fromTo(
node,
@@ -174,7 +197,7 @@ function runEnterFx(slide: Slide, section: HTMLElement) {
opacity: el.opacity,
y: 0,
duration: 0.55,
- delay: 0.12 + Math.min(i, 24) * 0.045,
+ delay: 0.12 + Math.min(step, 24) * 0.05,
ease: 'power2.out',
overwrite: 'auto',
},
@@ -221,19 +244,64 @@ function restartSvgAnimations(section: HTMLElement) {
}
}
-/** Continuous ambient motion (ken-burns style slow zoom on photos). */
+/** Continuous motion: ken-burns zoom, marching dashes, dots along paths. */
function runAmbientFx(slide: Slide, section: HTMLElement) {
for (const [el, node] of fxNodes(slide, section)) {
- if (el.fx!.ambient === 'kenburns') {
+ const fx = el.fx!
+ if (fx.ambient === 'kenburns') {
gsap.fromTo(
node,
{ scale: 1.02 },
{ scale: 1.1, duration: 26, ease: 'none', repeat: -1, yoyo: true, transformOrigin: '50% 40%' },
)
}
+ if (fx.loop?.type === 'dash-march') {
+ const target = node.querySelector('path, line, rect, ellipse, polygon')
+ if (target) {
+ gsap.fromTo(
+ target,
+ { strokeDashoffset: fx.loop.distance ?? 18 },
+ { strokeDashoffset: 0, duration: fx.loop.duration ?? 1.4, ease: 'none', repeat: -1 },
+ )
+ }
+ }
+ if (fx.loop?.type === 'motion-path') {
+ gsap.to(node, {
+ motionPath: { path: fx.loop.path },
+ duration: fx.loop.duration,
+ delay: fx.loop.delay ?? 0,
+ ease: 'none',
+ repeat: -1,
+ })
+ }
}
}
+/**
+ * Hover focus: on slides with hover:'focus-group', pointing at any grouped
+ * element dims every element belonging to a different group.
+ */
+function wireHoverFocus(slide: Slide, section: HTMLElement) {
+ if (slide?.hover?.type !== 'focus-group' || section.dataset.hoverWired) return
+ section.dataset.hoverWired = '1'
+ const dim = slide.hover.dim ?? 0.13
+ let current: string | null = null
+ const apply = (group: string | null) => {
+ if (group === current) return
+ current = group
+ for (const node of section.querySelectorAll<HTMLElement>('[data-group]')) {
+ const other = group !== null && node.dataset.group !== group
+ node.style.transition = 'opacity .25s ease'
+ node.style.opacity = other ? String(dim) : ''
+ }
+ }
+ section.addEventListener('mouseover', (ev) => {
+ const hit = (ev.target as HTMLElement).closest<HTMLElement>('[data-group]')
+ apply(hit ? hit.dataset.group! : null)
+ })
+ section.addEventListener('mouseleave', () => apply(null))
+}
+
// --- morph ------------------------------------------------------------------
function elementsById(root: HTMLElement): Map<string, HTMLElement> {
@@ -280,7 +348,7 @@ function runMorph(
y: 14,
duration: 0.45,
delay: MORPH_DURATION * 0.4,
- stagger: 0.03,
+ stagger: { amount: Math.min(0.45, entering.length * 0.03) },
ease: 'power2.out',
clearProps: 'opacity,transform',
})
@@ -325,7 +393,7 @@ function runMorph(
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')
+ const target = to.querySelector('rect,ellipse,polygon,line,path')
if (target) {
gsap.fromTo(
target,
diff --git a/slides/src/render.ts b/slides/src/render.ts
index 261043c8..34d9d30d 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -79,6 +79,14 @@ export function shapeSvg(el: ShapeElement): SVGSVGElement {
let node: SVGElement
switch (el.shape) {
+ case 'path': {
+ // arbitrary vector data, stretched from its authored viewBox into the box
+ if (el.pathBox) svg.setAttribute('viewBox', el.pathBox.join(' '))
+ node = document.createElementNS(SVG_NS, 'path')
+ node.setAttribute('d', el.d ?? '')
+ if (sw > 0) node.setAttribute('vector-effect', 'non-scaling-stroke')
+ break
+ }
case 'rect': {
node = document.createElementNS(SVG_NS, 'rect')
node.setAttribute('x', String(inset))
@@ -177,6 +185,7 @@ export function renderElement(el: SlideElement, doc: BentoDoc, opts: RenderOpts
node.dataset.elId = el.id
node.dataset.flipId = el.id
if (el.link) node.dataset.link = el.link
+ if (el.group) node.dataset.group = el.group
applyElementFrame(node, el)
switch (el.type) {