|
| 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 | +}); |
0 commit comments