about summary refs log tree commit diff stats
path: root/html/simple-shape/docs/app.js.html
blob: 613c89fe4e5359b9f1f0355406950b6dc4e0bd37 (plain) (blame)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: app.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: app.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * Configuration object for application-wide settings
 * Centralizes magic numbers and configuration values
 * @constant {Object}
 */
const CONFIG = {
    canvas: {
        dpi: 300,
        width: 8.5 * 300, // 8.5in at 300dpi
        height: 11 * 300, // 11in at 300dpi
    },
    grid: {
        rows: 4,
        columns: 5,
        topMargin: 100,
        bottomMargin: 300,
        patternSize: 400,
        gutterRatio: 0.1, // 10% of pattern size
        patternRatio: 0.8  // 80% of space for pattern
    },
    style: {
        strokeWidth: 4,
        strokeColor: '#000'
    }
};

/**
 * Canvas Setup and Configuration
 * This section initializes a high-resolution canvas optimized for both screen display and printing.
 */
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

canvas.width = CONFIG.canvas.width;
canvas.height = CONFIG.canvas.height;

/**
 * Utility Functions Module
 * Pure functions for common operations
 * @namespace Utils
 */
const Utils = {
    /**
     * Generates a random number within a range
     * @param {number} min - Minimum value
     * @param {number} max - Maximum value
     * @returns {number}
     */
    random: (min, max) => Math.random() * (max - min) + min,
    
    /**
     * Generates a random integer within a range
     * @param {number} min - Minimum value
     * @param {number} max - Maximum value
     * @returns {number}
     */
    randomInt: (min, max) => Math.floor(Utils.random(min, max)),
    
    /**
     * Randomly selects an item from an array
     * @param {Array} arr - Source array
     * @returns {*}
     */
    randomChoice: arr => arr[Math.floor(Math.random() * arr.length)],
    
    /**
     * Fisher-Yates shuffle implementation
     * @param {Array} arr - Array to shuffle
     * @returns {Array} New shuffled array
     */
    shuffle: arr => [...arr].sort(() => Math.random() - 0.5),
    
    /**
     * Creates a range array of numbers
     * @param {number} start - Start value
     * @param {number} end - End value
     * @returns {Array&lt;number>}
     */
    range: (start, end) => Array.from(
        { length: end - start }, 
        (_, i) => start + i
    )
};

/**
 * Drawing Context Manager
 * Handles canvas context state management using functional composition
 * @namespace ContextManager
 */
const ContextManager = {
    /**
     * Executes a drawing operation with saved context state
     * @param {Function} drawFn - Drawing function to execute
     * @returns {Function}
     */
    withContext: drawFn => (...args) => {
        ctx.save();
        drawFn(...args);
        ctx.restore();
    },

    /**
     * Applies a translation transformation
     * @param {number} x - X translation
     * @param {number} y - Y translation
     * @returns {Function}
     */
    withTranslation: (x, y) => drawFn => (...args) => {
        ctx.translate(x, y);
        drawFn(...args);
    },

    /**
     * Applies a rotation transformation around a point
     * @param {number} angle - Rotation angle in radians
     * @param {number} x - Center X coordinate
     * @param {number} y - Center Y coordinate
     * @returns {Function}
     */
    withRotation: (angle, x, y) => drawFn => (...args) => {
        ctx.translate(x, y);
        ctx.rotate(angle);
        ctx.translate(-x, -y);
        drawFn(...args);
    }
};

/**
 * Shape Factory Pattern
 * @namespace Shapes
 */
const Shapes = {
    /**
     * Creates a circle with optional fill
     * @param {number} x - Center X coordinate
     * @param {number} y - Center Y coordinate
     * @param {number} size - Reference size for radius calculation
     * @param {Object} params - Optional parameters for customization
     * @param {number} [params.radius] - Optional explicit radius
     * @param {boolean} [params.fill] - Whether to fill the circle
     */
    circle: (x, y, size, params = {}) => {
        const radius = params.radius || size/3;
        ctx.beginPath();
        ctx.arc(x, y, radius, 0, Math.PI * 2);
        params.fill ? ctx.fill() : ctx.stroke();
    },
    
    line: (x1, y1, x2, y2) => {
        ctx.beginPath();
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        ctx.stroke();
    },
    
    triangle: (x, y, size) => {
        ctx.beginPath();
        ctx.moveTo(x, y + size);
        ctx.lineTo(x + size/2, y);
        ctx.lineTo(x + size, y + size);
        ctx.closePath();
        ctx.stroke();
    },

    square: (x, y, size) => {
        ctx.strokeRect(x, y, size, size);
    }
};

