Skip to content

Commit 1469065

Browse files
author
almchung
committed
Merge branch 'friendly-err-sys' of https://github.com/processing/p5.js into friendly-err-sys
2 parents 9e8236e + 86cb6b6 commit 1469065

File tree

23 files changed

+375
-172
lines changed

23 files changed

+375
-172
lines changed

ISSUE_TEMPLATE.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1+
<!--
2+
Hi there! If you are here to report a bug, or to discuss a feature (new or existing), you can use the below template to get started quickly. Fill out all those parts which you're comfortable with, and delete the remaining ones.
3+
4+
To check any option, replace the space between the "[" and "]" with a "x".
5+
Feel free to remove any portion of the template that is not relevant for your issue.
6+
-->
7+
8+
#### Nature of issue?
9+
10+
- [ ] Found a bug
11+
- [ ] Existing feature enhancement
12+
- [ ] New feature request
13+
14+
#### Most appropriate sub-area of p5.js?
15+
16+
- [ ] Color
17+
- [ ] Core
18+
- [ ] Events
19+
- [ ] Image
20+
- [ ] IO
21+
- [ ] Math
22+
- [ ] Typography
23+
- [ ] Utilities
24+
- [ ] WebGL
25+
26+
#### Which platform were you using when you encountered this?
27+
28+
- [ ] Mobile/Tablet (touch devices)
29+
- [ ] Desktop/Laptop
30+
- [ ] Others (specify if possible)
31+
32+
<!-- If you found a bug, the following information is helpful, if you know it. -->
33+
#### Details about the bug:
34+
35+
- p5.js version:
36+
- Web browser and it's version:
37+
- Operating System:
38+
- Steps to reproduce this:
39+
<!-- Include a simple code snippet that demonstrates the problem. -->
40+
41+
<!-- If you want to enhance an existing feature, please describe here -->
42+
#### Feature enhancement details:
43+
44+
45+
<!-- If you want to request a new feature, please describe here -->
46+
#### New feature details:
147

src/color/color_conversion.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* module Conversion
3-
* submodule Color Conversion
2+
* @module Conversion
3+
* @submodule Color Conversion
44
* @for p5
55
* @requires core
66
*/

