2013-04-19 20:25:50 -04:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Shape
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*
|
|
|
|
* @extends Item
|
|
|
|
*/
|
|
|
|
var Shape = this.Shape = Item.extend(/** @lends Shape# */{
|
|
|
|
_class: 'Shape',
|
2013-05-13 21:57:17 -04:00
|
|
|
_applyMatrix: false,
|
2013-04-19 20:25:50 -04:00
|
|
|
|
2013-04-20 23:24:16 -04:00
|
|
|
initialize: function(type, point, size) {
|
2013-05-27 12:11:50 -04:00
|
|
|
Item.call(this, point);
|
2013-04-19 20:25:50 -04:00
|
|
|
this._type = type;
|
|
|
|
this._size = size;
|
|
|
|
},
|
|
|
|
|
|
|
|
_draw: function(ctx, param) {
|
|
|
|
var style = this._style,
|
|
|
|
size = this._size,
|
|
|
|
width = size.width,
|
|
|
|
height = size.height,
|
2013-04-19 22:31:29 -04:00
|
|
|
fillColor = style.getFillColor(),
|
|
|
|
strokeColor = style.getStrokeColor();
|
2013-04-19 20:25:50 -04:00
|
|
|
if (fillColor || strokeColor || param.clip) {
|
|
|
|
ctx.beginPath();
|
|
|
|
switch (this._type) {
|
|
|
|
case 'rect':
|
|
|
|
ctx.rect(-width / 2, -height / 2, width, height);
|
|
|
|
break;
|
|
|
|
case 'circle':
|
2013-04-20 23:53:40 -04:00
|
|
|
// Average half of width & height for radius...
|
|
|
|
ctx.arc(0, 0, (width + height) / 4, 0, Math.PI * 2, true);
|
2013-04-19 20:25:50 -04:00
|
|
|
break;
|
|
|
|
case 'ellipse':
|
2013-04-19 21:57:31 -04:00
|
|
|
var mx = width / 2,
|
|
|
|
my = height / 2,
|
|
|
|
kappa = Numerical.KAPPA,
|
|
|
|
cx = mx * kappa,
|
|
|
|
cy = my * kappa;
|
2013-04-19 20:25:50 -04:00
|
|
|
ctx.moveTo(0, my);
|
|
|
|
ctx.bezierCurveTo(0, my - cy, mx - cx, 0, mx, 0);
|
|
|
|
ctx.bezierCurveTo(mx + cx, 0, width, my - cy, width, my);
|
|
|
|
ctx.bezierCurveTo(width, my + cy, mx + cx, height, mx, height);
|
|
|
|
ctx.bezierCurveTo(mx - cx, height, 0, my + cy, 0, my);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!param.clip && (fillColor || strokeColor)) {
|
|
|
|
this._setStyles(ctx);
|
|
|
|
if (fillColor)
|
|
|
|
ctx.fill();
|
|
|
|
if (strokeColor)
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-27 13:04:05 -04:00
|
|
|
_contains: function _contains(point) {
|
2013-04-20 23:54:06 -04:00
|
|
|
switch (this._type) {
|
|
|
|
case 'rect':
|
2013-05-27 13:04:05 -04:00
|
|
|
return _contains.base.call(this, point);
|
2013-04-20 23:54:06 -04:00
|
|
|
case 'circle':
|
|
|
|
case 'ellipse':
|
|
|
|
return point.divide(this._size).getLength() <= 0.5;
|
|
|
|
}
|
2013-04-20 23:41:52 -04:00
|
|
|
},
|
|
|
|
|
2013-04-19 20:25:50 -04:00
|
|
|
_getBounds: function(getter, matrix) {
|
|
|
|
var rect = new Rectangle(this._size).setCenter(0, 0);
|
|
|
|
return matrix ? matrix._transformBounds(rect) : rect;
|
|
|
|
},
|
|
|
|
|
2013-04-20 23:24:16 -04:00
|
|
|
_hitTest: function(point, options) {
|
2013-04-20 23:41:52 -04:00
|
|
|
if (this.hasFill() && this.contains(point))
|
|
|
|
return new HitResult('fill', this);
|
2013-04-30 21:41:26 -04:00
|
|
|
// TODO: Implement stroke!
|
2013-04-20 23:24:16 -04:00
|
|
|
},
|
|
|
|
|
2013-04-19 20:25:50 -04:00
|
|
|
statics: {
|
|
|
|
Circle: function(/* center, radius */) {
|
|
|
|
var center = Point.readNamed(arguments, 'center'),
|
|
|
|
radius = Base.readNamed(arguments, 'radius');
|
2013-04-20 23:53:40 -04:00
|
|
|
return new Shape('circle', center, new Size(radius * 2));
|
2013-04-19 20:25:50 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
Rectangle: function(/* rectangle */) {
|
|
|
|
var rect = Rectangle.readNamed(arguments, 'rectangle');
|
2013-04-20 23:24:16 -04:00
|
|
|
return new Shape('rect', rect.getCenter(true), rect.getSize(true));
|
2013-04-19 20:25:50 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
Ellipse: function(/* rectangle */) {
|
|
|
|
var rect = Rectangle.readNamed(arguments, 'rectangle');
|
2013-04-20 23:24:16 -04:00
|
|
|
return new Shape('ellipse', rect.getCenter(true),
|
|
|
|
rect.getSize(true));
|
2013-04-19 20:25:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|