Use changes in latest Straps.js to remove as many hidden parameters as possible.

Hidden parameters through arguments[] are bad news for JS optimizer engines.
This commit is contained in:
Jürg Lehni 2014-01-05 17:40:54 +01:00
parent ea63d4c288
commit 6cb25fafe3
12 changed files with 95 additions and 77 deletions

View file

@ -14,7 +14,7 @@
"test"
],
"devDependencies": {
"straps": "~1.4.2",
"straps": "~1.4.3",
"acorn": "git://github.com/paperjs/acorn#0.3.2",
"esprima": "~1.0.3",
"stats.js": "r11"

View file

@ -491,7 +491,6 @@ var Point = Base.extend(/** @lends Point# */{
this.y * scale
);
}
return this;
},
/**
@ -533,28 +532,16 @@ var Point = Base.extend(/** @lends Point# */{
* @type Number
*/
getAngle: function(/* point */) {
// Hide parameters from Bootstrap so it injects bean too
return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI;
},
setAngle: function(angle) {
// We store a reference to _angle internally so we still preserve it
// when the vector's length is set to zero, and then anything else.
// Note that we cannot rely on it if x and y are something else than 0,
// since updating x / y does not automatically change _angle!
angle = this._angle = angle * Math.PI / 180;
if (!this.isZero()) {
var length = this.getLength();
// Use #set() instead of direct assignment of x/y, so LinkedPoint
// does not report changes twice.
this.set(
Math.cos(angle) * length,
Math.sin(angle) * length
);
}
return this;
this.setAngleInRadians.call(this, angle * Math.PI / 180);
},
getAngleInDegrees: '#getAngle',
setAngleInDegrees: '#setAngle',
/**
* Returns the smaller angle between two vectors in radians. The angle is
* unsigned, no information about rotational direction is given.
@ -572,8 +559,7 @@ var Point = Base.extend(/** @lends Point# */{
* @type Number
*/
getAngleInRadians: function(/* point */) {
// Hide parameters from Bootstrap so it injects bean too
if (arguments[0] === undefined) {
if (!arguments.length) {
return this.isZero()
// Return the preseved angle in case the vector has no
// length, and update the internal _angle in case the
@ -592,8 +578,21 @@ var Point = Base.extend(/** @lends Point# */{
}
},
getAngleInDegrees: function(/* point */) {
return this.getAngle(arguments[0]);
setAngleInRadians: function(angle) {
// We store a reference to _angle internally so we still preserve it
// when the vector's length is set to zero, and then anything else.
// Note that we cannot rely on it if x and y are something else than 0,
// since updating x / y does not automatically change _angle!
this._angle = angle;
if (!this.isZero()) {
var length = this.getLength();
// Use #set() instead of direct assignment of x/y, so LinkedPoint
// does not report changes twice.
this.set(
Math.cos(angle) * length,
Math.sin(angle) * length
);
}
},
/**

View file

@ -237,8 +237,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
* @type Point
* @bean
*/
getPoint: function(/* dontLink */) {
var ctor = arguments[0] ? Point : LinkedPoint;
getPoint: function(_dontLink) {
var ctor = _dontLink ? Point : LinkedPoint;
return new ctor(this.x, this.y, this, 'setPoint');
},
@ -255,8 +255,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
* @type Size
* @bean
*/
getSize: function(/* dontLink */) {
var ctor = arguments[0] ? Size : LinkedSize;
getSize: function(_dontLink) {
var ctor = _dontLink ? Size : LinkedSize;
return new ctor(this.width, this.height, this, 'setSize');
},
@ -401,8 +401,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
* @type Point
* @bean
*/
getCenter: function(/* dontLink */) {
var ctor = arguments[0] ? Point : LinkedPoint;
getCenter: function(_dontLink) {
var ctor = _dontLink ? Point : LinkedPoint;
return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter');
},
@ -812,8 +812,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
setY = 'set' + y,
get = 'get' + part,
set = 'set' + part;
this[get] = function(/* dontLink */) {
var ctor = arguments[0] ? Point : LinkedPoint;
this[get] = function(_dontLink) {
var ctor = _dontLink ? Point : LinkedPoint;
return new ctor(this[getX](), this[getY](), this, set);
};
this[set] = function(/* point */) {

View file

@ -762,11 +762,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* // Move the circle 100 points to the right
* circle.position.x += 100;
*/
getPosition: function(/* dontLink */) {
getPosition: function(_dontLink) {
// Cache position value.
// Pass true for dontLink in getCenter(), so receive back a normal point
// Pass true for _dontLink in getCenter(), so receive back a normal point
var position = this._position,
ctor = arguments[0] ? Point : LinkedPoint;
ctor = _dontLink ? Point : LinkedPoint;
// Do not cache LinkedPoints directly, since we would not be able to
// use them to calculate the difference in #setPosition, as when it is
// modified, it would hold new values already and only then cause the
@ -784,7 +784,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
setPosition: function(/* point */) {
// Calculate the distance to the current position, by which to
// translate the item. Pass true for dontLink, as we do not need a
// translate the item. Pass true for _dontLink, as we do not need a
// LinkedPoint to simply calculate this distance.
this.translate(Point.read(arguments).subtract(this.getPosition(true)));
},
@ -802,10 +802,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
*
* @example {@paperscript}
*/
getPivot: function(/* dontLink */) {
getPivot: function(_dontLink) {
var pivot = this._pivot;
if (pivot) {
var ctor = arguments[0] ? Point : LinkedPoint;
var ctor = _dontLink ? Point : LinkedPoint;
pivot = new ctor(pivot.x, pivot.y, this, 'setAnchor');
}
return pivot;
@ -2790,7 +2790,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
// color styles (only gradients so far) and pivot point:
var pivot = this._pivot,
style = this._style,
// pass true for dontMerge so we don't recursively transform
// pass true for _dontMerge so we don't recursively transform
// styles on groups' children.
fillColor = style.getFillColor(true),
strokeColor = style.getStrokeColor(true);

View file

@ -244,13 +244,13 @@ var Raster = Item.extend(/** @lends Raster# */{
* @type Context
* @bean
*/
getContext: function(/* modify */) {
getContext: function(modify) {
if (!this._context)
this._context = this.getCanvas().getContext('2d');
// Support a hidden parameter that indicates if the context will be used
// to modify the Raster object. We can notify such changes ahead since
// they are only used afterwards for redrawing.
if (arguments[0]) {
if (modify) {
// Also set _image to null since the Raster stops representing it.
// NOTE: This should theoretically be in our own _changed() handler
// for ChangeFlag.PIXELS, but since it's only happening in one place

View file

@ -228,11 +228,11 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
return area;
},
getPathData: function(/* precision */) {
getPathData: function(precision) {
var children = this._children,
paths = [];
for (var i = 0, l = children.length; i < l; i++)
paths.push(children[i].getPathData(arguments[0]));
paths.push(children[i].getPathData(precision));
return paths.join(' ');
},

View file

@ -61,13 +61,13 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @type Segment
* @bean
*/
getSegment: function(/* preferFirst */) {
getSegment: function(_preferFirst) {
if (!this._segment) {
var curve = this.getCurve(),
parameter = this.getParameter();
if (parameter === 1) {
this._segment = curve._segment2;
} else if (parameter === 0 || arguments[0]) {
} else if (parameter === 0 || _preferFirst) {
this._segment = curve._segment1;
} else if (parameter == null) {
return null;
@ -82,14 +82,21 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
return this._segment;
},
setSegment: function(segment) {
// NOTE: We only include this setter so the above getter can declare
// the _preferFirst parameter without having to hide it.
// See Strap.js beans conventions.
this._segment = segment;
},
/**
* The curve by which the location is defined.
*
* @type Curve
* @bean
*/
getCurve: function(/* uncached */) {
if (!this._curve || arguments[0]) {
getCurve: function(_uncached) {
if (!this._curve || _uncached) {
// If we're asked to get the curve uncached, access current curve
// objects through segment1 / segment2. Since path splitting or
// dividing might have happened in the meantime, try segment1's
@ -102,6 +109,11 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
return this._curve;
},
setCurve: function(curve) {
// See #setSegment()
this._curve = curve;
},
/**
* The curve location on the intersecting curve, if this location is the
* result of a call to {@link PathItem#getIntersections(path)} /
@ -179,14 +191,19 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @type Number
* @bean
*/
getParameter: function(/* uncached */) {
if ((this._parameter == null || arguments[0]) && this._point) {
var curve = this.getCurve(arguments[0] && this._point);
getParameter: function(_uncached) {
if ((this._parameter == null || _uncached) && this._point) {
var curve = this.getCurve(_uncached && this._point);
this._parameter = curve && curve.getParameterOf(this._point);
}
return this._parameter;
},
setParameter: function(parameter) {
// See #setSegment()
this._parameter = parameter;
},
/**
* The point which is defined by the {@link #curve} and
* {@link #parameter}.
@ -194,14 +211,19 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @type Point
* @bean
*/
getPoint: function(/* uncached */) {
if ((!this._point || arguments[0]) && this._parameter != null) {
getPoint: function(_uncached) {
if ((!this._point || _uncached) && this._parameter != null) {
var curve = this.getCurve();
this._point = curve && curve.getPointAt(this._parameter, true);
}
return this._point;
},
setPoint: function(point) {
// See #setSegment()
this._point = point;
},
/**
* The tangential vector to the {@link #curve} at the given location.
*

View file

@ -245,9 +245,8 @@ var Path = PathItem.extend(/** @lends Path# */{
* @type String
* @bean
*/
getPathData: function(/* precision */) {
getPathData: function(precision) {
var segments = this._segments,
precision = arguments[0],
f = Formatter.instance,
parts = [];
@ -690,7 +689,7 @@ var Path = PathItem.extend(/** @lends Path# */{
* // Select the path, so we can see its segments:
* path.selected = true;
*/
removeSegments: function(from, to/*, includeCurves */) {
removeSegments: function(from, to, _includeCurves) {
from = from || 0;
to = Base.pick(to, this._segments.length);
var segments = this._segments,
@ -724,7 +723,7 @@ var Path = PathItem.extend(/** @lends Path# */{
// Return the removed curves as well, if we're asked to include
// them, but exclude the first curve, since that's shared with the
// previous segment and does not connect the returned segments.
if (arguments[2])
if (_includeCurves)
removed._curves = curves.slice(1);
// Adjust segments for the curves before and after the removed ones
this._adjustCurves(index, index);

View file

@ -310,26 +310,24 @@ var Segment = Base.extend(/** @lends Segment# */{
* // Select the third segment point:
* path.segments[2].selected = true;
*/
isSelected: function(/* point */) {
var point = arguments[0], // Hidden, only used in SegmentPoint
state = this._selectionState;
return !point ? !!(state & /*#=*/ SelectionState.SEGMENT)
: point === this._point ? !!(state & /*#=*/ SelectionState.POINT)
: point === this._handleIn ? !!(state & /*#=*/ SelectionState.HANDLE_IN)
: point === this._handleOut ? !!(state & /*#=*/ SelectionState.HANDLE_OUT)
isSelected: function(_point) {
var state = this._selectionState;
return !_point ? !!(state & /*#=*/ SelectionState.SEGMENT)
: _point === this._point ? !!(state & /*#=*/ SelectionState.POINT)
: _point === this._handleIn ? !!(state & /*#=*/ SelectionState.HANDLE_IN)
: _point === this._handleOut ? !!(state & /*#=*/ SelectionState.HANDLE_OUT)
: false;
},
setSelected: function(selected /*, point */) {
var point = arguments[1]; // Hidden, only used in SegmentPoint
path = this._path,
setSelected: function(selected, _point) {
var path = this._path,
selected = !!selected, // convert to boolean
state = this._selectionState,
oldState = state,
flag = !point ? /*#=*/ SelectionState.SEGMENT
: point === this._point ? /*#=*/ SelectionState.POINT
: point === this._handleIn ? /*#=*/ SelectionState.HANDLE_IN
: point === this._handleOut ? /*#=*/ SelectionState.HANDLE_OUT
flag = !_point ? /*#=*/ SelectionState.SEGMENT
: _point === this._point ? /*#=*/ SelectionState.POINT
: _point === this._handleIn ? /*#=*/ SelectionState.HANDLE_IN
: _point === this._handleOut ? /*#=*/ SelectionState.HANDLE_OUT
: 0;
if (selected) {
state |= flag;

View file

@ -115,7 +115,7 @@ var Symbol = Base.extend(/** @lends Symbol# */{
return this._definition;
},
setDefinition: function(item /*, dontCenter */) {
setDefinition: function(item, _dontCenter) {
// Make sure we're not steatling another symbol's definition
if (item._parentSymbol)
item = item.clone();
@ -127,7 +127,7 @@ var Symbol = Base.extend(/** @lends Symbol# */{
item.remove();
item.setSelected(false);
// Move position to 0, 0, so it's centered when placed.
if (!arguments[1])
if (!_dontCenter)
item.setPosition(new Point());
item._parentSymbol = this;
this._changed(/*#=*/ Change.GEOMETRY);

View file

@ -168,13 +168,13 @@ var Style = Base.extend(new function() {
}
};
fields[get] = function(/* dontMerge */) {
fields[get] = function(_dontMerge) {
var value,
children = this._item && this._item._children;
// If this item has children, walk through all of them and see if
// they all have the same style.
// If true is passed for dontMerge, don't merge children styles
if (!children || children.length === 0 || arguments[0]
// If true is passed for _dontMerge, don't merge children styles
if (!children || children.length === 0 || _dontMerge
|| this._item instanceof CompoundPath) {
var value = this._values[key];
if (value === undefined) {

View file

@ -334,8 +334,8 @@ var View = Base.extend(Callback, /** @lends View# */{
* @type Size
* @bean
*/
getSize: function(/* dontLink */) {
return this.getBounds().getSize(arguments[0]);
getSize: function() {
return this.getBounds().getSize();
},
/**
@ -344,8 +344,8 @@ var View = Base.extend(Callback, /** @lends View# */{
* @type Point
* @bean
*/
getCenter: function(/* dontLink */) {
return this.getBounds().getCenter(arguments[0]);
getCenter: function() {
return this.getBounds().getCenter();
},
setCenter: function(center) {