-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathpixels.js
More file actions
1041 lines (1013 loc) · 29.4 KB
/
pixels.js
File metadata and controls
1041 lines (1013 loc) · 29.4 KB
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module Image
* @submodule Pixels
* @for p5
*/
import Filters from './filters';
function pixels(p5, fn){
/**
* Copies a region of pixels from one image to another.
*
* The first parameter, `srcImage`, is the
* <a href="#/p5.Image">p5.Image</a> object to blend.
*
* The next four parameters, `sx`, `sy`, `sw`, and `sh` determine the region
* to blend from the source image. `(sx, sy)` is the top-left corner of the
* region. `sw` and `sh` are the regions width and height.
*
* The next four parameters, `dx`, `dy`, `dw`, and `dh` determine the region
* of the canvas to blend into. `(dx, dy)` is the top-left corner of the
* region. `dw` and `dh` are the regions width and height.
*
* The tenth parameter, `blendMode`, sets the effect used to blend the images'
* colors. The options are `BLEND`, `DARKEST`, `LIGHTEST`, `DIFFERENCE`,
* `MULTIPLY`, `EXCLUSION`, `SCREEN`, `REPLACE`, `OVERLAY`, `HARD_LIGHT`,
* `SOFT_LIGHT`, `DODGE`, `BURN`, `ADD`, or `NORMAL`
*
* @method blend
* @param {p5.Image} srcImage source image.
* @param {Integer} sx x-coordinate of the source's upper-left corner.
* @param {Integer} sy y-coordinate of the source's upper-left corner.
* @param {Integer} sw source image width.
* @param {Integer} sh source image height.
* @param {Integer} dx x-coordinate of the destination's upper-left corner.
* @param {Integer} dy y-coordinate of the destination's upper-left corner.
* @param {Integer} dw destination image width.
* @param {Integer} dh destination image height.
* @param {(BLEND|DARKEST|LIGHTEST|DIFFERENCE|MULTIPLY|EXCLUSION|SCREEN|REPLACE|OVERLAY|HARD_LIGHT|SOFT_LIGHT|DODGE|BURN|ADD|NORMAL)} blendMode the blend mode. either
* BLEND, DARKEST, LIGHTEST, DIFFERENCE,
* MULTIPLY, EXCLUSION, SCREEN, REPLACE, OVERLAY, HARD_LIGHT,
* SOFT_LIGHT, DODGE, BURN, ADD or NORMAL.
*
* @example
* let img0;
* let img1;
*
* async function setup() {
* // Load the images.
* img0 = await loadImage('assets/rockies.jpg');
* img1 = await loadImage('assets/bricks_third.jpg');
*
* createCanvas(100, 100);
*
* // Use the mountains as the background.
* background(img0);
*
* // Display the bricks.
* image(img1, 0, 0);
*
* // Display the bricks faded into the landscape.
* blend(img1, 0, 0, 33, 100, 67, 0, 33, 100, LIGHTEST);
*
* describe('A wall of bricks in front of a mountain landscape. The same wall of bricks appears faded on the right of the image.');
* }
*
* @example
* let img0;
* let img1;
*
* async function setup() {
* // Load the images.
* img0 = await loadImage('assets/rockies.jpg');
* img1 = await loadImage('assets/bricks_third.jpg');
*
* createCanvas(100, 100);
*
* // Use the mountains as the background.
* background(img0);
*
* // Display the bricks.
* image(img1, 0, 0);
*
* // Display the bricks partially transparent.
* blend(img1, 0, 0, 33, 100, 67, 0, 33, 100, DARKEST);
*
* describe('A wall of bricks in front of a mountain landscape. The same wall of bricks appears transparent on the right of the image.');
* }
*
* @example
* let img0;
* let img1;
*
* async function setup() {
* // Load the images.
* img0 = await loadImage('assets/rockies.jpg');
* img1 = await loadImage('assets/bricks_third.jpg');
*
* createCanvas(100, 100);
*
* // Use the mountains as the background.
* background(img0);
*
* // Display the bricks.
* image(img1, 0, 0);
*
* // Display the bricks washed out into the landscape.
* blend(img1, 0, 0, 33, 100, 67, 0, 33, 100, ADD);
*
* describe('A wall of bricks in front of a mountain landscape. The same wall of bricks appears washed out on the right of the image.');
* }
*/
/**
* @method blend
* @param {Integer} sx
* @param {Integer} sy
* @param {Integer} sw
* @param {Integer} sh
* @param {Integer} dx
* @param {Integer} dy
* @param {Integer} dw
* @param {Integer} dh
* @param {(BLEND|DARKEST|LIGHTEST|DIFFERENCE|MULTIPLY|EXCLUSION|SCREEN|REPLACE|OVERLAY|HARD_LIGHT|SOFT_LIGHT|DODGE|BURN|ADD|NORMAL)} blendMode
*/
fn.blend = function(...args) {
// p5._validateParameters('blend', args);
if (this._renderer) {
this._renderer.blend(...args);
} else {
p5.Renderer2D.prototype.blend.apply(this, args);
}
};
/**
* Copies pixels from a source image to a region of the canvas.
*
* The first parameter, `srcImage`, is the
* <a href="#/p5.Image">p5.Image</a> object to blend. The source image can be
* the canvas itself or a
* <a href="#/p5.Image">p5.Image</a> object. `copy()` will scale pixels from
* the source region if it isn't the same size as the destination region.
*
* The next four parameters, `sx`, `sy`, `sw`, and `sh` determine the region
* to copy from the source image. `(sx, sy)` is the top-left corner of the
* region. `sw` and `sh` are the region's width and height.
*
* The next four parameters, `dx`, `dy`, `dw`, and `dh` determine the region
* of the canvas to copy into. `(dx, dy)` is the top-left corner of the
* region. `dw` and `dh` are the region's width and height.
*
* @method copy
* @param {p5.Image|p5.Element} srcImage source image.
* @param {Integer} sx x-coordinate of the source's upper-left corner.
* @param {Integer} sy y-coordinate of the source's upper-left corner.
* @param {Integer} sw source image width.
* @param {Integer} sh source image height.
* @param {Integer} dx x-coordinate of the destination's upper-left corner.
* @param {Integer} dy y-coordinate of the destination's upper-left corner.
* @param {Integer} dw destination image width.
* @param {Integer} dh destination image height.
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/rockies.jpg');
*
* createCanvas(100, 100);
*
* // Use the mountains as the background.
* background(img);
*
* // Copy a region of pixels to another spot.
* copy(img, 7, 22, 10, 10, 35, 25, 50, 50);
*
* // Outline the copied region.
* stroke(255);
* noFill();
* square(7, 22, 10);
*
* describe('An image of a mountain landscape. A square region is outlined in white. A larger square contains a pixelated view of the outlined region.');
* }
*/
/**
* @method copy
* @param {Integer} sx
* @param {Integer} sy
* @param {Integer} sw
* @param {Integer} sh
* @param {Integer} dx
* @param {Integer} dy
* @param {Integer} dw
* @param {Integer} dh
*/
fn.copy = function(...args) {
let srcImage, sx, sy, sw, sh, dx, dy, dw, dh;
if (args.length === 9) {
srcImage = args[0];
sx = args[1];
sy = args[2];
sw = args[3];
sh = args[4];
dx = args[5];
dy = args[6];
dw = args[7];
dh = args[8];
} else if (args.length === 8) {
srcImage = this;
sx = args[0];
sy = args[1];
sw = args[2];
sh = args[3];
dx = args[4];
dy = args[5];
dw = args[6];
dh = args[7];
} else {
throw new Error('Signature not supported');
}
fn._copyHelper(this, srcImage, sx, sy, sw, sh, dx, dy, dw, dh);
};
fn._copyHelper = (
dstImage,
srcImage,
sx,
sy,
sw,
sh,
dx,
dy,
dw,
dh
) => {
const s = srcImage.canvas.width / srcImage.width;
// adjust coord system for 3D when renderer
// ie top-left = -width/2, -height/2
let sxMod = 0;
let syMod = 0;
if (srcImage._renderer && srcImage._renderer.isP3D) {
sxMod = srcImage.width / 2;
syMod = srcImage.height / 2;
}
if (dstImage._renderer && dstImage._renderer.isP3D) {
dstImage.push();
dstImage.resetMatrix();
dstImage.noLights();
dstImage.blendMode(dstImage.BLEND);
dstImage.imageMode(dstImage.CORNER);
dstImage._renderer.image(
srcImage,
sx + sxMod,
sy + syMod,
sw,
sh,
dx,
dy,
dw,
dh
);
dstImage.pop();
} else {
dstImage.drawingContext.drawImage(
srcImage.canvas,
s * (sx + sxMod),
s * (sy + syMod),
s * sw,
s * sh,
dx,
dy,
dw,
dh
);
}
};
/**
* Applies an image filter to the canvas.
*
* The preset options are:
*
* `INVERT`
* Inverts the colors in the image. No parameter is used.
*
* `GRAY`
* Converts the image to grayscale. No parameter is used.
*
* `THRESHOLD`
* Converts the image to black and white. Pixels with a grayscale value
* above a given threshold are converted to white. The rest are converted to
* black. The threshold must be between 0.0 (black) and 1.0 (white). If no
* value is specified, 0.5 is used.
*
* `OPAQUE`
* Sets the alpha channel to entirely opaque. No parameter is used.
*
* `POSTERIZE`
* Limits the number of colors in the image. Each color channel is limited to
* the number of colors specified. Values between 2 and 255 are valid, but
* results are most noticeable with lower values. The default value is 4.
*
* `BLUR`
* Blurs the image. The level of blurring is specified by a blur radius. Larger
* values increase the blur. The default value is 4. A gaussian blur is used
* in `P2D` mode. A box blur is used in `WEBGL` mode.
*
* `ERODE`
* Reduces the light areas. No parameter is used.
*
* `DILATE`
* Increases the light areas. No parameter is used.
*
* `filter()` uses WebGL in the background by default because it's faster.
* This can be disabled in `P2D` mode by adding a `false` argument, as in
* `filter(BLUR, false)`. This may be useful to keep computation off the GPU
* or to work around a lack of WebGL support.
*
* In WebgL mode, `filter()` can also use custom shaders. See
* <a href="#/p5/createFilterShader">createFilterShader()</a> for more
* information.
*
*
* @method filter
* @param {(THRESHOLD|GRAY|OPAQUE|INVERT|POSTERIZE|BLUR|ERODE|DILATE|BLUR)} filterType either THRESHOLD, GRAY, OPAQUE, INVERT,
* POSTERIZE, BLUR, ERODE, DILATE or BLUR.
* @param {Number} [filterParam] parameter unique to each filter.
* @param {Boolean} [useWebGL=true] flag to control whether to use fast
* WebGL filters (GPU) or original image
* filters (CPU); defaults to `true`.
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the INVERT filter.
* filter(INVERT);
*
* describe('A blue brick wall.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the GRAY filter.
* filter(GRAY);
*
* describe('A brick wall drawn in grayscale.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the THRESHOLD filter.
* filter(THRESHOLD);
*
* describe('A brick wall drawn in black and white.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the OPAQUE filter.
* filter(OPAQUE);
*
* describe('A red brick wall.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the POSTERIZE filter.
* filter(POSTERIZE, 3);
*
* describe('An image of a red brick wall drawn with limited color palette.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the BLUR filter.
* filter(BLUR, 3);
*
* describe('A blurry image of a red brick wall.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the DILATE filter.
* filter(DILATE);
*
* describe('A red brick wall with bright lines between each brick.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the ERODE filter.
* filter(ERODE);
*
* describe('A red brick wall with faint lines between each brick.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/bricks.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Apply the BLUR filter.
* // Don't use WebGL.
* filter(BLUR, 3, false);
*
* describe('A blurry image of a red brick wall.');
* }
*/
/**
* @method getFilterGraphicsLayer
* @private
* @returns {p5.Graphics}
*/
fn.getFilterGraphicsLayer = function() {
return this._renderer.getFilterGraphicsLayer();
};
/**
* @method filter
* @param {(THRESHOLD|GRAY|OPAQUE|INVERT|POSTERIZE|BLUR|ERODE|DILATE|BLUR)} filterType
* @param {Number} [filterParam]
* @param {Boolean} [useWebGL=true]
*/
/**
* @method filter
* @param {p5.Shader} shaderFilter shader that's been loaded, with the
* frag shader using a `tex0` uniform.
*/
fn.filter = function(...args) {
// p5._validateParameters('filter', args);
let { shader, operation, value, useWebGL } = parseFilterArgs(...args);
// when passed a shader, use it directly
if (this._renderer.isP3D && shader) {
this._renderer.filter(shader);
return;
}
// when opting out of webgl, use old pixels method
if (!useWebGL && !this._renderer.isP3D) {
if (this.canvas !== undefined) {
Filters.apply(this.canvas, Filters[operation], value);
} else {
Filters.apply(this.elt, Filters[operation], value);
}
return;
}
if(!useWebGL && this._renderer.isP3D) {
console.warn('filter() with useWebGL=false is not supported in WEBGL');
}
// when this is a webgl renderer, apply constant shader filter
if (this._renderer.isP3D) {
this._renderer.filter(operation, value);
}
// when this is P2D renderer, create/use hidden webgl renderer
else {
if (shader) {
this._renderer.filterRenderer.setOperation(operation, value, shader);
} else {
this._renderer.filterRenderer.setOperation(operation, value);
}
this._renderer.filterRenderer.applyFilter();
}
};
function parseFilterArgs(...args) {
// args could be:
// - operation, value, [useWebGL]
// - operation, [useWebGL]
// - shader
let result = {
shader: undefined,
operation: undefined,
value: undefined,
useWebGL: true
};
if (args[0] instanceof p5.Shader) {
result.shader = args[0];
return result;
}
else {
result.operation = args[0];
}
if (args.length > 1 && typeof args[1] === 'number') {
result.value = args[1];
}
if (args[args.length-1] === false) {
result.useWebGL = false;
}
return result;
}
/**
* Gets a pixel or a region of pixels from the canvas.
*
* `get()` is easy to use but it's not as fast as
* <a href="#/p5/pixels">pixels</a>. Use <a href="#/p5/pixels">pixels</a>
* to read many pixel values.
*
* The version of `get()` with no parameters returns the entire canvas.
*
* The version of `get()` with two parameters interprets them as
* coordinates. It returns an array with the `[R, G, B, A]` values of the
* pixel at the given point.
*
* The version of `get()` with four parameters interprets them as coordinates
* and dimensions. It returns a subsection of the canvas as a
* <a href="#/p5.Image">p5.Image</a> object. The first two parameters are the
* coordinates for the upper-left corner of the subsection. The last two
* parameters are the width and height of the subsection.
*
* Use <a href="#/p5.Image/get">p5.Image.get()</a> to work directly with
* <a href="#/p5.Image">p5.Image</a> objects.
*
* @method get
* @param {Number} x x-coordinate of the pixel.
* @param {Number} y y-coordinate of the pixel.
* @param {Number} w width of the subsection to be returned.
* @param {Number} h height of the subsection to be returned.
* @return {p5.Image} subsection as a <a href="#/p5.Image">p5.Image</a> object.
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/rockies.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Get the entire canvas.
* let c = get();
*
* // Display half the canvas.
* image(c, 50, 0);
*
* describe('Two identical mountain landscapes shown side-by-side.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/rockies.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Get the color of a pixel.
* let c = get(50, 90);
*
* // Style the square with the pixel's color.
* fill(c);
* noStroke();
*
* // Display the square.
* square(25, 25, 50);
*
* describe('A mountain landscape with an olive green square in its center.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/rockies.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0);
*
* // Get a region of the image.
* let c = get(0, 0, 50, 50);
*
* // Display the region.
* image(c, 50, 50);
*
* describe('A mountain landscape drawn on top of another mountain landscape.');
* }
*/
/**
* @method get
* @return {p5.Image} whole canvas as a <a href="#/p5.Image">p5.Image</a>.
*/
/**
* @method get
* @param {Number} x
* @param {Number} y
* @return {Number[]} color of the pixel at (x, y) in array format `[R, G, B, A]`.
*/
fn.get = function(x, y, w, h) {
// p5._validateParameters('get', arguments);
return this._renderer.get(...arguments);
};
/**
* Loads the current value of each pixel on the canvas into the
* <a href="#/p5/pixels">pixels</a> array.
*
* `loadPixels()` must be called before reading from or writing to
* <a href="#/p5/pixels">pixels</a>.
*
* @method loadPixels
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/rockies.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0, 100, 100);
*
* // Get the pixel density.
* let d = pixelDensity();
*
* // Calculate the halfway index in the pixels array.
* let halfImage = 4 * (d * width) * (d * height / 2);
*
* // Load the pixels array.
* loadPixels();
*
* // Copy the top half of the canvas to the bottom.
* for (let i = 0; i < halfImage; i += 1) {
* pixels[i + halfImage] = pixels[i];
* }
*
* // Update the canvas.
* updatePixels();
*
* describe('Two identical images of mountain landscapes, one on top of the other.');
* }
*/
fn.loadPixels = function(...args) {
// p5._validateParameters('loadPixels', args);
return this._renderer.loadPixels();
};
/**
* Sets the color of a pixel or draws an image to the canvas.
*
* `set()` is easy to use but it's not as fast as
* <a href="#/p5/pixels">pixels</a>. Use <a href="#/p5/pixels">pixels</a>
* to set many pixel values.
*
* `set()` interprets the first two parameters as x- and y-coordinates. It
* interprets the last parameter as a grayscale value, a `[R, G, B, A]` pixel
* array, a <a href="#/p5.Color">p5.Color</a> object, or a
* <a href="#/p5.Image">p5.Image</a> object. If an image is passed, the first
* two parameters set the coordinates for the image's upper-left corner,
* regardless of the current <a href="#/p5/imageMode">imageMode()</a>.
*
* <a href="#/p5/updatePixels">updatePixels()</a> must be called after using
* `set()` for changes to appear.
*
* @method set
* @param {Number} x x-coordinate of the pixel.
* @param {Number} y y-coordinate of the pixel.
* @param {Number|Number[]|Object} c grayscale value | pixel array |
* <a href="#/p5.Color">p5.Color</a> object | <a href="#/p5.Image">p5.Image</a> to copy.
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Set four pixels to black.
* set(30, 20, 0);
* set(85, 20, 0);
* set(85, 75, 0);
* set(30, 75, 0);
*
* // Update the canvas.
* updatePixels();
*
* describe('Four black dots arranged in a square drawn on a gray background.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a p5.Color object.
* let black = color(0);
*
* // Set four pixels to black.
* set(30, 20, black);
* set(85, 20, black);
* set(85, 75, black);
* set(30, 75, black);
*
* // Update the canvas.
* updatePixels();
*
* describe('Four black dots arranged in a square drawn on a gray background.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(255);
*
* // Draw a horizontal color gradient.
* for (let x = 0; x < 100; x += 1) {
* for (let y = 0; y < 100; y += 1) {
* // Calculate the grayscale value.
* let c = map(x, 0, 100, 0, 255);
*
* // Set the pixel using the grayscale value.
* set(x, y, c);
* }
* }
*
* // Update the canvas.
* updatePixels();
*
* describe('A horiztonal color gradient from black to white.');
* }
*
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/rockies.jpg');
*
* createCanvas(100, 100);
*
* // Use the image to set all pixels.
* set(0, 0, img);
*
* // Update the canvas.
* updatePixels();
*
* describe('An image of a mountain landscape.');
* }
*/
fn.set = function(x, y, imgOrCol) {
this._renderer.set(x, y, imgOrCol);
};
/**
* Updates the canvas with the RGBA values in the
* <a href="#/p5/pixels">pixels</a> array.
*
* `updatePixels()` only needs to be called after changing values in the
* <a href="#/p5/pixels">pixels</a> array. Such changes can be made directly
* after calling <a href="#/p5/loadPixels">loadPixels()</a> or by calling
* <a href="#/p5/set">set()</a>.
*
* @method updatePixels
* @param {Number} [x] x-coordinate of the upper-left corner of region
* to update.
* @param {Number} [y] y-coordinate of the upper-left corner of region
* to update.
* @param {Number} [w] width of region to update.
* @param {Number} [h] height of region to update.
* @example
* let img;
*
* async function setup() {
* // Load the image.
* img = await loadImage('assets/rockies.jpg');
*
* createCanvas(100, 100);
*
* // Display the image.
* image(img, 0, 0, 100, 100);
*
* // Get the pixel density.
* let d = pixelDensity();
*
* // Calculate the halfway index in the pixels array.
* let halfImage = 4 * (d * width) * (d * height / 2);
*
* // Load the pixels array.
* loadPixels();
*
* // Copy the top half of the canvas to the bottom.
* for (let i = 0; i < halfImage; i += 1) {
* pixels[i + halfImage] = pixels[i];
* }
*
* // Update the canvas.
* updatePixels();
*
* describe('Two identical images of mountain landscapes, one on top of the other.');
* }
*/
fn.updatePixels = function(x, y, w, h) {
// p5._validateParameters('updatePixels', arguments);
// graceful fail - if loadPixels() or set() has not been called, pixel
// array will be empty, ignore call to updatePixels()
if (this.pixels.length === 0) {
return;
}
this._renderer.updatePixels(x, y, w, h);
};
/**
* An array containing the color of each pixel on the canvas.
*
* Colors are stored as numbers representing red, green, blue, and alpha
* (RGBA) values. `pixels` is a one-dimensional array for performance reasons.
*
* Each pixel occupies four elements in the `pixels` array, one for each RGBA
* value. For example, the pixel at coordinates (0, 0) stores its RGBA values
* at `pixels[0]`, `pixels[1]`, `pixels[2]`, and `pixels[3]`, respectively.
* The next pixel at coordinates (1, 0) stores its RGBA values at `pixels[4]`,
* `pixels[5]`, `pixels[6]`, and `pixels[7]`. And so on. The `pixels` array
* for a 100×100 canvas has 100 × 100 × 4 = 40,000 elements.
*
* Some displays use several smaller pixels to set the color at a single
* point. The <a href="#/p5/pixelDensity">pixelDensity()</a> function returns
* the pixel density of the canvas. High density displays often have a
* <a href="#/p5/pixelDensity">pixelDensity()</a> of 2. On such a display, the
* `pixels` array for a 100×100 canvas has 200 × 200 × 4 =
* 160,000 elements.
*
* Accessing the RGBA values for a point on the canvas requires a little math
* as shown below. The <a href="#/p5/loadPixels">loadPixels()</a> function
* must be called before accessing the `pixels` array. The
* <a href="#/p5/updatePixels">updatePixels()</a> function must be called
* after any changes are made.
*
* @property {Number[]} pixels
*
* @example
* function setup() {
* createCanvas(100, 100);
* background(128);
*
* // Load the pixels array.
* loadPixels();
*
* // Set the dot's coordinates.
* let x = 50;
* let y = 50;
*
* // Get the pixel density.
* let d = pixelDensity();
*
* // Set the pixel(s) at the center of the canvas black.
* for (let i = 0; i < d; i += 1) {
* for (let j = 0; j < d; j += 1) {
* let index = 4 * ((y * d + j) * width * d + (x * d + i));
* // Red.
* pixels[index] = 0;
* // Green.
* pixels[index + 1] = 0;
* // Blue.
* pixels[index + 2] = 0;
* // Alpha.
* pixels[index + 3] = 255;
* }
* }
*
* // Update the canvas.
* updatePixels();
*
* describe('A black dot in the middle of a gray rectangle.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Load the pixels array.
* loadPixels();
*
* // Get the pixel density.
* let d = pixelDensity();
*
* // Calculate the halfway index in the pixels array.
* let halfImage = 4 * (d * width) * (d * height / 2);
*
* // Make the top half of the canvas red.
* for (let i = 0; i < halfImage; i += 4) {
* // Red.
* pixels[i] = 255;
* // Green.
* pixels[i + 1] = 0;
* // Blue.
* pixels[i + 2] = 0;
* // Alpha.
* pixels[i + 3] = 255;
* }
*
* // Update the canvas.
* updatePixels();