Skip to content

Commit 3a09990

Browse files
authored
Merge pull request processing#6863 from nickmcintyre/ref-image
Update image references
2 parents 48d4027 + b7a6129 commit 3a09990

File tree

1 file changed

+182
-60
lines changed

1 file changed

+182
-60
lines changed

src/image/image.js

Lines changed: 182 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import p5 from '../core/main';
1313
import omggif from 'omggif';
1414

1515
/**
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.
1817
*
19-
* `createImage()` uses the `width` and `height` paremeters to set the new
18+
* `createImage()` uses the `width` and `height` parameters to set the new
2019
* <a href="#/p5.Image">p5.Image</a> object's dimensions in pixels. The new
2120
* <a href="#/p5.Image">p5.Image</a> can be modified by updating its
2221
* <a href="#/p5.Image/pixels">pixels</a> array or by calling its
@@ -27,65 +26,122 @@ import omggif from 'omggif';
2726
* <a href="#/p5.Image/updatePixels">updatePixels()</a> method must be called
2827
* for updates to take effect.
2928
*
29+
* Note: The new <a href="#/p5.Image">p5.Image</a> object is transparent by
30+
* default.
31+
*
3032
* @method createImage
3133
* @param {Integer} width width in pixels.
3234
* @param {Integer} height height in pixels.
3335
* @return {p5.Image} new <a href="#/p5.Image">p5.Image</a> object.
36+
*
3437
* @example
3538
* <div>
3639
* <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+
* }
4256
* }
43-
* }
44-
* img.updatePixels();
45-
* image(img, 17, 17);
4657
*
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+
* }
4866
* </code>
4967
* </div>
5068
*
5169
* <div>
5270
* <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+
* }
6094
* }
61-
* }
62-
* img.updatePixels();
63-
* image(img, 17, 17);
6495
*
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+
* }
66104
* </code>
67105
* </div>
68106
*
69107
* <div>
70108
* <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);
87124
*
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+
* }
89145
* </code>
90146
* </div>
91147
*/
@@ -95,8 +151,22 @@ p5.prototype.createImage = function(width, height) {
95151
};
96152

97153
/**
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.
100170
*
101171
* @method saveCanvas
102172
* @param {p5.Framebuffer|p5.Element|HTMLCanvasElement} selectedCanvas reference to a
@@ -110,7 +180,11 @@ p5.prototype.createImage = function(width, height) {
110180
* function setup() {
111181
* createCanvas(100, 100);
112182
* background(255);
183+
*
184+
* // Save the canvas to 'untitled.png'.
113185
* saveCanvas();
186+
*
187+
* describe('A white square.');
114188
* }
115189
* </code>
116190
* </div>
@@ -119,8 +193,13 @@ p5.prototype.createImage = function(width, height) {
119193
* <code>
120194
* function setup() {
121195
* createCanvas(100, 100);
196+
*
122197
* background(255);
198+
*
199+
* // Save the canvas to 'myCanvas.jpg'.
123200
* saveCanvas('myCanvas.jpg');
201+
*
202+
* describe('A white square.');
124203
* }
125204
* </code>
126205
* </div>
@@ -129,8 +208,13 @@ p5.prototype.createImage = function(width, height) {
129208
* <code>
130209
* function setup() {
131210
* createCanvas(100, 100);
211+
*
132212
* background(255);
213+
*
214+
* // Save the canvas to 'myCanvas.jpg'.
133215
* saveCanvas('myCanvas', 'jpg');
216+
*
217+
* describe('A white square.');
134218
* }
135219
* </code>
136220
* </div>
@@ -139,8 +223,13 @@ p5.prototype.createImage = function(width, height) {
139223
* <code>
140224
* function setup() {
141225
* let cnv = createCanvas(100, 100);
226+
*
142227
* background(255);
228+
*
229+
* // Save the canvas to 'untitled.png'.
143230
* saveCanvas(cnv);
231+
*
232+
* describe('A white square.');
144233
* }
145234
* </code>
146235
* </div>
@@ -149,8 +238,13 @@ p5.prototype.createImage = function(width, height) {
149238
* <code>
150239
* function setup() {
151240
* let cnv = createCanvas(100, 100);
241+
*
152242
* background(255);
243+
*
244+
* // Save the canvas to 'myCanvas.jpg'.
153245
* saveCanvas(cnv, 'myCanvas.jpg');
246+
*
247+
* describe('A white square.');
154248
* }
155249
* </code>
156250
* </div>
@@ -159,8 +253,13 @@ p5.prototype.createImage = function(width, height) {
159253
* <code>
160254
* function setup() {
161255
* let cnv = createCanvas(100, 100);
256+
*
162257
* background(255);
258+
*
259+
* // Save the canvas to 'myCanvas.jpg'.
163260
* saveCanvas(cnv, 'myCanvas', 'jpg');
261+
*
262+
* describe('A white square.');
164263
* }
165264
* </code>
166265
* </div>
@@ -457,27 +556,37 @@ p5.prototype.encodeAndDownloadGif = function(pImg, filename) {
457556
};
458557

459558
/**
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' }`.
462566
*
463567
* The first parameter, `filename`, sets the prefix for the file names. For
464568
* 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'`.
467573
*
468574
* 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.
474581
*
475582
* The fifth parameter, `callback`, is optional. If a function is passed,
476583
* image files won't be saved by default. The callback function can be used
477584
* to process an array containing the data for each captured frame. The array
478585
* of image data contains a sequence of objects with three properties for each
479586
* frame: `imageData`, `filename`, and `extension`.
480587
*
588+
* Note: Frames are downloaded as individual image files by default.
589+
*
481590
* @method saveFrames
482591
* @param {String} filename prefix of file name.
483592
* @param {String} extension file extension, either 'jpg' or 'png'.
@@ -492,15 +601,20 @@ p5.prototype.encodeAndDownloadGif = function(pImg, filename) {
492601
* @example
493602
* <div>
494603
* <code>
604+
* function setup() {
605+
* createCanvas(100, 100);
606+
*
607+
* describe('A square repeatedly changes color from blue to pink.');
608+
* }
609+
*
495610
* function draw() {
496611
* let r = frameCount % 255;
497612
* let g = 50;
498613
* let b = 100;
499614
* background(r, g, b);
500-
*
501-
* describe('A square repeatedly changes color from blue to pink.');
502615
* }
503616
*
617+
* // Save the frames when the user presses the 's' key.
504618
* function keyPressed() {
505619
* if (key === 's') {
506620
* saveFrames('frame', 'png', 1, 5);
@@ -511,21 +625,29 @@ p5.prototype.encodeAndDownloadGif = function(pImg, filename) {
511625
*
512626
* <div>
513627
* <code>
628+
* function setup() {
629+
* createCanvas(100, 100);
630+
*
631+
* describe('A square repeatedly changes color from blue to pink.');
632+
* }
633+
*
514634
* function draw() {
515635
* let r = frameCount % 255;
516636
* let g = 50;
517637
* let b = 100;
518638
* background(r, g, b);
519-
*
520-
* describe('A square repeatedly changes color from blue to pink.');
521639
* }
522640
*
641+
* // Print 5 frames when the user presses the mouse.
523642
* 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+
* }
529651
* }
530652
* </code>
531653
* </div>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy