-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathp5.Image.js
1593 lines (1546 loc) · 42.2 KB
/
p5.Image.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
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 Image
* @requires core
* @requires constants
* @requires filters
*/
/**
* This module defines the <a href="#/p5.Image">p5.Image</a> class and P5 methods for
* drawing images to the main display canvas.
*/
import p5 from '../core/main';
import Filters from './filters';
/*
* Class methods
*/
/**
* A class to describe an image. Images are rectangular grids of pixels that
* can be displayed and modified.
*
* Existing images can be loaded by calling
* <a href="#/p5/loadImage">loadImage()</a>. Blank images can be created by
* calling <a href="#/p5/createImage">createImage()</a>. `p5.Image` objects
* have methods for common tasks such as applying filters and modifying
* pixel values.
*
* @example
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/bricks.jpg');
* }
*
* function setup() {
* image(img, 0, 0);
*
* describe('An image of a brick wall.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/bricks.jpg');
* }
*
* function setup() {
* img.filter(GRAY);
* image(img, 0, 0);
*
* describe('A grayscale image of a brick wall.');
* }
* </code>
* </div>
*
* <div>
* <code>
* background(200);
* let img = createImage(66, 66);
* img.loadPixels();
* for (let x = 0; x < img.width; x += 1) {
* for (let y = 0; y < img.height; y += 1) {
* img.set(x, y, 0);
* }
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A black square drawn in the middle of a gray square.');
* </code>
* </div>
*
* @class p5.Image
* @constructor
* @param {Number} width
* @param {Number} height
*/
p5.Image = class {
constructor(width, height) {
/**
* Image width.
* @type {Number}
* @property {Number} width
* @name width
* @readOnly
* @example
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
*
* function setup() {
* image(img, 0, 0);
* let x = img.width / 2;
* let y = img.height / 2;
* let d = 20;
* circle(x, y, d);
*
* describe('An image of a mountain landscape with a white circle drawn in the middle.');
* }
* </code>
* </div>
*/
this.width = width;
/**
* Image height.
* @type {Number}
* @property height
* @name height
* @readOnly
* @example
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
*
* function setup() {
* image(img, 0, 0);
* let x = img.width / 2;
* let y = img.height / 2;
* let d = 20;
* circle(x, y, d);
*
* describe('An image of a mountain landscape with a white circle drawn in the middle.');
* }
* </code>
* </div>
*/
this.height = height;
this.canvas = document.createElement('canvas');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.drawingContext = this.canvas.getContext('2d');
this._pixelsState = this;
this._pixelDensity = 1;
//Object for working with GIFs, defaults to null
this.gifProperties = null;
//For WebGL Texturing only: used to determine whether to reupload texture to GPU
this._modified = false;
/**
* An array containing the color of each pixel in the
* <a href="#/p5.Image">p5.Image</a> object. Colors are stored as numbers
* representing red, green, blue, and alpha (RGBA) values. `img.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 `img.pixels[0]`, `img.pixels[1]`, `img.pixels[2]`,
* and `img.pixels[3]`, respectively. The next pixel at coordinates (1, 0)
* stores its RGBA values at `img.pixels[4]`, `img.pixels[5]`,
* `img.pixels[6]`, and `img.pixels[7]`. And so on. The `img.pixels` array
* for a 100×100 <a href="#/p5.Image">p5.Image</a> object has
* 100 × 100 × 4 = 40,000 elements.
*
* Accessing the RGBA values for a pixel in the
* <a href="#/p5.Image">p5.Image</a> object requires a little math as
* shown below. The <a href="#/p5.Image/loadPixels">img.loadPixels()</a>
* method must be called before accessing the `img.pixels` array. The
* <a href="#/p5.Image/updatePixels">img.updatePixels()</a> method must be
* called after any changes are made.
*
* @property {Number[]} pixels
* @name pixels
* @example
* <div>
* <code>
* let img = createImage(66, 66);
* img.loadPixels();
* let numPixels = 4 * img.width * img.height;
* for (let i = 0; i < numPixels; i += 4) {
* // Red.
* img.pixels[i] = 0;
* // Green.
* img.pixels[i + 1] = 0;
* // Blue.
* img.pixels[i + 2] = 0;
* // Alpha.
* img.pixels[i + 3] = 255;
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A black square drawn in the middle of a gray square.');
* </code>
* </div>
*
* <div>
* <code>
* let img = createImage(66, 66);
* img.loadPixels();
* let numPixels = 4 * img.width * img.height;
* for (let i = 0; i < numPixels; i += 4) {
* // Red.
* img.pixels[i] = 255;
* // Green.
* img.pixels[i + 1] = 0;
* // Blue.
* img.pixels[i + 2] = 0;
* // Alpha.
* img.pixels[i + 3] = 255;
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A red square drawn in the middle of a gray square.');
* </code>
* </div>
*/
this.pixels = [];
}
/**
* Gets or sets the pixel density for high pixel density displays. By default,
* the density will be set to 1.
*
* Call this method with no arguments to get the default density, or pass
* in a number to set the density. If a non-positive number is provided,
* it defaults to 1.
*
* @method pixelDensity
* @param {Number} [density] A scaling factor for the number of pixels per
* side
* @returns {Number} The current density if called without arguments, or the instance for chaining if setting density.
*/
pixelDensity(density) {
if (typeof density !== 'undefined') {
// Setter: set the density and handle resize
if (density <= 0) {
const errorObj = {
type: 'INVALID_VALUE',
format: { types: ['Number'] },
position: 1
};
p5._friendlyParamError(errorObj, 'pixelDensity');
// Default to 1 in case of an invalid value
density = 1;
}
this._pixelDensity = density;
// Adjust canvas dimensions based on pixel density
this.width /= density;
this.height /= density;
return this; // Return the image instance for chaining if needed
} else {
// Getter: return the default density
return this._pixelDensity;
}
}
/**
* Helper function for animating GIF-based images with time
*/
_animateGif(pInst) {
const props = this.gifProperties;
const curTime = pInst._lastRealFrameTime;
if (props.lastChangeTime === 0) {
props.lastChangeTime = curTime;
}
if (props.playing) {
props.timeDisplayed = curTime - props.lastChangeTime;
const curDelay = props.frames[props.displayIndex].delay;
if (props.timeDisplayed >= curDelay) {
//GIF is bound to 'realtime' so can skip frames
const skips = Math.floor(props.timeDisplayed / curDelay);
props.timeDisplayed = 0;
props.lastChangeTime = curTime;
props.displayIndex += skips;
props.loopCount = Math.floor(props.displayIndex / props.numFrames);
if (props.loopLimit !== null && props.loopCount >= props.loopLimit) {
props.playing = false;
} else {
const ind = props.displayIndex % props.numFrames;
this.drawingContext.putImageData(props.frames[ind].image, 0, 0);
props.displayIndex = ind;
this.setModified(true);
}
}
}
}
/**
* Helper fxn for sharing pixel methods
*/
_setProperty(prop, value) {
this[prop] = value;
this.setModified(true);
}
/**
* Loads the current value of each pixel in the
* <a href="#/p5.Image">p5.Image</a> object into the `img.pixels` array.
* This method must be called before reading or modifying pixel values.
*
* @method loadPixels
* @example
* <div>
* <code>
* let img = createImage(66, 66);
* img.loadPixels();
* for (let x = 0; x < img.width; x += 1) {
* for (let y = 0; y < img.height; y += 1) {
* img.set(x, y, 0);
* }
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A black square drawn in the middle of a gray square.');
* </code>
* </div>
*
* <div>
* <code>
* let img = createImage(66, 66);
* img.loadPixels();
* let numPixels = 4 * img.width * img.height;
* for (let i = 0; i < numPixels; i += 4) {
* // Red.
* img.pixels[i] = 0;
* // Green.
* img.pixels[i + 1] = 0;
* // Blue.
* img.pixels[i + 2] = 0;
* // Alpha.
* img.pixels[i + 3] = 255;
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A black square drawn in the middle of a gray square.');
* </code>
* </div>
*/
loadPixels() {
p5.Renderer2D.prototype.loadPixels.call(this);
this.setModified(true);
}
/**
* Updates the canvas with the RGBA values in the
* <a href="#/p5.Image/pixels">img.pixels</a> array.
*
* `img.updatePixels()` only needs to be called after changing values in
* the <a href="#/p5.Image/pixels">img.pixels</a> array. Such changes can be
* made directly after calling
* <a href="#/p5.Image/loadPixels">img.loadPixels()</a> or by calling
* <a href="#/p5.Image/set">img.set()</a>.
*
* The optional parameters `x`, `y`, `width`, and `height` define a
* subsection of the <a href="#/p5.Image">p5.Image</a> object to update.
* Doing so can improve performance in some cases.
*
* If the <a href="#/p5.Image">p5.Image</a> object was loaded from a GIF,
* then calling `img.updatePixels()` will update the pixels in current
* frame.
*
* @method updatePixels
* @param {Integer} x x-coordinate of the upper-left corner
* of the subsection to update.
* @param {Integer} y y-coordinate of the upper-left corner
* of the subsection to update.
* @param {Integer} w width of the subsection to update.
* @param {Integer} h height of the subsection to update.
* @example
* <div>
* <code>
* let img = createImage(66, 66);
* img.loadPixels();
* for (let x = 0; x < img.width; x += 1) {
* for (let y = 0; y < img.height; y += 1) {
* img.set(x, y, 0);
* }
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A black square drawn in the middle of a gray square.');
* </code>
* </div>
*
* <div>
* <code>
* let img = createImage(66, 66);
* img.loadPixels();
* let numPixels = 4 * img.width * img.height;
* for (let i = 0; i < numPixels; i += 4) {
* // Red.
* img.pixels[i] = 0;
* // Green.
* img.pixels[i + 1] = 0;
* // Blue.
* img.pixels[i + 2] = 0;
* // Alpha.
* img.pixels[i + 3] = 255;
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A black square drawn in the middle of a gray square.');
* </code>
* </div>
*/
/**
* @method updatePixels
*/
updatePixels(x, y, w, h) {
p5.Renderer2D.prototype.updatePixels.call(this, x, y, w, h);
this.setModified(true);
}
/**
* Gets a pixel or a region of pixels from a
* <a href="#/p5.Image">p5.Image</a> object.
*
* `img.get()` is easy to use but it's not as fast as
* <a href="#/p5.Image/pixels">img.pixels</a>. Use
* <a href="#/p5.Image/pixels">img.pixels</a> to read many pixel values.
*
* The version of `img.get()` with no parameters returns the entire image.
*
* The version of `img.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 `img.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">img.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
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
*
* function setup() {
* image(img, 0, 0);
* let img2 = get();
* image(img2, width / 2, 0);
*
* describe('Two identical mountain landscapes shown side-by-side.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
*
* function setup() {
* image(img, 0, 0);
* let c = img.get(50, 90);
* fill(c);
* noStroke();
* square(25, 25, 50);
*
* describe('A mountain landscape with an olive green square in its center.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
*
* function setup() {
* image(img, 0, 0);
* let img2 = img.get(0, 0, img.width / 2, img.height / 2);
* image(img2, width / 2, height / 2);
*
* describe('A mountain landscape drawn on top of another mountain landscape.');
* }
* </code>
* </div>
*/
/**
* @method get
* @return {p5.Image} whole <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]`.
*/
get(x, y, w, h) {
p5._validateParameters('p5.Image.get', arguments);
return p5.Renderer2D.prototype.get.apply(this, arguments);
}
_getPixel(...args) {
return p5.Renderer2D.prototype._getPixel.apply(this, args);
}
/**
* Sets the color of one or more pixels within a
* <a href="#/p5.Image">p5.Image</a> object.
*
* `img.set()` is easy to use but it's not as fast as
* <a href="#/p5.Image/pixels">img.pixels</a>. Use
* <a href="#/p5.Image/pixels">img.pixels</a> to set many pixel values.
*
* `img.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 another
* <a href="#/p5.Image">p5.Image</a> object.
*
* <a href="#/p5.Image/updatePixels">img.updatePixels()</a> must be called
* after using `img.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} a grayscale value | pixel array |
* <a href="#/p5.Color">p5.Color</a> object |
* <a href="#/p5.Image">p5.Image</a> to copy.
* @example
* <div>
* <code>
* let img = createImage(100, 100);
* img.set(30, 20, 0);
* img.set(85, 20, 0);
* img.set(85, 75, 0);
* img.set(30, 75, 0);
* img.updatePixels();
* image(img, 0, 0);
*
* describe('Four black dots arranged in a square drawn on a gray background.');
* </code>
* </div>
*
* <div>
* <code>
* let img = createImage(100, 100);
* let black = color(0);
* img.set(30, 20, black);
* img.set(85, 20, black);
* img.set(85, 75, black);
* img.set(30, 75, black);
* img.updatePixels();
* image(img, 0, 0);
*
* describe('Four black dots arranged in a square drawn on a gray background.');
* </code>
* </div>
*
* <div>
* <code>
* let img = createImage(66, 66);
* for (let x = 0; x < img.width; x += 1) {
* for (let y = 0; y < img.height; y += 1) {
* let c = map(x, 0, img.width, 0, 255);
* img.set(x, y, c);
* }
* }
* img.updatePixels();
* image(img, 17, 17);
*
* describe('A square with a horiztonal color gradient from black to white drawn on a gray background.');
* </code>
* </div>
*
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
*
* function setup() {
* let img2 = createImage(100, 100);
* img2.set(0, 0, img);
* image(img2, 0, 0);
*
* describe('An image of a mountain landscape.');
* }
* </code>
* </div>
*/
set(x, y, imgOrCol) {
p5.Renderer2D.prototype.set.call(this, x, y, imgOrCol);
this.setModified(true);
}
/**
* Resizes the <a href="#/p5.Image">p5.Image</a> object to a given `width`
* and `height`. The image's original aspect ratio can be kept by passing 0
* for either `width` or `height`. For example, calling `img.resize(50, 0)`
* on an image that was 500 × 300 pixels will resize it to
* 50 × 30 pixels.
*
* @method resize
* @param {Number} width resized image width.
* @param {Number} height resized image height.
* @example
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
* function setup() {
* image(img, 0, 0);
* img.resize(50, 100);
* image(img, 0, 0);
*
* describe('Two images of a mountain landscape. One copy of the image is squeezed horizontally.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
* function setup() {
* image(img, 0, 0);
* img.resize(0, 30);
* image(img, 0, 0);
*
* describe('Two images of a mountain landscape. The small copy of the image covers the top-left corner of the larger image.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
* function setup() {
* image(img, 0, 0);
* img.resize(60, 0);
* image(img, 0, 0);
*
* describe('Two images of a mountain landscape. The small copy of the image covers the top-left corner of the larger image.');
* }
* </code>
* </div>
*/
resize(width, height) {
// Copy contents to a temporary canvas, resize the original
// and then copy back.
//
// There is a faster approach that involves just one copy and swapping the
// this.canvas reference. We could switch to that approach if (as i think
// is the case) there an expectation that the user would not hold a
// reference to the backing canvas of a p5.Image. But since we do not
// enforce that at the moment, I am leaving in the slower, but safer
// implementation.
// auto-resize
if (width === 0 && height === 0) {
width = this.canvas.width;
height = this.canvas.height;
} else if (width === 0) {
width = this.canvas.width * height / this.canvas.height;
} else if (height === 0) {
height = this.canvas.height * width / this.canvas.width;
}
width = Math.floor(width);
height = Math.floor(height);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = width;
tempCanvas.height = height;
if (this.gifProperties) {
const props = this.gifProperties;
//adapted from github.com/LinusU/resize-image-data
const nearestNeighbor = (src, dst) => {
let pos = 0;
for (let y = 0; y < dst.height; y++) {
for (let x = 0; x < dst.width; x++) {
const srcX = Math.floor(x * src.width / dst.width);
const srcY = Math.floor(y * src.height / dst.height);
let srcPos = (srcY * src.width + srcX) * 4;
dst.data[pos++] = src.data[srcPos++]; // R
dst.data[pos++] = src.data[srcPos++]; // G
dst.data[pos++] = src.data[srcPos++]; // B
dst.data[pos++] = src.data[srcPos++]; // A
}
}
};
for (let i = 0; i < props.numFrames; i++) {
const resizedImageData = this.drawingContext.createImageData(
width,
height
);
nearestNeighbor(props.frames[i].image, resizedImageData);
props.frames[i].image = resizedImageData;
}
}
tempCanvas.getContext('2d').drawImage(
this.canvas,
0, 0, this.canvas.width, this.canvas.height,
0, 0, tempCanvas.width, tempCanvas.height
);
// Resize the original canvas, which will clear its contents
this.canvas.width = this.width = width;
this.canvas.height = this.height = height;
//Copy the image back
this.drawingContext.drawImage(
tempCanvas,
0, 0, width, height,
0, 0, width, height
);
if (this.pixels.length > 0) {
this.loadPixels();
}
this.setModified(true);
}
/**
*
* Copies pixels from a source <a href="#/p5.Image">p5.Image</a>
* to this one. Calling `img.copy()` will scale pixels from the source
* region if it isn't the same size as the destination region.
*
* @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
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/rockies.jpg');
* }
*
* function setup() {
* img.copy(7, 22, 10, 10, 35, 25, 50, 50);
* image(img, 0, 0);
* // Outline 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.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let mountains;
* let bricks;
*
* function preload() {
* mountains = loadImage('assets/rockies.jpg');
* bricks = loadImage('assets/bricks.jpg');
* }
*
* function setup() {
* let x = bricks.width / 2;
* let y = bricks.height / 2;
* mountains.copy(bricks, 0, 0, x, y, 0, 0, x, y);
* image(mountains, 0, 0);
*
* describe('An image of a brick wall drawn at the top-left of an image of a mountain landscape.');
* }
* </code>
* </div>
*/
/**
* @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
*/
copy(...args) {
p5.prototype.copy.apply(this, args);
}
/**
* Masks part of an image from displaying by loading another
* image and using its alpha channel as an alpha channel for
* this image. Masks are cumulative, once applied to an image
* object, they cannot be removed.
*
* @method mask
* @param {p5.Image} srcImage source image.
*
* @example
* <div>
* <code>
* let photo;
* let maskImage;
*
* function preload() {
* photo = loadImage('assets/rockies.jpg');
* maskImage = loadImage('assets/mask2.png');
* }
*
* function setup() {
* photo.mask(maskImage);
* image(photo, 0, 0);
*
* describe('An image of a mountain landscape. The right side of the image has a faded patch of white.');
* }
* </code>
* </div>
*/
// TODO: - Accept an array of alpha values.
mask(p5Image) {
if (p5Image === undefined) {
p5Image = this;
}
const currBlend = this.drawingContext.globalCompositeOperation;
let scaleFactor = 1;
if (p5Image instanceof p5.Renderer) {
scaleFactor = p5Image._pInst._pixelDensity;
}
const copyArgs = [
p5Image,
0,
0,
scaleFactor * p5Image.width,
scaleFactor * p5Image.height,
0,
0,
this.width,
this.height
];
this.drawingContext.globalCompositeOperation = 'destination-in';
if (this.gifProperties) {
for (let i = 0; i < this.gifProperties.frames.length; i++) {
this.drawingContext.putImageData(
this.gifProperties.frames[i].image,
0,
0
);
this.copy(...copyArgs);
this.gifProperties.frames[i].image = this.drawingContext.getImageData(
0,
0,
this.width,
this.height
);
}
this.drawingContext.putImageData(
this.gifProperties.frames[this.gifProperties.displayIndex].image,
0,
0
);
} else {
this.copy(...copyArgs);
}
this.drawingContext.globalCompositeOperation = currBlend;
this.setModified(true);
}
/**
* Applies an image filter to the <a href="#/p5.Image">p5.Image</a> object.
* 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 be 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.
*
* @method filter
* @param {Constant} filterType either THRESHOLD, GRAY, OPAQUE, INVERT,
* POSTERIZE, ERODE, DILATE or BLUR.
* @param {Number} [filterParam] parameter unique to each filter.
* @example
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/bricks.jpg');
* }
*
* function setup() {
* img.filter(INVERT);
* image(img, 0, 0);
*
* describe('A blue brick wall.');
* }
* </code>
* </div>
*
* <div>
* <code>
* let img;
*
* function preload() {
* img = loadImage('assets/bricks.jpg');
* }
*
* function setup() {
* img.filter(GRAY);
* image(img, 0, 0);
*
* describe('A brick wall drawn in grayscale.');
* }
* </code>
* </div>
*
* <div>