Skip to content

Commit 975d150

Browse files
author
lauren mccarthy
committed
docs
1 parent 6773433 commit 975d150

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

src/objects/p5.Image.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,45 @@ define(function (require) {
6464
* (indices 0-3) in the array will be the R, G, B, A values of the pixel at
6565
* (0, 0). The second four values (indices 4-7) will contain the R, G, B, A
6666
* values of the pixel at (1, 0). More generally, to set values for a pixel
67-
* at (x, y):
68-
* <code>pixels[y*width+x] = r;
69-
* pixels[y*width+x+1] = g;
70-
* pixels[y*width+x+2] = b;
67+
* at (x, y):<br>
68+
* <code>pixels[y*width+x] = r; <br>
69+
* pixels[y*width+x+1] = g;<br>
70+
* pixels[y*width+x+2] = b;<br>
7171
* pixels[y*width+x+3] = a;</code>
7272
* <br><br>
7373
* Before accessing this array, the data must loaded with the loadPixels()
7474
* function. After the array data has been modified, the updatePixels()
7575
* function must be run to update the changes.
7676
* @property pixels[]
77+
* @example
78+
* <div>
79+
* <code>
80+
* img = createImage(66, 66);
81+
* img.loadPixels();
82+
* for (i = 0; i < img.width; i++) {
83+
* for (j = 0; j < img.height; j++) {
84+
* img.set(i, j, color(0, 90, 102));
85+
* }
86+
* }
87+
* img.updatePixels();
88+
* image(img, 17, 17);
89+
* </code>
90+
* </div>
91+
* <div>
92+
* <code>
93+
* var pink = color(255, 102, 204);
94+
* img = createImage(66, 66);
95+
* img.loadPixels();
96+
* for (var i = 0; i < 4*(width*height/2); i+=4) {
97+
* img.pixels[i] = red(pink);
98+
* img.pixels[i+1] = green(pink);
99+
* img.pixels[i+2] = blue(pink);
100+
* img.pixels[i+3] = alpha(pink);
101+
* }
102+
* img.updatePixels();
103+
* image(img, 17, 17);
104+
* </code>
105+
* </div>
77106
*/
78107
this.pixels = [];
79108
};
@@ -141,13 +170,28 @@ define(function (require) {
141170
*
142171
* Note that for a large number of pixels this will
143172
* be slower than directly manipulating the pixels array
144-
* and then calling updatePixels()
173+
* and then calling updatePixels().
145174
*
146175
* @method set
147176
* @param {Number} x x-coordinate of the pixel
148177
* @param {Number} y y-coordinate of the pixel
149178
* @param {Number|Array|Object} a grayscale value | pixel array |
150179
* a p5.Color | image to copy
180+
* @example
181+
* <div>
182+
* <code>
183+
* img = createImage(66, 66);
184+
* img.loadPixels();
185+
* for (i = 0; i < img.width; i++) {
186+
* for (j = 0; j < img.height; j++) {
187+
* img.set(i, j, color(0, 90, 102, i % img.width * 2));
188+
* }
189+
* }
190+
* img.updatePixels();
191+
* image(img, 17, 17);
192+
* image(img, 34, 34);
193+
* </code>
194+
* </div>
151195
*/
152196
p5.Image.prototype.set = function(x, y, imgOrCol){
153197
p5.prototype.set.call(this, x, y, imgOrCol);

0 commit comments

Comments
 (0)