Skip to content

Commit 3e8d389

Browse files
author
Gareth Lewin
committed
Handle resize events and allow canvas resizing
* Added windowResized callback * Made the createCanvas keep an id around so we can find user created canvases later * Changed createCanvas to resize the graphics object if it already exists * Added a resizeCanvas method * Added resize to the p5.Graphics object * (Minor) My editor stripped whitespace on some lines. It was only a small subset so I believe your standard is to strip them so I left it in.
1 parent 4b1b9fc commit 3e8d389

File tree

5 files changed

+128
-47
lines changed

5 files changed

+128
-47
lines changed

lib/p5.js

100644100755
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,14 @@ var p5Graphics = function (require, core, constants) {
583583
this.drawingContext.lineCap = constants.ROUND;
584584
};
585585
p5.Graphics.prototype = Object.create(p5.Element.prototype);
586+
p5.Graphics.prototype.resize = function (w, h) {
587+
this.width = w;
588+
this.height = h;
589+
if (this._pInst) {
590+
this._pInst._setProperty('width', this.width);
591+
this._pInst._setProperty('height', this.height);
592+
}
593+
};
586594
return p5.Graphics;
587595
}({}, core, constants);
588596
var filters = function (require) {
@@ -1999,6 +2007,14 @@ var environment = function (require, core, constants) {
19992007
window.addEventListener('resize', function (e) {
20002008
this.windowWidth = window.innerWidth;
20012009
this.windowHeight = window.innerHeight;
2010+
var context = this._isGlobal ? window : this;
2011+
var executeDefault;
2012+
if (typeof context.windowResized === 'function') {
2013+
executeDefault = context.windowResized(e);
2014+
if (executeDefault !== undefined && !executeDefault) {
2015+
e.preventDefault();
2016+
}
2017+
}
20022018
});
20032019
p5.prototype.width = 0;
20042020
p5.prototype.height = 0;
@@ -3884,7 +3900,7 @@ var renderingrendering = function (require, core, constants) {
38843900
} else {
38853901
c = document.getElementById('defaultCanvas');
38863902
if (c) {
3887-
c.id = '';
3903+
c.id = 'userCanvas';
38883904
} else {
38893905
var warn = 'Warning: createCanvas more than once NOT recommended.';
38903906
warn += ' Very unpredictable behavior may result.';
@@ -3903,13 +3919,32 @@ var renderingrendering = function (require, core, constants) {
39033919
} else {
39043920
document.body.appendChild(c);
39053921
}
3906-
var pg = new p5.Graphics(c, this);
3907-
if (isDefault) {
3922+
var pg;
3923+
pg = this._defaultGraphics;
3924+
if (!pg) {
3925+
pg = new p5.Graphics(c, this);
39083926
this._elements.push(pg);
3927+
this._defaultGraphics = pg;
3928+
} else {
3929+
pg.resize(w * this._pixelDensity, h * this._pixelDensity);
39093930
}
39103931
this.scale(this._pixelDensity, this._pixelDensity);
39113932
return pg;
39123933
};
3934+
p5.prototype.resizeCanvas = function (w, h) {
3935+
var c = document.getElementById('defaultCanvas');
3936+
if (!c) {
3937+
c = document.getElementById('userCanvas');
3938+
}
3939+
c.setAttribute('width', w * this._pixelDensity);
3940+
c.setAttribute('height', h * this._pixelDensity);
3941+
c.setAttribute('style', 'width:' + w + 'px !important; height:' + h + 'px !important;');
3942+
var pg = this._defaultGraphics;
3943+
if (pg) {
3944+
pg.resize(w * this._pixelDensity, h * this._pixelDensity);
3945+
}
3946+
this.scale(this._pixelDensity, this._pixelDensity);
3947+
};
39133948
p5.prototype.noCanvas = function () {
39143949
var c = document.getElementById('defaultCanvas');
39153950
if (c) {

lib/p5.min.js

100644100755
Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/environment/environment.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ define(function(require) {
5555
* </code></div>
5656
*/
5757
p5.prototype.focused = true;
58-
58+
5959
/**
6060
* Sets the cursor to a predefined symbol or an image, or makes it visible
6161
* if already hidden. If you are trying to set an image as the cursor, the
6262
* recommended size is 16x16 or 32x32 pixels. It is not possible to load an
6363
* image as the cursor if you are exporting your program for the Web, and not
6464
* all MODES work with all browsers. The values for parameters x and y must
65-
* be less than the dimensions of the image.
65+
* be less than the dimensions of the image.
6666
*
6767
* @method cursor
6868
* @param {Number/Constant} type either ARROW, CROSS, HAND, MOVE, TEXT, or
@@ -74,7 +74,7 @@ define(function(require) {
7474
* // Move the mouse left and right across the image
7575
* // to see the cursor change from a cross to a hand
7676
* function draw() {
77-
* line(width/2, 0, width/2, height);
77+
* line(width/2, 0, width/2, height);
7878
* if (mouseX < 50) {
7979
* cursor(CROSS);
8080
* } else {
@@ -159,8 +159,8 @@ define(function(require) {
159159
};
160160

161161
/**
162-
* Hides the cursor from view.
163-
*
162+
* Hides the cursor from view.
163+
*
164164
* @method noCursor
165165
* @example
166166
* <div><code>
@@ -170,7 +170,7 @@ define(function(require) {
170170
*
171171
* function draw() {
172172
* background(200);
173-
* ellipse(mouseX, mouseY, 10, 10);
173+
* ellipse(mouseX, mouseY, 10, 10);
174174
* }
175175
* </code></div>
176176
*/
@@ -229,6 +229,16 @@ define(function(require) {
229229
// remap the window width and height on resize
230230
this.windowWidth = window.innerWidth;
231231
this.windowHeight = window.innerHeight;
232+
233+
var context = this._isGlobal ? window : this;
234+
var executeDefault;
235+
if (typeof context.windowResized === 'function') {
236+
executeDefault = context.windowResized(e);
237+
if(executeDefault !== undefined && !executeDefault) {
238+
e.preventDefault();
239+
}
240+
}
241+
232242
});
233243

234244
/**
@@ -255,7 +265,7 @@ define(function(require) {
255265

256266
/**
257267
* If argument is given, sets the sketch to fullscreen or not based on the
258-
* value of the argument. If no argument is given, returns the current
268+
* value of the argument. If no argument is given, returns the current
259269
* fullscreen state. Note that due to browser restrictions this can only
260270
* be called on user input, for example, on mouse press like the example
261271
* below.
@@ -326,4 +336,4 @@ define(function(require) {
326336

327337
return p5;
328338

329-
});
339+
});

src/objects/p5.Graphics.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ define(function(require) {
99
var constants = require('constants');
1010

1111
/**
12-
* Main graphics and rendering context, as well as the base API
13-
* implementation for p5.js "core". Use this class if you need to draw into
14-
* an off-screen graphics buffer. A p5.Graphics object can be constructed
15-
* with the <code>createGraphics()</code> function. The fields and methods
12+
* Main graphics and rendering context, as well as the base API
13+
* implementation for p5.js "core". Use this class if you need to draw into
14+
* an off-screen graphics buffer. A p5.Graphics object can be constructed
15+
* with the <code>createGraphics()</code> function. The fields and methods
1616
* for this class are extensive, but mirror the normal drawing API for p5.
17-
*
17+
*
1818
* @class p5.Graphics
1919
* @constructor
2020
* @extends p5.Element
@@ -33,7 +33,7 @@ define(function(require) {
3333
* pg.background(100);
3434
* pg.noStroke();
3535
* pg.ellipse(pg.width/2, pg.height/2, 50, 50);
36-
* image(pg, 9, 30);
36+
* image(pg, 9, 30);
3737
* image(pg, 51, 30);
3838
* }
3939
* </code>
@@ -53,14 +53,22 @@ define(function(require) {
5353
} else { // hide if offscreen buffer
5454
this.canvas.style.display = 'none';
5555
}
56-
56+
5757
this.drawingContext.fillStyle = '#FFFFFF';
5858
this.drawingContext.strokeStyle = '#000000';
5959
this.drawingContext.lineCap = constants.ROUND;
6060
};
6161

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

64+
p5.Graphics.prototype.resize = function(w, h) {
65+
this.width = w;
66+
this.height = h;
67+
if (this._pInst) {
68+
this._pInst._setProperty('width', this.width);
69+
this._pInst._setProperty('height', this.height);
70+
}
71+
};
6472

6573
return p5.Graphics;
6674
});

src/rendering/rendering.js

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ define(function(require) {
1313
* Creates a canvas element in the document, and sets the dimensions of it
1414
* in pixels. This method should be called only once at the start of setup.
1515
* <br>
16-
* The system variables width and height are set by the parameters passed
17-
* to this function. If createCanvas() is not used, the window will be
18-
* given a default size of 100x100 pixels.
16+
* The system variables width and height are set by the parameters passed
17+
* to this function. If createCanvas() is not used, the window will be
18+
* given a default size of 100x100 pixels.
1919
*
2020
* @method createCanvas
2121
* @param {Number} w width of the canvas
2222
* @param {Number} h height of the canvas
2323
* @return {Object} canvas generated
24-
* @example
24+
* @example
2525
* <div>
2626
* <code>
2727
* function setup() {
@@ -40,7 +40,7 @@ define(function(require) {
4040
} else { // resize the default canvas if new one is created
4141
c = document.getElementById('defaultCanvas');
4242
if (c) {
43-
c.id = ''; // remove default id
43+
c.id = 'userCanvas'; // remove default id
4444
} else { // probably user calling createCanvas more than once... uhoh
4545
// c = document.createElement('canvas');
4646

@@ -53,11 +53,11 @@ define(function(require) {
5353
}
5454
}
5555

56+
5657
c.setAttribute('width', w*this._pixelDensity);
5758
c.setAttribute('height', h*this._pixelDensity);
5859
c.setAttribute('style',
5960
'width:'+w+'px !important; height:'+h+'px !important;');
60-
6161
// set to invisible if still in setup (to prevent flashing with manipulate)
6262
if (!this._setupDone) {
6363
c.className += ' p5_hidden'; // tag to show later
@@ -70,21 +70,49 @@ define(function(require) {
7070
document.body.appendChild(c);
7171
}
7272

73-
var pg = new p5.Graphics(c, this);
74-
if (isDefault) {
75-
// store in elements array
73+
var pg;
74+
pg = this._defaultGraphics;
75+
if (!pg) {
76+
pg = new p5.Graphics(c, this);
7677
this._elements.push(pg);
78+
this._defaultGraphics = pg;
79+
}
80+
else
81+
{
82+
pg.resize (w*this._pixelDensity, h*this._pixelDensity);
7783
}
84+
7885
this.scale(this._pixelDensity, this._pixelDensity);
7986
return pg;
8087
};
8188

89+
p5.prototype.resizeCanvas = function(w, h) {
90+
// What do we do if we have two canvases due to userNode
91+
92+
var c = document.getElementById('defaultCanvas');
93+
if (!c) {
94+
c = document.getElementById('userCanvas');
95+
}
96+
97+
c.setAttribute('width', w*this._pixelDensity);
98+
c.setAttribute('height', h*this._pixelDensity);
99+
c.setAttribute('style',
100+
'width:'+w+'px !important; height:'+h+'px !important;');
101+
102+
var pg = this._defaultGraphics;
103+
if (pg) {
104+
pg.resize (w*this._pixelDensity, h*this._pixelDensity);
105+
}
106+
this.scale(this._pixelDensity, this._pixelDensity);
107+
108+
};
109+
82110

83111
/**
84-
* Removes the default canvas for a p5 sketch that doesn't
112+
* Removes the default canvas for a p5 sketch that doesn't
85113
* require a canvas
86114
* @method noCanvas
87-
* @example
115+
* @example
88116
* <div>
89117
* <code>
90118
* function setup() {
@@ -101,10 +129,10 @@ define(function(require) {
101129
};
102130

103131
/**
104-
* Creates and returns a new p5.Graphics object. Use this class if you need
132+
* Creates and returns a new p5.Graphics object. Use this class if you need
105133
* to draw into an off-screen graphics buffer. The two parameters define the
106134
* width and height in pixels.
107-
*
135+
*
108136
* @method createGraphics
109137
* @param {Number} w width of the offscreen graphics buffer
110138
* @param {Number} h height of the offscreen graphics buffer
@@ -122,7 +150,7 @@ define(function(require) {
122150
* pg.background(100);
123151
* pg.noStroke();
124152
* pg.ellipse(pg.width/2, pg.height/2, 50, 50);
125-
* image(pg, 50, 50);
153+
* image(pg, 50, 50);
126154
* image(pg, 0, 0, 50, 50);
127155
* }
128156
* </code>
@@ -137,7 +165,7 @@ define(function(require) {
137165
//c.style.visibility='hidden';
138166
var node = this._userNode || document.body;
139167
node.appendChild(c);
140-
168+
141169
var pg = new p5.Graphics(c);
142170
// store in elements array
143171
this._elements.push(pg);
@@ -156,39 +184,39 @@ define(function(require) {
156184
};
157185

158186
/**
159-
* Blends the pixels in the display window according to the defined mode.
160-
* There is a choice of the following modes to blend the source pixels (A)
187+
* Blends the pixels in the display window according to the defined mode.
188+
* There is a choice of the following modes to blend the source pixels (A)
161189
* with the ones of pixels already in the display window (B):
162190
* <ul>
163-
* <li><code>BLEND</code> - linear interpolation of colours: C =
191+
* <li><code>BLEND</code> - linear interpolation of colours: C =
164192
* A*factor + B. This is the default blending mode.</li>
165193
* <li><code>ADD</code> - sum of A and B</li>
166-
* <li><code>DARKEST</code> - only the darkest colour succeeds: C =
194+
* <li><code>DARKEST</code> - only the darkest colour succeeds: C =
167195
* min(A*factor, B).</li>
168-
* <li><code>LIGHTEST</code> - only the lightest colour succeeds: C =
196+
* <li><code>LIGHTEST</code> - only the lightest colour succeeds: C =
169197
* max(A*factor, B).</li>
170198
* <li><code>DIFFERENCE</code> - subtract colors from underlying image.</li>
171199
* <li><code>EXCLUSION</code> - similar to <code>DIFFERENCE</code>, but less
172200
* extreme.</li>
173-
* <li><code>MULTIPLY</code> - multiply the colors, result will always be
201+
* <li><code>MULTIPLY</code> - multiply the colors, result will always be
174202
* darker.</li>
175-
* <li><code>SCREEN</code> - opposite multiply, uses inverse values of the
203+
* <li><code>SCREEN</code> - opposite multiply, uses inverse values of the
176204
* colors.</li>
177205
* <li><code>REPLACE</code> - the pixels entirely replace the others and
178206
* don't utilize alpha (transparency) values.</li>
179207
* <li><code>OVERLAY</code> - mix of <code>MULTIPLY</code> and <code>SCREEN
180208
* </code>. Multiplies dark values, and screens light values.</li>
181-
* <li><code>HARD_LIGHT</code> - <code>SCREEN</code> when greater than 50%
209+
* <li><code>HARD_LIGHT</code> - <code>SCREEN</code> when greater than 50%
182210
* gray, <code>MULTIPLY</code> when lower.</li>
183-
* <li><code>SOFT_LIGHT</code> - mix of <code>DARKEST</code> and
211+
* <li><code>SOFT_LIGHT</code> - mix of <code>DARKEST</code> and
184212
* <code>LIGHTEST</code>. Works like <code>OVERLAY</code>, but not as harsh.
185213
* </li>
186-
* <li><code>DODGE</code> - lightens light tones and increases contrast,
214+
* <li><code>DODGE</code> - lightens light tones and increases contrast,
187215
* ignores darks.</li>
188216
* <li><code>BURN</code> - darker areas are applied, increasing contrast,
189217
* ignores lights.</li>
190218
* </ul>
191-
*
219+
*
192220
* @method blendMode
193221
* @param {String/Constant} mode blend mode to set for canvas
194222
* @example

0 commit comments

Comments
 (0)