Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Element fx (enter stagger, count-up, ken-burns) and slide links
nyblnet5cbb0ee62e3d
diff --git a/slides/src/model.ts b/slides/src/model.tsindex 1455bf2c..725f2ca7 100644--- a/slides/src/model.ts+++ b/slides/src/model.ts@@ -16,6 +16,19 @@ export interface ElementBase {/** degrees, clockwise */rotation: numberopacity: number+ /** presentation effects, run in present mode only */+ fx?: {+ /** entrance animation when the slide is shown */+ enter?: 'fade-up' | 'fade'+ /** stagger position within the slide's entrance sequence */+ 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'+ }+ /** while presenting, clicking this element jumps to the slide with this id */+ link?: string}export interface TextElement extends ElementBase {diff --git a/slides/src/present.ts b/slides/src/present.tsindex 51bb6096..eb2e7bf3 100644--- a/slides/src/present.ts+++ b/slides/src/present.ts@@ -7,7 +7,7 @@ import RevealNotes from 'reveal.js/plugin/notes/notes'import 'reveal.js/dist/reveal.css'import { gsap } from 'gsap'import { Flip } from 'gsap/Flip'-import type { BentoDoc, SlideElement } from './model'+import type { BentoDoc, Slide, SlideElement } from './model'import { renderSlide } from './render'gsap.registerPlugin(Flip)@@ -100,17 +100,33 @@ export function startPresentation(deck.on('slidechanged', ((event: any) => {const from = event.previousSlide as HTMLElement | undefinedconst to = event.currentSlide as HTMLElement- if (!from || !to) return- const fromIdx = [...slidesEl.children].indexOf(from)+ if (!to) return+ const fromIdx = from ? [...slidesEl.children].indexOf(from) : -1const toIdx = [...slidesEl.children].indexOf(to)+ if (from) gsap.killTweensOf(from.querySelectorAll('.bento-el'))const forward = toIdx > fromIdx// Morph forward into a morph slide, and un-morph when backing out of one.const morphing =- (forward && doc.slides[toIdx]?.transition === 'morph') ||- (!forward && doc.slides[fromIdx]?.transition === 'morph')- if (morphing) runMorph(doc, from, to, fromIdx, toIdx)+ from &&+ ((forward && doc.slides[toIdx]?.transition === 'morph') ||+ (!forward && doc.slides[fromIdx]?.transition === 'morph'))+ if (morphing) runMorph(doc, from!, to, fromIdx, toIdx)+ else runEnterFx(doc.slides[toIdx], to)+ runAmbientFx(doc.slides[toIdx], to)}) as any)+ // Clicking an element with a link jumps to its target slide.+ slidesEl.addEventListener('click', (ev) => {+ const target = (ev.target as HTMLElement).closest<HTMLElement>('[data-link]')+ if (!target) return+ const idx = doc.slides.findIndex((s) => s.id === target.dataset.link)+ if (idx >= 0) {+ ev.preventDefault()+ ev.stopPropagation()+ deck.slide(idx, 0)+ }+ })+deck.initialize().then(() => {if (startIndex > 0) deck.slide(startIndex, 0)// late layout: fonts/images that finish loading after init can change@@ -118,11 +134,93 @@ export function startPresentation(window.addEventListener('resize', onResize)setTimeout(onResize, 120)setTimeout(onResize, 600)+ const first = slidesEl.children[startIndex] as HTMLElement | undefined+ if (first) {+ runEnterFx(doc.slides[startIndex], first)+ runAmbientFx(doc.slides[startIndex], first)+ }})return { exit }}+// --- element fx -------------------------------------------------------------++function fxNodes(slide: Slide, section: HTMLElement): Array<[SlideElement, HTMLElement]> {+ const pairs: Array<[SlideElement, HTMLElement]> = []+ for (const el of slide?.elements ?? []) {+ if (!el.fx) continue+ const node = section.querySelector<HTMLElement>(`[data-el-id="${CSS.escape(el.id)}"]`)+ if (node) pairs.push([el, node])+ }+ return pairs+}++/** Staggered entrance animations + count-ups for the incoming slide. */+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))+ entering.forEach(([el, node], i) => {+ const fx = el.fx!+ if (fx.enter) {+ gsap.fromTo(+ node,+ { opacity: 0, y: fx.enter === 'fade-up' ? 16 : 0 },+ {+ opacity: el.opacity,+ y: 0,+ duration: 0.55,+ delay: 0.12 + Math.min(i, 24) * 0.045,+ ease: 'power2.out',+ overwrite: 'auto',+ },+ )+ }+ if (fx.countUp) runCountUp(node)+ })+}++/** Animate every number in the element's text from 0 to its final value. */+function runCountUp(node: HTMLElement) {+ const inner = node.querySelector<HTMLElement>('.bento-text-inner') ?? node+ const final = inner.textContent ?? ''+ const tokens = [...final.matchAll(/\d+(?:[.,]\d+)?/g)]+ if (!tokens.length) return+ const state = { p: 0 }+ gsap.to(state, {+ p: 1,+ duration: 1.15,+ delay: 0.15,+ ease: 'power2.out',+ onUpdate() {+ let out = ''+ let last = 0+ for (const m of tokens) {+ out += final.slice(last, m.index)+ const raw = m[0].replace(',', '.')+ const decimals = raw.includes('.') ? raw.split('.')[1].length : 0+ out += (parseFloat(raw) * state.p).toFixed(decimals)+ last = m.index! + m[0].length+ }+ inner.textContent = out + final.slice(last)+ },+ })+}++/** Continuous ambient motion (ken-burns style slow zoom on photos). */+function runAmbientFx(slide: Slide, section: HTMLElement) {+ for (const [el, node] of fxNodes(slide, section)) {+ if (el.fx!.ambient === 'kenburns') {+ gsap.fromTo(+ node,+ { scale: 1.02 },+ { scale: 1.1, duration: 26, ease: 'none', repeat: -1, yoyo: true, transformOrigin: '50% 40%' },+ )+ }+ }+}+// --- morph ------------------------------------------------------------------function elementsById(root: HTMLElement): Map<string, HTMLElement> {diff --git a/slides/src/render.ts b/slides/src/render.tsindex 04d7ea63..0bdfde9f 100644--- a/slides/src/render.ts+++ b/slides/src/render.ts@@ -122,6 +122,7 @@ export function renderElement(el: SlideElement, doc: BentoDoc): HTMLElement {node.className = `bento-el bento-el-${el.type}`node.dataset.elId = el.idnode.dataset.flipId = el.id+ if (el.link) node.dataset.link = el.linkapplyElementFrame(node, el)switch (el.type) {diff --git a/slides/src/styles.css b/slides/src/styles.cssindex 79a9e4cf..e29a55e9 100644--- a/slides/src/styles.css+++ b/slides/src/styles.css@@ -345,6 +345,7 @@ html, body {--r-overlay-element-fg-color: 0, 0, 0;}.bento-present-overlay .reveal .slides section { padding: 0; }+.bento-present-overlay .bento-el[data-link] { cursor: pointer; }/* moveable accents */.moveable-control-box {