-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp49.js
More file actions
356 lines (317 loc) · 11.5 KB
/
Copy pathp49.js
File metadata and controls
356 lines (317 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* Practice 49: Shape with 2x2 Block
*
* Randomly selects a basic shape and displays it in a 3x3 grid.
* The grid consists of one random 2x2 block and five 1x1 cells.
*
* Code by Jace Yang
*/
// Category selection: 'geometric', 'symmetry', 'fill', or 'density'
let selectedCategory = 'geometric'; // Click buttons on canvas to change
let singleShapeMode = false; // When true, use same shape for entire grid
const geometricTerms = ['dot', 'square', 'circle', 'triangle', 'line'];
const symmetryTerms = ['sym', 'nonsym'];
const fillTerms = ['outline', 'filled'];
const densityTerms = ['sparse', 'dense'];
let shapes = [];
let filteredShapes = [];
let filteredSvgFiles = [];
let shapeAssignments; // Maps positions to which shape they use
let usedTerms = []; // Track which specific terms were used (e.g., "dot", "circle")
const svgFiles = [
'/anthropic_basic_shapes/triangle_nonsym_outline_sparse.svg',
'/anthropic_basic_shapes/triangle_nonsym_filled_sparse.svg',
'/anthropic_basic_shapes/square_sym_filled_sparse.svg',
'/anthropic_basic_shapes/dot_sym_outline_dense.svg',
'/anthropic_basic_shapes/line_nonsym_outline_dense.svg',
'/anthropic_basic_shapes/dot_sym_outline_sparse.svg',
'/anthropic_basic_shapes/square_sym_filled_dense.svg',
'/anthropic_basic_shapes/dot_nonsym_outline_dense.svg',
'/anthropic_basic_shapes/square_sym_filled_dense_2.svg',
'/anthropic_basic_shapes/square_nonsym_filled_dense.svg',
'/anthropic_basic_shapes/square_sym_filled_sparse_2.svg',
'/anthropic_basic_shapes/circle_nonsym_filled_sparse.svg',
'/anthropic_basic_shapes/square_sym_filled_dense_3.svg',
'/anthropic_basic_shapes/circle_sym_outline_dense.svg',
'/anthropic_basic_shapes/dot_nonsym_outline_sparse.svg',
'/anthropic_basic_shapes/dot_sym_outline_dense_2.svg',
'/anthropic_basic_shapes/line_nonsym_outline_dense_2.svg',
'/anthropic_basic_shapes/line_nonsym_outline_dense_3.svg',
'/anthropic_basic_shapes/circle_sym_filled_dense.svg',
'/anthropic_basic_shapes/circle_sym_filled_sparse.svg',
'/anthropic_basic_shapes/line_sym_outline_dense.svg',
'/anthropic_basic_shapes/circle_sym_filled_dense_2.svg',
'/anthropic_basic_shapes/dot_sym_outline_dense_3.svg'
];
const colors = [
'#000000', // Black
'#528FCC', // Soft blue
'#728058', // Olive green
'#AB5C76', // Dusty rose
'#B2D1C7', // Seafoam mint
'#C8C7D6', // Lavender gray
'#EBC7C7' // Blush pink
];
let blockPosition; // Position of the 2x2 block: 'top-left', 'top-right', 'bottom-left', 'bottom-right'
let coloredPosition; // Which position gets the color: 'block' or a cell coordinate like {row: 0, col: 2}
let selectedColor; // The color for the colored shape (selected once)
function parseShapeName(filename) {
// Remove path and extension, handle _2, _3 suffixes
const name = filename.split('/').pop().replace('.svg', '').replace(/_\d+$/, '');
const parts = name.split('_');
return {
geometric: parts[0],
symmetry: parts[1],
fill: parts[2],
density: parts[3]
};
}
function shapeMatchesCategory(shapeName, category) {
const parsed = parseShapeName(shapeName);
switch (category) {
case 'geometric':
return geometricTerms.includes(parsed.geometric);
case 'symmetry':
return symmetryTerms.includes(parsed.symmetry);
case 'fill':
return fillTerms.includes(parsed.fill);
case 'density':
return densityTerms.includes(parsed.density);
default:
return true; // If category is invalid, show all
}
}
function isIn2x2Block(row, col) {
switch (blockPosition) {
case 'top-left':
return row < 2 && col < 2;
case 'top-right':
return row < 2 && col >= 1;
case 'bottom-left':
return row >= 1 && col < 2;
case 'bottom-right':
return row >= 1 && col >= 1;
default:
return false;
}
}
function getFirstCellOfBlock() {
switch (blockPosition) {
case 'top-left':
return {row: 0, col: 0};
case 'top-right':
return {row: 0, col: 1};
case 'bottom-left':
return {row: 1, col: 0};
case 'bottom-right':
return {row: 1, col: 1};
default:
return {row: 0, col: 0};
}
}
function setupShapes() {
// First, pick a random specific term from the selected category
let selectedTerm;
let terms;
switch (selectedCategory) {
case 'geometric':
terms = geometricTerms;
break;
case 'symmetry':
terms = symmetryTerms;
break;
case 'fill':
terms = fillTerms;
break;
case 'density':
terms = densityTerms;
break;
}
selectedTerm = random(terms);
usedTerms = [selectedTerm]; // Store the single term used
// Filter shapes to only those matching the specific term
filteredShapes = [];
filteredSvgFiles = [];
for (let i = 0; i < svgFiles.length; i++) {
const parsed = parseShapeName(svgFiles[i]);
let matches = false;
switch (selectedCategory) {
case 'geometric':
matches = parsed.geometric === selectedTerm;
break;
case 'symmetry':
matches = parsed.symmetry === selectedTerm;
break;
case 'fill':
matches = parsed.fill === selectedTerm;
break;
case 'density':
matches = parsed.density === selectedTerm;
break;
}
if (matches) {
filteredShapes.push(shapes[i]);
filteredSvgFiles.push(svgFiles[i]);
}
}
// Use filtered shapes for selection
if (filteredShapes.length === 0) {
console.error('No shapes match the selected term:', selectedTerm);
return;
}
// Randomly select position for 2x2 block
const positions = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
blockPosition = random(positions);
// Get all positions (block + 5 individual cells)
const firstCell = getFirstCellOfBlock();
const individualCells = [];
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
if (!isIn2x2Block(row, col)) {
individualCells.push({row, col});
}
}
}
// Create list of all positions
const allPositions = [
{type: 'block', row: firstCell.row, col: firstCell.col},
...individualCells.map(c => ({type: 'cell', row: c.row, col: c.col}))
];
// Assign shapes to each position
shapeAssignments = new Map();
const singleShape = singleShapeMode ? random(filteredShapes) : null;
for (let i = 0; i < allPositions.length; i++) {
const pos = allPositions[i];
const key = pos.type + '-' + pos.row + '-' + pos.col;
const shapeToAssign = singleShapeMode ? singleShape : random(filteredShapes);
shapeAssignments.set(key, shapeToAssign);
}
// Randomly select which position gets the color
if (random() < 0.5) {
coloredPosition = 'block';
} else {
coloredPosition = random(individualCells);
}
// Select the color once (not in draw loop)
selectedColor = colors[floor(random(1, colors.length))];
}
function preload() {
// Load all shapes first
for (let i = 0; i < svgFiles.length; i++) {
shapes.push(loadImage(svgFiles[i]));
}
}
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function setup() {
createCanvas(800, 800);
setupShapes();
noLoop(); // Only draw once, redraw on category change
// Listen for messages from parent React component
window.addEventListener('message', function(event) {
if (event.data && event.data.type === 'setCategory') {
selectedCategory = event.data.category;
setupShapes();
redraw();
}
if (event.data && event.data.type === 'setSingleShapeMode') {
singleShapeMode = event.data.enabled;
setupShapes();
redraw();
}
});
}
function draw() {
background(255);
const padding = 40;
const cols = 3;
const rows = 3;
const gridWidth = width - padding * 2;
const gridHeight = height - padding * 2;
const cellWidth = gridWidth / cols;
const cellHeight = gridHeight / rows;
const shapeSize1x1 = min(cellWidth, cellHeight) * 0.85;
const shapeSize2x2 = min(cellWidth * 2, cellHeight * 2) * 0.925;
// Draw grid cells - each cell gets a shape centered within it
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
// Calculate cell center
const cellCenterX = padding + col * cellWidth + cellWidth / 2;
const cellCenterY = padding + row * cellHeight + cellHeight / 2;
if (isIn2x2Block(row, col)) {
// For 2x2 block, only draw in the first cell (top-left of the block)
const firstCell = getFirstCellOfBlock();
if (row === firstCell.row && col === firstCell.col) {
// Get the shape assigned to this block
const shapeKey = 'block-' + firstCell.row + '-' + firstCell.col;
const shapeToUse = shapeAssignments.get(shapeKey);
// Determine if this block should be colored
const isColored = coloredPosition === 'block';
const color = isColored ? selectedColor : colors[0]; // Black if not colored
if (shapeToUse && shapeToUse.width > 0) {
const scale = shapeSize2x2 / max(shapeToUse.width, shapeToUse.height);
const w = shapeToUse.width * scale;
const h = shapeToUse.height * scale;
const g = createGraphics(w, h);
g.imageMode(CORNER);
g.image(shapeToUse, 0, 0, w, h);
g.loadPixels();
const rgb = hexToRgb(color);
for (let i = 0; i < g.pixels.length; i += 4) {
if (g.pixels[i + 3] > 0) {
g.pixels[i] = rgb.r;
g.pixels[i + 1] = rgb.g;
g.pixels[i + 2] = rgb.b;
}
}
g.updatePixels();
// Center the 2x2 block within its 2x2 cell area
const blockCenterX = padding + firstCell.col * cellWidth + cellWidth;
const blockCenterY = padding + firstCell.row * cellHeight + cellHeight;
imageMode(CENTER);
image(g, blockCenterX, blockCenterY);
g.remove(); // Clean up to prevent memory leak
}
}
} else {
// Draw 1x1 cell shape centered within cell
// Get the shape assigned to this cell
const shapeKey = 'cell-' + row + '-' + col;
const shapeToUse = shapeAssignments.get(shapeKey);
// Determine if this cell should be colored
const isColored = coloredPosition !== 'block' &&
coloredPosition.row === row &&
coloredPosition.col === col;
const color = isColored ? selectedColor : colors[0]; // Black if not colored
if (shapeToUse && shapeToUse.width > 0) {
const scale = shapeSize1x1 / max(shapeToUse.width, shapeToUse.height);
const w = shapeToUse.width * scale;
const h = shapeToUse.height * scale;
const g = createGraphics(w, h);
g.imageMode(CORNER);
g.image(shapeToUse, 0, 0, w, h);
g.loadPixels();
const rgb = hexToRgb(color);
for (let i = 0; i < g.pixels.length; i += 4) {
if (g.pixels[i + 3] > 0) {
g.pixels[i] = rgb.r;
g.pixels[i + 1] = rgb.g;
g.pixels[i + 2] = rgb.b;
}
}
g.updatePixels();
// Center the shape within its cell
imageMode(CENTER);
image(g, cellCenterX, cellCenterY);
g.remove(); // Clean up to prevent memory leak
}
}
}
}
}