-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathattributes.js
605 lines (595 loc) · 15.1 KB
/
attributes.js
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/**
* @module Shape
* @submodule Attributes
* @for p5
* @requires core
* @requires constants
*/
import * as constants from '../core/constants';
function attributes(p5, fn){
/**
* Changes where ellipses, circles, and arcs are drawn.
*
* By default, the first two parameters of
* <a href="#/p5/ellipse">ellipse()</a>, <a href="#/p5/circle">circle()</a>,
* and <a href="#/p5/arc">arc()</a>
* are the x- and y-coordinates of the shape's center. The next parameters set
* the shape's width and height. This is the same as calling
* `ellipseMode(CENTER)`.
*
* `ellipseMode(RADIUS)` also uses the first two parameters to set the x- and
* y-coordinates of the shape's center. The next parameters are half of the
* shapes's width and height. Calling `ellipse(0, 0, 10, 15)` draws a shape
* with a width of 20 and height of 30.
*
* `ellipseMode(CORNER)` uses the first two parameters as the upper-left
* corner of the shape. The next parameters are its width and height.
*
* `ellipseMode(CORNERS)` uses the first two parameters as the location of one
* corner of the ellipse's bounding box. The next parameters are the location
* of the opposite corner.
*
* The argument passed to `ellipseMode()` must be written in ALL CAPS because
* the constants `CENTER`, `RADIUS`, `CORNER`, and `CORNERS` are defined this
* way. JavaScript is a case-sensitive language.
*
* @method ellipseMode
* @param {(CENTER|RADIUS|CORNER|CORNERS)} mode either CENTER, RADIUS, CORNER, or CORNERS
* @chainable
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // White ellipse.
* ellipseMode(RADIUS);
* fill(255);
* ellipse(50, 50, 30, 30);
*
* // Gray ellipse.
* ellipseMode(CENTER);
* fill(100);
* ellipse(50, 50, 30, 30);
*
* describe('A white circle with a gray circle at its center. Both circles have black outlines.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // White ellipse.
* ellipseMode(CORNER);
* fill(255);
* ellipse(25, 25, 50, 50);
*
* // Gray ellipse.
* ellipseMode(CORNERS);
* fill(100);
* ellipse(25, 25, 50, 50);
*
* describe('A white circle with a gray circle at its top-left corner. Both circles have black outlines.');
* }
* </code>
* </div>
*/
fn.ellipseMode = function(m) {
// p5._validateParameters('ellipseMode', arguments);
if (
m === constants.CORNER ||
m === constants.CORNERS ||
m === constants.RADIUS ||
m === constants.CENTER
) {
this._renderer.states.setValue('ellipseMode', m);
}
return this;
};
/**
* Draws certain features with jagged (aliased) edges.
*
* <a href="#/p5/smooth">smooth()</a> is active by default. In 2D mode,
* `noSmooth()` is helpful for scaling up images without blurring. The
* functions don't affect shapes or fonts.
*
* In WebGL mode, `noSmooth()` causes all shapes to be drawn with jagged
* (aliased) edges. The functions don't affect images or fonts.
*
* @method noSmooth
* @chainable
*
* @example
* <div>
* <code>
* let heart;
*
* async function setup() {
* // Load a pixelated heart image from an image data string.
* heart = await loadImage('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAYAAADEUlfTAAAAAXNSR0IArs4c6QAAAEZJREFUGFd9jcsNACAIQ9tB2MeR3YdBMBBq8CIXPi2vBICIiOwkOedatllqWO6Y8yOWoyuNf1GZwgmf+RRG2YXr+xVFmA8HZ9Mx/KGPMtcAAAAASUVORK5CYII=');
* createCanvas(100, 100);
*
* background(50);
*
* // Antialiased hearts.
* image(heart, 10, 10);
* image(heart, 20, 10, 16, 16);
* image(heart, 40, 10, 32, 32);
*
* // Aliased hearts.
* noSmooth();
* image(heart, 10, 60);
* image(heart, 20, 60, 16, 16);
* image(heart, 40, 60, 32, 32);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* circle(0, 0, 80);
*
* describe('A white circle on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Disable smoothing.
* noSmooth();
*
* background(200);
*
* circle(0, 0, 80);
*
* describe('A pixelated white circle on a gray background.');
* }
* </code>
* </div>
*/
fn.noSmooth = function() {
if (!this._renderer.isP3D) {
if ('imageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.imageSmoothingEnabled = false;
}
} else {
// Only change if necessary to prevent canvas recreation
if (this._renderer.attributes.antialias !== false) {
this.setAttributes('antialias', false);
}
}
return this;
};
/**
* Changes where rectangles and squares are drawn.
*
* By default, the first two parameters of
* <a href="#/p5/rect">rect()</a> and <a href="#/p5/square">square()</a>,
* are the x- and y-coordinates of the shape's upper left corner. The next parameters set
* the shape's width and height. This is the same as calling
* `rectMode(CORNER)`.
*
* `rectMode(CORNERS)` also uses the first two parameters as the location of
* one of the corners. The next parameters are the location of the opposite
* corner. This mode only works for <a href="#/p5/rect">rect()</a>.
*
* `rectMode(CENTER)` uses the first two parameters as the x- and
* y-coordinates of the shape's center. The next parameters are its width and
* height.
*
* `rectMode(RADIUS)` also uses the first two parameters as the x- and
* y-coordinates of the shape's center. The next parameters are
* half of the shape's width and height.
*
* The argument passed to `rectMode()` must be written in ALL CAPS because the
* constants `CENTER`, `RADIUS`, `CORNER`, and `CORNERS` are defined this way.
* JavaScript is a case-sensitive language.
*
* @method rectMode
* @param {(CENTER|RADIUS|CORNER|CORNERS)} mode either CORNER, CORNERS, CENTER, or RADIUS
* @chainable
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* rectMode(CORNER);
* fill(255);
* rect(25, 25, 50, 50);
*
* rectMode(CORNERS);
* fill(100);
* rect(25, 25, 50, 50);
*
* describe('A small gray square drawn at the top-left corner of a white square.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* rectMode(RADIUS);
* fill(255);
* rect(50, 50, 30, 30);
*
* rectMode(CENTER);
* fill(100);
* rect(50, 50, 30, 30);
*
* describe('A small gray square drawn at the center of a white square.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* rectMode(CORNER);
* fill(255);
* square(25, 25, 50);
*
* describe('A white square.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* rectMode(RADIUS);
* fill(255);
* square(50, 50, 30);
*
* rectMode(CENTER);
* fill(100);
* square(50, 50, 30);
*
* describe('A small gray square drawn at the center of a white square.');
* }
* </code>
* </div>
*/
fn.rectMode = function(m) {
// p5._validateParameters('rectMode', arguments);
if (
m === constants.CORNER ||
m === constants.CORNERS ||
m === constants.RADIUS ||
m === constants.CENTER
) {
this._renderer.states.setValue('rectMode', m);
}
return this; // return current rectMode ?
};
/**
* Draws certain features with smooth (antialiased) edges.
*
* `smooth()` is active by default. In 2D mode,
* <a href="#/p5/noSmooth">noSmooth()</a> is helpful for scaling up images
* without blurring. The functions don't affect shapes or fonts.
*
* In WebGL mode, <a href="#/p5/noSmooth">noSmooth()</a> causes all shapes to
* be drawn with jagged (aliased) edges. The functions don't affect images or
* fonts.
*
* @method smooth
* @chainable
*
* @example
* <div>
* <code>
* let heart;
*
* async function setup() {
* // Load a pixelated heart image from an image data string.
* heart = await loadImage('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAYAAADEUlfTAAAAAXNSR0IArs4c6QAAAEZJREFUGFd9jcsNACAIQ9tB2MeR3YdBMBBq8CIXPi2vBICIiOwkOedatllqWO6Y8yOWoyuNf1GZwgmf+RRG2YXr+xVFmA8HZ9Mx/KGPMtcAAAAASUVORK5CYII=');
*
* createCanvas(100, 100);
*
* background(50);
*
* // Antialiased hearts.
* image(heart, 10, 10);
* image(heart, 20, 10, 16, 16);
* image(heart, 40, 10, 32, 32);
*
* // Aliased hearts.
* noSmooth();
* image(heart, 10, 60);
* image(heart, 20, 60, 16, 16);
* image(heart, 40, 60, 32, 32);
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* background(200);
*
* circle(0, 0, 80);
*
* describe('A white circle on a gray background.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Disable smoothing.
* noSmooth();
*
* background(200);
*
* circle(0, 0, 80);
*
* describe('A pixelated white circle on a gray background.');
* }
* </code>
* </div>
*/
fn.smooth = function() {
if (!this._renderer.isP3D) {
if ('imageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.imageSmoothingEnabled = true;
}
} else {
this.setAttributes('antialias', true);
}
return this;
};
/**
* Sets the style for rendering the ends of lines.
*
* The caps for line endings are either rounded (`ROUND`), squared
* (`SQUARE`), or extended (`PROJECT`). The default cap is `ROUND`.
*
* The argument passed to `strokeCap()` must be written in ALL CAPS because
* the constants `ROUND`, `SQUARE`, and `PROJECT` are defined this way.
* JavaScript is a case-sensitive language.
*
* @method strokeCap
* @param {(ROUND|SQUARE|PROJECT)} cap either ROUND, SQUARE, or PROJECT
* @chainable
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* strokeWeight(12);
*
* // Top.
* strokeCap(ROUND);
* line(20, 30, 80, 30);
*
* // Middle.
* strokeCap(SQUARE);
* line(20, 50, 80, 50);
*
* // Bottom.
* strokeCap(PROJECT);
* line(20, 70, 80, 70);
*
* describe(
* 'Three horizontal lines. The top line has rounded ends, the middle line has squared ends, and the bottom line has longer, squared ends.'
* );
* }
* </code>
* </div>
*/
fn.strokeCap = function(cap) {
// p5._validateParameters('strokeCap', arguments);
if (
cap === constants.ROUND ||
cap === constants.SQUARE ||
cap === constants.PROJECT
) {
this._renderer.strokeCap(cap);
}
return this;
};
/**
* Sets the style of the joints that connect line segments.
*
* Joints are either mitered (`MITER`), beveled (`BEVEL`), or rounded
* (`ROUND`). The default joint is `MITER` in 2D mode and `ROUND` in WebGL
* mode.
*
* The argument passed to `strokeJoin()` must be written in ALL CAPS because
* the constants `MITER`, `BEVEL`, and `ROUND` are defined this way.
* JavaScript is a case-sensitive language.
*
* @method strokeJoin
* @param {(MITER|BEVEL|ROUND)} join either MITER, BEVEL, or ROUND
* @chainable
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the line.
* noFill();
* strokeWeight(10);
* strokeJoin(MITER);
*
* // Draw the line.
* beginShape();
* vertex(35, 20);
* vertex(65, 50);
* vertex(35, 80);
* endShape();
*
* describe('A right-facing arrowhead shape with a pointed tip in center of canvas.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the line.
* noFill();
* strokeWeight(10);
* strokeJoin(BEVEL);
*
* // Draw the line.
* beginShape();
* vertex(35, 20);
* vertex(65, 50);
* vertex(35, 80);
* endShape();
*
* describe('A right-facing arrowhead shape with a flat tip in center of canvas.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the line.
* noFill();
* strokeWeight(10);
* strokeJoin(ROUND);
*
* // Draw the line.
* beginShape();
* vertex(35, 20);
* vertex(65, 50);
* vertex(35, 80);
* endShape();
*
* describe('A right-facing arrowhead shape with a rounded tip in center of canvas.');
* }
* </code>
* </div>
*/
fn.strokeJoin = function(join) {
// p5._validateParameters('strokeJoin', arguments);
if (
join === constants.ROUND ||
join === constants.BEVEL ||
join === constants.MITER
) {
this._renderer.strokeJoin(join);
}
return this;
};
/**
* Sets the width of the stroke used for points, lines, and the outlines of
* shapes.
*
* Note: `strokeWeight()` is affected by transformations, especially calls to
* <a href="#/p5/scale">scale()</a>.
*
* @method strokeWeight
* @param {Number} weight the weight of the stroke (in pixels).
* @chainable
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Top.
* line(20, 20, 80, 20);
*
* // Middle.
* strokeWeight(4);
* line(20, 40, 80, 40);
*
* // Bottom.
* strokeWeight(10);
* line(20, 70, 80, 70);
*
* describe('Three horizontal black lines. The top line is thin, the middle is medium, and the bottom is thick.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Top.
* line(20, 20, 80, 20);
*
* // Scale by a factor of 5.
* scale(5);
*
* // Bottom. Coordinates are adjusted for scaling.
* line(4, 8, 16, 8);
*
* describe('Two horizontal black lines. The top line is thin and the bottom is five times thicker than the top.');
* }
* </code>
* </div>
*/
fn.strokeWeight = function(w) {
// p5._validateParameters('strokeWeight', arguments);
this._renderer.strokeWeight(w);
return this;
};
}
export default attributes;
if(typeof p5 !== 'undefined'){
attributes(p5, p5.prototype);
}