Skip to content

Commit 2b84597

Browse files
author
lauren mccarthy
committed
resize working
2 parents 3e02286 + 3e8d389 commit 2b84597

File tree

6 files changed

+162
-56
lines changed

6 files changed

+162
-56
lines changed

lib/p5.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ var core = function (require, shim, constants) {
116116
'keypress': null,
117117
'touchstart': null,
118118
'touchmove': null,
119-
'touchend': null
119+
'touchend': null,
120+
'resize': null
120121
};
121122
this._start = function () {
122123
if (this._userNode) {
@@ -585,6 +586,14 @@ var p5Graphics = function (require, core, constants) {
585586
this.drawingContext.lineCap = constants.ROUND;
586587
};
587588
p5.Graphics.prototype = Object.create(p5.Element.prototype);
589+
p5.Graphics.prototype.resize = function (w, h) {
590+
this.width = w;
591+
this.height = h;
592+
if (this._pInst) {
593+
this._pInst._setProperty('width', this.width);
594+
this._pInst._setProperty('height', this.height);
595+
}
596+
};
588597
return p5.Graphics;
589598
}({}, core, constants);
590599
var filters = function (require) {
@@ -1998,10 +2007,18 @@ var environment = function (require, core, constants) {
19982007
p5.prototype.displayHeight = screen.height;
19992008
p5.prototype.windowWidth = window.innerWidth;
20002009
p5.prototype.windowHeight = window.innerHeight;
2001-
window.addEventListener('resize', function (e) {
2002-
this.windowWidth = window.innerWidth;
2003-
this.windowHeight = window.innerHeight;
2004-
});
2010+
p5.prototype.onresize = function (e) {
2011+
this._setProperty('windowWidth', window.innerWidth);
2012+
this._setProperty('windowHeight', window.innerHeight);
2013+
var context = this._isGlobal ? window : this;
2014+
var executeDefault;
2015+
if (typeof context.windowResized === 'function') {
2016+
executeDefault = context.windowResized(e);
2017+
if (executeDefault !== undefined && !executeDefault) {
2018+
e.preventDefault();
2019+
}
2020+
}
2021+
};
20052022
p5.prototype.width = 0;
20062023
p5.prototype.height = 0;
20072024
p5.prototype.fullscreen = function (val) {
@@ -3911,13 +3928,27 @@ var renderingrendering = function (require, core, constants) {
39113928
} else {
39123929
document.body.appendChild(c);
39133930
}
3914-
var pg = new p5.Graphics(c, this);
3915-
if (isDefault) {
3931+
var pg = this._defaultGraphics;
3932+
if (!pg) {
3933+
pg = new p5.Graphics(c, this);
39163934
this._elements.push(pg);
3935+
this._defaultGraphics = pg;
3936+
} else {
3937+
pg.resize(w * this._pixelDensity, h * this._pixelDensity);
39173938
}
39183939
this.scale(this._pixelDensity, this._pixelDensity);
39193940
return pg;
39203941
};
3942+
p5.prototype.resizeCanvas = function (w, h) {
3943+
var pg = this._defaultGraphics;
3944+
if (pg) {
3945+
pg.resize(w * this._pixelDensity, h * this._pixelDensity);
3946+
pg.elt.setAttribute('width', w * this._pixelDensity);
3947+
pg.elt.setAttribute('height', h * this._pixelDensity);
3948+
pg.elt.setAttribute('style', 'width:' + w + 'px !important; height:' + h + 'px !important;');
3949+
this.scale(this._pixelDensity, this._pixelDensity);
3950+
}
3951+
};
39213952
p5.prototype.noCanvas = function () {
39223953
var c = document.getElementById('defaultCanvas');
39233954
if (c) {

lib/p5.min.js

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/core/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ define(function (require) {
164164
'keypress': null,
165165
'touchstart': null,
166166
'touchmove': null,
167-
'touchend': null
167+
'touchend': null,
168+
'resize': null
168169
};
169170

170171
this._start = function () {

src/environment/environment.js

Lines changed: 42 additions & 13 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
*/
@@ -225,11 +225,40 @@ define(function(require) {
225225
* </code></div>
226226
*/
227227
p5.prototype.windowHeight = window.innerHeight;
228-
window.addEventListener('resize', function (e) {
229-
// remap the window width and height on resize
230-
this.windowWidth = window.innerWidth;
231-
this.windowHeight = window.innerHeight;
232-
});
228+
229+
/**
230+
* The windowResized() function is called once every time the browser window
231+
* is resized. This is a good place to resize the canvas or do any other
232+
* adjustements to accomodate the new window size.
233+
*
234+
* @property windowResized
235+
* @example
236+
* <div class="norender"><code>
237+
* function setup() {
238+
* createCanvas(windowWidth, windowHeight);
239+
* }
240+
*
241+
* function draw() {
242+
* background(0, 100, 200);
243+
* }
244+
*
245+
* function windowResized() {
246+
* resizeCanvas(windowWidth, windowHeight);
247+
* }
248+
* </code></div>
249+
*/
250+
p5.prototype.onresize = function(e){
251+
this._setProperty('windowWidth', window.innerWidth);
252+
this._setProperty('windowHeight', window.innerHeight);
253+
var context = this._isGlobal ? window : this;
254+
var executeDefault;
255+
if (typeof context.windowResized === 'function') {
256+
executeDefault = context.windowResized(e);
257+
if (executeDefault !== undefined && !executeDefault) {
258+
e.preventDefault();
259+
}
260+
}
261+
};
233262

234263
/**
235264
* System variable that stores the width of the drawing canvas. This value
@@ -255,7 +284,7 @@ define(function(require) {
255284

256285
/**
257286
* 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
287+
* value of the argument. If no argument is given, returns the current
259288
* fullscreen state. Note that due to browser restrictions this can only
260289
* be called on user input, for example, on mouse press like the example
261290
* below.
@@ -326,4 +355,4 @@ define(function(require) {
326355

327356
return p5;
328357

329-
});
358+
});

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
});

0 commit comments

Comments
 (0)