Skip to content

Commit e71b2cf

Browse files
authored
reset() for p5.Graphics (#3662)
* p5.Graphics.endFrame * p5.Graphics.reset()
1 parent 5ea5907 commit e71b2cf

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/core/p5.Graphics.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,58 @@ p5.Graphics = function(w, h, renderer, pInst) {
6262

6363
p5.Graphics.prototype = Object.create(p5.Element.prototype);
6464

65+
/**
66+
* Resets certain values such as those modified by functions in the Transform category
67+
* and in the Lights category that are not automatically reset
68+
* with graphics buffer objects. Calling this in <a href='#/p5/draw'>draw()</a> will copy the behavior
69+
* of the standard canvas.
70+
*
71+
* @method reset
72+
* @example
73+
*
74+
* <div><code>
75+
* let pg;
76+
* function setup() {
77+
* createCanvas(100, 100);
78+
* background(0);
79+
* pg = createGraphics(50, 100);
80+
* pg.fill(0);
81+
* frameRate(5);
82+
* }
83+
* function draw() {
84+
* image(pg, width / 2, 0);
85+
* pg.background(255);
86+
* // p5.Graphics object behave a bit differently in some cases
87+
* // The normal canvas on the left resets the translate
88+
* // with every loop through draw()
89+
* // the graphics object on the right doesn't automatically reset
90+
* // so translate() is additive and it moves down the screen
91+
* rect(0, 0, width / 2, 5);
92+
* pg.rect(0, 0, width / 2, 5);
93+
* translate(0, 5, 0);
94+
* pg.translate(0, 5, 0);
95+
* }
96+
* function mouseClicked() {
97+
* // if you click you will see that
98+
* // reset() resets the translate back to the initial state
99+
* // of the Graphics object
100+
* pg.reset();
101+
* }
102+
* </code></div>
103+
*
104+
* @alt
105+
* A white line on a black background stays still on the top-left half.
106+
* A black line animates from top to bottom on a white background on the right half.
107+
* When clicked, the black line starts back over at the top.
108+
*
109+
*/
110+
p5.Graphics.prototype.reset = function() {
111+
this._renderer.resetMatrix();
112+
if (this._renderer.isP3D) {
113+
this._renderer._update();
114+
}
115+
};
116+
65117
/**
66118
* Removes a Graphics object from the page and frees any resources
67119
* associated with it.

0 commit comments

Comments
 (0)