/**
 * Pattern Generator System
 * @namespace Patterns
 */
const Patterns = {
    /**
     * Creates a pattern generator with transformation capabilities
     * @param {Function} patternFn - Base pattern drawing function
     * @returns {Function} Enhanced pattern generator
     */
    createGenerator: patternFn => {
        return ContextManager.withContext((x, y, size) => {
            const rotation = Math.PI/2 * Utils.randomInt(0, 4);
            if (rotation > 0) {
                ContextManager.withRotation(
                    rotation,
                    x + size/2,
                    y + size/2
                )(patternFn)(x, y, size);
            } else {
                patternFn(x, y, size);
            }
        });
    },

    /**
     * Collection of base pattern implementations
     * @type {Array&lt;Function>}
     */
    types: [
        /**
         * Grid-based pattern strategy
         * Demonstrates use of nested loops for regular grid generation
         * @param {number} x - Starting X coordinate
         * @param {number} y - Starting Y coordinate
         * @param {number} size - Pattern size
         */
        (x, y, size) => {
            const spacing = size/3;
            for(let i = 0; i &lt; 3; i++) {
                for(let j = 0; j &lt; 3; j++) {
                    if((i + j) % 2 === 0) { // Checkerboard pattern
                        Shapes.circle(
                            x + spacing/2 + i * spacing,
                            y + spacing/2 + j * spacing,
                            spacing/2
                        );
                    }
                }
            }
        },

        // Nested squares
        (x, y, size) => {
            for(let i = 3; i > 0; i--) {
                const offset = (3 - i) * size/6;
                const squareSize = size - offset * 2;
                Shapes.square(x + offset, y + offset, squareSize);
            }
        },

        // Simple flower pattern
        (x, y, size) => {
            const center = size/2;
            const radius = size/4;
            
            // Center circle
            Shapes.circle(x + center, y + center, size/6);
            
            // Petals
            for(let i = 0; i &lt; 6; i++) {
                const angle = (i / 6) * Math.PI * 2;
                const petalX = x + center + Math.cos(angle) * radius;
                const petalY = y + center + Math.sin(angle) * radius;
                Shapes.circle(petalX, petalY, size/6);
            }
        },

        // Triangles in a row
        (x, y, size) => {
            const triSize = size/3;
            for(let i = 0; i &lt; 3; i++) {
                Shapes.triangle(
                    x + i * triSize,
                    y + (i % 2) * triSize/2,
                    triSize
                );
            }
        },

        // Simple grid of squares
        (x, y, size) => {
            const gridSize = size/2;
            for(let i = 0; i &lt; 2; i++) {
                for(let j = 0; j &lt; 2; j++) {
                    Shapes.square(
                        x + i * gridSize + size/8,
                        y + j * gridSize + size/8,
                        gridSize * 0.75
                    );
                }
            }
        },

        // Alternating circles and squares
        (x, y, size) => {
            const spacing = size/2;
            for(let i = 0; i &lt; 2; i++) {
                for(let j = 0; j &lt; 2; j++) {
                    if((i + j) % 2 === 0) {
                        Shapes.circle(
                            x + spacing/2 + i * spacing,
                            y + spacing/2 + j * spacing,
                            spacing/3
                        );
                    } else {
                        Shapes.square(
                            x + i * spacing + spacing/6,
                            y + j * spacing + spacing/6,
                            spacing * 2/3
                        );
                    }
                }
            }
        },

        // Simple star pattern
        (x, y, size) => {
            const center = size/2;
            // Horizontal and vertical lines
            Shapes.line(x, y + center, x + size, y + center);
            Shapes.line(x + center, y, x + center, y + size);
            // Diagonal lines
            Shapes.line(x, y, x + size, y + size);
            Shapes.line(x + size, y, x, y + size);
        },

        // Nested arcs
        (x, y, size) => {
            const center = size/2;
            for(let i = 1; i &lt;= 4; i++) {
                const radius = (size/2) * (i/4);
                ctx.beginPath();
                ctx.arc(x + center, y + center, radius, 0, Math.PI);
                ctx.stroke();
            }
        },

        // Quarter circles in corners
        (x, y, size) => {
            const radius = size/2;
            // Top left
            ctx.beginPath();
            ctx.arc(x, y, radius, 0, Math.PI/2);
            ctx.stroke();
            // Top right
            ctx.beginPath();
            ctx.arc(x + size, y, radius, Math.PI/2, Math.PI);
            ctx.stroke();
            // Bottom right
            ctx.beginPath();
            ctx.arc(x + size, y + size, radius, Math.PI, Math.PI * 3/2);
            ctx.stroke();
            // Bottom left
            ctx.beginPath();
            ctx.arc(x, y + size, radius, Math.PI * 3/2, Math.PI * 2);
            ctx.stroke();
        },

        // Concentric circles
        (x, y, size) => {
            const center = size/2;
            for(let i = 1; i &lt;= 3; i++) {
                Shapes.circle(
                    x + center,
                    y + center,
                    size,
                    {radius: (size/2) * (i/3)}
                );
            }
        },

        // Nested diamonds
        (x, y, size) => {
            const center = size/2;
            for(let i = 1; i &lt;= 3; i++) {
                const offset = (size/2) * (i/3);
                ctx.beginPath();
                ctx.moveTo(x + center, y + center - offset);
                ctx.lineTo(x + center + offset, y + center);
                ctx.lineTo(x + center, y + center + offset);
                ctx.lineTo(x + center - offset, y + center);
                ctx.closePath();
                ctx.stroke();
            }
        },

        // Radiating arcs
        (x, y, size) => {
            const center = size/2;
            const radius = size/3;
            for(let i = 0; i &lt; 4; i++) {
                const startAngle = (Math.PI/2) * i;
                ctx.beginPath();
                ctx.arc(x + center, y + center, radius, startAngle, startAngle + Math.PI/2);
                ctx.stroke();
            }
        },

        // Stacked semicircles
        (x, y, size) => {
            const width = size * 0.8;
            for(let i = 0; i &lt; 3; i++) {
                ctx.beginPath();
                ctx.arc(
                    x + size/2,
                    y + (size/3) * (i + 1),
                    width/2,
                    0,
                    Math.PI,
                    i % 2 === 0
                );
                ctx.stroke();
            }
        }
    ]
};

