Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Chart data morphing, line styles, and line tip decorations

nyblnet2494e8c67622
diff --git a/CLAUDE.md b/CLAUDE.md
index 8d1c79bc..98409a62 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -40,6 +40,13 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.
exit. Live node exposes `__bentoChart` for scripting. Panel: preset select
re-seeds the option; JSON textarea is the escape hatch. NOTICE block in
index.html carries MIT + Apache/BSD notices into every saved document.
+ Charts on morph/state transitions data-morph: the incoming chart paints the
+ outgoing side's option first, then setOption's to its own with
+ universalTransition (values tween in place, bar⇄pie works). Shapes:
+ `strokeStyle` solid/dashed/dotted (legacy `strokeDash` still honoured);
+ line shapes have `lineStart`/`lineEnd` tips (arrow/dot/bar) rendered as
+ per-instance svg markers (sized in strokeWidth units, endpoints inset);
+ line color morphs tween the STROKE attr (lines paint stroke, not fill).
- **Diagram philosophy**: complex diagrams are ordinary Bento elements (rects, texts,
`path` shapes) with groups — interactivity = linked state slides + morph (filters,
era sequences), hover = focus-group, motion = fx.loop. Opaque `svg` elements are
diff --git a/slides/src/charts.ts b/slides/src/charts.ts
index 53e06000..3a1c1489 100644
--- a/slides/src/charts.ts
+++ b/slides/src/charts.ts
@@ -19,13 +19,14 @@ import {
TooltipComponent,
} from 'echarts/components'
import { SVGRenderer } from 'echarts/renderers'
+import { UniversalTransition } from 'echarts/features'
import type { ChartElement } from './model'
echarts.use([
BarChart, LineChart, PieChart, ScatterChart,
DatasetComponent, DataZoomComponent, GridComponent,
LegendComponent, TitleComponent, TooltipComponent,
- SVGRenderer,
+ SVGRenderer, UniversalTransition,
])
/** Presets seed new charts and let the panel swap types without data loss. */
@@ -76,12 +77,36 @@ export const CHART_PRESETS: Record<string, () => Record<string, unknown>> = {
}),
}
-/** Live instance for present mode. Returns a dispose handle. */
-export function mountChart(el: ChartElement, host: HTMLElement): () => void {
+/** Enable smooth data/type transitions on every series of an option. */
+const withTransition = (option: Record<string, unknown>, animate: boolean): Record<string, unknown> => ({
+ ...option,
+ animation: animate,
+ animationDurationUpdate: 650,
+ animationEasingUpdate: 'quadraticInOut',
+ series: Array.isArray(option.series)
+ ? (option.series as Array<Record<string, unknown>>).map((s) => ({ ...s, universalTransition: true }))
+ : option.series,
+})
+
+/**
+ * Live instance for present mode. Returns a dispose handle. When `fromOption`
+ * is given (morph/state transition with a matching chart on the other side),
+ * the chart first paints that state and then ANIMATES to its own — value
+ * changes tween in place, and universalTransition even morphs across series
+ * types (bar → pie).
+ */
+export function mountChart(el: ChartElement, host: HTMLElement, fromOption?: Record<string, unknown>): () => void {
host.innerHTML = ''
const inst = echarts.init(host, undefined, { renderer: 'svg', width: el.w, height: el.h })
try {
- inst.setOption(el.option as never)
+ if (fromOption && JSON.stringify(fromOption) !== JSON.stringify(el.option)) {
+ inst.setOption(withTransition(fromOption, false) as never)
+ setTimeout(() => {
+ if (!inst.isDisposed()) inst.setOption(withTransition(el.option, true) as never, { notMerge: true })
+ }, 60)
+ } else {
+ inst.setOption(el.option as never)
+ }
} catch {
host.textContent = '⚠ invalid chart option'
}
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.ts
index 5d9193b7..b7b38555 100644
--- a/slides/src/editor/panels.ts
+++ b/slides/src/editor/panels.ts
@@ -3,7 +3,7 @@
// into a single undo checkpoint.
import type { Store } from '../store'
-import { uid, type ChartElement, type ShapeElement, type SlideElement, type TextElement, type TransitionKind } from '../model'
+import { uid, type ChartElement, type LineEnding, type ShapeElement, type SlideElement, type TextElement, type TransitionKind } from '../model'
import { CHART_PRESETS } from '../charts'
import { FONT_CHOICES, firstFamily, injectFonts } from '../fonts'
import { ICONS } from '../icons'
@@ -579,6 +579,21 @@ export class PropsPanel {
this.mutate(el.id, (e) => { (e as ShapeElement).stroke = v }, fin)))
this.row('Stroke width', this.number(el.strokeWidth, 0.5, (v, fin) =>
this.mutate(el.id, (e) => { (e as ShapeElement).strokeWidth = Math.max(v, 0) }, fin)))
+ this.row('Line style', this.select(
+ ['solid', 'dashed', 'dotted'],
+ el.strokeStyle ?? (el.strokeDash ? 'dashed' : 'solid'),
+ (v) => this.mutate(el.id, (e) => {
+ const s = e as ShapeElement
+ s.strokeStyle = v === 'solid' ? undefined : (v as 'dashed' | 'dotted')
+ if (v === 'solid') delete s.strokeDash // clear the legacy dash too
+ }, true)))
+ if (el.shape === 'line') {
+ const ENDINGS = ['none', 'arrow', 'dot', 'bar']
+ this.row('Start tip', this.select(ENDINGS, el.lineStart ?? 'none', (v) =>
+ this.mutate(el.id, (e) => { (e as ShapeElement).lineStart = v === 'none' ? undefined : (v as LineEnding) }, true)))
+ this.row('End tip', this.select(ENDINGS, el.lineEnd ?? 'none', (v) =>
+ this.mutate(el.id, (e) => { (e as ShapeElement).lineEnd = v === 'none' ? undefined : (v as LineEnding) }, true)))
+ }
if (el.shape === 'rect') {
this.row('Corner radius', this.number(el.radius, 1, (v, fin) =>
this.mutate(el.id, (e) => { (e as ShapeElement).radius = Math.max(v, 0) }, fin)))
diff --git a/slides/src/model.ts b/slides/src/model.ts
index a6745289..57f04f58 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -81,6 +81,9 @@ export interface GradientFill {
stops: Array<{ at: number; color: string }>
}
+/** Decoration at a line's tip. Sized relative to the stroke width. */
+export type LineEnding = 'none' | 'arrow' | 'dot' | 'bar'
+
export interface ShapeElement extends ElementBase {
type: 'shape'
shape: ShapeKind
@@ -91,8 +94,13 @@ export interface ShapeElement extends ElementBase {
strokeWidth: number
/** corner radius, rect only */
radius: number
- /** dash length in px; 0/undefined = solid stroke */
+ /** dash length in px; 0/undefined = solid stroke (legacy — see strokeStyle) */
strokeDash?: number
+ /** stroke pattern; wins over strokeDash when set */
+ strokeStyle?: 'solid' | 'dashed' | 'dotted'
+ /** line shape only: tip decorations */
+ lineStart?: LineEnding
+ lineEnd?: LineEnding
/** 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 */
diff --git a/slides/src/present.ts b/slides/src/present.ts
index 1413948e..a4ee5a7b 100644
--- a/slides/src/present.ts
+++ b/slides/src/present.ts
@@ -185,7 +185,7 @@ export function startPresentation(
restartSvgAnimations(to)
wireHoverFocus(doc.slides[toIdx], to)
if (from) disposeLiveCharts(doc.slides[fromIdx], from)
- mountLiveCharts(doc.slides[toIdx], to)
+ mountLiveCharts(doc.slides[toIdx], to, morphing ? doc.slides[fromIdx] : undefined)
}) as any)
// Clicking an element with a link jumps to its target slide.
@@ -227,13 +227,15 @@ export function startPresentation(
// snapshot so the section stays presentable in Reveal's viewDistance cache.
const chartHandles = new WeakMap<HTMLElement, Array<() => void>>()
-function mountLiveCharts(slide: Slide, section: HTMLElement) {
+function mountLiveCharts(slide: Slide, section: HTMLElement, fromSlide?: Slide) {
const handles: Array<() => void> = []
for (const el of slide?.elements ?? []) {
if (el.type !== 'chart') continue
const node = section.querySelector<HTMLElement>(`[data-el-id="${CSS.escape(el.id)}"]`)
if (!node) continue
- const dispose = mountChart(el, node)
+ // a matching chart on the other side of a morph: animate its data over
+ const fromEl = fromSlide?.elements.find((e) => e.id === el.id && e.type === 'chart')
+ const dispose = mountChart(el, node, fromEl && fromEl.type === 'chart' ? fromEl.option : undefined)
handles.push(() => {
dispose()
node.innerHTML = chartSnapshotSvg(el)
@@ -624,6 +626,11 @@ function sampleGradient(stops: GradientFill['stops'], t: number): string {
*/
function morphShapeFill(target: SVGElement, a: ShapeElement, b: ShapeElement) {
if (a.fill === b.fill && JSON.stringify(a.fillGradient) === JSON.stringify(b.fillGradient)) return
+ // line shapes paint with stroke (fill is the line color in the model)
+ if (b.shape === 'line' && target.tagName === 'line') {
+ anim.fromTo(target, { attr: { stroke: a.fill } }, { attr: { stroke: b.fill }, duration: MORPH_DURATION, ease: MORPH_EASE })
+ return
+ }
const ag = a.fillGradient?.stops.length ? a.fillGradient : undefined
const bg = b.fillGradient?.stops.length ? b.fillGradient : undefined
if (!ag && !bg) {
diff --git a/slides/src/render.ts b/slides/src/render.ts
index 4f663ab3..9560fd65 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -105,6 +105,58 @@ function gradientRef(svg: SVGSVGElement, g: NonNullable<ShapeElement['fillGradie
return `url(#${id})`
}
+/** stroke-dasharray for the element's line style (undefined = solid). */
+function dashArray(el: ShapeElement, w: number): string | undefined {
+ if (el.strokeStyle === 'dashed') return `${Math.max(w * 2.4, 7)} ${Math.max(w * 1.8, 5)}`
+ if (el.strokeStyle === 'dotted') return `0.1 ${Math.max(w * 2.2, 5)}`
+ if (el.strokeStyle === 'solid') return undefined
+ if (el.strokeDash) return `${el.strokeDash} ${el.strokeDash}` // legacy numeric dash
+ return undefined
+}
+
+let markSeq = 0
+
+/** A line-tip marker in <defs>; sized in strokeWidth units, colored like the line. */
+function markerRef(svg: SVGSVGElement, kind: NonNullable<ShapeElement['lineStart']>, color: string, start: boolean): string | null {
+ if (kind === 'none') return null
+ const id = `bento-mark-${markSeq++}`
+ const marker = document.createElementNS(SVG_NS, 'marker')
+ marker.setAttribute('id', id)
+ marker.setAttribute('viewBox', '0 0 8 8')
+ marker.setAttribute('refY', '4')
+ marker.setAttribute('orient', start ? 'auto-start-reverse' : 'auto')
+ marker.setAttribute('markerWidth', '5.5')
+ marker.setAttribute('markerHeight', '5.5')
+ let tip: SVGElement
+ if (kind === 'arrow') {
+ tip = document.createElementNS(SVG_NS, 'path')
+ tip.setAttribute('d', 'M 0 0.4 L 7.6 4 L 0 7.6 Z')
+ marker.setAttribute('refX', '6.4')
+ } else if (kind === 'dot') {
+ tip = document.createElementNS(SVG_NS, 'circle')
+ tip.setAttribute('cx', '4')
+ tip.setAttribute('cy', '4')
+ tip.setAttribute('r', '2.6')
+ marker.setAttribute('refX', '4')
+ } else {
+ tip = document.createElementNS(SVG_NS, 'rect')
+ tip.setAttribute('x', '3.2')
+ tip.setAttribute('y', '0.4')
+ tip.setAttribute('width', '1.6')
+ tip.setAttribute('height', '7.2')
+ marker.setAttribute('refX', '4')
+ }
+ tip.setAttribute('fill', color)
+ marker.appendChild(tip)
+ let defs = svg.querySelector('defs')
+ if (!defs) {
+ defs = document.createElementNS(SVG_NS, 'defs')
+ svg.appendChild(defs)
+ }
+ defs.appendChild(marker)
+ return `url(#${id})`
+}
+
export function shapeSvg(el: ShapeElement): SVGSVGElement {
const svg = document.createElementNS(SVG_NS, 'svg')
const { w, h } = el
@@ -160,13 +212,22 @@ export function shapeSvg(el: ShapeElement): SVGSVGElement {
}
case 'line': {
node = document.createElementNS(SVG_NS, 'line')
- node.setAttribute('x1', '0')
+ const lw = Math.max(sw, 2)
+ // inset the endpoints so tip decorations sit inside the element box
+ const tipPad = (k?: string) => (k && k !== 'none' ? lw * 2.6 : 0)
+ node.setAttribute('x1', String(tipPad(el.lineStart)))
node.setAttribute('y1', String(h / 2))
- node.setAttribute('x2', String(w))
+ node.setAttribute('x2', String(w - tipPad(el.lineEnd)))
node.setAttribute('y2', String(h / 2))
node.setAttribute('stroke', el.fill)
- node.setAttribute('stroke-width', String(Math.max(sw, 2)))
- node.setAttribute('stroke-linecap', 'round')
+ node.setAttribute('stroke-width', String(lw))
+ node.setAttribute('stroke-linecap', el.strokeStyle === 'dashed' ? 'butt' : 'round')
+ const lineDash = dashArray(el, lw)
+ if (lineDash) node.setAttribute('stroke-dasharray', lineDash)
+ const mStart = el.lineStart ? markerRef(svg, el.lineStart, el.fill, true) : null
+ const mEnd = el.lineEnd ? markerRef(svg, el.lineEnd, el.fill, false) : null
+ if (mStart) node.setAttribute('marker-start', mStart)
+ if (mEnd) node.setAttribute('marker-end', mEnd)
svg.appendChild(node)
return svg
}
@@ -175,7 +236,9 @@ export function shapeSvg(el: ShapeElement): SVGSVGElement {
if (el.stroke && el.stroke !== 'transparent' && sw > 0) {
node.setAttribute('stroke', el.stroke)
node.setAttribute('stroke-width', String(sw))
- if (el.strokeDash) node.setAttribute('stroke-dasharray', `${el.strokeDash} ${el.strokeDash}`)
+ const dash = dashArray(el, sw)
+ if (dash) node.setAttribute('stroke-dasharray', dash)
+ if (el.strokeStyle === 'dotted') node.setAttribute('stroke-linecap', 'round')
}
svg.appendChild(node)
return svg