mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Use shorter zero checks for array indices and length.
Keeping === 0 for mathematical algorithms seems clearer.
This commit is contained in:
parent
3d57216ffe
commit
becac4c921
14 changed files with 30 additions and 29 deletions
|
@ -83,7 +83,7 @@ var Matrix = Base.extend(/** @lends Matrix# */{
|
||||||
} else {
|
} else {
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
} else if (count === 0) {
|
} else if (!count) {
|
||||||
this.reset();
|
this.reset();
|
||||||
} else {
|
} else {
|
||||||
ok = false;
|
ok = false;
|
||||||
|
|
|
@ -829,7 +829,7 @@ new function() { // Injection scope for various item event handlers
|
||||||
// If we're returning '#bounds', create a LinkedRectangle that uses
|
// If we're returning '#bounds', create a LinkedRectangle that uses
|
||||||
// the setBounds() setter to update the Item whenever the bounds are
|
// the setBounds() setter to update the Item whenever the bounds are
|
||||||
// changed:
|
// changed:
|
||||||
return arguments.length === 0
|
return !arguments.length
|
||||||
? new LinkedRectangle(bounds.x, bounds.y, bounds.width,
|
? new LinkedRectangle(bounds.x, bounds.y, bounds.width,
|
||||||
bounds.height, this, 'setBounds')
|
bounds.height, this, 'setBounds')
|
||||||
: bounds;
|
: bounds;
|
||||||
|
@ -878,7 +878,7 @@ new function() { // Injection scope for various item event handlers
|
||||||
var children = this._children;
|
var children = this._children;
|
||||||
// TODO: What to return if nothing is defined, e.g. empty Groups?
|
// TODO: What to return if nothing is defined, e.g. empty Groups?
|
||||||
// Scriptographer behaves weirdly then too.
|
// Scriptographer behaves weirdly then too.
|
||||||
if (!children || children.length === 0)
|
if (!children || !children.length)
|
||||||
return new Rectangle();
|
return new Rectangle();
|
||||||
// Call _updateBoundsCache() even when the group only holds empty /
|
// Call _updateBoundsCache() even when the group only holds empty /
|
||||||
// invisible items), so future changes in these items will cause right
|
// invisible items), so future changes in these items will cause right
|
||||||
|
@ -2667,7 +2667,8 @@ new function() { // Injection scope for hit-test functions shared with project
|
||||||
* @return Boolean
|
* @return Boolean
|
||||||
*/
|
*/
|
||||||
isEmpty: function() {
|
isEmpty: function() {
|
||||||
return !this._children || this._children.length === 0;
|
var children = this._children;
|
||||||
|
return !children || !children.length;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4309,7 +4310,7 @@ new function() { // Injection scope for hit-test functions shared with project
|
||||||
// bounds corners, and draw the corners.
|
// bounds corners, and draw the corners.
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
for (var i = 0; i < 8; i++) {
|
for (var i = 0; i < 8; i++) {
|
||||||
ctx[i === 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);
|
ctx[!i ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);
|
||||||
}
|
}
|
||||||
ctx.closePath();
|
ctx.closePath();
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
|
|
|
@ -135,7 +135,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
|
||||||
* @return Boolean
|
* @return Boolean
|
||||||
*/
|
*/
|
||||||
isEmpty: function() {
|
isEmpty: function() {
|
||||||
return this._children.length === 0;
|
return !this._children.length;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -142,7 +142,7 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
|
||||||
if (path.isEmpty())
|
if (path.isEmpty())
|
||||||
path.remove();
|
path.remove();
|
||||||
}
|
}
|
||||||
if (children.length === 0) { // Replace with a simple empty Path
|
if (!children.length) { // Replace with a simple empty Path
|
||||||
var path = new Path(Item.NO_INSERT);
|
var path = new Path(Item.NO_INSERT);
|
||||||
path.copyAttributes(this);
|
path.copyAttributes(this);
|
||||||
path.insertAbove(this);
|
path.insertAbove(this);
|
||||||
|
@ -301,7 +301,7 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
|
||||||
_draw: function(ctx, param, viewMatrix, strokeMatrix) {
|
_draw: function(ctx, param, viewMatrix, strokeMatrix) {
|
||||||
var children = this._children;
|
var children = this._children;
|
||||||
// Return early if the compound path doesn't have any children:
|
// Return early if the compound path doesn't have any children:
|
||||||
if (children.length === 0)
|
if (!children.length)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
param = param.extend({ dontStart: true, dontFinish: true });
|
param = param.extend({ dontStart: true, dontFinish: true });
|
||||||
|
@ -342,7 +342,7 @@ new function() { // Injection scope for PostScript-like drawing functions
|
||||||
*/
|
*/
|
||||||
function getCurrentPath(that, check) {
|
function getCurrentPath(that, check) {
|
||||||
var children = that._children;
|
var children = that._children;
|
||||||
if (check && children.length === 0)
|
if (check && !children.length)
|
||||||
throw new Error('Use a moveTo() command first');
|
throw new Error('Use a moveTo() command first');
|
||||||
return children[children.length - 1];
|
return children[children.length - 1];
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ var Curve = Base.extend(/** @lends Curve# */{
|
||||||
this._path = arg0;
|
this._path = arg0;
|
||||||
seg1 = arg1;
|
seg1 = arg1;
|
||||||
seg2 = arg2;
|
seg2 = arg2;
|
||||||
} else if (count === 0) {
|
} else if (!count) {
|
||||||
seg1 = new Segment();
|
seg1 = new Segment();
|
||||||
seg2 = new Segment();
|
seg2 = new Segment();
|
||||||
} else if (count === 1) {
|
} else if (count === 1) {
|
||||||
|
@ -299,7 +299,7 @@ var Curve = Base.extend(/** @lends Curve# */{
|
||||||
* @return {Boolean} {@true if this is the first curve}
|
* @return {Boolean} {@true if this is the first curve}
|
||||||
*/
|
*/
|
||||||
isFirst: function() {
|
isFirst: function() {
|
||||||
return this._segment1._index === 0;
|
return !this._segment1._index;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -651,7 +651,7 @@ statics: /** @lends Curve */{
|
||||||
tMax = 1 - tMin,
|
tMax = 1 - tMin,
|
||||||
roots = [],
|
roots = [],
|
||||||
n = Numerical.solveQuadratic(a, b, c, roots, tMin, tMax);
|
n = Numerical.solveQuadratic(a, b, c, roots, tMin, tMax);
|
||||||
if (n === 0) {
|
if (!n) {
|
||||||
curves.push(v);
|
curves.push(v);
|
||||||
} else {
|
} else {
|
||||||
roots.sort();
|
roots.sort();
|
||||||
|
@ -2110,13 +2110,13 @@ new function() { // Scope for intersection using bezier fat-line clipping
|
||||||
if (t2 != null) { // If point is on curve
|
if (t2 != null) { // If point is on curve
|
||||||
var pair = i === 0 ? [t1, t2] : [t2, t1];
|
var pair = i === 0 ? [t1, t2] : [t2, t1];
|
||||||
// Filter out tiny overlaps.
|
// Filter out tiny overlaps.
|
||||||
if (pairs.length === 0 ||
|
if (!pairs.length ||
|
||||||
abs(pair[0] - pairs[0][0]) > timeEpsilon &&
|
abs(pair[0] - pairs[0][0]) > timeEpsilon &&
|
||||||
abs(pair[1] - pairs[0][1]) > timeEpsilon)
|
abs(pair[1] - pairs[0][1]) > timeEpsilon)
|
||||||
pairs.push(pair);
|
pairs.push(pair);
|
||||||
}
|
}
|
||||||
// We checked 3 points but found no match, curves can't overlap.
|
// We checked 3 points but found no match, curves can't overlap.
|
||||||
if (i === 1 && pairs.length === 0)
|
if (i === 1 && !pairs.length)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (pairs.length !== 2) {
|
if (pairs.length !== 2) {
|
||||||
|
|
|
@ -346,7 +346,7 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
outY = coords[5];
|
outY = coords[5];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length === 0)
|
if (!length)
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
for (var i = 0; i < length; i++)
|
for (var i = 0; i < length; i++)
|
||||||
|
@ -364,7 +364,7 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
// taken into account.
|
// taken into account.
|
||||||
|
|
||||||
isEmpty: function() {
|
isEmpty: function() {
|
||||||
return this._segments.length === 0;
|
return !this._segments.length;
|
||||||
},
|
},
|
||||||
|
|
||||||
_transformContent: function(matrix) {
|
_transformContent: function(matrix) {
|
||||||
|
@ -457,7 +457,7 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
}
|
}
|
||||||
// If it's the first segment, correct the last segment of closed
|
// If it's the first segment, correct the last segment of closed
|
||||||
// paths too:
|
// paths too:
|
||||||
if (curve = curves[this._closed && start === 0 ? segments.length - 1
|
if (curve = curves[this._closed && !start ? segments.length - 1
|
||||||
: start - 1]) {
|
: start - 1]) {
|
||||||
curve._segment2 = segments[start] || segments[0];
|
curve._segment2 = segments[start] || segments[0];
|
||||||
curve._changed();
|
curve._changed();
|
||||||
|
@ -2303,7 +2303,7 @@ new function() { // PostScript-style drawing commands
|
||||||
*/
|
*/
|
||||||
function getCurrentSegment(that) {
|
function getCurrentSegment(that) {
|
||||||
var segments = that._segments;
|
var segments = that._segments;
|
||||||
if (segments.length === 0)
|
if (!segments.length)
|
||||||
throw new Error('Use a moveTo() command first');
|
throw new Error('Use a moveTo() command first');
|
||||||
return segments[segments.length - 1];
|
return segments[segments.length - 1];
|
||||||
}
|
}
|
||||||
|
@ -2522,7 +2522,7 @@ new function() { // PostScript-style drawing commands
|
||||||
pt = center.add(vector);
|
pt = center.add(vector);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i === 0) {
|
if (!i) {
|
||||||
// Modify startSegment
|
// Modify startSegment
|
||||||
current.setHandleOut(out);
|
current.setHandleOut(out);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2864,7 +2864,7 @@ statics: {
|
||||||
segment._transformCoordinates(matrix, coords);
|
segment._transformCoordinates(matrix, coords);
|
||||||
for (var j = 0; j < 6; j += 2) {
|
for (var j = 0; j < 6; j += 2) {
|
||||||
// Use different padding for points or handles
|
// Use different padding for points or handles
|
||||||
var padding = j === 0 ? joinPadding : strokePadding,
|
var padding = !j ? joinPadding : strokePadding,
|
||||||
paddingX = padding ? padding[0] : 0,
|
paddingX = padding ? padding[0] : 0,
|
||||||
paddingY = padding ? padding[1] : 0,
|
paddingY = padding ? padding[1] : 0,
|
||||||
x = coords[j],
|
x = coords[j],
|
||||||
|
|
|
@ -482,7 +482,7 @@ PathItem.inject(new function() {
|
||||||
var curve = curves[i],
|
var curve = curves[i],
|
||||||
path = curve._path,
|
path = curve._path,
|
||||||
v = curve.getValues();
|
v = curve.getValues();
|
||||||
if (i === 0 || curves[i - 1]._path !== path) {
|
if (!i || curves[i - 1]._path !== path) {
|
||||||
// We're on a new (sub-)path, so we need to determine values of
|
// We're on a new (sub-)path, so we need to determine values of
|
||||||
// the last non-horizontal curve on this path.
|
// the last non-horizontal curve on this path.
|
||||||
vPrev = null;
|
vPrev = null;
|
||||||
|
|
|
@ -169,7 +169,7 @@ var PathItem = Item.extend(/** @lends PathItem# */{
|
||||||
case 'l':
|
case 'l':
|
||||||
var move = lower === 'm';
|
var move = lower === 'm';
|
||||||
for (var j = 0; j < length; j += 2)
|
for (var j = 0; j < length; j += 2)
|
||||||
this[j === 0 && move ? 'moveTo' : 'lineTo'](
|
this[!j && move ? 'moveTo' : 'lineTo'](
|
||||||
current = getPoint(j));
|
current = getPoint(j));
|
||||||
control = current;
|
control = current;
|
||||||
if (move)
|
if (move)
|
||||||
|
|
|
@ -110,7 +110,7 @@ var PathIterator = Base.extend({
|
||||||
var i, j = this.index;
|
var i, j = this.index;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
i = j;
|
i = j;
|
||||||
if (j === 0 || this.parts[--j].offset < offset)
|
if (!j || this.parts[--j].offset < offset)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Find the part that succeeds the given offset, then interpolate
|
// Find the part that succeeds the given offset, then interpolate
|
||||||
|
|
|
@ -496,7 +496,7 @@ var Segment = Base.extend(/** @lends Segment# */{
|
||||||
* @return {Boolean} {@true if this is the first segment}
|
* @return {Boolean} {@true if this is the first segment}
|
||||||
*/
|
*/
|
||||||
isFirst: function() {
|
isFirst: function() {
|
||||||
return this._index === 0;
|
return !this._index;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -588,7 +588,7 @@ var Color = Base.extend(new function() {
|
||||||
// Allow implicit definition of gradients through
|
// Allow implicit definition of gradients through
|
||||||
// stops / radial properties. Conversion happens
|
// stops / radial properties. Conversion happens
|
||||||
// here on the fly:
|
// here on the fly:
|
||||||
if (value == null && i === 0 && type === 'gradient'
|
if (value == null && !i && type === 'gradient'
|
||||||
&& 'stops' in arg) {
|
&& 'stops' in arg) {
|
||||||
value = {
|
value = {
|
||||||
stops: arg.stops,
|
stops: arg.stops,
|
||||||
|
|
|
@ -119,7 +119,7 @@ var Gradient = Base.extend(/** @lends Gradient# */{
|
||||||
var index = this._owners ? this._owners.indexOf(color) : -1;
|
var index = this._owners ? this._owners.indexOf(color) : -1;
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
this._owners.splice(index, 1);
|
this._owners.splice(index, 1);
|
||||||
if (this._owners.length === 0)
|
if (!this._owners.length)
|
||||||
this._owners = undefined;
|
this._owners = undefined;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -203,7 +203,7 @@ var Style = Base.extend(new function() {
|
||||||
// If the owner has children, walk through all of them and see if
|
// If the owner has children, walk through all of them and see if
|
||||||
// they all have the same style.
|
// they all have the same style.
|
||||||
// If true is passed for _dontMerge, don't merge children styles
|
// If true is passed for _dontMerge, don't merge children styles
|
||||||
if (key in this._defaults && (!children || children.length === 0
|
if (key in this._defaults && (!children || !children.length
|
||||||
|| _dontMerge || owner instanceof CompoundPath)) {
|
|| _dontMerge || owner instanceof CompoundPath)) {
|
||||||
var value = this._values[key];
|
var value = this._values[key];
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
|
@ -224,7 +224,7 @@ var Style = Base.extend(new function() {
|
||||||
} else if (children) {
|
} else if (children) {
|
||||||
for (var i = 0, l = children.length; i < l; i++) {
|
for (var i = 0, l = children.length; i < l; i++) {
|
||||||
var childValue = children[i]._style[get]();
|
var childValue = children[i]._style[get]();
|
||||||
if (i === 0) {
|
if (!i) {
|
||||||
value = childValue;
|
value = childValue;
|
||||||
} else if (!Base.equals(value, childValue)) {
|
} else if (!Base.equals(value, childValue)) {
|
||||||
// If there is another child with a different
|
// If there is another child with a different
|
||||||
|
|
|
@ -23,7 +23,7 @@ test('moveTo() / lineTo()', function() {
|
||||||
for (var i = 0; i < lists.length; i++) {
|
for (var i = 0; i < lists.length; i++) {
|
||||||
var list = lists[i];
|
var list = lists[i];
|
||||||
for (var j = 0; j < list.length; j++) {
|
for (var j = 0; j < list.length; j++) {
|
||||||
path[j === 0 ? 'moveTo' : 'lineTo'](list[j]);
|
path[!j ? 'moveTo' : 'lineTo'](list[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue