Skip to content

Commit 25216af

Browse files
committed
Added message support to triangle-art
1 parent 64798c5 commit 25216af

File tree

8 files changed

+89
-15
lines changed

8 files changed

+89
-15
lines changed

src/apps/makelogo/TriangleMorphTest/art_data/heart.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "Valentine Heart",
3+
"message": "Happy Valentine's Day!",
34
"numCols": 10,
45
"numRows": 10,
56
"directionPattern": "alternating",

src/apps/makelogo/TriangleMorphTest/art_data/jack-o-lantern.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "Jack-o-Lantern",
3+
"message": "Happy Halloween!",
34
"numCols": 10,
45
"numRows": 9,
56
"directionPattern": "alternating",

src/apps/makelogo/TriangleMorphTest/art_data/pumpkin.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "Pumpkin",
3+
"message": "Happy Thanksgiving!",
34
"numCols": 10,
45
"numRows": 9,
56
"directionPattern": "alternating",

src/apps/makelogo/TriangleMorphTest/art_data/santa.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "Santa",
3+
"message": "Happy Holidays!",
34
"numCols": 10,
45
"numRows": 9,
56

src/apps/makelogo/TriangleMorphTest/art_data/shamrock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "Shamrock",
3+
"message": "Happy St. Patrick's Day!",
34
"numCols": 10,
45
"numRows": 9,
56
"directionPattern": "alternating",

src/apps/makelogo/TriangleMorphTest/art_data/uw.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "UW Purple W",
3+
"message": "Go Dawgs!",
34
"numCols": 13,
45
"numRows": 10,
56
"directionPattern": "alternating",

src/apps/makelogo/TriangleMorphTest/sketch.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
* 4. Morphs it into the Makeability Lab Logo
77
*/
88

9+
// In the future, we could consider:
10+
// 4th of July — star shape (5-pointed), red/white/blue
11+
// 🦃 Thanksgiving (Nov) — turkey? Might be tough at this resolution. A fall leaf could work better — maple leaf shape in red/orange/yellow
12+
// ⭐ New Year's — star or firework burst
13+
// 🌸 Spring/Cherry Blossom — simple flower shape, pink (could double for UW cherry blossom season)
914

