Skip to content

Commit 9da392a

Browse files
committed
Commit initial support for Shape class.
Hit testing is still missing.
1 parent 90f2614 commit 9da392a

4 files changed

Lines changed: 97 additions & 4 deletions

File tree

src/item/Shape.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
3+
* http://paperjs.org/
4+
*
5+
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
6+
* http://lehni.org/ & http://jonathanpuckey.com/
7+
*
8+
* Distributed under the MIT license. See LICENSE file for details.
9+
*
10+
* All rights reserved.
11+
*/
12+
13+
/**
14+
* @name Shape
15+
*
16+
* @class
17+
*
18+
* @extends Item
19+
*/
20+
var Shape = this.Shape = Item.extend(/** @lends Shape# */{
21+
_class: 'Shape',
22+
23+
initialize: function(type, size) {
24+
this.base();
25+
this._type = type;
26+
this._size = size;
27+
},
28+
29+
_draw: function(ctx, param) {
30+
var style = this._style,
31+
size = this._size,
32+
width = size.width,
33+
height = size.height,
34+
fillColor = style._fillColor,
35+
strokeColor = style._strokeColor;
36+
if (fillColor || strokeColor || param.clip) {
37+
ctx.beginPath();
38+
switch (this._type) {
39+
case 'rect':
40+
ctx.rect(-width / 2, -height / 2, width, height);
41+
break;
42+
case 'circle':
43+
ctx.arc(0, 0, width, 0, Math.PI * 2, true);
44+
break;
45+
case 'ellipse':
46+
var kappa = Numerical.KAPPA,
47+
cx = width * kappa,
48+
cy = height * kappa,
49+
mx = width / 2,
50+
my = height / 2;
51+
ctx.moveTo(0, my);
52+
ctx.bezierCurveTo(0, my - cy, mx - cx, 0, mx, 0);
53+
ctx.bezierCurveTo(mx + cx, 0, width, my - cy, width, my);
54+
ctx.bezierCurveTo(width, my + cy, mx + cx, height, mx, height);
55+
ctx.bezierCurveTo(mx - cx, height, 0, my + cy, 0, my);
56+
break;
57+
}
58+
}
59+
if (!param.clip && (fillColor || strokeColor)) {
60+
this._setStyles(ctx);
61+
if (fillColor)
62+
ctx.fill();
63+
if (strokeColor)
64+
ctx.stroke();
65+
}
66+
},
67+
68+
_getBounds: function(getter, matrix) {
69+
var rect = new Rectangle(this._size).setCenter(0, 0);
70+
return matrix ? matrix._transformBounds(rect) : rect;
71+
},
72+
73+
statics: {
74+
Circle: function(/* center, radius */) {
75+
var center = Point.readNamed(arguments, 'center'),
76+
radius = Base.readNamed(arguments, 'radius');
77+
return new Shape('circle', new Size(radius)).translate(center);
78+
},
79+
80+
Rectangle: function(/* rectangle */) {
81+
var rect = Rectangle.readNamed(arguments, 'rectangle');
82+
return new Shape('rect', rect.getSize(true)).translate(
83+
rect.getCenter(true));
84+
},
85+
86+
Ellipse: function(/* rectangle */) {
87+
var rect = Rectangle.readNamed(arguments, 'rectangle');
88+
return new Shape('ellipse', rect.getSize(true)).translate(
89+
rect.getCenter(true));
90+
}
91+
}
92+
});

src/paper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ var paper = new function() {
6262
/*#*/ include('item/Item.js');
6363
/*#*/ include('item/Group.js');
6464
/*#*/ include('item/Layer.js');
65+
/*#*/ include('item/Shape.js');
6566
/*#*/ include('item/Raster.js');
6667
/*#*/ include('item/PlacedSymbol.js');
6768
/*#*/ include('item/HitResult.js');

src/path/Path.Constructors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Path.inject({ statics: new function() {
2020
return path;
2121
}
2222

23-
function createRectangle(/* rect */) {
23+
function createRectangle(/* rectangle */) {
2424
var rect = Rectangle.readNamed(arguments, 'rectangle'),
2525
radius = Size.readNamed(arguments, 'radius', 0, 0, false, true), // readNull
2626
bl = rect.getBottomLeft(true),
@@ -65,7 +65,7 @@ Path.inject({ statics: new function() {
6565
new Segment([0.5, 1], [kappa, 0 ], [-kappa, 0])
6666
];
6767

68-
function createEllipse(/* rect */) {
68+
function createEllipse(/* rectangle */) {
6969
var rect = Rectangle.readNamed(arguments, 'rectangle'),
7070
path = createPath(arguments),
7171
point = rect.getPoint(true),

src/path/Path.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,8 +1831,8 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
18311831

18321832
// Prepare the canvas path if we have any situation that requires it
18331833
// to be defined.
1834-
if (param.compound || param.clip || fillColor || strokeColor
1835-
&& !drawDash)
1834+
if (fillColor || strokeColor && !drawDash || param.compound
1835+
|| param.clip)
18361836
drawSegments(ctx, this);
18371837

18381838
if (this._closed)

0 commit comments

Comments
 (0)