Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

feat(text): optional colorGradient fill on TextElement

sergioerm972af0f9aef3
diff --git a/slides/src/model.ts b/slides/src/model.ts
index 54082973..b2870039 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -112,6 +112,8 @@ export interface TextElement extends ElementBase {
fontFamily: string
fontWeight: number
color: string
+ /** When set (and stops non-empty), painted into the glyphs; wins over `color`. */
+ colorGradient?: GradientFill
align: 'left' | 'center' | 'right'
valign: 'top' | 'middle' | 'bottom'
lineHeight: number
diff --git a/slides/src/render.ts b/slides/src/render.ts
index 89610716..c0620b9a 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -148,6 +148,15 @@ export function gradientLineCoords(angle: number) {
return { x1: 0.5 - dx, y1: 0.5 - dy, x2: 0.5 + dx, y2: 0.5 + dy }
}
+/** CSS linear-gradient() from a GradientFill. CSS angle convention matches the
+ * model (0deg = bottom->top, 90deg = left->right), so pass angle straight. */
+export function cssLinearGradient(g: NonNullable<ShapeElement['fillGradient']>): string {
+ const stops = g.stops
+ .map((s) => `${s.color} ${Math.round(Math.min(Math.max(s.at, 0), 1) * 100)}%`)
+ .join(', ')
+ return `linear-gradient(${g.angle}deg, ${stops})`
+}
+
/** 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++}`
@@ -411,7 +420,15 @@ export function renderElement(el: SlideElement, doc: BentoDoc, opts: RenderOpts
inner.style.fontSize = `${el.fontSize}px`
inner.style.fontFamily = el.fontFamily || doc.theme.fontFamily
inner.style.fontWeight = String(el.fontWeight)
- inner.style.color = el.color
+ const cg = el.colorGradient
+ if (cg && cg.stops && cg.stops.length) {
+ inner.style.backgroundImage = cssLinearGradient(cg)
+ inner.style.setProperty('-webkit-background-clip', 'text')
+ inner.style.setProperty('background-clip', 'text')
+ inner.style.color = 'transparent'
+ } else {
+ inner.style.color = el.color
+ }
inner.style.textAlign = el.align
inner.style.lineHeight = String(el.lineHeight)
if (el.letterSpacing) inner.style.letterSpacing = `${el.letterSpacing}px`
diff --git a/slides/src/styles.css b/slides/src/styles.css
index 1d13efb3..f676903a 100644
--- a/slides/src/styles.css
+++ b/slides/src/styles.css
@@ -1181,6 +1181,8 @@ body.ed-col-resizing { cursor: col-resize; user-select: none; }
#bento-print { display: block; }
@page { size: 1600px 900px; margin: 0; }
#bento-print .bp-page {
+ -webkit-print-color-adjust: exact;
+ print-color-adjust: exact;
width: 1600px;
height: 900px;
overflow: hidden;