/**
 * Layout System
 * Handles grid layout and composition
 * @namespace Layout
 */
const Layout = {
    /**
     * Calculates layout metrics for the grid
     * @returns {Object} Layout calculations
     */
    calculateMetrics: () => {
        const availableHeight = CONFIG.canvas.height - 
            (CONFIG.grid.topMargin + CONFIG.grid.bottomMargin);
        
        const totalPatternHeight = CONFIG.grid.rows * CONFIG.grid.patternSize;
        const totalGapHeight = availableHeight - totalPatternHeight;
        const rowGap = totalGapHeight / (CONFIG.grid.rows - 1);
        
        const totalWidth = CONFIG.grid.patternSize * CONFIG.grid.columns;
        const xOffset = (CONFIG.canvas.width - totalWidth) / 2;
        
        return { rowGap, xOffset };
    },

    /**
     * Draws a row of patterns
     */
    drawRow: (xOffset, y, size) => {
        const gutter = size * CONFIG.grid.gutterRatio;
        const patternSize = size * CONFIG.grid.patternRatio;
        
        const patterns = Utils.shuffle(patternGenerators);
        Utils.range(0, CONFIG.grid.columns).forEach(i => {
            const xPos = xOffset + i * size + gutter;
            const yPos = y + gutter;
            patterns[i](xPos, yPos, patternSize);
        });
    },

    /**
     * Draws the complete grid
     */
    drawGrid: () => {
        ctx.clearRect(0, 0, CONFIG.canvas.width, CONFIG.canvas.height);
        
        ctx.strokeStyle = CONFIG.style.strokeColor;
        ctx.lineWidth = CONFIG.style.strokeWidth;
        
        const { rowGap, xOffset } = Layout.calculateMetrics();
        
        Utils.range(0, CONFIG.grid.rows).forEach(i => {
            const y = CONFIG.grid.topMargin + 
                i * (CONFIG.grid.patternSize + rowGap);
            Layout.drawRow(xOffset, y, CONFIG.grid.patternSize);
        });
    }
};

// Generate pattern instances
const patternGenerators = Utils.range(0, 10)
    .map(() => Patterns.createGenerator(
        Utils.randomChoice(Patterns.types)
    ));

// Initialize and set up interaction
Layout.drawGrid();
canvas.addEventListener('click', Layout.drawGrid);</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="ContextManager.html">ContextManager</a></li><li><a href="Layout.html">Layout</a></li><li><a href="Patterns.html">Patterns</a></li><li><a href="Shapes.html">Shapes</a></li><li><a href="Utils.html">Utils</a></li></ul><h3>Global</h3><ul><li><a href="global.html#CONFIG">CONFIG</a></li><li><a href="global.html#canvas">canvas</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.3</a> on Mon Feb 17 2025 15:13:39 GMT-0500 (Eastern Standard Time)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>