@@ -13,10 +13,9 @@ import p5 from '../core/main';
13
13
import omggif from 'omggif' ;
14
14
15
15
/**
16
- * Creates a new <a href="#/p5.Image">p5.Image</a> object. The new image is
17
- * transparent by default.
16
+ * Creates a new <a href="#/p5.Image">p5.Image</a> object.
18
17
*
19
- * `createImage()` uses the `width` and `height` paremeters to set the new
18
+ * `createImage()` uses the `width` and `height` parameters to set the new
20
19
* <a href="#/p5.Image">p5.Image</a> object's dimensions in pixels. The new
21
20
* <a href="#/p5.Image">p5.Image</a> can be modified by updating its
22
21
* <a href="#/p5.Image/pixels">pixels</a> array or by calling its
@@ -27,65 +26,122 @@ import omggif from 'omggif';
27
26
* <a href="#/p5.Image/updatePixels">updatePixels()</a> method must be called
28
27
* for updates to take effect.
29
28
*
29
+ * Note: The new <a href="#/p5.Image">p5.Image</a> object is transparent by
30
+ * default.
31
+ *
30
32
* @method createImage
31
33
* @param {Integer } width width in pixels.
32
34
* @param {Integer } height height in pixels.
33
35
* @return {p5.Image } new <a href="#/p5.Image">p5.Image</a> object.
36
+ *
34
37
* @example
35
38
* <div>
36
39
* <code>
37
- * let img = createImage(66, 66);
38
- * img.loadPixels();
39
- * for (let x = 0; x < img.width; x += 1) {
40
- * for (let y = 0; y < img.height; y += 1) {
41
- * img.set(x, y, 0);
40
+ * function setup() {
41
+ * createCanvas(100, 100);
42
+ *
43
+ * background(200);
44
+ *
45
+ * // Create a p5.Image object.
46
+ * let img = createImage(66, 66);
47
+ *
48
+ * // Load the image's pixels into memory.
49
+ * img.loadPixels();
50
+ *
51
+ * // Set all the image's pixels to black.
52
+ * for (let x = 0; x < img.width; x += 1) {
53
+ * for (let y = 0; y < img.height; y += 1) {
54
+ * img.set(x, y, 0);
55
+ * }
42
56
* }
43
- * }
44
- * img.updatePixels();
45
- * image(img, 17, 17);
46
57
*
47
- * describe('A black square drawn in the middle of a gray square.');
58
+ * // Update the image's pixel values.
59
+ * img.updatePixels();
60
+ *
61
+ * // Draw the image.
62
+ * image(img, 17, 17);
63
+ *
64
+ * describe('A black square drawn in the middle of a gray square.');
65
+ * }
48
66
* </code>
49
67
* </div>
50
68
*
51
69
* <div>
52
70
* <code>
53
- * let img = createImage(66, 66);
54
- * img.loadPixels();
55
- * for (let x = 0; x < img.width; x += 1) {
56
- * for (let y = 0; y < img.height; y += 1) {
57
- * let a = map(x, 0, img.width, 0, 255);
58
- * let c = color(0, a);
59
- * img.set(x, y, c);
71
+ * function setup() {
72
+ * createCanvas(100, 100);
73
+ *
74
+ * background(200);
75
+ *
76
+ * // Create a p5.Image object.
77
+ * let img = createImage(66, 66);
78
+ *
79
+ * // Load the image's pixels into memory.
80
+ * img.loadPixels();
81
+ *
82
+ * // Create a color gradient.
83
+ * for (let x = 0; x < img.width; x += 1) {
84
+ * for (let y = 0; y < img.height; y += 1) {
85
+ * // Calculate the transparency.
86
+ * let a = map(x, 0, img.width, 0, 255);
87
+ *
88
+ * // Create a p5.Color object.
89
+ * let c = color(0, a);
90
+ *
91
+ * // Set the pixel's color.
92
+ * img.set(x, y, c);
93
+ * }
60
94
* }
61
- * }
62
- * img.updatePixels();
63
- * image(img, 17, 17);
64
95
*
65
- * describe('A square with a horizontal color gradient that transitions from gray to black.');
96
+ * // Update the image's pixels.
97
+ * img.updatePixels();
98
+ *
99
+ * // Display the image.
100
+ * image(img, 17, 17);
101
+ *
102
+ * describe('A square with a horizontal color gradient that transitions from gray to black.');
103
+ * }
66
104
* </code>
67
105
* </div>
68
106
*
69
107
* <div>
70
108
* <code>
71
- * let img = createImage(66, 66);
72
- * img.loadPixels();
73
- * let d = pixelDensity();
74
- * let halfImage = 4 * (d * img.width) * (d * img.height / 2);
75
- * for (let i = 0; i < halfImage; i += 4) {
76
- * // Red.
77
- * img.pixels[i] = 0;
78
- * // Green.
79
- * img.pixels[i + 1] = 0;
80
- * // Blue.
81
- * img.pixels[i + 2] = 0;
82
- * // Alpha.
83
- * img.pixels[i + 3] = 255;
84
- * }
85
- * img.updatePixels();
86
- * image(img, 17, 17);
109
+ * function setup() {
110
+ * createCanvas(100, 100);
111
+ *
112
+ * background(200);
113
+ *
114
+ * // Create a p5.Image object.
115
+ * let img = createImage(66, 66);
116
+ *
117
+ * // Load the pixels into memory.
118
+ * img.loadPixels();
119
+ * // Get the current pixel density.
120
+ * let d = pixelDensity();
121
+ *
122
+ * // Calculate the pixel that is halfway through the image's pixel array.
123
+ * let halfImage = 4 * (d * img.width) * (d * img.height / 2);
87
124
*
88
- * describe('A black square drawn in the middle of a gray square.');
125
+ * // Set half of the image's pixels to black.
126
+ * for (let i = 0; i < halfImage; i += 4) {
127
+ * // Red.
128
+ * img.pixels[i] = 0;
129
+ * // Green.
130
+ * img.pixels[i + 1] = 0;
131
+ * // Blue.
132
+ * img.pixels[i + 2] = 0;
133
+ * // Alpha.
134
+ * img.pixels[i + 3] = 255;
135
+ * }
136
+ *
137
+ * // Update the image's pixels.
138
+ * img.updatePixels();
139
+ *
140
+ * // Display the image.
141
+ * image(img, 17, 17);
142
+ *
143
+ * describe('A black square drawn in the middle of a gray square.');
144
+ * }
89
145
* </code>
90
146
* </div>
91
147
*/
@@ -95,8 +151,22 @@ p5.prototype.createImage = function(width, height) {
95
151
} ;
96
152
97
153
/**
98
- * Saves the current canvas as an image. The browser will either save the
99
- * file immediately or prompt the user with a dialogue window.
154
+ * Saves the current canvas as an image.
155
+ *
156
+ * By default, `saveCanvas()` saves the canvas as a PNG image called
157
+ * `untitled.png`.
158
+ *
159
+ * The first parameter, `filename`, is optional. It's a string that sets the
160
+ * file's name. If a file extension is included, as in
161
+ * `saveCanvas('drawing.png')`, then the image will be saved using that
162
+ * format.
163
+ *
164
+ * The second parameter, `extension`, is also optional. It sets the files format.
165
+ * Either `'png'` or `'jpg'` can be used. For example, `saveCanvas('drawing', 'jpg')`
166
+ * saves the canvas to a file called `drawing.jpg`.
167
+ *
168
+ * Note: The browser will either save the file immediately or prompt the user
169
+ * with a dialogue window.
100
170
*
101
171
* @method saveCanvas
102
172
* @param {p5.Framebuffer|p5.Element|HTMLCanvasElement } selectedCanvas reference to a
@@ -110,7 +180,11 @@ p5.prototype.createImage = function(width, height) {
110
180
* function setup() {
111
181
* createCanvas(100, 100);
112
182
* background(255);
183
+ *
184
+ * // Save the canvas to 'untitled.png'.
113
185
* saveCanvas();
186
+ *
187
+ * describe('A white square.');
114
188
* }
115
189
* </code>
116
190
* </div>
@@ -119,8 +193,13 @@ p5.prototype.createImage = function(width, height) {
119
193
* <code>
120
194
* function setup() {
121
195
* createCanvas(100, 100);
196
+ *
122
197
* background(255);
198
+ *
199
+ * // Save the canvas to 'myCanvas.jpg'.
123
200
* saveCanvas('myCanvas.jpg');
201
+ *
202
+ * describe('A white square.');
124
203
* }
125
204
* </code>
126
205
* </div>
@@ -129,8 +208,13 @@ p5.prototype.createImage = function(width, height) {
129
208
* <code>
130
209
* function setup() {
131
210
* createCanvas(100, 100);
211
+ *
132
212
* background(255);
213
+ *
214
+ * // Save the canvas to 'myCanvas.jpg'.
133
215
* saveCanvas('myCanvas', 'jpg');
216
+ *
217
+ * describe('A white square.');
134
218
* }
135
219
* </code>
136
220
* </div>
@@ -139,8 +223,13 @@ p5.prototype.createImage = function(width, height) {
139
223
* <code>
140
224
* function setup() {
141
225
* let cnv = createCanvas(100, 100);
226
+ *
142
227
* background(255);
228
+ *
229
+ * // Save the canvas to 'untitled.png'.
143
230
* saveCanvas(cnv);
231
+ *
232
+ * describe('A white square.');
144
233
* }
145
234
* </code>
146
235
* </div>
@@ -149,8 +238,13 @@ p5.prototype.createImage = function(width, height) {
149
238
* <code>
150
239
* function setup() {
151
240
* let cnv = createCanvas(100, 100);
241
+ *
152
242
* background(255);
243
+ *
244
+ * // Save the canvas to 'myCanvas.jpg'.
153
245
* saveCanvas(cnv, 'myCanvas.jpg');
246
+ *
247
+ * describe('A white square.');
154
248
* }
155
249
* </code>
156
250
* </div>
@@ -159,8 +253,13 @@ p5.prototype.createImage = function(width, height) {
159
253
* <code>
160
254
* function setup() {
161
255
* let cnv = createCanvas(100, 100);
256
+ *
162
257
* background(255);
258
+ *
259
+ * // Save the canvas to 'myCanvas.jpg'.
163
260
* saveCanvas(cnv, 'myCanvas', 'jpg');
261
+ *
262
+ * describe('A white square.');
164
263
* }
165
264
* </code>
166
265
* </div>
@@ -457,27 +556,37 @@ p5.prototype.encodeAndDownloadGif = function(pImg, filename) {
457
556
} ;
458
557
459
558
/**
460
- * Captures a sequence of frames from the canvas that can be used to create a
461
- * movie. Frames are downloaded as individual image files by default.
559
+ * Captures a sequence of frames from the canvas that can be saved as images.
560
+ *
561
+ * `saveFrames()` creates an array of frame objects. Each frame is stored as
562
+ * an object with its file type, file name, and image data as a string. For
563
+ * example, the first saved frame might have the following properties:
564
+ *
565
+ * `{ ext: 'png', filenmame: 'frame0', imageData: 'data:image/octet-stream;base64, abc123' }`.
462
566
*
463
567
* The first parameter, `filename`, sets the prefix for the file names. For
464
568
* example, setting the prefix to `'frame'` would generate the image files
465
- * `frame0.png`, `frame1.png`, and so on. The second parameter, `extension`,
466
- * sets the file type to either `'png'` or `'jpg'`.
569
+ * `frame0.png`, `frame1.png`, and so on.
570
+ *
571
+ * The second parameter, `extension`, sets the file type to either `'png'` or
572
+ * `'jpg'`.
467
573
*
468
574
* The third parameter, `duration`, sets the duration to record in seconds.
469
- * The maximum duration is 15 seconds. The fourth parameter, `framerate`, sets
470
- * the number of frames to record per second. The maximum frame rate value is
471
- * 22. Limits are placed on `duration` and `framerate` to avoid using too much
472
- * memory. Recording large canvases can easily crash sketches or even web
473
- * browsers.
575
+ * The maximum duration is 15 seconds.
576
+ *
577
+ * The fourth parameter, `framerate`, sets the number of frames to record per
578
+ * second. The maximum frame rate value is 22. Limits are placed on `duration`
579
+ * and `framerate` to avoid using too much memory. Recording large canvases
580
+ * can easily crash sketches or even web browsers.
474
581
*
475
582
* The fifth parameter, `callback`, is optional. If a function is passed,
476
583
* image files won't be saved by default. The callback function can be used
477
584
* to process an array containing the data for each captured frame. The array
478
585
* of image data contains a sequence of objects with three properties for each
479
586
* frame: `imageData`, `filename`, and `extension`.
480
587
*
588
+ * Note: Frames are downloaded as individual image files by default.
589
+ *
481
590
* @method saveFrames
482
591
* @param {String } filename prefix of file name.
483
592
* @param {String } extension file extension, either 'jpg' or 'png'.
@@ -492,15 +601,20 @@ p5.prototype.encodeAndDownloadGif = function(pImg, filename) {
492
601
* @example
493
602
* <div>
494
603
* <code>
604
+ * function setup() {
605
+ * createCanvas(100, 100);
606
+ *
607
+ * describe('A square repeatedly changes color from blue to pink.');
608
+ * }
609
+ *
495
610
* function draw() {
496
611
* let r = frameCount % 255;
497
612
* let g = 50;
498
613
* let b = 100;
499
614
* background(r, g, b);
500
- *
501
- * describe('A square repeatedly changes color from blue to pink.');
502
615
* }
503
616
*
617
+ * // Save the frames when the user presses the 's' key.
504
618
* function keyPressed() {
505
619
* if (key === 's') {
506
620
* saveFrames('frame', 'png', 1, 5);
@@ -511,21 +625,29 @@ p5.prototype.encodeAndDownloadGif = function(pImg, filename) {
511
625
*
512
626
* <div>
513
627
* <code>
628
+ * function setup() {
629
+ * createCanvas(100, 100);
630
+ *
631
+ * describe('A square repeatedly changes color from blue to pink.');
632
+ * }
633
+ *
514
634
* function draw() {
515
635
* let r = frameCount % 255;
516
636
* let g = 50;
517
637
* let b = 100;
518
638
* background(r, g, b);
519
- *
520
- * describe('A square repeatedly changes color from blue to pink.');
521
639
* }
522
640
*
641
+ * // Print 5 frames when the user presses the mouse.
523
642
* function mousePressed() {
524
- * saveFrames('frame', 'png', 1, 5, data => {
525
- * // Prints an array of objects containing raw image data,
526
- * // filenames, and extensions.
527
- * print(data);
528
- * });
643
+ * saveFrames('frame', 'png', 1, 5, printFrames);
644
+ * }
645
+ *
646
+ * // Prints an array of objects containing raw image data, filenames, and extensions.
647
+ * function printFrames(frames) {
648
+ * for (let frame of frames) {
649
+ * print(frame);
650
+ * }
529
651
* }
530
652
* </code>
531
653
* </div>
0 commit comments