mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Clean up recently caused Base.read() mess by introducing _readNull property,m setting it to true on Color and reverting to previous behavior for Point, Size and Rectangle, where dimensions of 0 are read by default if nothing is provided.
This commit is contained in:
parent
1cd6daada1
commit
9d4af0ce9e
14 changed files with 144 additions and 191 deletions
|
@ -139,11 +139,10 @@ var Matrix = this.Matrix = Base.extend({
|
||||||
* @return {Matrix} This affine transform.
|
* @return {Matrix} This affine transform.
|
||||||
*/
|
*/
|
||||||
translate: function(point) {
|
translate: function(point) {
|
||||||
if (point = Point.read(arguments)) {
|
point = Point.read(arguments);
|
||||||
var x = point.x, y = point.y;
|
var x = point.x, y = point.y;
|
||||||
this._m02 += x * this._m00 + y * this._m01;
|
this._m02 += x * this._m00 + y * this._m01;
|
||||||
this._m12 += x * this._m10 + y * this._m11;
|
this._m12 += x * this._m10 + y * this._m11;
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -303,14 +302,12 @@ var Matrix = this.Matrix = Base.extend({
|
||||||
}
|
}
|
||||||
return dst;
|
return dst;
|
||||||
} else if (arguments.length > 0) {
|
} else if (arguments.length > 0) {
|
||||||
var point = Point.read(arguments);
|
var point = Point.read(arguments),
|
||||||
if (point) {
|
x = point.x, y = point.y;
|
||||||
var x = point.x, y = point.y;
|
return Point.create(
|
||||||
return Point.create(
|
x * this._m00 + y * this._m01 + this._m02,
|
||||||
x * this._m00 + y * this._m01 + this._m02,
|
x * this._m10 + y * this._m11 + this._m12
|
||||||
x * this._m10 + y * this._m11 + this._m12
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
@ -417,10 +414,8 @@ var Matrix = this.Matrix = Base.extend({
|
||||||
* @return {Matrix} This affine transform.
|
* @return {Matrix} This affine transform.
|
||||||
*/
|
*/
|
||||||
setToTranslation: function(delta) {
|
setToTranslation: function(delta) {
|
||||||
if (delta = Point.read(arguments)) {
|
delta = Point.read(arguments);
|
||||||
this.set(1, 0, 0, 1, delta.x, delta.y);
|
return this.set(1, 0, 0, 1, delta.x, delta.y);
|
||||||
}
|
|
||||||
return this;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -443,17 +438,15 @@ var Matrix = this.Matrix = Base.extend({
|
||||||
* @return {Matrix} This affine transform.
|
* @return {Matrix} This affine transform.
|
||||||
*/
|
*/
|
||||||
setToRotation: function(angle, center) {
|
setToRotation: function(angle, center) {
|
||||||
if (center = Point.read(arguments, 1)) {
|
center = Point.read(arguments, 1);
|
||||||
angle = angle * Math.PI / 180.0;
|
angle = angle * Math.PI / 180.0;
|
||||||
var x = center.x,
|
var x = center.x,
|
||||||
y = center.y,
|
y = center.y,
|
||||||
cos = Math.cos(angle),
|
cos = Math.cos(angle),
|
||||||
sin = Math.sin(angle);
|
sin = Math.sin(angle);
|
||||||
this.set(cos, sin, -sin, cos,
|
return this.set(cos, sin, -sin, cos,
|
||||||
x - x * cos + y * sin,
|
x - x * cos + y * sin,
|
||||||
y - x * sin - y * cos);
|
y - x * sin - y * cos);
|
||||||
}
|
|
||||||
return this;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -78,33 +78,28 @@ var Point = this.Point = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
add: function(point) {
|
add: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
? Point.create(this.x + point.x, this.y + point.y)
|
return Point.create(this.x + point.x, this.y + point.y);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
subtract: function(point) {
|
subtract: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
? Point.create(this.x - point.x, this.y - point.y)
|
return Point.create(this.x - point.x, this.y - point.y);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
multiply: function(point) {
|
multiply: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
? Point.create(this.x * point.x, this.y * point.y)
|
return Point.create(this.x * point.x, this.y * point.y);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
divide: function(point) {
|
divide: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
? Point.create(this.x / point.x, this.y / point.y)
|
return Point.create(this.x / point.x, this.y / point.y);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
modulo: function(point) {
|
modulo: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
? Point.create(this.x % point.x, this.y % point.y)
|
return Point.create(this.x % point.x, this.y % point.y);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
negate: function() {
|
negate: function() {
|
||||||
|
@ -112,8 +107,8 @@ var Point = this.Point = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
equals: function(point) {
|
equals: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
&& this.x == point.x && this.y == point.y;
|
return this.x == point.x && this.y == point.y;
|
||||||
},
|
},
|
||||||
|
|
||||||
transform: function(matrix) {
|
transform: function(matrix) {
|
||||||
|
@ -137,19 +132,17 @@ var Point = this.Point = Base.extend({
|
||||||
* @param py
|
* @param py
|
||||||
*/
|
*/
|
||||||
getDistance: function(point) {
|
getDistance: function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return null;
|
var x = point.x - this.x,
|
||||||
var px = point.x - this.x;
|
y = point.y - this.y;
|
||||||
var py = point.y - this.y;
|
return Math.sqrt(x * x + y * y);
|
||||||
return Math.sqrt(px * px + py * py);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getDistanceSquared: function(point) {
|
getDistanceSquared: function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return null;
|
var x = point.x - this.x,
|
||||||
var px = point.x - this.x;
|
y = point.y - this.y;
|
||||||
var py = point.y - this.y;
|
return x * x + y * y;
|
||||||
return px * px + py * py;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -273,17 +266,15 @@ var Point = this.Point = Base.extend({
|
||||||
* @param point
|
* @param point
|
||||||
*/
|
*/
|
||||||
getDirectedAngle: function(point) {
|
getDirectedAngle: function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return null;
|
|
||||||
var angle = this.getAngle() - point.getAngle();
|
var angle = this.getAngle() - point.getAngle();
|
||||||
var bounds = 180;
|
var bounds = 180;
|
||||||
if (angle < - bounds) {
|
if (angle < - bounds) {
|
||||||
return angle + bounds * 2;
|
return angle + bounds * 2;
|
||||||
} else if (angle > bounds) {
|
} else if (angle > bounds) {
|
||||||
return angle - bounds * 2;
|
return angle - bounds * 2;
|
||||||
} else {
|
|
||||||
return angle;
|
|
||||||
}
|
}
|
||||||
|
return angle;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -452,9 +443,8 @@ var Point = this.Point = Base.extend({
|
||||||
* @return the dot product of the two points
|
* @return the dot product of the two points
|
||||||
*/
|
*/
|
||||||
dot: function(point) {
|
dot: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
? this.x * point.x + this.y * point.y
|
return this.x * point.x + this.y * point.y;
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -463,9 +453,8 @@ var Point = this.Point = Base.extend({
|
||||||
* @return the cross product of the two points
|
* @return the cross product of the two points
|
||||||
*/
|
*/
|
||||||
cross: function(point) {
|
cross: function(point) {
|
||||||
return (point = Point.read(arguments))
|
point = Point.read(arguments);
|
||||||
? this.x * point.y - this.y * point.x
|
return this.x * point.y - this.y * point.x;
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -476,8 +465,7 @@ var Point = this.Point = Base.extend({
|
||||||
* @return the project of the point on another point
|
* @return the project of the point on another point
|
||||||
*/
|
*/
|
||||||
project: function(point) {
|
project: function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return null;
|
|
||||||
if (point.isZero()) {
|
if (point.isZero()) {
|
||||||
return Point.create(0, 0);
|
return Point.create(0, 0);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -75,8 +75,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setPoint: function(point) {
|
setPoint: function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return this;
|
|
||||||
this.x = point.x;
|
this.x = point.x;
|
||||||
this.y = point.y;
|
this.y = point.y;
|
||||||
return this;
|
return this;
|
||||||
|
@ -87,8 +86,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setSize: function(size) {
|
setSize: function(size) {
|
||||||
if (!(size = Size.read(arguments)))
|
size = Size.read(arguments);
|
||||||
return this;
|
|
||||||
this.width = size.width;
|
this.width = size.width;
|
||||||
this.height = size.height;
|
this.height = size.height;
|
||||||
return this;
|
return this;
|
||||||
|
@ -156,8 +154,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setCenter: function(point) {
|
setCenter: function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return this;
|
|
||||||
return this.setCenterX(point.x).setCenterY(point.y);
|
return this.setCenterX(point.x).setCenterY(point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -197,8 +194,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
intersect: function(rect) {
|
intersect: function(rect) {
|
||||||
if (!(rect = Rectangle.read(arguments)))
|
rect = Rectangle.read(arguments);
|
||||||
return null;
|
|
||||||
var x1 = Math.max(this.x, rect.x),
|
var x1 = Math.max(this.x, rect.x),
|
||||||
y1 = Math.max(this.y, rect.y),
|
y1 = Math.max(this.y, rect.y),
|
||||||
x2 = Math.min(this.x + this.width, rect.x + rect.width),
|
x2 = Math.min(this.x + this.width, rect.x + rect.width),
|
||||||
|
@ -207,8 +203,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
unite: function(rect) {
|
unite: function(rect) {
|
||||||
if (!(rect = Rectangle.read(arguments)))
|
rect = Rectangle.read(arguments);
|
||||||
return null;
|
|
||||||
var x1 = Math.min(this.x, rect.x),
|
var x1 = Math.min(this.x, rect.x),
|
||||||
y1 = Math.min(this.y, rect.y),
|
y1 = Math.min(this.y, rect.y),
|
||||||
x2 = Math.max(this.x + this.width, rect.x + rect.width),
|
x2 = Math.max(this.x + this.width, rect.x + rect.width),
|
||||||
|
@ -217,8 +212,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
include: function(point) {
|
include: function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return null;
|
|
||||||
var x1 = Math.min(this.x, point.x),
|
var x1 = Math.min(this.x, point.x),
|
||||||
y1 = Math.min(this.y, point.y),
|
y1 = Math.min(this.y, point.y),
|
||||||
x2 = Math.max(this.x + this.width, point.x),
|
x2 = Math.max(this.x + this.width, point.x),
|
||||||
|
@ -265,8 +259,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
return Point.create(this[getX](), this[getY]());
|
return Point.create(this[getX](), this[getY]());
|
||||||
};
|
};
|
||||||
this['set' + key] = function(point) {
|
this['set' + key] = function(point) {
|
||||||
if (!(point = Point.read(arguments)))
|
point = Point.read(arguments);
|
||||||
return this;
|
|
||||||
return this[setX](point.x)[setY](point.y); // Note: call chaining!
|
return this[setX](point.x)[setY](point.y); // Note: call chaining!
|
||||||
};
|
};
|
||||||
}, { beans: true });
|
}, { beans: true });
|
||||||
|
|
|
@ -47,44 +47,38 @@ var Size = this.Size = Base.extend({
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
add: function() {
|
add: function(size) {
|
||||||
return (size = Size.read(arguments))
|
size = Size.read(arguments);
|
||||||
? Size.create(this.width + size.width, this.height + size.height)
|
return Size.create(this.width + size.width, this.height + size.height);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
subtract: function() {
|
subtract: function(size) {
|
||||||
return (size = Size.read(arguments))
|
size = Size.read(arguments);
|
||||||
? Size.create(this.width - size.width, this.height - size.height)
|
return Size.create(this.width - size.width, this.height - size.height);
|
||||||
: null;
|
|
||||||
;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
multiply: function() {
|
multiply: function(size) {
|
||||||
return (size = Size.read(arguments))
|
size = Size.read(arguments);
|
||||||
? Size.create(this.width * size.width, this.height * size.height)
|
return Size.create(this.width * size.width, this.height * size.height);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
divide: function() {
|
divide: function(size) {
|
||||||
return (size = Size.read(arguments))
|
size = Size.read(arguments);
|
||||||
? Size.create(this.width / size.width, this.height / size.height)
|
return Size.create(this.width / size.width, this.height / size.height);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
modulo: function() {
|
modulo: function(size) {
|
||||||
return (size = Size.read(arguments))
|
size = Size.read(arguments);
|
||||||
? Size.create(this.width % size.width, this.height % size.height)
|
return Size.create(this.width % size.width, this.height % size.height);
|
||||||
: null;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
negate: function() {
|
negate: function() {
|
||||||
return Size.create(-this.width, -this.height);
|
return Size.create(-this.width, -this.height);
|
||||||
},
|
},
|
||||||
|
|
||||||
equals: function() {
|
equals: function(size) {
|
||||||
return (size = Size.read(arguments))
|
size = Size.read(arguments);
|
||||||
&& this.width == size.width && this.height == size.height;
|
return this.width == size.width && this.height == size.height;
|
||||||
},
|
},
|
||||||
|
|
||||||
isNaN: function() {
|
isNaN: function() {
|
||||||
|
|
|
@ -88,6 +88,7 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
beans: true,
|
beans: true,
|
||||||
|
_readNull: true,
|
||||||
|
|
||||||
initialize: function(arg) {
|
initialize: function(arg) {
|
||||||
var isArray = Array.isArray(arg);
|
var isArray = Array.isArray(arg);
|
||||||
|
|
|
@ -30,11 +30,10 @@ var GradientColor = this.GradientColor = Color.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setOrigin: function(origin) {
|
setOrigin: function(origin) {
|
||||||
if (origin = Point.read(arguments)) {
|
origin = Point.read(arguments);
|
||||||
this._origin = origin;
|
this._origin = origin;
|
||||||
if (this._destination)
|
if (this._destination)
|
||||||
this._radius = this._destination.getDistance(this._origin);
|
this._radius = this._destination.getDistance(this._origin);
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -43,10 +42,9 @@ var GradientColor = this.GradientColor = Color.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setDestination: function(destination) {
|
setDestination: function(destination) {
|
||||||
if (destination = Point.read(arguments)) {
|
destination = Point.read(arguments);
|
||||||
this._destination = destination;
|
this._destination = destination;
|
||||||
this._radius = this._destination.getDistance(this._origin);
|
this._radius = this._destination.getDistance(this._origin);
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -55,14 +53,13 @@ var GradientColor = this.GradientColor = Color.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setHilite: function(hilite) {
|
setHilite: function(hilite) {
|
||||||
if (hilite = Point.read(arguments)) {
|
hilite = Point.read(arguments);
|
||||||
var vector = hilite.subtract(this._origin);
|
var vector = hilite.subtract(this._origin);
|
||||||
if (vector.getLength() > this._radius) {
|
if (vector.getLength() > this._radius) {
|
||||||
this._hilite = this._origin.add(vector.normalize(
|
this._hilite = this._origin.add(vector.normalize(
|
||||||
this._radius - 0.1));
|
this._radius - 0.1));
|
||||||
} else {
|
} else {
|
||||||
this._hilite = hilite;
|
this._hilite = hilite;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
|
@ -30,13 +30,11 @@ var DocumentView = this.DocumentView = Base.extend({
|
||||||
return this._center;
|
return this._center;
|
||||||
},
|
},
|
||||||
|
|
||||||
setCenter: function() {
|
setCenter: function(center) {
|
||||||
var center = Point.read(arguments);
|
center = Point.read(arguments);
|
||||||
if (center) {
|
var delta = center.subtract(this._center);
|
||||||
var delta = center.subtract(this._center);
|
this.scrollBy(delta);
|
||||||
this.scrollBy(delta);
|
this._center = center;
|
||||||
this._center = center;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getZoom: function() {
|
getZoom: function() {
|
||||||
|
@ -51,10 +49,9 @@ var DocumentView = this.DocumentView = Base.extend({
|
||||||
this._zoom = zoom;
|
this._zoom = zoom;
|
||||||
},
|
},
|
||||||
|
|
||||||
scrollBy: function() {
|
scrollBy: function(point) {
|
||||||
var point = Point.read(arguments).negate();
|
point = Point.read(arguments);
|
||||||
var mx = new Matrix().translate(point);
|
this.transform(new Matrix().translate(point.negate()));
|
||||||
this.transform(mx);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
transform: function(matrix, flags) {
|
transform: function(matrix, flags) {
|
||||||
|
|
|
@ -394,14 +394,12 @@ var Item = this.Item = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setBounds: function(rect) {
|
setBounds: function(rect) {
|
||||||
var bounds = this.getBounds();
|
|
||||||
rect = Rectangle.read(arguments);
|
rect = Rectangle.read(arguments);
|
||||||
if (!rect)
|
var bounds = this.getBounds(),
|
||||||
return;
|
matrix = new Matrix(),
|
||||||
var matrix = new Matrix();
|
center = rect.center;
|
||||||
// Read this from bottom to top:
|
// Read this from bottom to top:
|
||||||
// Translate to new center:
|
// Translate to new center:
|
||||||
var center = rect.center;
|
|
||||||
matrix.translate(center);
|
matrix.translate(center);
|
||||||
// Scale to new Size, if size changes and avoid divisions by 0:
|
// Scale to new Size, if size changes and avoid divisions by 0:
|
||||||
if (rect.width != bounds.width || rect.height != bounds.height) {
|
if (rect.width != bounds.width || rect.height != bounds.height) {
|
||||||
|
|
|
@ -122,17 +122,17 @@ var Raster = this.Raster = Item.extend({
|
||||||
this._bounds = null;
|
this._bounds = null;
|
||||||
},
|
},
|
||||||
|
|
||||||
getSubImage: function(/* rectangle */) {
|
getSubImage: function(rect) {
|
||||||
var rectangle = Rectangle.read(arguments),
|
rect = Rectangle.read(arguments);
|
||||||
canvas = CanvasProvider.getCanvas(rectangle.getSize()),
|
var canvas = CanvasProvider.getCanvas(rect.getSize()),
|
||||||
context = canvas.getContext('2d');
|
context = canvas.getContext('2d');
|
||||||
context.drawImage(this.getCanvas(), rectangle.x, rectangle.y,
|
context.drawImage(this.getCanvas(), rect.x, rect.y,
|
||||||
canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
|
canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
|
||||||
return canvas;
|
return canvas;
|
||||||
},
|
},
|
||||||
|
|
||||||
drawImage: function(image, point) {
|
drawImage: function(image, point) {
|
||||||
var point = Point.read(arguments, 1);
|
point = Point.read(arguments, 1);
|
||||||
this.getContext().drawImage(image, point.x, point.y);
|
this.getContext().drawImage(image, point.x, point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -143,9 +143,9 @@ var Raster = this.Raster = Item.extend({
|
||||||
* @param x
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
*/
|
*/
|
||||||
getPixel: function() {
|
getPixel: function(point) {
|
||||||
var point = Point.read(arguments),
|
point = Point.read(arguments);
|
||||||
ctx = this.getContext(),
|
var ctx = this.getContext(),
|
||||||
pixels = ctx.getImageData(point.x, point.y, 1, 1).data,
|
pixels = ctx.getImageData(point.x, point.y, 1, 1).data,
|
||||||
channels = new Array(4);
|
channels = new Array(4);
|
||||||
for (var i = 0; i < 4; i++)
|
for (var i = 0; i < 4; i++)
|
||||||
|
@ -156,6 +156,8 @@ var Raster = this.Raster = Item.extend({
|
||||||
// TODO: setPixel(point, color)
|
// TODO: setPixel(point, color)
|
||||||
setPixel: function(x, y, color) {
|
setPixel: function(x, y, color) {
|
||||||
color = Color.read(arguments, 2);
|
color = Color.read(arguments, 2);
|
||||||
|
// TODO: Instead of getting image data, use context.createImageData()
|
||||||
|
// TODO: Cache imageData used for setting pixel, so it cna be reused.
|
||||||
var ctx = this.getContext(),
|
var ctx = this.getContext(),
|
||||||
imageData = ctx.getImageData(x, y, 1, 1),
|
imageData = ctx.getImageData(x, y, 1, 1),
|
||||||
alpha = color.getAlpha();
|
alpha = color.getAlpha();
|
||||||
|
@ -171,16 +173,16 @@ var Raster = this.Raster = Item.extend({
|
||||||
return this.getContext().createImageData(size.width, size.height);
|
return this.getContext().createImageData(size.width, size.height);
|
||||||
},
|
},
|
||||||
|
|
||||||
getData: function(/* [rect] */) {
|
getData: function(rect) {
|
||||||
var rect = Rectangle.read(arguments),
|
rect = Rectangle.read(arguments);
|
||||||
ctx = this.getContext();
|
var ctx = this.getContext();
|
||||||
rect = rect.isEmpty() ? new Rectangle(this.getSize()) : rect;
|
rect = rect.isEmpty() ? new Rectangle(this.getSize()) : rect;
|
||||||
return ctx.getImageData(rect.x, rect.y, rect.width, rect.height);
|
return ctx.getImageData(rect.x, rect.y, rect.width, rect.height);
|
||||||
},
|
},
|
||||||
|
|
||||||
setData: function(data /* [, point] */) {
|
setData: function(data, point) {
|
||||||
var point = Rectangle.read(arguments, 1),
|
point = Point.read(arguments, 1);
|
||||||
ctx = this.getContext();
|
var ctx = this.getContext();
|
||||||
ctx.putImageData(data, point.x, point.y);
|
ctx.putImageData(data, point.x, point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -55,8 +55,9 @@ Base.inject({
|
||||||
read: function(args, index, length) {
|
read: function(args, index, length) {
|
||||||
var index = index || 0, length = length || args.length - index;
|
var index = index || 0, length = length || args.length - index;
|
||||||
var arg = args[index];
|
var arg = args[index];
|
||||||
// Return null when nothing was provided
|
// If the class defines _readNull, return null when nothing was provided
|
||||||
if (arg instanceof this || arg == null && length <= 1)
|
if (arg instanceof this
|
||||||
|
|| this.prototype._readNull && arg == null && length <= 1)
|
||||||
return arg;
|
return arg;
|
||||||
var obj = new this(this.dont);
|
var obj = new this(this.dont);
|
||||||
obj = obj.initialize.apply(obj, index > 0 || length < args.length
|
obj = obj.initialize.apply(obj, index > 0 || length < args.length
|
||||||
|
|
|
@ -60,8 +60,8 @@ var Curve = this.Curve = Base.extend({
|
||||||
return this._segment1._point;
|
return this._segment1._point;
|
||||||
},
|
},
|
||||||
|
|
||||||
setPoint1: function() {
|
setPoint1: function(point) {
|
||||||
var point = Point.read(arguments);
|
point = Point.read(arguments);
|
||||||
this._segment1._point.set(point.x, point.y);
|
this._segment1._point.set(point.x, point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -72,8 +72,8 @@ var Curve = this.Curve = Base.extend({
|
||||||
return this._segment2._point;
|
return this._segment2._point;
|
||||||
},
|
},
|
||||||
|
|
||||||
setPoint2: function() {
|
setPoint2: function(point) {
|
||||||
var point = Point.read(arguments);
|
point = Point.read(arguments);
|
||||||
this._segment2._point.set(point.x, point.y);
|
this._segment2._point.set(point.x, point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -84,8 +84,8 @@ var Curve = this.Curve = Base.extend({
|
||||||
return this._segment1._handleOut;
|
return this._segment1._handleOut;
|
||||||
},
|
},
|
||||||
|
|
||||||
setHandle1: function() {
|
setHandle1: function(point) {
|
||||||
var point = Point.read(arguments);
|
point = Point.read(arguments);
|
||||||
this._segment1._handleOut.set(point.x, point.y);
|
this._segment1._handleOut.set(point.x, point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -96,8 +96,8 @@ var Curve = this.Curve = Base.extend({
|
||||||
return this._segment2._handleIn;
|
return this._segment2._handleIn;
|
||||||
},
|
},
|
||||||
|
|
||||||
setHandle2: function() {
|
setHandle2: function(point) {
|
||||||
var point = Point.read(arguments);
|
point = Point.read(arguments);
|
||||||
this._segment2._handleIn.set(point.x, point.y);
|
this._segment2._handleIn.set(point.x, point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -26,18 +26,15 @@ Path.inject({ statics: new function() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
Line: function() {
|
Line: function() {
|
||||||
if (arguments.length >= 2) {
|
var step = Math.floor(arguments.length / 2);
|
||||||
var step = Math.floor(arguments.length / 2);
|
return new Path(
|
||||||
return new Path(
|
Segment.read(arguments, 0, step),
|
||||||
Segment.read(arguments, 0, step),
|
Segment.read(arguments, step, step)
|
||||||
Segment.read(arguments, step, step)
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
Rectangle: function(rect) {
|
Rectangle: function(rect) {
|
||||||
if (!(rect = Rectangle.read(arguments)))
|
rect = Rectangle.read(arguments);
|
||||||
return null;
|
|
||||||
var path = new Path(),
|
var path = new Path(),
|
||||||
corners = ['getBottomLeft', 'getTopLeft', 'getTopRight',
|
corners = ['getBottomLeft', 'getTopLeft', 'getTopRight',
|
||||||
'getBottomRight'];
|
'getBottomRight'];
|
||||||
|
@ -56,8 +53,6 @@ Path.inject({ statics: new function() {
|
||||||
rect = Rectangle.read(arguments, 0, 4);
|
rect = Rectangle.read(arguments, 0, 4);
|
||||||
size = Size.read(arguments, 4, 2);
|
size = Size.read(arguments, 4, 2);
|
||||||
}
|
}
|
||||||
if (!rect || !size)
|
|
||||||
return null;
|
|
||||||
size = Size.min(size, rect.getSize().divide(2));
|
size = Size.min(size, rect.getSize().divide(2));
|
||||||
var path = new Path(),
|
var path = new Path(),
|
||||||
uSize = size.multiply(kappa * 2),
|
uSize = size.multiply(kappa * 2),
|
||||||
|
@ -84,8 +79,7 @@ Path.inject({ statics: new function() {
|
||||||
},
|
},
|
||||||
|
|
||||||
Oval: function(rect) {
|
Oval: function(rect) {
|
||||||
if (!(rect = Rectangle.read(arguments)))
|
rect = Rectangle.read(arguments);
|
||||||
return null;
|
|
||||||
var path = new Path(),
|
var path = new Path(),
|
||||||
topLeft = rect.getTopLeft(),
|
topLeft = rect.getTopLeft(),
|
||||||
size = new Size(rect.width, rect.height);
|
size = new Size(rect.width, rect.height);
|
||||||
|
@ -108,8 +102,6 @@ Path.inject({ statics: new function() {
|
||||||
} else {
|
} else {
|
||||||
center = Point.read(arguments, 0, 1);
|
center = Point.read(arguments, 0, 1);
|
||||||
}
|
}
|
||||||
if (!center || !radius)
|
|
||||||
return null;
|
|
||||||
return Path.Oval(new Rectangle(center.subtract(radius),
|
return Path.Oval(new Rectangle(center.subtract(radius),
|
||||||
new Size(radius * 2, radius * 2)));
|
new Size(radius * 2, radius * 2)));
|
||||||
},
|
},
|
||||||
|
@ -122,8 +114,7 @@ Path.inject({ statics: new function() {
|
||||||
},
|
},
|
||||||
|
|
||||||
RegularPolygon: function(center, numSides, radius) {
|
RegularPolygon: function(center, numSides, radius) {
|
||||||
if (!(center = Point.read(arguments, 0)))
|
center = Point.read(arguments, 0);
|
||||||
return null;
|
|
||||||
var path = new Path(),
|
var path = new Path(),
|
||||||
three = !(numSides % 3),
|
three = !(numSides % 3),
|
||||||
vector = new Point(0, three ? -radius : radius),
|
vector = new Point(0, three ? -radius : radius),
|
||||||
|
|
|
@ -529,12 +529,10 @@ var Path = this.Path = PathItem.extend({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
lineBy: function() {
|
lineBy: function(vector) {
|
||||||
var vector = Point.read(arguments);
|
vector = Point.read(arguments);
|
||||||
if (vector) {
|
var current = getCurrentSegment(this);
|
||||||
var current = getCurrentSegment(this);
|
this.lineTo(current._point.add(vector));
|
||||||
this.lineTo(current._point.add(vector));
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
curveBy: function(throughVector, toVector, parameter) {
|
curveBy: function(throughVector, toVector, parameter) {
|
||||||
|
|
|
@ -55,10 +55,10 @@ var Segment = this.Segment = Base.extend({
|
||||||
return this._point;
|
return this._point;
|
||||||
},
|
},
|
||||||
|
|
||||||
setPoint: function() {
|
setPoint: function(point) {
|
||||||
|
point = Point.read(arguments);
|
||||||
// Do not replace the internal object but update it instead, so
|
// Do not replace the internal object but update it instead, so
|
||||||
// references to it are kept alive.
|
// references to it are kept alive.
|
||||||
var point = Point.read(arguments);
|
|
||||||
this._point.set(point.x, point.y);
|
this._point.set(point.x, point.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -66,9 +66,9 @@ var Segment = this.Segment = Base.extend({
|
||||||
return this._handleIn;
|
return this._handleIn;
|
||||||
},
|
},
|
||||||
|
|
||||||
setHandleIn: function() {
|
setHandleIn: function(point) {
|
||||||
|
point = Point.read(arguments);
|
||||||
// See #setPoint:
|
// See #setPoint:
|
||||||
var point = Point.read(arguments);
|
|
||||||
this._handleIn.set(point.x, point.y);
|
this._handleIn.set(point.x, point.y);
|
||||||
// Update corner accordingly
|
// Update corner accordingly
|
||||||
// this.corner = !this._handleIn.isParallel(this._handleOut);
|
// this.corner = !this._handleIn.isParallel(this._handleOut);
|
||||||
|
@ -83,9 +83,9 @@ var Segment = this.Segment = Base.extend({
|
||||||
return this._handleOut;
|
return this._handleOut;
|
||||||
},
|
},
|
||||||
|
|
||||||
setHandleOut: function() {
|
setHandleOut: function(point) {
|
||||||
|
point = Point.read(arguments);
|
||||||
// See #setPoint:
|
// See #setPoint:
|
||||||
var point = Point.read(arguments);
|
|
||||||
this._handleOut.set(point.x, point.y);
|
this._handleOut.set(point.x, point.y);
|
||||||
// Update corner accordingly
|
// Update corner accordingly
|
||||||
// this.corner = !this._handleIn.isParallel(this._handleOut);
|
// this.corner = !this._handleIn.isParallel(this._handleOut);
|
||||||
|
|
Loading…
Reference in a new issue