forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp5.Graphics.js
213 lines (198 loc) · 5.41 KB
/
p5.Graphics.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
/**
* @module Rendering
* @submodule Rendering
* @for p5
*/
import p5 from './main';
import * as constants from './constants';
/**
* Thin wrapper around a renderer, to be used for creating a
* graphics buffer object. Use this class if you need
* to draw into an off-screen graphics buffer. The two parameters define the
* width and height in pixels. The fields and methods for this class are
* extensive, but mirror the normal drawing API for p5.
*
* @class p5.Graphics
* @constructor
* @extends p5.Element
* @param {Number} w width
* @param {Number} h height
* @param {Constant} renderer the renderer to use, either P2D or WEBGL
* @param {p5} [pInst] pointer to p5 instance
* @param {Object} [canvas] existing html canvas element
*/
p5.Graphics = class extends p5.Element {
constructor(w, h, renderer, pInst, canvas) {
let canvasTemp;
if (canvas) {
canvasTemp = canvas;
} else {
canvasTemp = document.createElement('canvas');
}
super(canvasTemp, pInst);
this.canvas = canvasTemp;
const r = renderer || constants.P2D;
const node = pInst._userNode || document.body;
if (!canvas) {
node.appendChild(this.canvas);
}
// bind methods and props of p5 to the new object
for (const p in p5.prototype) {
if (!this[p]) {
if (typeof p5.prototype[p] === 'function') {
this[p] = p5.prototype[p].bind(this);
} else {
this[p] = p5.prototype[p];
}
}
}
p5.prototype._initializeInstanceVariables.apply(this);
this.width = w;
this.height = h;
this._pixelDensity = pInst._pixelDensity;
if (r === constants.WEBGL) {
this._renderer = new p5.RendererGL(this.canvas, this, false);
} else {
this._renderer = new p5.Renderer2D(this.canvas, this, false);
}
pInst._elements.push(this);
Object.defineProperty(this, 'deltaTime', {
get() {
return this._pInst.deltaTime;
}
});
this._renderer.resize(w, h);
this._renderer._applyDefaults();
return this;
}
/**
* Resets certain values such as those modified by functions in the Transform category
* and in the Lights category that are not automatically reset
* with graphics buffer objects. Calling this in <a href='#/p5/draw'>draw()</a> will copy the behavior
* of the standard canvas.
*
* @method reset
* @example
*
* <div><code>
* let pg;
* function setup() {
* createCanvas(100, 100);
* background(0);
* pg = createGraphics(50, 100);
* pg.fill(0);
* frameRate(5);
* }
*
* function draw() {
* image(pg, width / 2, 0);
* pg.background(255);
* // p5.Graphics object behave a bit differently in some cases
* // The normal canvas on the left resets the translate
* // with every loop through draw()
* // the graphics object on the right doesn't automatically reset
* // so translate() is additive and it moves down the screen
* rect(0, 0, width / 2, 5);
* pg.rect(0, 0, width / 2, 5);
* translate(0, 5, 0);
* pg.translate(0, 5, 0);
* }
* function mouseClicked() {
* // if you click you will see that
* // reset() resets the translate back to the initial state
* // of the Graphics object
* pg.reset();
* }
* </code></div>
*
* @alt
* A white line on a black background stays still on the top-left half.
* A black line animates from top to bottom on a white background on the right half.
* When clicked, the black line starts back over at the top.
*/
reset() {
this._renderer.resetMatrix();
if (this._renderer.isP3D) {
this._renderer._update();
}
}
/**
* Removes a Graphics object from the page and frees any resources
* associated with it.
*
* @method remove
*
* @example
* <div class='norender'><code>
* let bg;
* function setup() {
* bg = createCanvas(100, 100);
* bg.background(0);
* image(bg, 0, 0);
* bg.remove();
* }
* </code></div>
*
* <div><code>
* let bg;
* function setup() {
* pixelDensity(1);
* createCanvas(100, 100);
* stroke(255);
* fill(0);
*
* // create and draw the background image
* bg = createGraphics(100, 100);
* bg.background(200);
* bg.ellipse(50, 50, 80, 80);
* }
* function draw() {
* let t = millis() / 1000;
* // draw the background
* if (bg) {
* image(bg, frameCount % 100, 0);
* image(bg, frameCount % 100 - 100, 0);
* }
* // draw the foreground
* let p = p5.Vector.fromAngle(t, 35).add(50, 50);
* ellipse(p.x, p.y, 30);
* }
* function mouseClicked() {
* // remove the background
* if (bg) {
* bg.remove();
* bg = null;
* }
* }
* </code></div>
*
* @alt
* no image
* a multi-colored circle moving back and forth over a scrolling background.
*/
remove() {
if (this.elt.parentNode) {
this.elt.parentNode.removeChild(this.elt);
}
const idx = this._pInst._elements.indexOf(this);
if (idx !== -1) {
this._pInst._elements.splice(idx, 1);
}
for (const elt_ev in this._events) {
this.elt.removeEventListener(elt_ev, this._events[elt_ev]);
}
}
/**
* Creates and returns a new <a href="#/p5.Framebuffer">p5.Framebuffer</a>
* inside a p5.Graphics WebGL context.
*
* This takes the same parameters as the <a href="#/p5/createFramebuffer">global
* createFramebuffer function.</a>
*
* @method createFramebuffer
*/
createFramebuffer(options) {
return new p5.Framebuffer(this, options);
}
};
export default p5.Graphics;