1015
import {
1116
MakeabilityLabLogo, Grid, Triangle, ORIGINAL_COLOR_ARRAY,
@@ -28,6 +33,7 @@ let mouseX = 0;
2833
let makeLabLogoStatic = null;
2934
let currentTriangleArt = null;
3035
let makeLabGrid = null;
36+
let currentLerpAmt = 0;
3137

3238
// The Morph Data
3339
let animatedTriangles = [];
@@ -108,6 +114,7 @@ function setupCanvas(artData) {
108114

109115
makeLabLogoStatic = new MakeabilityLabLogo(logoX, logoY, TRIANGLE_SIZE);
110116
makeLabLogoStatic.visible = false;
117+
makeLabLogoStatic.isLabelVisible = true;
111118

112119
// Re-map the triangles for the new layout
113120
createAnimationMapping();
@@ -168,38 +175,47 @@ function createAnimationMapping() {
168175
function updateAnimation() {
169176
const smallBuffer = WIDTH_MARGIN_PCT * logicalWidth;
170177
const effectiveWidth = logicalWidth - 2 * smallBuffer;
171-
172-
// Calculate morph progress (0.0 to 1.0) based on Mouse X
173-
const lerpAmt = map(mouseX, smallBuffer, effectiveWidth, 0, 1, true);
178+
currentLerpAmt = map(mouseX, smallBuffer, effectiveWidth, 0, 1, true);
174179

175180
for (const tri of animatedTriangles) {
176-
tri.x = lerp(tri._originState.x, tri._destState.x, lerpAmt);
177-
tri.y = lerp(tri._originState.y, tri._destState.y, lerpAmt);
178-
tri.fillColor = lerpColor(tri._originState.fill, tri._destState.fill, lerpAmt);
179-
tri.strokeColor = lerpColor(tri._originState.stroke, tri._destState.stroke, lerpAmt);
181+
tri.x = lerp(tri._originState.x, tri._destState.x, currentLerpAmt);
182+
tri.y = lerp(tri._originState.y, tri._destState.y, currentLerpAmt);
183+
tri.fillColor = lerpColor(tri._originState.fill, tri._destState.fill, currentLerpAmt);
184+
tri.strokeColor = lerpColor(tri._originState.stroke, tri._destState.stroke, currentLerpAmt);
180185
}
181186
}
182187

183188
function drawLoop() {
184-
// Clear Screen
189+
// Clear screen first
185190
ctx.fillStyle = 'rgb(250, 250, 250)';
186191
ctx.fillRect(0, 0, logicalWidth, logicalHeight);
187192

188-
updateAnimation();
193+
updateAnimation(); // sets currentLerpAmt
189194

190195
if (makeLabGrid && makeLabGrid.visible) makeLabGrid.draw(ctx);
191-
192-
// Draw the animated morphing triangles
193-
for (const tri of animatedTriangles) {
194-
tri.draw(ctx);
195-
}
196196

197-
// Draw Logo Overlay (Debugging: press 'h' to toggle)
197+
for (const tri of animatedTriangles) tri.draw(ctx);
198+
198199
if (makeLabLogoStatic && makeLabLogoStatic.visible) makeLabLogoStatic.draw(ctx);
199200

201+
// Labels: art message fades out, logo label fades in
202+
if (currentTriangleArt?.message) currentTriangleArt.drawMessage(ctx, 1 - currentLerpAmt);
203+
drawLogoLabel(ctx, currentLerpAmt);
204+
200205
requestAnimationFrame(drawLoop);
201206
}
202207

208+
const LABEL_APPEAR_THRESHOLD = 0.7;
209+
const LABEL_SLIDE_FRACTION = 0.4;
210+
211+
function drawLogoLabel(ctx, lerpAmt) {
212+
if (lerpAmt <= LABEL_APPEAR_THRESHOLD) return;
213+
const progress = (lerpAmt - LABEL_APPEAR_THRESHOLD) / (1 - LABEL_APPEAR_THRESHOLD);
214+
const eased = progress * progress * (3 - 2 * progress); // smoothstep
215+
const slideOffset = makeLabLogoStatic.labelFontSize * LABEL_SLIDE_FRACTION * (1 - eased);
216+
makeLabLogoStatic.drawLabel(ctx, { opacity: eased, yOffset: slideOffset });
217+
}
218+
203219
// --- Inputs ---
204220
function onMouseMove(e) {
205221
const rect = canvas.getBoundingClientRect();

src/apps/makelogo/TriangleMorphTest/triangle-art.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ export class TriangleArt {
5454

5555
/** @type {boolean} */
5656
this.visible = true;
57+
58+
this.showMessage = true;
59+
60+
/** @type {string} CSS color for the message text. Falls back to palette's first entry base, then black. */
61+
this.messageColor = data.messageColor ?? TriangleArt._defaultMessageColor(data);
62+
63+
console.log("Initialized TriangleArt with name: ", this.name);
64+
console.log("Message: ", this.message);
5765
}
5866

5967
// -----------------------------------------------------------------------
@@ -87,6 +95,8 @@ export class TriangleArt {
8795
get numCols() { return this.data.numCols; }
8896
get width() { return this.numCols * this.triangleSize; }
8997
get height() { return this.numRows * this.triangleSize; }
98+
get name() { return this.data.name ?? ''; }
99+
get message() { return this.data.message ?? ''; }
90100

91101
// -----------------------------------------------------------------------
92102
// Triangle access
@@ -152,13 +162,45 @@ export class TriangleArt {
152162
*/
153163
draw(ctx) {
154164
if (!this.visible) return;
165+
166+
if (this.showMessage && this.message){
167+
this.drawMessage(ctx);
168+
console.log("Drawing message: ", this.message);
169+
}else{
170+
console.log("Message hidden or empty, skipping drawMessage.");
171+
}
172+
155173
for (const row of this.artArray) {
156174
for (const cell of row) {
157175
cell.draw(ctx);
158176
}
159177
}
160178
}
161179

180+
/**
181+
* Draws the message string centered just above the artwork.
182+
*
183+
* @param {CanvasRenderingContext2D} ctx
184+
* @param {number} [alpha=1] - Opacity 0–1, modulates messageColor.
185+
* @param {number|null} [x=null] - Center x override; defaults to grid center.
186+
* @param {number|null} [y=null] - Baseline y override; defaults to just above grid.
187+
*/
188+
drawMessage(ctx, alpha = 1, x = null, y = null) {
189+
if (!this.message) return;
190+
const fontSize = Math.round(this.triangleSize * 0.7);
191+
const cx = x ?? (this.x + this.width / 2);
192+
const cy = y ?? (this.y - fontSize);
193+
194+
ctx.save();
195+
ctx.globalAlpha = Math.max(0, Math.min(1, alpha));
196+
ctx.font = `bold ${fontSize}px sans-serif`;
197+
ctx.fillStyle = this.messageColor;
198+
ctx.textAlign = 'center';
199+
ctx.textBaseline = 'alphabetic';
200+
ctx.fillText(this.message, cx, cy);
201+
ctx.restore();
202+
}
203+
162204
// -----------------------------------------------------------------------
163205
// Static factory: load from URL
164206
// -----------------------------------------------------------------------
@@ -434,4 +476,14 @@ export class TriangleArt {
434476
}
435477
return random(jitter.brightness[0], jitter.brightness[1]);
436478
}
479+
480+
/**
481+
*
482+
* @param {*} data
483+
* @returns
484+
*/
485+
static _defaultMessageColor(data) {
486+
const keys = Object.keys(data.palette ?? {});
487+
return keys.length > 0 ? data.palette[keys[0]].base : '#000000';
488+
}
437489
}

0 commit comments

Comments
 (0)