src/core/core.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,10 @@ var p5 = function(sketch, node, sync) {
271271

272272
this._decrementPreload = function(){
273273
var context = this._isGlobal ? window : this;
274-
context._setProperty('_preloadCount', context._preloadCount - 1);
275-
context._runIfPreloadsAreDone();
274+
if(typeof context.preload === 'function'){
275+
context._setProperty('_preloadCount', context._preloadCount - 1);
276+
context._runIfPreloadsAreDone();
277+
}
276278
};
277279

278280
this._wrapPreload = function(obj, fnName){
@@ -284,7 +286,7 @@ var p5 = function(sketch, node, sync) {
284286
for (var i = 0; i < args.length; ++i) {
285287
args[i] = arguments[i];
286288
}
287-
args.push(this._decrementPreload.bind(this));
289+
// args.push(this._decrementPreload.bind(this));
288290
return this._registeredPreloadMethods[fnName].apply(obj, args);
289291
}.bind(this);
290292
};

src/core/environment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ p5.prototype.cursor = function(type, x, y) {
246246
*
247247
*/
248248
p5.prototype.frameRate = function(fps) {
249-
if (typeof fps !== 'number' || fps <= 0) {
249+
if (typeof fps !== 'number' || fps < 0) {
250250
return this._frameRate;
251251
} else {
252252
this._setProperty('_targetFrameRate', fps);

src/core/p5.Renderer2D.js

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ p5.Renderer2D = function(elt, pInst, isMainCanvas){
2525
p5.Renderer2D.prototype = Object.create(p5.Renderer.prototype);
2626

2727
p5.Renderer2D.prototype._applyDefaults = function() {
28-
this.drawingContext.fillStyle = constants._DEFAULT_FILL;
29-
this.drawingContext.strokeStyle = constants._DEFAULT_STROKE;
28+
this._setFill(constants._DEFAULT_FILL);
29+
this._setStroke(constants._DEFAULT_STROKE);
3030
this.drawingContext.lineCap = constants.ROUND;
3131
this.drawingContext.font = 'normal 12px sans-serif';
3232
};
@@ -50,14 +50,14 @@ p5.Renderer2D.prototype.background = function() {
5050
if (arguments[0] instanceof p5.Image) {
5151
this._pInst.image(arguments[0], 0, 0, this.width, this.height);
5252
} else {
53-
var curFill = this.drawingContext.fillStyle;
53+
var curFill = this._getFill();
5454
// create background rect
5555
var color = this._pInst.color.apply(this, arguments);
5656
var newFill = color.toString();
57-
this.drawingContext.fillStyle = newFill;
57+
this._setFill(newFill);
5858
this.drawingContext.fillRect(0, 0, this.width, this.height);
5959
// reset fill
60-
this.drawingContext.fillStyle = curFill;
60+
this._setFill(curFill);
6161
}
6262
this.drawingContext.restore();
6363
};
@@ -67,16 +67,13 @@ p5.Renderer2D.prototype.clear = function() {
6767
};
6868

6969
p5.Renderer2D.prototype.fill = function() {
70-
71-
var ctx = this.drawingContext;
7270
var color = this._pInst.color.apply(this, arguments);
73-
ctx.fillStyle = color.toString();
71+
this._setFill(color.toString());
7472
};
7573

7674
p5.Renderer2D.prototype.stroke = function() {
77-
var ctx = this.drawingContext;
7875
var color = this._pInst.color.apply(this, arguments);
79-
ctx.strokeStyle = color.toString();
76+
this._setStroke(color.toString());
8077
};
8178

8279
//////////////////////////////////////////////
@@ -453,11 +450,11 @@ p5.Renderer2D.prototype.ellipse = function(args) {
453450
w = args[2],
454451
h = args[3];
455452
if (doFill && !doStroke) {
456-
if(ctx.fillStyle === styleEmpty) {
453+
if(this._getFill() === styleEmpty) {
457454
return this;
458455
}
459456
} else if (!doFill && doStroke) {
460-
if(ctx.strokeStyle === styleEmpty) {
457+
if(this._getStroke() === styleEmpty) {
461458
return this;
462459
}
463460
}
@@ -487,7 +484,7 @@ p5.Renderer2D.prototype.line = function(x1, y1, x2, y2) {
487484
var ctx = this.drawingContext;
488485
if (!this._doStroke) {
489486
return this;
490-
} else if(ctx.strokeStyle === styleEmpty){
487+
} else if(this._getStroke() === styleEmpty){
491488
return this;
492489
}
493490
// Translate the line by (0.5, 0.5) to draw it crisp
@@ -506,16 +503,13 @@ p5.Renderer2D.prototype.line = function(x1, y1, x2, y2) {
506503

507504
p5.Renderer2D.prototype.point = function(x, y) {
508505
var ctx = this.drawingContext;
509-
var s = ctx.strokeStyle;
510-
var f = ctx.fillStyle;
511506
if (!this._doStroke) {
512507
return this;
513-
} else if(ctx.strokeStyle === styleEmpty){
508+
} else if(this._getStroke() === styleEmpty){
514509
return this;
515510
}
516511
x = Math.round(x);
517512
y = Math.round(y);
518-
ctx.fillStyle = s;
519513
if (ctx.lineWidth > 1) {
520514
ctx.beginPath();
521515
ctx.arc(
@@ -530,19 +524,18 @@ p5.Renderer2D.prototype.point = function(x, y) {
530524
} else {
531525
ctx.fillRect(x, y, 1, 1);
532526
}
533-
ctx.fillStyle = f;
534527
};
535528

536529
p5.Renderer2D.prototype.quad =
537530
function(x1, y1, x2, y2, x3, y3, x4, y4) {
538531
var ctx = this.drawingContext;
539532
var doFill = this._doFill, doStroke = this._doStroke;
540533
if (doFill && !doStroke) {
541-
if(ctx.fillStyle === styleEmpty) {
534+
if(this._getFill() === styleEmpty) {
542535
return this;
543536
}
544537
} else if (!doFill && doStroke) {
545-
if(ctx.strokeStyle === styleEmpty) {
538+
if(this._getStroke() === styleEmpty) {
546539
return this;
547540
}
548541
}
@@ -573,11 +566,11 @@ p5.Renderer2D.prototype.rect = function(args) {
573566
var ctx = this.drawingContext;
574567
var doFill = this._doFill, doStroke = this._doStroke;
575568
if (doFill && !doStroke) {
576-
if(ctx.fillStyle === styleEmpty) {
569+
if(this._getFill() === styleEmpty) {
577570
return this;
578571
}
579572
} else if (!doFill && doStroke) {
580-
if(ctx.strokeStyle === styleEmpty) {
573+
if(this._getStroke() === styleEmpty) {
581574
return this;
582575
}
583576
}
@@ -638,11 +631,11 @@ p5.Renderer2D.prototype.triangle = function(args) {
638631
var x2=args[2], y2=args[3];
639632
var x3=args[4], y3=args[5];
640633
if (doFill && !doStroke) {
641-
if(ctx.fillStyle === styleEmpty) {
634+
if(this._getFill() === styleEmpty) {
642635
return this;
643636
}
644637
} else if (!doFill && doStroke) {
645-
if(ctx.strokeStyle === styleEmpty) {
638+
if(this._getStroke() === styleEmpty) {
646639
return this;
647640
}
648641
}
@@ -798,31 +791,35 @@ function (mode, vertices, isCurve, isBezier,
798791
}
799792
} else if (shapeKind === constants.TRIANGLE_FAN) {
800793
if (numVerts > 2) {
794+
// For performance reasons, try to batch as many of the
795+
// fill and stroke calls as possible.
801796
this.drawingContext.beginPath();
802-
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
803-
this.drawingContext.lineTo(vertices[1][0], vertices[1][1]);
804-
this.drawingContext.lineTo(vertices[2][0], vertices[2][1]);
805-
if (this._doFill) {
806-
this._pInst.fill(vertices[2][5]);
807-
}
808-
if (this._doStroke) {
809-
this._pInst.stroke(vertices[2][6]);
810-
}
811-
this._doFillStrokeClose();
812-
for (i = 3; i < numVerts; i++) {
797+
for (i = 2; i < numVerts; i++) {
813798
v = vertices[i];
814-
this.drawingContext.beginPath();
815799
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
816800
this.drawingContext.lineTo(vertices[i - 1][0], vertices[i - 1][1]);
817801
this.drawingContext.lineTo(v[0], v[1]);
818-
if (this._doFill) {
819-
this._pInst.fill(v[5]);
820-
}
821-
if (this._doStroke) {
822-
this._pInst.stroke(v[6]);
802+
this.drawingContext.lineTo(vertices[0][0], vertices[0][1]);
803+
// If the next colour is going to be different, stroke / fill now
804+
if (i < numVerts - 1) {
805+
if ( (this._doFill && v[5] !== vertices[i + 1][5]) ||
806+
(this._doStroke && v[6] !== vertices[i + 1][6])) {
807+
if (this._doFill) {
808+
this._pInst.fill(v[5]);
809+
this.drawingContext.fill();
810+
this._pInst.fill(vertices[i + 1][5]);
811+
}
812+
if (this._doStroke) {
813+
this._pInst.stroke(v[6]);
814+
this.drawingContext.stroke();
815+
this._pInst.stroke(vertices[i + 1][6]);
816+
}
817+
this.drawingContext.closePath();
818+
this.drawingContext.beginPath(); // Begin the next one
819+
}
823820
}
824-
this._doFillStrokeClose();
825821
}
822+
this._doFillStrokeClose();
826823
}
827824
} else if (shapeKind === constants.QUADS) {
828825
for (i = 0; i + 3 < numVerts; i += 4) {
@@ -954,13 +951,29 @@ p5.Renderer2D.prototype.strokeWeight = function(w) {
954951
};
955952

956953
p5.Renderer2D.prototype._getFill = function(){
957-
return this.drawingContext.fillStyle;
954+
return this._cachedFillStyle;
955+
};
956+
957+
p5.Renderer2D.prototype._setFill = function(fillStyle){
958+
if (fillStyle !== this._cachedFillStyle) {
959+
this.drawingContext.fillStyle = fillStyle;
960+
this._cachedFillStyle = fillStyle;
961+
}
958962
};
959963

960964
p5.Renderer2D.prototype._getStroke = function(){
961-
return this.drawingContext.strokeStyle;
965+
return this._cachedStrokeStyle;
962966
};
963967

968+
p5.Renderer2D.prototype._setStroke = function(strokeStyle){
969+
if (strokeStyle !== this._cachedStrokeStyle) {
970+
this.drawingContext.strokeStyle = strokeStyle;
971+
this._cachedStrokeStyle = strokeStyle;
972+
}
973+
};
974+
975+
976+
964977
//////////////////////////////////////////////
965978
// SHAPE | Curves
966979
//////////////////////////////////////////////
@@ -1187,8 +1200,9 @@ p5.Renderer2D.prototype._renderText = function(p, line, x, y, maxY) {
11871200
if (this._doFill) {
11881201

11891202
// if fill hasn't been set by user, use default text fill
1190-
this.drawingContext.fillStyle = this._fillSet ?
1191-
this.drawingContext.fillStyle : constants._DEFAULT_TEXT_FILL;
1203+
if (! this._fillSet) {
1204+
this._setFill(constants._DEFAULT_TEXT_FILL);
1205+
}
11921206

11931207
this.drawingContext.fillText(line, x, y);
11941208
}
@@ -1287,6 +1301,9 @@ p5.Renderer2D.prototype.push = function() {
12871301

12881302
p5.Renderer2D.prototype.pop = function() {
12891303
this.drawingContext.restore();
1304+
// Re-cache the fill / stroke state
1305+
this._cachedFillStyle = this.drawingContext.fillStyle;
1306+
this._cachedStrokeStyle = this.drawingContext.strokeStyle;
12901307
};
12911308

12921309
module.exports = p5.Renderer2D;

src/core/rendering.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ p5.prototype.createCanvas = function(w, h, renderer) {
121121
* and draw will be called immediately, allowing the sketch to re-render itself
122122
* in the resized canvas.
123123
* @method resizeCanvas
124+
* @param {Number} w width of the canvas
125+
* @param {Number} h height of the canvas
126+
* @param {Boolean} noRedraw don't redraw the canvas immediately
124127
* @example
125128
* <div class="norender"><code>
126129
* function setup() {

src/image/loading_displaying.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ require('../core/error_helpers');
6666
p5.prototype.loadImage = function(path, successCallback, failureCallback) {
6767
var img = new Image();
6868
var pImg = new p5.Image(1, 1, this);
69-
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);
7069

70+
var self = this;
7171
img.onload = function() {
7272
pImg.width = pImg.canvas.width = img.width;
7373
pImg.height = pImg.canvas.height = img.height;
@@ -78,15 +78,12 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) {
7878
if (typeof successCallback === 'function') {
7979
successCallback(pImg);
8080
}
81-
if (decrementPreload && (successCallback !== decrementPreload)) {
82-
decrementPreload();
83-
}
81+
82+
self._decrementPreload();
8483
};
8584
img.onerror = function(e) {
8685
p5._friendlyFileLoadError(0,img.src);
87-
// don't get failure callback mixed up with decrementPreload
88-
if ((typeof failureCallback === 'function') &&
89-
(failureCallback !== decrementPreload)) {
86+
if (typeof failureCallback === 'function') {
9087
failureCallback(e);
9188
}
9289
};

0 commit comments

Comments
 (0)