Birthday balloons
Birthday Balloons
Section titled “Birthday Balloons”A personalised birthday scene with interactive parameters. Each balloon has the recipient’s age embossed on it, tied with a rope, and arranged in a circle. A greeting and optional birth date are rendered below in extruded vector text.
Parameters
Section titled “Parameters”| Parameter | Effect |
|---|---|
| Big? | Toggles between large and small balloons |
| Color? | Balloon fill colour |
| How many? | Number of balloons arranged in the circle |
| Name? | Personalises the greeting (leave blank for generic) |
| Birthday? | Renders the birth date beneath the greeting |
| Age? | Number embossed on each balloon |
"use strict";
const jscad = require('@jscad/modeling')const { subtract, union } = jscad.booleansconst { colorize, hexToRgb } = jscad.colorsconst { extrudeFromSlices, extrudeLinear, slice } = jscad.extrusionsconst { geom2 } = jscad.geometriesconst { hullChain } = jscad.hullsconst { mat4 } = jscad.mathsconst { measureBoundingBox } = jscad.measurementsconst { circle, ellipsoid } = jscad.primitivesconst { vectorText } = jscad.textconst { translate, scale, rotateX, center } = jscad.transforms
const getParameterDefinitions = () => [ { name: 'balloon', type: 'group', caption: 'Balloons' }, { name: 'isBig', type: 'boolean', default: true, caption: 'Big?' }, { name: 'color', type: 'color', default: '#ffb431', caption: 'Color?' }, { name: 'count', type: 'number', widget: 'slider', default: 4, min: 2, max: 10, step: 1, caption: 'How many?' }, { name: 'friend', type: 'group', caption: 'Friend' }, { name: 'name', type: 'text', default: '', size: 20, maxLength: 20, caption: 'Name?', placeholder: '20 characters' }, { name: 'birthdate', type: 'date', default: '', min: '1915-01-01', max: '2030-12-31', caption: 'Birthday?', placeholder: 'YYYY-MM-DD' }, { name: 'age', type: 'number', widget: 'slider', default: 20, min: 1, max: 100, step: 1, caption: 'Age?' },]
const main = (params) => { params.bRadius = (params.isBig === true) ? 16 : 10 params.bColor = hexToRgb(params.color)
return [ createBalloons(params), createSalutation(params.name), createBirthDate(params.birthdate), ]}
// Build text by creating the font strokes (2D), then extruding up (3D).const text = (message, extrusionHeight, characterLineWidth) => { if (message === undefined || message.length === 0) return []
const lineRadius = characterLineWidth / 2 const lineCorner = circle({ radius: lineRadius })
const lineSegmentPointArrays = vectorText({ x: 0, y: 0, input: message }) const lineSegments = [] lineSegmentPointArrays.forEach((segmentPoints) => { const corners = segmentPoints.map((point) => translate(point, lineCorner)) lineSegments.push(hullChain(corners)) }) const message2D = union(lineSegments) const message3D = extrudeLinear({ height: extrusionHeight }, message2D) return center({ axes: [true, true, false] }, message3D)}
const createSingleBalloon = (params) => { let t = rotateX(Math.PI / 2, text(params.age.toString(), 2, 2)) const m = measureBoundingBox(t) const x = (params.bRadius * 0.70) / Math.max(m[1][0], m[1][2]) const y = params.bRadius * 3 const z = x t = translate([0, y / 2, 0], scale([x, y, z], t))
const b = ellipsoid({ radius: [params.bRadius, params.bRadius, params.bRadius], segments: params.segments || 32, }) return subtract(b, t)}
const createRope = (to) => { const base = slice.fromSides(geom2.toSides(circle({ radius: 0.25 }))) const rope = extrudeFromSlices({ callback: (p, i, b) => { if (i === 1) { const matrix = mat4.fromTranslation(mat4.create(), to) b = slice.transform(matrix, b) } return b }, }, base) return colorize([0, 0, 0], rope)}
const createBalloons = (params) => { const balloon = createSingleBalloon(params) const out = [] const startingAngle = 360 * Math.random() const angleSpread = 360 / params.count const ropeOffset = params.bRadius - 1
for (let i = 0; i < params.count; i++) { const angle = Math.floor(startingAngle + (angleSpread * i)) % 360 const x = Math.cos(angle * Math.PI / 180) * 2 * params.bRadius const y = Math.sin(angle * Math.PI / 180) * 2 * params.bRadius const z = params.bRadius * 4 + (40 * Math.random()) const aBalloon = colorize(params.bColor, translate([x, y, z], balloon)) const aRope = createRope([x, y, z - ropeOffset]) out.push(aBalloon, aRope) } return out}
const createSalutation = (name) => { const salutation = name ? `Happy Birthday, ${name}!` : 'Happy Birthday!' return translate([0, -10, 0], scale([0.5, 0.5, 0.5], text(salutation, 2, 2)))}
const createBirthDate = (birthDate) => { const birthDateStr = birthDate.toString() if (birthDateStr.length === 0) return [] let birthDate3D = text(birthDate.toString(), 2, 1) birthDate3D = scale([0.7, 0.7, 0.7], birthDate3D) birthDate3D = translate([0, -30, 0], birthDate3D) return birthDate3D}
module.exports = { main, getParameterDefinitions }