EaselJS v1.0.0 API Documentation : easeljs/display/Graphics.js

API Documentation for: 1.0.0
Show:

File:Graphics.js

  1. /*
  2. * Graphics
  3. * Visit http://createjs.com/ for documentation, updates and examples.
  4. *
  5. * Copyright (c) 2010 gskinner.com, inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28.  
  29. /**
  30. * @module EaselJS
  31. */
  32.  
  33. // namespace:
  34. this.createjs = this.createjs||{};
  35.  
  36. (function() {
  37. "use strict";
  38.  
  39.  
  40. // constructor:
  41. /**
  42. * The Graphics class exposes an easy to use API for generating vector drawing instructions and drawing them to a
  43. * specified context. Note that you can use Graphics without any dependency on the EaselJS framework by calling {{#crossLink "Graphics/draw"}}{{/crossLink}}
  44. * directly, or it can be used with the {{#crossLink "Shape"}}{{/crossLink}} object to draw vector graphics within the
  45. * context of an EaselJS display list.
  46. *
  47. * There are two approaches to working with Graphics object: calling methods on a Graphics instance (the "Graphics API"), or
  48. * instantiating Graphics command objects and adding them to the graphics queue via {{#crossLink "Graphics/append"}}{{/crossLink}}.
  49. * The former abstracts the latter, simplifying beginning and ending paths, fills, and strokes.
  50. *
  51. * var g = new createjs.Graphics();
  52. * g.setStrokeStyle(1);
  53. * g.beginStroke("#000000");
  54. * g.beginFill("red");
  55. * g.drawCircle(0,0,30);
  56. *
  57. * All drawing methods in Graphics return the Graphics instance, so they can be chained together. For example,
  58. * the following line of code would generate the instructions to draw a rectangle with a red stroke and blue fill:
  59. *
  60. * myGraphics.beginStroke("red").beginFill("blue").drawRect(20, 20, 100, 50);
  61. *
  62. * Each graphics API call generates a command object (see below). The last command to be created can be accessed via
  63. * {{#crossLink "Graphics/command:property"}}{{/crossLink}}:
  64. *
  65. * var fillCommand = myGraphics.beginFill("red").command;
  66. * // ... later, update the fill style/color:
  67. * fillCommand.style = "blue";
  68. * // or change it to a bitmap fill:
  69. * fillCommand.bitmap(myImage);
  70. *
  71. * For more direct control of rendering, you can instantiate and append command objects to the graphics queue directly. In this case, you
  72. * need to manage path creation manually, and ensure that fill/stroke is applied to a defined path:
  73. *
  74. * // start a new path. Graphics.beginCmd is a reusable BeginPath instance:
  75. * myGraphics.append(createjs.Graphics.beginCmd);
  76. * // we need to define the path before applying the fill:
  77. * var circle = new createjs.Graphics.Circle(0,0,30);
  78. * myGraphics.append(circle);
  79. * // fill the path we just defined:
  80. * var fill = new createjs.Graphics.Fill("red");
  81. * myGraphics.append(fill);
  82. *
  83. * These approaches can be used together, for example to insert a custom command:
  84. *
  85. * myGraphics.beginFill("red");
  86. * var customCommand = new CustomSpiralCommand(etc);
  87. * myGraphics.append(customCommand);
  88. * myGraphics.beginFill("blue");
  89. * myGraphics.drawCircle(0, 0, 30);
  90. *
  91. * See {{#crossLink "Graphics/append"}}{{/crossLink}} for more info on creating custom commands.
  92. *
  93. * <h4>Tiny API</h4>
  94. * The Graphics class also includes a "tiny API", which is one or two-letter methods that are shortcuts for all of the
  95. * Graphics methods. These methods are great for creating compact instructions, and is used by the Toolkit for CreateJS
  96. * to generate readable code. All tiny methods are marked as protected, so you can view them by enabling protected
  97. * descriptions in the docs.
  98. *
  99. * <table>
  100. * <tr><td><b>Tiny</b></td><td><b>Method</b></td><td><b>Tiny</b></td><td><b>Method</b></td></tr>
  101. * <tr><td>mt</td><td>{{#crossLink "Graphics/moveTo"}}{{/crossLink}} </td>
  102. * <td>lt</td> <td>{{#crossLink "Graphics/lineTo"}}{{/crossLink}}</td></tr>
  103. * <tr><td>a/at</td><td>{{#crossLink "Graphics/arc"}}{{/crossLink}} / {{#crossLink "Graphics/arcTo"}}{{/crossLink}} </td>
  104. * <td>bt</td><td>{{#crossLink "Graphics/bezierCurveTo"}}{{/crossLink}} </td></tr>
  105. * <tr><td>qt</td><td>{{#crossLink "Graphics/quadraticCurveTo"}}{{/crossLink}} (also curveTo)</td>
  106. * <td>r</td><td>{{#crossLink "Graphics/rect"}}{{/crossLink}} </td></tr>
  107. * <tr><td>cp</td><td>{{#crossLink "Graphics/closePath"}}{{/crossLink}} </td>
  108. * <td>c</td><td>{{#crossLink "Graphics/clear"}}{{/crossLink}} </td></tr>
  109. * <tr><td>f</td><td>{{#crossLink "Graphics/beginFill"}}{{/crossLink}} </td>
  110. * <td>lf</td><td>{{#crossLink "Graphics/beginLinearGradientFill"}}{{/crossLink}} </td></tr>
  111. * <tr><td>rf</td><td>{{#crossLink "Graphics/beginRadialGradientFill"}}{{/crossLink}} </td>
  112. * <td>bf</td><td>{{#crossLink "Graphics/beginBitmapFill"}}{{/crossLink}} </td></tr>
  113. * <tr><td>ef</td><td>{{#crossLink "Graphics/endFill"}}{{/crossLink}} </td>
  114. * <td>ss / sd</td><td>{{#crossLink "Graphics/setStrokeStyle"}}{{/crossLink}} / {{#crossLink "Graphics/setStrokeDash"}}{{/crossLink}} </td></tr>
  115. * <tr><td>s</td><td>{{#crossLink "Graphics/beginStroke"}}{{/crossLink}} </td>
  116. * <td>ls</td><td>{{#crossLink "Graphics/beginLinearGradientStroke"}}{{/crossLink}} </td></tr>
  117. * <tr><td>rs</td><td>{{#crossLink "Graphics/beginRadialGradientStroke"}}{{/crossLink}} </td>
  118. * <td>bs</td><td>{{#crossLink "Graphics/beginBitmapStroke"}}{{/crossLink}} </td></tr>
  119. * <tr><td>es</td><td>{{#crossLink "Graphics/endStroke"}}{{/crossLink}} </td>
  120. * <td>dr</td><td>{{#crossLink "Graphics/drawRect"}}{{/crossLink}} </td></tr>
  121. * <tr><td>rr</td><td>{{#crossLink "Graphics/drawRoundRect"}}{{/crossLink}} </td>
  122. * <td>rc</td><td>{{#crossLink "Graphics/drawRoundRectComplex"}}{{/crossLink}} </td></tr>
  123. * <tr><td>dc</td><td>{{#crossLink "Graphics/drawCircle"}}{{/crossLink}} </td>
  124. * <td>de</td><td>{{#crossLink "Graphics/drawEllipse"}}{{/crossLink}} </td></tr>
  125. * <tr><td>dp</td><td>{{#crossLink "Graphics/drawPolyStar"}}{{/crossLink}} </td>
  126. * <td>p</td><td>{{#crossLink "Graphics/decodePath"}}{{/crossLink}} </td></tr>
  127. * </table>
  128. *
  129. * Here is the above example, using the tiny API instead.
  130. *
  131. * myGraphics.s("red").f("blue").r(20, 20, 100, 50);
  132. *
  133. * @class Graphics
  134. * @constructor
  135. **/
  136. function Graphics() {
  137.  
  138.  
  139. // public properties
  140. /**
  141. * Holds a reference to the last command that was created or appended. For example, you could retain a reference
  142. * to a Fill command in order to dynamically update the color later by using:
  143. *
  144. * var myFill = myGraphics.beginFill("red").command;
  145. * // update color later:
  146. * myFill.style = "yellow";
  147. *
  148. * @property command
  149. * @type Object
  150. **/
  151. this.command = null;
  152.  
  153.  
  154. // private properties
  155. /**
  156. * @property _stroke
  157. * @protected
  158. * @type {Stroke}
  159. **/
  160. this._stroke = null;
  161.  
  162. /**
  163. * @property _strokeStyle
  164. * @protected
  165. * @type {StrokeStyle}
  166. **/
  167. this._strokeStyle = null;
  168. /**
  169. * @property _oldStrokeStyle
  170. * @protected
  171. * @type {StrokeStyle}
  172. **/
  173. this._oldStrokeStyle = null;
  174. /**
  175. * @property _strokeDash
  176. * @protected
  177. * @type {StrokeDash}
  178. **/
  179. this._strokeDash = null;
  180. /**
  181. * @property _oldStrokeDash
  182. * @protected
  183. * @type {StrokeDash}
  184. **/
  185. this._oldStrokeDash = null;
  186.  
  187. /**
  188. * @property _strokeIgnoreScale
  189. * @protected
  190. * @type Boolean
  191. **/
  192. this._strokeIgnoreScale = false;
  193.  
  194. /**
  195. * @property _fill
  196. * @protected
  197. * @type {Fill}
  198. **/
  199. this._fill = null;
  200.  
  201. /**
  202. * @property _instructions
  203. * @protected
  204. * @type {Array}
  205. **/
  206. this._instructions = [];
  207.  
  208. /**
  209. * Indicates the last instruction index that was committed.
  210. * @property _commitIndex
  211. * @protected
  212. * @type {Number}
  213. **/
  214. this._commitIndex = 0;
  215.  
  216. /**
  217. * Uncommitted instructions.
  218. * @property _activeInstructions
  219. * @protected
  220. * @type {Array}
  221. **/
  222. this._activeInstructions = [];
  223.  
  224. /**
  225. * This indicates that there have been changes to the activeInstruction list since the last updateInstructions call.
  226. * @property _dirty
  227. * @protected
  228. * @type {Boolean}
  229. * @default false
  230. **/
  231. this._dirty = false;
  232.  
  233. /**
  234. * Index to draw from if a store operation has happened.
  235. * @property _storeIndex
  236. * @protected
  237. * @type {Number}
  238. * @default 0
  239. **/
  240. this._storeIndex = 0;
  241.  
  242. // setup:
  243. this.clear();
  244. }
  245. var p = Graphics.prototype;
  246. var G = Graphics; // shortcut
  247.  
  248. // static public methods:
  249. /**
  250. * Returns a CSS compatible color string based on the specified RGB numeric color values in the format
  251. * "rgba(255,255,255,1.0)", or if alpha is null then in the format "rgb(255,255,255)". For example,
  252. *
  253. * createjs.Graphics.getRGB(50, 100, 150, 0.5);
  254. * // Returns "rgba(50,100,150,0.5)"
  255. *
  256. * It also supports passing a single hex color value as the first param, and an optional alpha value as the second
  257. * param. For example,
  258. *
  259. * createjs.Graphics.getRGB(0xFF00FF, 0.2);
  260. * // Returns "rgba(255,0,255,0.2)"
  261. *
  262. * @method getRGB
  263. * @static
  264. * @param {Number} r The red component for the color, between 0 and 0xFF (255).
  265. * @param {Number} g The green component for the color, between 0 and 0xFF (255).
  266. * @param {Number} b The blue component for the color, between 0 and 0xFF (255).
  267. * @param {Number} [alpha] The alpha component for the color where 0 is fully transparent and 1 is fully opaque.
  268. * @return {String} A CSS compatible color string based on the specified RGB numeric color values in the format
  269. * "rgba(255,255,255,1.0)", or if alpha is null then in the format "rgb(255,255,255)".
  270. **/
  271. Graphics.getRGB = function(r, g, b, alpha) {
  272. if (r != null && b == null) {
  273. alpha = g;
  274. b = r&0xFF;
  275. g = r>>8&0xFF;
  276. r = r>>16&0xFF;
  277. }
  278. if (alpha == null) {
  279. return "rgb("+r+","+g+","+b+")";
  280. } else {
  281. return "rgba("+r+","+g+","+b+","+alpha+")";
  282. }
  283. };
  284.  
  285. /**
  286. * Returns a CSS compatible color string based on the specified HSL numeric color values in the format "hsla(360,100,100,1.0)",
  287. * or if alpha is null then in the format "hsl(360,100,100)".
  288. *
  289. * createjs.Graphics.getHSL(150, 100, 70);
  290. * // Returns "hsl(150,100,70)"
  291. *
  292. * @method getHSL
  293. * @static
  294. * @param {Number} hue The hue component for the color, between 0 and 360.
  295. * @param {Number} saturation The saturation component for the color, between 0 and 100.
  296. * @param {Number} lightness The lightness component for the color, between 0 and 100.
  297. * @param {Number} [alpha] The alpha component for the color where 0 is fully transparent and 1 is fully opaque.
  298. * @return {String} A CSS compatible color string based on the specified HSL numeric color values in the format
  299. * "hsla(360,100,100,1.0)", or if alpha is null then in the format "hsl(360,100,100)".
  300. **/
  301. Graphics.getHSL = function(hue, saturation, lightness, alpha) {
  302. if (alpha == null) {
  303. return "hsl("+(hue%360)+","+saturation+"%,"+lightness+"%)";
  304. } else {
  305. return "hsla("+(hue%360)+","+saturation+"%,"+lightness+"%,"+alpha+")";
  306. }
  307. };
  308.  
  309.  
  310. // static properties:
  311. /**
  312. * A reusable instance of {{#crossLink "Graphics.BeginPath"}}{{/crossLink}} to avoid
  313. * unnecessary instantiation.
  314. * @property beginCmd
  315. * @type {Graphics.BeginPath}
  316. * @static
  317. **/
  318. // defined at the bottom of this file.
  319.  
  320. /**
  321. * Map of Base64 characters to values. Used by {{#crossLink "Graphics/decodePath"}}{{/crossLink}}.
  322. * @property BASE_64
  323. * @static
  324. * @final
  325. * @readonly
  326. * @type {Object}
  327. **/
  328. Graphics.BASE_64 = {"A":0,"B":1,"C":2,"D":3,"E":4,"F":5,"G":6,"H":7,"I":8,"J":9,"K":10,"L":11,"M":12,"N":13,"O":14,"P":15,"Q":16,"R":17,"S":18,"T":19,"U":20,"V":21,"W":22,"X":23,"Y":24,"Z":25,"a":26,"b":27,"c":28,"d":29,"e":30,"f":31,"g":32,"h":33,"i":34,"j":35,"k":36,"l":37,"m":38,"n":39,"o":40,"p":41,"q":42,"r":43,"s":44,"t":45,"u":46,"v":47,"w":48,"x":49,"y":50,"z":51,"0":52,"1":53,"2":54,"3":55,"4":56,"5":57,"6":58,"7":59,"8":60,"9":61,"+":62,"/":63};
  329.  
  330. /**
  331. * Maps numeric values for the caps parameter of {{#crossLink "Graphics/setStrokeStyle"}}{{/crossLink}} to
  332. * corresponding string values. This is primarily for use with the tiny API. The mappings are as follows: 0 to
  333. * "butt", 1 to "round", and 2 to "square".
  334. * For example, to set the line caps to "square":
  335. *
  336. * myGraphics.ss(16, 2);
  337. *
  338. * @property STROKE_CAPS_MAP
  339. * @static
  340. * @final
  341. * @readonly
  342. * @type {Array}
  343. **/
  344. Graphics.STROKE_CAPS_MAP = ["butt", "round", "square"];
  345.  
  346. /**
  347. * Maps numeric values for the joints parameter of {{#crossLink "Graphics/setStrokeStyle"}}{{/crossLink}} to
  348. * corresponding string values. This is primarily for use with the tiny API. The mappings are as follows: 0 to
  349. * "miter", 1 to "round", and 2 to "bevel".
  350. * For example, to set the line joints to "bevel":
  351. *
  352. * myGraphics.ss(16, 0, 2);
  353. *
  354. * @property STROKE_JOINTS_MAP
  355. * @static
  356. * @final
  357. * @readonly
  358. * @type {Array}
  359. **/
  360. Graphics.STROKE_JOINTS_MAP = ["miter", "round", "bevel"];
  361.  
  362. /**
  363. * @property _ctx
  364. * @static
  365. * @protected
  366. * @type {CanvasRenderingContext2D}
  367. **/
  368. var canvas = (createjs.createCanvas?createjs.createCanvas():document.createElement("canvas"));
  369. if (canvas.getContext) {
  370. Graphics._ctx = canvas.getContext("2d");
  371. canvas.width = canvas.height = 1;
  372. }
  373.  
  374.  
  375. // getter / setters:
  376. /**
  377. * Use the {{#crossLink "Graphics/instructions:property"}}{{/crossLink}} property instead.
  378. * @method _getInstructions
  379. * @protected
  380. * @return {Array} The instructions array, useful for chaining
  381. **/
  382. p._getInstructions = function() {
  383. this._updateInstructions();
  384. return this._instructions;
  385. };
  386.  
  387. /**
  388. * Use the {{#crossLink "Graphics/instructions:property"}}{{/crossLink}} property instead.
  389. * @method getInstructions
  390. * @deprecated
  391. */
  392. // Graphics.getInstructions is @deprecated. Remove for 1.1+
  393. p.getInstructions = createjs.deprecate(p._getInstructions, "Graphics.getInstructions");
  394.  
  395. /**
  396. * Returns the graphics instructions array. Each entry is a graphics command object (ex. Graphics.Fill, Graphics.Rect)
  397. * Modifying the returned array directly is not recommended, and is likely to result in unexpected behaviour.
  398. *
  399. * This property is mainly intended for introspection of the instructions (ex. for graphics export).
  400. * @property instructions
  401. * @type {Array}
  402. * @readonly
  403. **/
  404. try {
  405. Object.defineProperties(p, {
  406. instructions: { get: p._getInstructions }
  407. });
  408. } catch (e) {}
  409.  
  410.  
  411. // public methods:
  412. /**
  413. * Returns true if this Graphics instance has no drawing commands.
  414. * @method isEmpty
  415. * @return {Boolean} Returns true if this Graphics instance has no drawing commands.
  416. **/
  417. p.isEmpty = function() {
  418. return !(this._instructions.length || this._activeInstructions.length);
  419. };
  420.  
  421. /**
  422. * Draws the display object into the specified context ignoring its visible, alpha, shadow, and transform.
  423. * Returns true if the draw was handled (useful for overriding functionality).
  424. *
  425. * NOTE: This method is mainly for internal use, though it may be useful for advanced uses.
  426. * @method draw
  427. * @param {CanvasRenderingContext2D} ctx The canvas 2D context object to draw into.
  428. * @param {Object} data Optional data that is passed to graphics command exec methods. When called from a Shape instance, the shape passes itself as the data parameter. This can be used by custom graphic commands to insert contextual data.
  429. **/
  430. p.draw = function(ctx, data) {
  431. this._updateInstructions();
  432. var instr = this._instructions;
  433. for (var i=this._storeIndex, l=instr.length; i<l; i++) {
  434. instr[i].exec(ctx, data);
  435. }
  436. };
  437.  
  438. /**
  439. * Draws only the path described for this Graphics instance, skipping any non-path instructions, including fill and
  440. * stroke descriptions. Used for <code>DisplayObject.mask</code> to draw the clipping path, for example.
  441. *
  442. * NOTE: This method is mainly for internal use, though it may be useful for advanced uses.
  443. * @method drawAsPath
  444. * @param {CanvasRenderingContext2D} ctx The canvas 2D context object to draw into.
  445. **/
  446. p.drawAsPath = function(ctx) {
  447. this._updateInstructions();
  448. var instr, instrs = this._instructions;
  449. for (var i=this._storeIndex, l=instrs.length; i<l; i++) {
  450. // the first command is always a beginPath command.
  451. if ((instr = instrs[i]).path !== false) { instr.exec(ctx); }
  452. }
  453. };
  454.  
  455.  
  456. // public methods that map directly to context 2D calls:
  457. /**
  458. * Moves the drawing point to the specified position. A tiny API method "mt" also exists.
  459. * @method moveTo
  460. * @param {Number} x The x coordinate the drawing point should move to.
  461. * @param {Number} y The y coordinate the drawing point should move to.
  462. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls).
  463. * @chainable
  464. **/
  465. p.moveTo = function(x, y) {
  466. return this.append(new G.MoveTo(x,y), true);
  467. };
  468.  
  469. /**
  470. * Draws a line from the current drawing point to the specified position, which become the new current drawing
  471. * point. Note that you *must* call {{#crossLink "Graphics/moveTo"}}{{/crossLink}} before the first `lineTo()`.
  472. * A tiny API method "lt" also exists.
  473. *
  474. * For detailed information, read the
  475. * <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#complex-shapes-(paths)">
  476. * whatwg spec</a>.
  477. * @method lineTo
  478. * @param {Number} x The x coordinate the drawing point should draw to.
  479. * @param {Number} y The y coordinate the drawing point should draw to.
  480. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  481. * @chainable
  482. **/
  483. p.lineTo = function(x, y) {
  484. return this.append(new G.LineTo(x,y));
  485. };
  486.  
  487. /**
  488. * Draws an arc with the specified control points and radius. For detailed information, read the
  489. * <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-arcto">
  490. * whatwg spec</a>. A tiny API method "at" also exists.
  491. * @method arcTo
  492. * @param {Number} x1
  493. * @param {Number} y1
  494. * @param {Number} x2
  495. * @param {Number} y2
  496. * @param {Number} radius
  497. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  498. * @chainable
  499. **/
  500. p.arcTo = function(x1, y1, x2, y2, radius) {
  501. return this.append(new G.ArcTo(x1, y1, x2, y2, radius));
  502. };
  503.  
  504. /**
  505. * Draws an arc defined by the radius, startAngle and endAngle arguments, centered at the position (x, y). For
  506. * example, to draw a full circle with a radius of 20 centered at (100, 100):
  507. *
  508. * arc(100, 100, 20, 0, Math.PI*2);
  509. *
  510. * For detailed information, read the
  511. * <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-arc">whatwg spec</a>.
  512. * A tiny API method "a" also exists.
  513. * @method arc
  514. * @param {Number} x
  515. * @param {Number} y
  516. * @param {Number} radius
  517. * @param {Number} startAngle Measured in radians.
  518. * @param {Number} endAngle Measured in radians.
  519. * @param {Boolean} anticlockwise
  520. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  521. * @chainable
  522. **/
  523. p.arc = function(x, y, radius, startAngle, endAngle, anticlockwise) {
  524. return this.append(new G.Arc(x, y, radius, startAngle, endAngle, anticlockwise));
  525. };
  526.  
  527. /**
  528. * Draws a quadratic curve from the current drawing point to (x, y) using the control point (cpx, cpy). For detailed
  529. * information, read the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-quadraticcurveto">
  530. * whatwg spec</a>. A tiny API method "qt" also exists.
  531. * @method quadraticCurveTo
  532. * @param {Number} cpx
  533. * @param {Number} cpy
  534. * @param {Number} x
  535. * @param {Number} y
  536. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  537. * @chainable
  538. **/
  539. p.quadraticCurveTo = function(cpx, cpy, x, y) {
  540. return this.append(new G.QuadraticCurveTo(cpx, cpy, x, y));
  541. };
  542.  
  543. /**
  544. * Draws a bezier curve from the current drawing point to (x, y) using the control points (cp1x, cp1y) and (cp2x,
  545. * cp2y). For detailed information, read the
  546. * <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-beziercurveto">
  547. * whatwg spec</a>. A tiny API method "bt" also exists.
  548. * @method bezierCurveTo
  549. * @param {Number} cp1x
  550. * @param {Number} cp1y
  551. * @param {Number} cp2x
  552. * @param {Number} cp2y
  553. * @param {Number} x
  554. * @param {Number} y
  555. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  556. * @chainable
  557. **/
  558. p.bezierCurveTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
  559. return this.append(new G.BezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y));
  560. };
  561.  
  562. /**
  563. * Draws a rectangle at (x, y) with the specified width and height using the current fill and/or stroke.
  564. * For detailed information, read the
  565. * <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-rect">
  566. * whatwg spec</a>. A tiny API method "r" also exists.
  567. * @method rect
  568. * @param {Number} x
  569. * @param {Number} y
  570. * @param {Number} w Width of the rectangle
  571. * @param {Number} h Height of the rectangle
  572. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  573. * @chainable
  574. **/
  575. p.rect = function(x, y, w, h) {
  576. return this.append(new G.Rect(x, y, w, h));
  577. };
  578.  
  579. /**
  580. * Closes the current path, effectively drawing a line from the current drawing point to the first drawing point specified
  581. * since the fill or stroke was last set. A tiny API method "cp" also exists.
  582. * @method closePath
  583. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  584. * @chainable
  585. **/
  586. p.closePath = function() {
  587. return this._activeInstructions.length ? this.append(new G.ClosePath()) : this;
  588. };
  589.  
  590.  
  591. // public methods that roughly map to Adobe Flash/Animate graphics APIs:
  592. /**
  593. * Clears all drawing instructions, effectively resetting this Graphics instance. Any line and fill styles will need
  594. * to be redefined to draw shapes following a clear call. A tiny API method "c" also exists.
  595. * @method clear
  596. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  597. * @chainable
  598. **/
  599. p.clear = function() {
  600. this._instructions.length = this._activeInstructions.length = this._commitIndex = 0;
  601. this._strokeStyle = this._oldStrokeStyle = this._stroke = this._fill = this._strokeDash = this._oldStrokeDash = null;
  602. this._dirty = this._strokeIgnoreScale = false;
  603. return this;
  604. };
  605.  
  606. /**
  607. * Begins a fill with the specified color. This ends the current sub-path. A tiny API method "f" also exists.
  608. * @method beginFill
  609. * @param {String} color A CSS compatible color value (ex. "red", "#FF0000", or "rgba(255,0,0,0.5)"). Setting to
  610. * null will result in no fill.
  611. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  612. * @chainable
  613. **/
  614. p.beginFill = function(color) {
  615. return this._setFill(color ? new G.Fill(color) : null);
  616. };
  617.  
  618. /**
  619. * Begins a linear gradient fill defined by the line (x0, y0) to (x1, y1). This ends the current sub-path. For
  620. * example, the following code defines a black to white vertical gradient ranging from 20px to 120px, and draws a
  621. * square to display it:
  622. *
  623. * myGraphics.beginLinearGradientFill(["#000","#FFF"], [0, 1], 0, 20, 0, 120).drawRect(20, 20, 120, 120);
  624. *
  625. * A tiny API method "lf" also exists.
  626. * @method beginLinearGradientFill
  627. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define a gradient
  628. * drawing from red to blue.
  629. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1, 0.9] would draw
  630. * the first color to 10% then interpolating to the second color at 90%.
  631. * @param {Number} x0 The position of the first point defining the line that defines the gradient direction and size.
  632. * @param {Number} y0 The position of the first point defining the line that defines the gradient direction and size.
  633. * @param {Number} x1 The position of the second point defining the line that defines the gradient direction and size.
  634. * @param {Number} y1 The position of the second point defining the line that defines the gradient direction and size.
  635. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  636. * @chainable
  637. **/
  638. p.beginLinearGradientFill = function(colors, ratios, x0, y0, x1, y1) {
  639. return this._setFill(new G.Fill().linearGradient(colors, ratios, x0, y0, x1, y1));
  640. };
  641.  
  642. /**
  643. * Begins a radial gradient fill. This ends the current sub-path. For example, the following code defines a red to
  644. * blue radial gradient centered at (100, 100), with a radius of 50, and draws a circle to display it:
  645. *
  646. * myGraphics.beginRadialGradientFill(["#F00","#00F"], [0, 1], 100, 100, 0, 100, 100, 50).drawCircle(100, 100, 50);
  647. *
  648. * A tiny API method "rf" also exists.
  649. * @method beginRadialGradientFill
  650. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define
  651. * a gradient drawing from red to blue.
  652. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1,
  653. * 0.9] would draw the first color to 10% then interpolating to the second color at 90%.
  654. * @param {Number} x0 Center position of the inner circle that defines the gradient.
  655. * @param {Number} y0 Center position of the inner circle that defines the gradient.
  656. * @param {Number} r0 Radius of the inner circle that defines the gradient.
  657. * @param {Number} x1 Center position of the outer circle that defines the gradient.
  658. * @param {Number} y1 Center position of the outer circle that defines the gradient.
  659. * @param {Number} r1 Radius of the outer circle that defines the gradient.
  660. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  661. * @chainable
  662. **/
  663. p.beginRadialGradientFill = function(colors, ratios, x0, y0, r0, x1, y1, r1) {
  664. return this._setFill(new G.Fill().radialGradient(colors, ratios, x0, y0, r0, x1, y1, r1));
  665. };
  666.  
  667. /**
  668. * Begins a pattern fill using the specified image. This ends the current sub-path. A tiny API method "bf" also
  669. * exists.
  670. * @method beginBitmapFill
  671. * @param {HTMLImageElement | HTMLCanvasElement | HTMLVideoElement} image The Image, Canvas, or Video object to use
  672. * as the pattern. Must be loaded prior to creating a bitmap fill, or the fill will be empty.
  673. * @param {String} repetition Optional. Indicates whether to repeat the image in the fill area. One of "repeat",
  674. * "repeat-x", "repeat-y", or "no-repeat". Defaults to "repeat". Note that Firefox does not support "repeat-x" or
  675. * "repeat-y" (latest tests were in FF 20.0), and will default to "repeat".
  676. * @param {Matrix2D} matrix Optional. Specifies a transformation matrix for the bitmap fill. This transformation
  677. * will be applied relative to the parent transform.
  678. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  679. * @chainable
  680. **/
  681. p.beginBitmapFill = function(image, repetition, matrix) {
  682. return this._setFill(new G.Fill(null,matrix).bitmap(image, repetition));
  683. };
  684.  
  685. /**
  686. * Ends the current sub-path, and begins a new one with no fill. Functionally identical to <code>beginFill(null)</code>.
  687. * A tiny API method "ef" also exists.
  688. * @method endFill
  689. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  690. * @chainable
  691. **/
  692. p.endFill = function() {
  693. return this.beginFill();
  694. };
  695.  
  696. /**
  697. * Sets the stroke style. Like all drawing methods, this can be chained, so you can define
  698. * the stroke style and color in a single line of code like so:
  699. *
  700. * myGraphics.setStrokeStyle(8,"round").beginStroke("#F00");
  701. *
  702. * A tiny API method "ss" also exists.
  703. * @method setStrokeStyle
  704. * @param {Number} thickness The width of the stroke.
  705. * @param {String | Number} [caps=0] Indicates the type of caps to use at the end of lines. One of butt,
  706. * round, or square. Defaults to "butt". Also accepts the values 0 (butt), 1 (round), and 2 (square) for use with
  707. * the tiny API.
  708. * @param {String | Number} [joints=0] Specifies the type of joints that should be used where two lines meet.
  709. * One of bevel, round, or miter. Defaults to "miter". Also accepts the values 0 (miter), 1 (round), and 2 (bevel)
  710. * for use with the tiny API.
  711. * @param {Number} [miterLimit=10] If joints is set to "miter", then you can specify a miter limit ratio which
  712. * controls at what point a mitered joint will be clipped.
  713. * @param {Boolean} [ignoreScale=false] If true, the stroke will be drawn at the specified thickness regardless
  714. * of active transformations.
  715. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  716. * @chainable
  717. **/
  718. p.setStrokeStyle = function(thickness, caps, joints, miterLimit, ignoreScale) {
  719. this._updateInstructions(true);
  720. this._strokeStyle = this.command = new G.StrokeStyle(thickness, caps, joints, miterLimit, ignoreScale);
  721.  
  722. // ignoreScale lives on Stroke, not StrokeStyle, so we do a little trickery:
  723. if (this._stroke) { this._stroke.ignoreScale = ignoreScale; }
  724. this._strokeIgnoreScale = ignoreScale;
  725. return this;
  726. };
  727. /**
  728. * Sets or clears the stroke dash pattern.
  729. *
  730. * myGraphics.setStrokeDash([20, 10], 0);
  731. *
  732. * A tiny API method `sd` also exists.
  733. * @method setStrokeDash
  734. * @param {Array} [segments] An array specifying the dash pattern, alternating between line and gap.
  735. * For example, `[20,10]` would create a pattern of 20 pixel lines with 10 pixel gaps between them.
  736. * Passing null or an empty array will clear the existing stroke dash.
  737. * @param {Number} [offset=0] The offset of the dash pattern. For example, you could increment this value to create a "marching ants" effect.
  738. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  739. * @chainable
  740. **/
  741. p.setStrokeDash = function(segments, offset) {
  742. this._updateInstructions(true);
  743. this._strokeDash = this.command = new G.StrokeDash(segments, offset);
  744. return this;
  745. };
  746.  
  747. /**
  748. * Begins a stroke with the specified color. This ends the current sub-path. A tiny API method "s" also exists.
  749. * @method beginStroke
  750. * @param {String} color A CSS compatible color value (ex. "#FF0000", "red", or "rgba(255,0,0,0.5)"). Setting to
  751. * null will result in no stroke.
  752. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  753. * @chainable
  754. **/
  755. p.beginStroke = function(color) {
  756. return this._setStroke(color ? new G.Stroke(color) : null);
  757. };
  758.  
  759. /**
  760. * Begins a linear gradient stroke defined by the line (x0, y0) to (x1, y1). This ends the current sub-path. For
  761. * example, the following code defines a black to white vertical gradient ranging from 20px to 120px, and draws a
  762. * square to display it:
  763. *
  764. * myGraphics.setStrokeStyle(10).
  765. * beginLinearGradientStroke(["#000","#FFF"], [0, 1], 0, 20, 0, 120).drawRect(20, 20, 120, 120);
  766. *
  767. * A tiny API method "ls" also exists.
  768. * @method beginLinearGradientStroke
  769. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define
  770. * a gradient drawing from red to blue.
  771. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1,
  772. * 0.9] would draw the first color to 10% then interpolating to the second color at 90%.
  773. * @param {Number} x0 The position of the first point defining the line that defines the gradient direction and size.
  774. * @param {Number} y0 The position of the first point defining the line that defines the gradient direction and size.
  775. * @param {Number} x1 The position of the second point defining the line that defines the gradient direction and size.
  776. * @param {Number} y1 The position of the second point defining the line that defines the gradient direction and size.
  777. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  778. * @chainable
  779. **/
  780. p.beginLinearGradientStroke = function(colors, ratios, x0, y0, x1, y1) {
  781. return this._setStroke(new G.Stroke().linearGradient(colors, ratios, x0, y0, x1, y1));
  782. };
  783.  
  784. /**
  785. * Begins a radial gradient stroke. This ends the current sub-path. For example, the following code defines a red to
  786. * blue radial gradient centered at (100, 100), with a radius of 50, and draws a rectangle to display it:
  787. *
  788. * myGraphics.setStrokeStyle(10)
  789. * .beginRadialGradientStroke(["#F00","#00F"], [0, 1], 100, 100, 0, 100, 100, 50)
  790. * .drawRect(50, 90, 150, 110);
  791. *
  792. * A tiny API method "rs" also exists.
  793. * @method beginRadialGradientStroke
  794. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define
  795. * a gradient drawing from red to blue.
  796. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1,
  797. * 0.9] would draw the first color to 10% then interpolating to the second color at 90%, then draw the second color
  798. * to 100%.
  799. * @param {Number} x0 Center position of the inner circle that defines the gradient.
  800. * @param {Number} y0 Center position of the inner circle that defines the gradient.
  801. * @param {Number} r0 Radius of the inner circle that defines the gradient.
  802. * @param {Number} x1 Center position of the outer circle that defines the gradient.
  803. * @param {Number} y1 Center position of the outer circle that defines the gradient.
  804. * @param {Number} r1 Radius of the outer circle that defines the gradient.
  805. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  806. * @chainable
  807. **/
  808. p.beginRadialGradientStroke = function(colors, ratios, x0, y0, r0, x1, y1, r1) {
  809. return this._setStroke(new G.Stroke().radialGradient(colors, ratios, x0, y0, r0, x1, y1, r1));
  810. };
  811.  
  812. /**
  813. * Begins a pattern fill using the specified image. This ends the current sub-path. Note that unlike bitmap fills,
  814. * strokes do not currently support a matrix parameter due to limitations in the canvas API. A tiny API method "bs"
  815. * also exists.
  816. * @method beginBitmapStroke
  817. * @param {HTMLImageElement | HTMLCanvasElement | HTMLVideoElement} image The Image, Canvas, or Video object to use
  818. * as the pattern. Must be loaded prior to creating a bitmap fill, or the fill will be empty.
  819. * @param {String} [repetition=repeat] Optional. Indicates whether to repeat the image in the fill area. One of
  820. * "repeat", "repeat-x", "repeat-y", or "no-repeat". Defaults to "repeat".
  821. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  822. * @chainable
  823. **/
  824. p.beginBitmapStroke = function(image, repetition) {
  825. // NOTE: matrix is not supported for stroke because transforms on strokes also affect the drawn stroke width.
  826. return this._setStroke(new G.Stroke().bitmap(image, repetition));
  827. };
  828.  
  829. /**
  830. * Ends the current sub-path, and begins a new one with no stroke. Functionally identical to <code>beginStroke(null)</code>.
  831. * A tiny API method "es" also exists.
  832. * @method endStroke
  833. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  834. * @chainable
  835. **/
  836. p.endStroke = function() {
  837. return this.beginStroke();
  838. };
  839.  
  840. /**
  841. * Maps the familiar ActionScript <code>curveTo()</code> method to the functionally similar {{#crossLink "Graphics/quadraticCurveTo"}}{{/crossLink}}
  842. * method.
  843. * @method curveTo
  844. * @param {Number} cpx
  845. * @param {Number} cpy
  846. * @param {Number} x
  847. * @param {Number} y
  848. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  849. * @chainable
  850. **/
  851. p.curveTo = p.quadraticCurveTo;
  852.  
  853. /**
  854. *
  855. * Maps the familiar ActionScript <code>drawRect()</code> method to the functionally similar {{#crossLink "Graphics/rect"}}{{/crossLink}}
  856. * method.
  857. * @method drawRect
  858. * @param {Number} x
  859. * @param {Number} y
  860. * @param {Number} w Width of the rectangle
  861. * @param {Number} h Height of the rectangle
  862. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  863. * @chainable
  864. **/
  865. p.drawRect = p.rect;
  866.  
  867. /**
  868. * Draws a rounded rectangle with all corners with the specified radius.
  869. * @method drawRoundRect
  870. * @param {Number} x
  871. * @param {Number} y
  872. * @param {Number} w
  873. * @param {Number} h
  874. * @param {Number} radius Corner radius.
  875. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  876. * @chainable
  877. **/
  878. p.drawRoundRect = function(x, y, w, h, radius) {
  879. return this.drawRoundRectComplex(x, y, w, h, radius, radius, radius, radius);
  880. };
  881.  
  882. /**
  883. * Draws a rounded rectangle with different corner radii. Supports positive and negative corner radii. A tiny API
  884. * method "rc" also exists.
  885. * @method drawRoundRectComplex
  886. * @param {Number} x The horizontal coordinate to draw the round rect.
  887. * @param {Number} y The vertical coordinate to draw the round rect.
  888. * @param {Number} w The width of the round rect.
  889. * @param {Number} h The height of the round rect.
  890. * @param {Number} radiusTL Top left corner radius.
  891. * @param {Number} radiusTR Top right corner radius.
  892. * @param {Number} radiusBR Bottom right corner radius.
  893. * @param {Number} radiusBL Bottom left corner radius.
  894. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  895. * @chainable
  896. **/
  897. p.drawRoundRectComplex = function(x, y, w, h, radiusTL, radiusTR, radiusBR, radiusBL) {
  898. return this.append(new G.RoundRect(x, y, w, h, radiusTL, radiusTR, radiusBR, radiusBL));
  899. };
  900.  
  901. /**
  902. * Draws a circle with the specified radius at (x, y).
  903. *
  904. * var g = new createjs.Graphics();
  905. * g.setStrokeStyle(1);
  906. * g.beginStroke(createjs.Graphics.getRGB(0,0,0));
  907. * g.beginFill(createjs.Graphics.getRGB(255,0,0));
  908. * g.drawCircle(0,0,3);
  909. *
  910. * var s = new createjs.Shape(g);
  911. * s.x = 100;
  912. * s.y = 100;
  913. *
  914. * stage.addChild(s);
  915. * stage.update();
  916. *
  917. * A tiny API method "dc" also exists.
  918. * @method drawCircle
  919. * @param {Number} x x coordinate center point of circle.
  920. * @param {Number} y y coordinate center point of circle.
  921. * @param {Number} radius Radius of circle.
  922. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  923. * @chainable
  924. **/
  925. p.drawCircle = function(x, y, radius) {
  926. return this.append(new G.Circle(x, y, radius));
  927. };
  928.  
  929. /**
  930. * Draws an ellipse (oval) with a specified width (w) and height (h). Similar to {{#crossLink "Graphics/drawCircle"}}{{/crossLink}},
  931. * except the width and height can be different. A tiny API method "de" also exists.
  932. * @method drawEllipse
  933. * @param {Number} x The left coordinate point of the ellipse. Note that this is different from {{#crossLink "Graphics/drawCircle"}}{{/crossLink}}
  934. * which draws from center.
  935. * @param {Number} y The top coordinate point of the ellipse. Note that this is different from {{#crossLink "Graphics/drawCircle"}}{{/crossLink}}
  936. * which draws from the center.
  937. * @param {Number} w The height (horizontal diameter) of the ellipse. The horizontal radius will be half of this
  938. * number.
  939. * @param {Number} h The width (vertical diameter) of the ellipse. The vertical radius will be half of this number.
  940. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  941. * @chainable
  942. **/
  943. p.drawEllipse = function(x, y, w, h) {
  944. return this.append(new G.Ellipse(x, y, w, h));
  945. };
  946.  
  947. /**
  948. * Draws a star if pointSize is greater than 0, or a regular polygon if pointSize is 0 with the specified number of
  949. * points. For example, the following code will draw a familiar 5 pointed star shape centered at 100, 100 and with a
  950. * radius of 50:
  951. *
  952. * myGraphics.beginFill("#FF0").drawPolyStar(100, 100, 50, 5, 0.6, -90);
  953. * // Note: -90 makes the first point vertical
  954. *
  955. * A tiny API method "dp" also exists.
  956. *
  957. * @method drawPolyStar
  958. * @param {Number} x Position of the center of the shape.
  959. * @param {Number} y Position of the center of the shape.
  960. * @param {Number} radius The outer radius of the shape.
  961. * @param {Number} sides The number of points on the star or sides on the polygon.
  962. * @param {Number} pointSize The depth or "pointy-ness" of the star points. A pointSize of 0 will draw a regular
  963. * polygon (no points), a pointSize of 1 will draw nothing because the points are infinitely pointy.
  964. * @param {Number} angle The angle of the first point / corner. For example a value of 0 will draw the first point
  965. * directly to the right of the center.
  966. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  967. * @chainable
  968. **/
  969. p.drawPolyStar = function(x, y, radius, sides, pointSize, angle) {
  970. return this.append(new G.PolyStar(x, y, radius, sides, pointSize, angle));
  971. };
  972.  
  973. /**
  974. * Appends a graphics command object to the graphics queue. Command objects expose an "exec" method
  975. * that accepts two parameters: the Context2D to operate on, and an arbitrary data object passed into
  976. * {{#crossLink "Graphics/draw"}}{{/crossLink}}. The latter will usually be the Shape instance that called draw.
  977. *
  978. * This method is used internally by Graphics methods, such as drawCircle, but can also be used directly to insert
  979. * built-in or custom graphics commands. For example:
  980. *
  981. * // attach data to our shape, so we can access it during the draw:
  982. * myShape.color = "red";
  983. *
  984. * // append a Circle command object:
  985. * myShape.graphics.append(new createjs.Graphics.Circle(50, 50, 30));
  986. *
  987. * // append a custom command object with an exec method that sets the fill style
  988. * // based on the shape's data, and then fills the circle.
  989. * myShape.graphics.append({exec:function(ctx, shape) {
  990. * ctx.fillStyle = shape.color;
  991. * ctx.fill();
  992. * }});
  993. *
  994. * @method append
  995. * @param {Object} command A graphics command object exposing an "exec" method.
  996. * @param {boolean} clean The clean param is primarily for internal use. A value of true indicates that a command does not generate a path that should be stroked or filled.
  997. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  998. * @chainable
  999. **/
  1000. p.append = function(command, clean) {
  1001. this._activeInstructions.push(command);
  1002. this.command = command;
  1003. if (!clean) { this._dirty = true; }
  1004. return this;
  1005. };
  1006.  
  1007. /**
  1008. * Decodes a compact encoded path string into a series of draw instructions.
  1009. * This format is not intended to be human readable, and is meant for use by authoring tools.
  1010. * The format uses a base64 character set, with each character representing 6 bits, to define a series of draw
  1011. * commands.
  1012. *
  1013. * Each command is comprised of a single "header" character followed by a variable number of alternating x and y
  1014. * position values. Reading the header bits from left to right (most to least significant): bits 1 to 3 specify the
  1015. * type of operation (0-moveTo, 1-lineTo, 2-quadraticCurveTo, 3-bezierCurveTo, 4-closePath, 5-7 unused). Bit 4
  1016. * indicates whether position values use 12 bits (2 characters) or 18 bits (3 characters), with a one indicating the
  1017. * latter. Bits 5 and 6 are currently unused.
  1018. *
  1019. * Following the header is a series of 0 (closePath), 2 (moveTo, lineTo), 4 (quadraticCurveTo), or 6 (bezierCurveTo)
  1020. * parameters. These parameters are alternating x/y positions represented by 2 or 3 characters (as indicated by the
  1021. * 4th bit in the command char). These characters consist of a 1 bit sign (1 is negative, 0 is positive), followed
  1022. * by an 11 (2 char) or 17 (3 char) bit integer value. All position values are in tenths of a pixel. Except in the
  1023. * case of move operations which are absolute, this value is a delta from the previous x or y position (as
  1024. * appropriate).
  1025. *
  1026. * For example, the string "A3cAAMAu4AAA" represents a line starting at -150,0 and ending at 150,0.
  1027. * <br />A - bits 000000. First 3 bits (000) indicate a moveTo operation. 4th bit (0) indicates 2 chars per
  1028. * parameter.
  1029. * <br />n0 - 110111011100. Absolute x position of -150.0px. First bit indicates a negative value, remaining bits
  1030. * indicate 1500 tenths of a pixel.
  1031. * <br />AA - 000000000000. Absolute y position of 0.
  1032. * <br />I - 001100. First 3 bits (001) indicate a lineTo operation. 4th bit (1) indicates 3 chars per parameter.
  1033. * <br />Au4 - 000000101110111000. An x delta of 300.0px, which is added to the previous x value of -150.0px to
  1034. * provide an absolute position of +150.0px.
  1035. * <br />AAA - 000000000000000000. A y delta value of 0.
  1036. *
  1037. * A tiny API method "p" also exists.
  1038. * @method decodePath
  1039. * @param {String} str The path string to decode.
  1040. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1041. * @chainable
  1042. **/
  1043. p.decodePath = function(str) {
  1044. var instructions = [this.moveTo, this.lineTo, this.quadraticCurveTo, this.bezierCurveTo, this.closePath];
  1045. var paramCount = [2, 2, 4, 6, 0];
  1046. var i=0, l=str.length;
  1047. var params = [];
  1048. var x=0, y=0;
  1049. var base64 = Graphics.BASE_64;
  1050.  
  1051. while (i<l) {
  1052. var c = str.charAt(i);
  1053. var n = base64[c];
  1054. var fi = n>>3; // highest order bits 1-3 code for operation.
  1055. var f = instructions[fi];
  1056. // check that we have a valid instruction & that the unused bits are empty:
  1057. if (!f || (n&3)) { throw("bad path data (@"+i+"): "+c); }
  1058. var pl = paramCount[fi];
  1059. if (!fi) { x=y=0; } // move operations reset the position.
  1060. params.length = 0;
  1061. i++;
  1062. var charCount = (n>>2&1)+2; // 4th header bit indicates number size for this operation.
  1063. for (var p=0; p<pl; p++) {
  1064. var num = base64[str.charAt(i)];
  1065. var sign = (num>>5) ? -1 : 1;
  1066. num = ((num&31)<<6)|(base64[str.charAt(i+1)]);
  1067. if (charCount == 3) { num = (num<<6)|(base64[str.charAt(i+2)]); }
  1068. num = sign*num/10;
  1069. if (p%2) { x = (num += x); }
  1070. else { y = (num += y); }
  1071. params[p] = num;
  1072. i += charCount;
  1073. }
  1074. f.apply(this,params);
  1075. }
  1076. return this;
  1077. };
  1078.  
  1079. /**
  1080. * Stores all graphics commands so they won't be executed in future draws. Calling store() a second time adds to
  1081. * the existing store. This also affects `drawAsPath()`.
  1082. *
  1083. * This is useful in cases where you are creating vector graphics in an iterative manner (ex. generative art), so
  1084. * that only new graphics need to be drawn (which can provide huge performance benefits), but you wish to retain all
  1085. * of the vector instructions for later use (ex. scaling, modifying, or exporting).
  1086. *
  1087. * Note that calling store() will force the active path (if any) to be ended in a manner similar to changing
  1088. * the fill or stroke.
  1089. *
  1090. * For example, consider a application where the user draws lines with the mouse. As each line segment (or collection of
  1091. * segments) are added to a Shape, it can be rasterized using {{#crossLink "DisplayObject/updateCache"}}{{/crossLink}},
  1092. * and then stored, so that it can be redrawn at a different scale when the application is resized, or exported to SVG.
  1093. *
  1094. * // set up cache:
  1095. * myShape.cache(0,0,500,500,scale);
  1096. *
  1097. * // when the user drags, draw a new line:
  1098. * myShape.graphics.moveTo(oldX,oldY).lineTo(newX,newY);
  1099. * // then draw it into the existing cache:
  1100. * myShape.updateCache("source-over");
  1101. * // store the new line, so it isn't redrawn next time:
  1102. * myShape.store();
  1103. *
  1104. * // then, when the window resizes, we can re-render at a different scale:
  1105. * // first, unstore all our lines:
  1106. * myShape.unstore();
  1107. * // then cache using the new scale:
  1108. * myShape.cache(0,0,500,500,newScale);
  1109. * // finally, store the existing commands again:
  1110. * myShape.store();
  1111. *
  1112. * @method store
  1113. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1114. * @chainable
  1115. **/
  1116. p.store = function() {
  1117. this._updateInstructions(true);
  1118. this._storeIndex = this._instructions.length;
  1119. return this;
  1120. };
  1121.  
  1122. /**
  1123. * Unstores any graphics commands that were previously stored using {{#crossLink "Graphics/store"}}{{/crossLink}}
  1124. * so that they will be executed in subsequent draw calls.
  1125. *
  1126. * @method unstore
  1127. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1128. * @chainable
  1129. **/
  1130. p.unstore = function() {
  1131. this._storeIndex = 0;
  1132. return this;
  1133. };
  1134.  
  1135. /**
  1136. * Returns a clone of this Graphics instance. Note that the individual command objects are not cloned.
  1137. * @method clone
  1138. * @return {Graphics} A clone of the current Graphics instance.
  1139. **/
  1140. p.clone = function() {
  1141. var o = new Graphics();
  1142. o.command = this.command;
  1143. o._stroke = this._stroke;
  1144. o._strokeStyle = this._strokeStyle;
  1145. o._strokeDash = this._strokeDash;
  1146. o._strokeIgnoreScale = this._strokeIgnoreScale;
  1147. o._fill = this._fill;
  1148. o._instructions = this._instructions.slice();
  1149. o._commitIndex = this._commitIndex;
  1150. o._activeInstructions = this._activeInstructions.slice();
  1151. o._dirty = this._dirty;
  1152. o._storeIndex = this._storeIndex;
  1153. return o;
  1154. };
  1155.  
  1156. /**
  1157. * Returns a string representation of this object.
  1158. * @method toString
  1159. * @return {String} a string representation of the instance.
  1160. **/
  1161. p.toString = function() {
  1162. return "[Graphics]";
  1163. };
  1164.  
  1165.  
  1166. // tiny API:
  1167. /**
  1168. * Shortcut to moveTo.
  1169. * @method mt
  1170. * @param {Number} x The x coordinate the drawing point should move to.
  1171. * @param {Number} y The y coordinate the drawing point should move to.
  1172. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls).
  1173. * @chainable
  1174. * @protected
  1175. **/
  1176. p.mt = p.moveTo;
  1177.  
  1178. /**
  1179. * Shortcut to lineTo.
  1180. * @method lt
  1181. * @param {Number} x The x coordinate the drawing point should draw to.
  1182. * @param {Number} y The y coordinate the drawing point should draw to.
  1183. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1184. * @chainable
  1185. * @protected
  1186. **/
  1187. p.lt = p.lineTo;
  1188.  
  1189. /**
  1190. * Shortcut to arcTo.
  1191. * @method at
  1192. * @param {Number} x1
  1193. * @param {Number} y1
  1194. * @param {Number} x2
  1195. * @param {Number} y2
  1196. * @param {Number} radius
  1197. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1198. * @chainable
  1199. * @protected
  1200. **/
  1201. p.at = p.arcTo;
  1202.  
  1203. /**
  1204. * Shortcut to bezierCurveTo.
  1205. * @method bt
  1206. * @param {Number} cp1x
  1207. * @param {Number} cp1y
  1208. * @param {Number} cp2x
  1209. * @param {Number} cp2y
  1210. * @param {Number} x
  1211. * @param {Number} y
  1212. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1213. * @chainable
  1214. * @protected
  1215. **/
  1216. p.bt = p.bezierCurveTo;
  1217.  
  1218. /**
  1219. * Shortcut to quadraticCurveTo / curveTo.
  1220. * @method qt
  1221. * @param {Number} cpx
  1222. * @param {Number} cpy
  1223. * @param {Number} x
  1224. * @param {Number} y
  1225. * @protected
  1226. * @chainable
  1227. **/
  1228. p.qt = p.quadraticCurveTo;
  1229.  
  1230. /**
  1231. * Shortcut to arc.
  1232. * @method a
  1233. * @param {Number} x
  1234. * @param {Number} y
  1235. * @param {Number} radius
  1236. * @param {Number} startAngle Measured in radians.
  1237. * @param {Number} endAngle Measured in radians.
  1238. * @param {Boolean} anticlockwise
  1239. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1240. * @protected
  1241. * @chainable
  1242. **/
  1243. p.a = p.arc;
  1244.  
  1245. /**
  1246. * Shortcut to rect.
  1247. * @method r
  1248. * @param {Number} x
  1249. * @param {Number} y
  1250. * @param {Number} w Width of the rectangle
  1251. * @param {Number} h Height of the rectangle
  1252. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1253. * @chainable
  1254. * @protected
  1255. **/
  1256. p.r = p.rect;
  1257.  
  1258. /**
  1259. * Shortcut to closePath.
  1260. * @method cp
  1261. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1262. * @chainable
  1263. * @protected
  1264. **/
  1265. p.cp = p.closePath;
  1266.  
  1267. /**
  1268. * Shortcut to clear.
  1269. * @method c
  1270. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1271. * @chainable
  1272. * @protected
  1273. **/
  1274. p.c = p.clear;
  1275.  
  1276. /**
  1277. * Shortcut to beginFill.
  1278. * @method f
  1279. * @param {String} color A CSS compatible color value (ex. "red", "#FF0000", or "rgba(255,0,0,0.5)"). Setting to
  1280. * null will result in no fill.
  1281. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1282. * @chainable
  1283. * @protected
  1284. **/
  1285. p.f = p.beginFill;
  1286.  
  1287. /**
  1288. * Shortcut to beginLinearGradientFill.
  1289. * @method lf
  1290. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define a gradient
  1291. * drawing from red to blue.
  1292. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1, 0.9] would draw
  1293. * the first color to 10% then interpolating to the second color at 90%.
  1294. * @param {Number} x0 The position of the first point defining the line that defines the gradient direction and size.
  1295. * @param {Number} y0 The position of the first point defining the line that defines the gradient direction and size.
  1296. * @param {Number} x1 The position of the second point defining the line that defines the gradient direction and size.
  1297. * @param {Number} y1 The position of the second point defining the line that defines the gradient direction and size.
  1298. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1299. * @chainable
  1300. * @protected
  1301. **/
  1302. p.lf = p.beginLinearGradientFill;
  1303.  
  1304. /**
  1305. * Shortcut to beginRadialGradientFill.
  1306. * @method rf
  1307. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define
  1308. * a gradient drawing from red to blue.
  1309. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1,
  1310. * 0.9] would draw the first color to 10% then interpolating to the second color at 90%.
  1311. * @param {Number} x0 Center position of the inner circle that defines the gradient.
  1312. * @param {Number} y0 Center position of the inner circle that defines the gradient.
  1313. * @param {Number} r0 Radius of the inner circle that defines the gradient.
  1314. * @param {Number} x1 Center position of the outer circle that defines the gradient.
  1315. * @param {Number} y1 Center position of the outer circle that defines the gradient.
  1316. * @param {Number} r1 Radius of the outer circle that defines the gradient.
  1317. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1318. * @chainable
  1319. * @protected
  1320. **/
  1321. p.rf = p.beginRadialGradientFill;
  1322.  
  1323. /**
  1324. * Shortcut to beginBitmapFill.
  1325. * @method bf
  1326. * @param {HTMLImageElement | HTMLCanvasElement | HTMLVideoElement} image The Image, Canvas, or Video object to use
  1327. * as the pattern.
  1328. * @param {String} repetition Optional. Indicates whether to repeat the image in the fill area. One of "repeat",
  1329. * "repeat-x", "repeat-y", or "no-repeat". Defaults to "repeat". Note that Firefox does not support "repeat-x" or
  1330. * "repeat-y" (latest tests were in FF 20.0), and will default to "repeat".
  1331. * @param {Matrix2D} matrix Optional. Specifies a transformation matrix for the bitmap fill. This transformation
  1332. * will be applied relative to the parent transform.
  1333. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1334. * @chainable
  1335. * @protected
  1336. **/
  1337. p.bf = p.beginBitmapFill;
  1338.  
  1339. /**
  1340. * Shortcut to endFill.
  1341. * @method ef
  1342. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1343. * @chainable
  1344. * @protected
  1345. **/
  1346. p.ef = p.endFill;
  1347.  
  1348. /**
  1349. * Shortcut to setStrokeStyle.
  1350. * @method ss
  1351. * @param {Number} thickness The width of the stroke.
  1352. * @param {String | Number} [caps=0] Indicates the type of caps to use at the end of lines. One of butt,
  1353. * round, or square. Defaults to "butt". Also accepts the values 0 (butt), 1 (round), and 2 (square) for use with
  1354. * the tiny API.
  1355. * @param {String | Number} [joints=0] Specifies the type of joints that should be used where two lines meet.
  1356. * One of bevel, round, or miter. Defaults to "miter". Also accepts the values 0 (miter), 1 (round), and 2 (bevel)
  1357. * for use with the tiny API.
  1358. * @param {Number} [miterLimit=10] If joints is set to "miter", then you can specify a miter limit ratio which
  1359. * controls at what point a mitered joint will be clipped.
  1360. * @param {Boolean} [ignoreScale=false] If true, the stroke will be drawn at the specified thickness regardless
  1361. * of active transformations.
  1362. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1363. * @chainable
  1364. * @protected
  1365. **/
  1366. p.ss = p.setStrokeStyle;
  1367. /**
  1368. * Shortcut to setStrokeDash.
  1369. * @method sd
  1370. * @param {Array} [segments] An array specifying the dash pattern, alternating between line and gap.
  1371. * For example, [20,10] would create a pattern of 20 pixel lines with 10 pixel gaps between them.
  1372. * Passing null or an empty array will clear any existing dash.
  1373. * @param {Number} [offset=0] The offset of the dash pattern. For example, you could increment this value to create a "marching ants" effect.
  1374. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1375. * @chainable
  1376. * @protected
  1377. **/
  1378. p.sd = p.setStrokeDash;
  1379.  
  1380. /**
  1381. * Shortcut to beginStroke.
  1382. * @method s
  1383. * @param {String} color A CSS compatible color value (ex. "#FF0000", "red", or "rgba(255,0,0,0.5)"). Setting to
  1384. * null will result in no stroke.
  1385. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1386. * @chainable
  1387. * @protected
  1388. **/
  1389. p.s = p.beginStroke;
  1390.  
  1391. /**
  1392. * Shortcut to beginLinearGradientStroke.
  1393. * @method ls
  1394. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define
  1395. * a gradient drawing from red to blue.
  1396. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1,
  1397. * 0.9] would draw the first color to 10% then interpolating to the second color at 90%.
  1398. * @param {Number} x0 The position of the first point defining the line that defines the gradient direction and size.
  1399. * @param {Number} y0 The position of the first point defining the line that defines the gradient direction and size.
  1400. * @param {Number} x1 The position of the second point defining the line that defines the gradient direction and size.
  1401. * @param {Number} y1 The position of the second point defining the line that defines the gradient direction and size.
  1402. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1403. * @chainable
  1404. * @protected
  1405. **/
  1406. p.ls = p.beginLinearGradientStroke;
  1407.  
  1408. /**
  1409. * Shortcut to beginRadialGradientStroke.
  1410. * @method rs
  1411. * @param {Array} colors An array of CSS compatible color values. For example, ["#F00","#00F"] would define
  1412. * a gradient drawing from red to blue.
  1413. * @param {Array} ratios An array of gradient positions which correspond to the colors. For example, [0.1,
  1414. * 0.9] would draw the first color to 10% then interpolating to the second color at 90%, then draw the second color
  1415. * to 100%.
  1416. * @param {Number} x0 Center position of the inner circle that defines the gradient.
  1417. * @param {Number} y0 Center position of the inner circle that defines the gradient.
  1418. * @param {Number} r0 Radius of the inner circle that defines the gradient.
  1419. * @param {Number} x1 Center position of the outer circle that defines the gradient.
  1420. * @param {Number} y1 Center position of the outer circle that defines the gradient.
  1421. * @param {Number} r1 Radius of the outer circle that defines the gradient.
  1422. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1423. * @chainable
  1424. * @protected
  1425. **/
  1426. p.rs = p.beginRadialGradientStroke;
  1427.  
  1428. /**
  1429. * Shortcut to beginBitmapStroke.
  1430. * @method bs
  1431. * @param {HTMLImageElement | HTMLCanvasElement | HTMLVideoElement} image The Image, Canvas, or Video object to use
  1432. * as the pattern.
  1433. * @param {String} [repetition=repeat] Optional. Indicates whether to repeat the image in the fill area. One of
  1434. * "repeat", "repeat-x", "repeat-y", or "no-repeat". Defaults to "repeat".
  1435. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1436. * @chainable
  1437. * @protected
  1438. **/
  1439. p.bs = p.beginBitmapStroke;
  1440.  
  1441. /**
  1442. * Shortcut to endStroke.
  1443. * @method es
  1444. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1445. * @chainable
  1446. * @protected
  1447. **/
  1448. p.es = p.endStroke;
  1449.  
  1450. /**
  1451. * Shortcut to drawRect.
  1452. * @method dr
  1453. * @param {Number} x
  1454. * @param {Number} y
  1455. * @param {Number} w Width of the rectangle
  1456. * @param {Number} h Height of the rectangle
  1457. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1458. * @chainable
  1459. * @protected
  1460. **/
  1461. p.dr = p.drawRect;
  1462.  
  1463. /**
  1464. * Shortcut to drawRoundRect.
  1465. * @method rr
  1466. * @param {Number} x
  1467. * @param {Number} y
  1468. * @param {Number} w
  1469. * @param {Number} h
  1470. * @param {Number} radius Corner radius.
  1471. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1472. * @chainable
  1473. * @protected
  1474. **/
  1475. p.rr = p.drawRoundRect;
  1476.  
  1477. /**
  1478. * Shortcut to drawRoundRectComplex.
  1479. * @method rc
  1480. * @param {Number} x The horizontal coordinate to draw the round rect.
  1481. * @param {Number} y The vertical coordinate to draw the round rect.
  1482. * @param {Number} w The width of the round rect.
  1483. * @param {Number} h The height of the round rect.
  1484. * @param {Number} radiusTL Top left corner radius.
  1485. * @param {Number} radiusTR Top right corner radius.
  1486. * @param {Number} radiusBR Bottom right corner radius.
  1487. * @param {Number} radiusBL Bottom left corner radius.
  1488. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1489. * @chainable
  1490. * @protected
  1491. **/
  1492. p.rc = p.drawRoundRectComplex;
  1493.  
  1494. /**
  1495. * Shortcut to drawCircle.
  1496. * @method dc
  1497. * @param {Number} x x coordinate center point of circle.
  1498. * @param {Number} y y coordinate center point of circle.
  1499. * @param {Number} radius Radius of circle.
  1500. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1501. * @chainable
  1502. * @protected
  1503. **/
  1504. p.dc = p.drawCircle;
  1505.  
  1506. /**
  1507. * Shortcut to drawEllipse.
  1508. * @method de
  1509. * @param {Number} x The left coordinate point of the ellipse. Note that this is different from {{#crossLink "Graphics/drawCircle"}}{{/crossLink}}
  1510. * which draws from center.
  1511. * @param {Number} y The top coordinate point of the ellipse. Note that this is different from {{#crossLink "Graphics/drawCircle"}}{{/crossLink}}
  1512. * which draws from the center.
  1513. * @param {Number} w The height (horizontal diameter) of the ellipse. The horizontal radius will be half of this
  1514. * number.
  1515. * @param {Number} h The width (vertical diameter) of the ellipse. The vertical radius will be half of this number.
  1516. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1517. * @chainable
  1518. * @protected
  1519. **/
  1520. p.de = p.drawEllipse;
  1521.  
  1522. /**
  1523. * Shortcut to drawPolyStar.
  1524. * @method dp
  1525. * @param {Number} x Position of the center of the shape.
  1526. * @param {Number} y Position of the center of the shape.
  1527. * @param {Number} radius The outer radius of the shape.
  1528. * @param {Number} sides The number of points on the star or sides on the polygon.
  1529. * @param {Number} pointSize The depth or "pointy-ness" of the star points. A pointSize of 0 will draw a regular
  1530. * polygon (no points), a pointSize of 1 will draw nothing because the points are infinitely pointy.
  1531. * @param {Number} angle The angle of the first point / corner. For example a value of 0 will draw the first point
  1532. * directly to the right of the center.
  1533. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1534. * @chainable
  1535. * @protected
  1536. **/
  1537. p.dp = p.drawPolyStar;
  1538.  
  1539. /**
  1540. * Shortcut to decodePath.
  1541. * @method p
  1542. * @param {String} str The path string to decode.
  1543. * @return {Graphics} The Graphics instance the method is called on (useful for chaining calls.)
  1544. * @chainable
  1545. * @protected
  1546. **/
  1547. p.p = p.decodePath;
  1548.  
  1549.  
  1550. // private methods:
  1551. /**
  1552. * @method _updateInstructions
  1553. * @param commit
  1554. * @protected
  1555. **/
  1556. p._updateInstructions = function(commit) {
  1557. var instr = this._instructions, active = this._activeInstructions, commitIndex = this._commitIndex;
  1558.  
  1559. if (this._dirty && active.length) {
  1560. instr.length = commitIndex; // remove old, uncommitted commands
  1561. instr.push(Graphics.beginCmd);
  1562.  
  1563. var l = active.length, ll = instr.length;
  1564. instr.length = ll+l;
  1565. for (var i=0; i<l; i++) { instr[i+ll] = active[i]; }
  1566.  
  1567. if (this._fill) { instr.push(this._fill); }
  1568. if (this._stroke) {
  1569. // doesn't need to be re-applied if it hasn't changed.
  1570. if (this._strokeDash !== this._oldStrokeDash) {
  1571. instr.push(this._strokeDash);
  1572. }
  1573. if (this._strokeStyle !== this._oldStrokeStyle) {
  1574. instr.push(this._strokeStyle);
  1575. }
  1576. if (commit) {
  1577. this._oldStrokeStyle = this._strokeStyle;
  1578. this._oldStrokeDash = this._strokeDash;
  1579. }
  1580. instr.push(this._stroke);
  1581. }
  1582.  
  1583. this._dirty = false;
  1584. }
  1585.  
  1586. if (commit) {
  1587. active.length = 0;
  1588. this._commitIndex = instr.length;
  1589. }
  1590. };
  1591.  
  1592. /**
  1593. * @method _setFill
  1594. * @param fill
  1595. * @protected
  1596. **/
  1597. p._setFill = function(fill) {
  1598. this._updateInstructions(true);
  1599. this.command = this._fill = fill;
  1600. return this;
  1601. };
  1602.  
  1603. /**
  1604. * @method _setStroke
  1605. * @param stroke
  1606. * @protected
  1607. **/
  1608. p._setStroke = function(stroke) {
  1609. this._updateInstructions(true);
  1610. if (this.command = this._stroke = stroke) {
  1611. stroke.ignoreScale = this._strokeIgnoreScale;
  1612. }
  1613. return this;
  1614. };
  1615.  
  1616. // Command Objects:
  1617. /**
  1618. * @namespace Graphics
  1619. */
  1620. /**
  1621. * Graphics command object. See {{#crossLink "Graphics/lineTo"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information. See {{#crossLink "Graphics"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1622. * @class LineTo
  1623. * @constructor
  1624. * @param {Number} x
  1625. * @param {Number} y
  1626. **/
  1627. /**
  1628. * @property x
  1629. * @type Number
  1630. */
  1631. /**
  1632. * @property y
  1633. * @type Number
  1634. */
  1635. /**
  1636. * Execute the Graphics command in the provided Canvas context.
  1637. * @method exec
  1638. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1639. */
  1640. (G.LineTo = function(x, y) {
  1641. this.x = x; this.y = y;
  1642. }).prototype.exec = function(ctx) { ctx.lineTo(this.x,this.y); };
  1643.  
  1644. /**
  1645. * Graphics command object. See {{#crossLink "Graphics/moveTo"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1646. * @class MoveTo
  1647. * @constructor
  1648. * @param {Number} x
  1649. * @param {Number} y
  1650. **/
  1651. /**
  1652. * @property x
  1653. * @type Number
  1654. */
  1655. /**
  1656. * @property y
  1657. * @type Number
  1658. */
  1659. /**
  1660. * @method exec
  1661. * @param {CanvasRenderingContext2D} ctx
  1662. */
  1663. (G.MoveTo = function(x, y) {
  1664. this.x = x; this.y = y;
  1665. }).prototype.exec = function(ctx) { ctx.moveTo(this.x, this.y); };
  1666.  
  1667.  
  1668. /**
  1669. * Graphics command object. See {{#crossLink "Graphics/arcTo"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1670. * @class ArcTo
  1671. * @constructor
  1672. * @param {Number} x1
  1673. * @param {Number} y1
  1674. * @param {Number} x2
  1675. * @param {Number} y2
  1676. * @param {Number} radius
  1677. **/
  1678. /**
  1679. * @property x1
  1680. * @type Number
  1681. */
  1682. /**
  1683. * @property y1
  1684. * @type Number
  1685. */
  1686. /**
  1687. * @property x2
  1688. * @type Number
  1689. */
  1690. /**
  1691. * @property y2
  1692. * @type Number
  1693. */
  1694. /**
  1695. * @property radius
  1696. * @type Number
  1697. */
  1698. /**
  1699. * Execute the Graphics command in the provided Canvas context.
  1700. * @method exec
  1701. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1702. */
  1703. (G.ArcTo = function(x1, y1, x2, y2, radius) {
  1704. this.x1 = x1; this.y1 = y1;
  1705. this.x2 = x2; this.y2 = y2;
  1706. this.radius = radius;
  1707. }).prototype.exec = function(ctx) { ctx.arcTo(this.x1, this.y1, this.x2, this.y2, this.radius); };
  1708.  
  1709. /**
  1710. * Graphics command object. See {{#crossLink "Graphics/arc"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1711. * @class Arc
  1712. * @constructor
  1713. * @param {Number} x
  1714. * @param {Number} y
  1715. * @param {Number} radius
  1716. * @param {Number} startAngle
  1717. * @param {Number} endAngle
  1718. * @param {Number} anticlockwise
  1719. **/
  1720. /**
  1721. * @property x
  1722. * @type Number
  1723. */
  1724. /**
  1725. * @property y
  1726. * @type Number
  1727. */
  1728. /**
  1729. * @property radius
  1730. * @type Number
  1731. */
  1732. /**
  1733. * @property startAngle
  1734. * @type Number
  1735. */
  1736. /**
  1737. * @property endAngle
  1738. * @type Number
  1739. */
  1740. /**
  1741. * @property anticlockwise
  1742. * @type Number
  1743. */
  1744. /**
  1745. * Execute the Graphics command in the provided Canvas context.
  1746. * @method exec
  1747. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1748. */
  1749. (G.Arc = function(x, y, radius, startAngle, endAngle, anticlockwise) {
  1750. this.x = x; this.y = y;
  1751. this.radius = radius;
  1752. this.startAngle = startAngle; this.endAngle = endAngle;
  1753. this.anticlockwise = !!anticlockwise;
  1754. }).prototype.exec = function(ctx) { ctx.arc(this.x, this.y, this.radius, this.startAngle, this.endAngle, this.anticlockwise); };
  1755.  
  1756. /**
  1757. * Graphics command object. See {{#crossLink "Graphics/quadraticCurveTo"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1758. * @class QuadraticCurveTo
  1759. * @constructor
  1760. * @param {Number} cpx
  1761. * @param {Number} cpy
  1762. * @param {Number} x
  1763. * @param {Number} y
  1764. **/
  1765. /**
  1766. * @property cpx
  1767. * @type Number
  1768. */
  1769. /**
  1770. * @property cpy
  1771. * @type Number
  1772. */
  1773. /**
  1774. * @property x
  1775. * @type Number
  1776. */
  1777. /**
  1778. * @property y
  1779. * @type Number
  1780. */
  1781. /**
  1782. * Execute the Graphics command in the provided Canvas context.
  1783. * @method exec
  1784. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1785. */
  1786. (G.QuadraticCurveTo = function(cpx, cpy, x, y) {
  1787. this.cpx = cpx; this.cpy = cpy;
  1788. this.x = x; this.y = y;
  1789. }).prototype.exec = function(ctx) { ctx.quadraticCurveTo(this.cpx, this.cpy, this.x, this.y); };
  1790.  
  1791. /**
  1792. * Graphics command object. See {{#crossLink "Graphics/bezierCurveTo"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1793. * @class BezierCurveTo
  1794. * @constructor
  1795. * @param {Number} cp1x
  1796. * @param {Number} cp1y
  1797. * @param {Number} cp2x
  1798. * @param {Number} cp2y
  1799. * @param {Number} x
  1800. * @param {Number} y
  1801. **/
  1802. /**
  1803. * @property cp1x
  1804. * @type Number
  1805. */
  1806. /**
  1807. * @property cp1y
  1808. * @type Number
  1809. */
  1810. /**
  1811. * @property cp2x
  1812. * @type Number
  1813. */
  1814. /**
  1815. * @property cp2y
  1816. * @type Number
  1817. */
  1818. /**
  1819. * @property x
  1820. * @type Number
  1821. */
  1822. /**
  1823. * @property y
  1824. * @type Number
  1825. */
  1826. /**
  1827. * Execute the Graphics command in the provided Canvas context.
  1828. * @method exec
  1829. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1830. */
  1831. (G.BezierCurveTo = function(cp1x, cp1y, cp2x, cp2y, x, y) {
  1832. this.cp1x = cp1x; this.cp1y = cp1y;
  1833. this.cp2x = cp2x; this.cp2y = cp2y;
  1834. this.x = x; this.y = y;
  1835. }).prototype.exec = function(ctx) { ctx.bezierCurveTo(this.cp1x, this.cp1y, this.cp2x, this.cp2y, this.x, this.y); };
  1836.  
  1837. /**
  1838. * Graphics command object. See {{#crossLink "Graphics/rect"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1839. * @class Rect
  1840. * @constructor
  1841. * @param {Number} x
  1842. * @param {Number} y
  1843. * @param {Number} w
  1844. * @param {Number} h
  1845. **/
  1846. /**
  1847. * @property x
  1848. * @type Number
  1849. */
  1850. /**
  1851. * @property y
  1852. * @type Number
  1853. */
  1854. /**
  1855. * @property w
  1856. * @type Number
  1857. */
  1858. /**
  1859. * @property h
  1860. * @type Number
  1861. */
  1862. /**
  1863. * Execute the Graphics command in the provided Canvas context.
  1864. * @method exec
  1865. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1866. */
  1867. (G.Rect = function(x, y, w, h) {
  1868. this.x = x; this.y = y;
  1869. this.w = w; this.h = h;
  1870. }).prototype.exec = function(ctx) { ctx.rect(this.x, this.y, this.w, this.h); };
  1871.  
  1872. /**
  1873. * Graphics command object. See {{#crossLink "Graphics/closePath"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1874. * @class ClosePath
  1875. * @constructor
  1876. **/
  1877. /**
  1878. * Execute the Graphics command in the provided Canvas context.
  1879. * @method exec
  1880. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1881. */
  1882. (G.ClosePath = function() {
  1883. }).prototype.exec = function(ctx) { ctx.closePath(); };
  1884.  
  1885. /**
  1886. * Graphics command object to begin a new path. See {{#crossLink "Graphics"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1887. * @class BeginPath
  1888. * @constructor
  1889. **/
  1890. /**
  1891. * Execute the Graphics command in the provided Canvas context.
  1892. * @method exec
  1893. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1894. */
  1895. (G.BeginPath = function() {
  1896. }).prototype.exec = function(ctx) { ctx.beginPath(); };
  1897.  
  1898. /**
  1899. * Graphics command object. See {{#crossLink "Graphics/beginFill"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1900. * @class Fill
  1901. * @constructor
  1902. * @param {Object} style A valid Context2D fillStyle.
  1903. * @param {Matrix2D} matrix
  1904. **/
  1905. /**
  1906. * A valid Context2D fillStyle.
  1907. * @property style
  1908. * @type Object
  1909. */
  1910. /**
  1911. * @property matrix
  1912. * @type Matrix2D
  1913. */
  1914. /**
  1915. * Execute the Graphics command in the provided Canvas context.
  1916. * @method exec
  1917. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  1918. */
  1919. p = (G.Fill = function(style, matrix) {
  1920. this.style = style;
  1921. this.matrix = matrix;
  1922. }).prototype;
  1923. p.exec = function(ctx) {
  1924. if (!this.style) { return; }
  1925. ctx.fillStyle = this.style;
  1926. var mtx = this.matrix;
  1927. if (mtx) { ctx.save(); ctx.transform(mtx.a, mtx.b, mtx.c, mtx.d, mtx.tx, mtx.ty); }
  1928. ctx.fill();
  1929. if (mtx) { ctx.restore(); }
  1930. };
  1931. /**
  1932. * Creates a linear gradient style and assigns it to {{#crossLink "Fill/style:property"}}{{/crossLink}}.
  1933. * See {{#crossLink "Graphics/beginLinearGradientFill"}}{{/crossLink}} for more information.
  1934. * @method linearGradient
  1935. * @param {Array} colors
  1936. *
  1937. * @param {Array} ratios
  1938. * @param {Number} x0
  1939. * @param {Number} y0
  1940. * @param {Number} x1
  1941. * @param {Number} y1
  1942. * @return {Fill} Returns this Fill object for chaining or assignment.
  1943. */
  1944. p.linearGradient = function(colors, ratios, x0, y0, x1, y1) {
  1945. var o = this.style = Graphics._ctx.createLinearGradient(x0, y0, x1, y1);
  1946. for (var i=0, l=colors.length; i<l; i++) { o.addColorStop(ratios[i], colors[i]); }
  1947. o.props = {colors:colors, ratios:ratios, x0:x0, y0:y0, x1:x1, y1:y1, type:"linear"};
  1948. return this;
  1949. };
  1950. /**
  1951. * Creates a radial gradient style and assigns it to {{#crossLink "Fill/style:property"}}{{/crossLink}}.
  1952. * See {{#crossLink "Graphics/beginRadialGradientFill"}}{{/crossLink}} for more information.
  1953. * @method radialGradient
  1954. * @param {Array} colors
  1955. * @param {Array} ratios
  1956. * @param {Number} x0
  1957. * @param {Number} y0
  1958. * @param {Number} r0
  1959. * @param {Number} x1
  1960. * @param {Number} y1
  1961. * @param {Number} r1
  1962. * @return {Fill} Returns this Fill object for chaining or assignment.
  1963. */
  1964. p.radialGradient = function(colors, ratios, x0, y0, r0, x1, y1, r1) {
  1965. var o = this.style = Graphics._ctx.createRadialGradient(x0, y0, r0, x1, y1, r1);
  1966. for (var i=0, l=colors.length; i<l; i++) { o.addColorStop(ratios[i], colors[i]); }
  1967. o.props = {colors:colors, ratios:ratios, x0:x0, y0:y0, r0:r0, x1:x1, y1:y1, r1:r1, type:"radial"};
  1968. return this;
  1969. };
  1970. /**
  1971. * Creates a bitmap fill style and assigns it to the {{#crossLink "Fill/style:property"}}{{/crossLink}}.
  1972. * See {{#crossLink "Graphics/beginBitmapFill"}}{{/crossLink}} for more information.
  1973. * @method bitmap
  1974. * @param {HTMLImageElement | HTMLCanvasElement | HTMLVideoElement} image Must be loaded prior to creating a bitmap fill, or the fill will be empty.
  1975. * @param {String} [repetition] One of: repeat, repeat-x, repeat-y, or no-repeat.
  1976. * @return {Fill} Returns this Fill object for chaining or assignment.
  1977. */
  1978. p.bitmap = function(image, repetition) {
  1979. if (image.naturalWidth || image.getContext || image.readyState >= 2) {
  1980. var o = this.style = Graphics._ctx.createPattern(image, repetition || "");
  1981. o.props = {image: image, repetition: repetition, type: "bitmap"};
  1982. }
  1983. return this;
  1984. };
  1985. p.path = false;
  1986.  
  1987. /**
  1988. * Graphics command object. See {{#crossLink "Graphics/beginStroke"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  1989. * @class Stroke
  1990. * @constructor
  1991. * @param {Object} style A valid Context2D fillStyle.
  1992. * @param {Boolean} ignoreScale
  1993. **/
  1994. /**
  1995. * A valid Context2D strokeStyle.
  1996. * @property style
  1997. * @type Object
  1998. */
  1999. /**
  2000. * @property ignoreScale
  2001. * @type Boolean
  2002. */
  2003. /**
  2004. * Execute the Graphics command in the provided Canvas context.
  2005. * @method exec
  2006. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  2007. */
  2008. p = (G.Stroke = function(style, ignoreScale) {
  2009. this.style = style;
  2010. this.ignoreScale = ignoreScale;
  2011. }).prototype;
  2012. p.exec = function(ctx) {
  2013. if (!this.style) { return; }
  2014. ctx.strokeStyle = this.style;
  2015. if (this.ignoreScale) { ctx.save(); ctx.setTransform(1,0,0,1,0,0); }
  2016. ctx.stroke();
  2017. if (this.ignoreScale) { ctx.restore(); }
  2018. };
  2019. /**
  2020. * Creates a linear gradient style and assigns it to {{#crossLink "Stroke/style:property"}}{{/crossLink}}.
  2021. * See {{#crossLink "Graphics/beginLinearGradientStroke"}}{{/crossLink}} for more information.
  2022. * @method linearGradient
  2023. * @param {Array} colors
  2024. * @param {Array} ratios
  2025. * @param {Number} x0
  2026. * @param {Number} y0
  2027. * @param {Number} x1
  2028. * @param {Number} y1
  2029. * @return {Fill} Returns this Stroke object for chaining or assignment.
  2030. */
  2031. p.linearGradient = G.Fill.prototype.linearGradient;
  2032. /**
  2033. * Creates a radial gradient style and assigns it to {{#crossLink "Stroke/style:property"}}{{/crossLink}}.
  2034. * See {{#crossLink "Graphics/beginRadialGradientStroke"}}{{/crossLink}} for more information.
  2035. * @method radialGradient
  2036. * @param {Array} colors
  2037. * @param {Array} ratios
  2038. * @param {Number} x0
  2039. * @param {Number} y0
  2040. * @param {Number} r0
  2041. * @param {Number} x1
  2042. * @param {Number} y1
  2043. * @param {Number} r1
  2044. * @return {Fill} Returns this Stroke object for chaining or assignment.
  2045. */
  2046. p.radialGradient = G.Fill.prototype.radialGradient;
  2047. /**
  2048. * Creates a bitmap fill style and assigns it to {{#crossLink "Stroke/style:property"}}{{/crossLink}}.
  2049. * See {{#crossLink "Graphics/beginBitmapStroke"}}{{/crossLink}} for more information.
  2050. * @method bitmap
  2051. * @param {HTMLImageElement} image
  2052. * @param {String} [repetition] One of: repeat, repeat-x, repeat-y, or no-repeat.
  2053. * @return {Fill} Returns this Stroke object for chaining or assignment.
  2054. */
  2055. p.bitmap = G.Fill.prototype.bitmap;
  2056. p.path = false;
  2057.  
  2058. /**
  2059. * Graphics command object. See {{#crossLink "Graphics/setStrokeStyle"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  2060. * @class StrokeStyle
  2061. * @constructor
  2062. * @param {Number} width
  2063. * @param {String} [caps=butt]
  2064. * @param {String} [joints=miter]
  2065. * @param {Number} [miterLimit=10]
  2066. * @param {Boolean} [ignoreScale=false]
  2067. **/
  2068. /**
  2069. * @property width
  2070. * @type Number
  2071. */
  2072. /**
  2073. * One of: butt, round, square
  2074. * @property caps
  2075. * @type String
  2076. */
  2077. /**
  2078. * One of: round, bevel, miter
  2079. * @property joints
  2080. * @type String
  2081. */
  2082. /**
  2083. * @property miterLimit
  2084. * @type Number
  2085. */
  2086. /**
  2087. * Execute the Graphics command in the provided Canvas context.
  2088. * @method exec
  2089. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  2090. */
  2091. p = (G.StrokeStyle = function(width, caps, joints, miterLimit, ignoreScale) {
  2092. this.width = width;
  2093. this.caps = caps;
  2094. this.joints = joints;
  2095. this.miterLimit = miterLimit;
  2096. this.ignoreScale = ignoreScale;
  2097. }).prototype;
  2098. p.exec = function(ctx) {
  2099. ctx.lineWidth = (this.width == null ? "1" : this.width);
  2100. ctx.lineCap = (this.caps == null ? "butt" : (isNaN(this.caps) ? this.caps : Graphics.STROKE_CAPS_MAP[this.caps]));
  2101. ctx.lineJoin = (this.joints == null ? "miter" : (isNaN(this.joints) ? this.joints : Graphics.STROKE_JOINTS_MAP[this.joints]));
  2102. ctx.miterLimit = (this.miterLimit == null ? "10" : this.miterLimit);
  2103. ctx.ignoreScale = (this.ignoreScale == null ? false : this.ignoreScale);
  2104. };
  2105. p.path = false;
  2106. /**
  2107. * Graphics command object. See {{#crossLink "Graphics/setStrokeDash"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  2108. * @class StrokeDash
  2109. * @constructor
  2110. * @param {Array} [segments]
  2111. * @param {Number} [offset=0]
  2112. **/
  2113. /**
  2114. * @property segments
  2115. * @type Array
  2116. */
  2117. /**
  2118. * @property offset
  2119. * @type Number
  2120. */
  2121. /**
  2122. * Execute the Graphics command in the provided Canvas context.
  2123. * @method exec
  2124. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  2125. */
  2126. (G.StrokeDash = function(segments, offset) {
  2127. this.segments = segments;
  2128. this.offset = offset||0;
  2129. }).prototype.exec = function(ctx) {
  2130. if (ctx.setLineDash) { // feature detection.
  2131. ctx.setLineDash(this.segments|| G.StrokeDash.EMPTY_SEGMENTS); // instead of [] to reduce churn.
  2132. ctx.lineDashOffset = this.offset||0;
  2133. }
  2134. };
  2135. /**
  2136. * The default value for segments (ie. no dash).
  2137. * @property EMPTY_SEGMENTS
  2138. * @static
  2139. * @final
  2140. * @readonly
  2141. * @protected
  2142. * @type {Array}
  2143. **/
  2144. G.StrokeDash.EMPTY_SEGMENTS = [];
  2145.  
  2146. /**
  2147. * Graphics command object. See {{#crossLink "Graphics/drawRoundRectComplex"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  2148. * @class RoundRect
  2149. * @constructor
  2150. * @param {Number} x
  2151. * @param {Number} y
  2152. * @param {Number} w
  2153. * @param {Number} h
  2154. * @param {Number} radiusTL
  2155. * @param {Number} radiusTR
  2156. * @param {Number} radiusBR
  2157. * @param {Number} radiusBL
  2158. **/
  2159. /**
  2160. * @property x
  2161. * @type Number
  2162. */
  2163. /**
  2164. * @property y
  2165. * @type Number
  2166. */
  2167. /**
  2168. * @property w
  2169. * @type Number
  2170. */
  2171. /**
  2172. * @property h
  2173. * @type Number
  2174. */
  2175. /**
  2176. * @property radiusTL
  2177. * @type Number
  2178. */
  2179. /**
  2180. * @property radiusTR
  2181. * @type Number
  2182. */
  2183. /**
  2184. * @property radiusBR
  2185. * @type Number
  2186. */
  2187. /**
  2188. * @property radiusBL
  2189. * @type Number
  2190. */
  2191. /**
  2192. * Execute the Graphics command in the provided Canvas context.
  2193. * @method exec
  2194. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  2195. */
  2196. (G.RoundRect = function(x, y, w, h, radiusTL, radiusTR, radiusBR, radiusBL) {
  2197. this.x = x; this.y = y;
  2198. this.w = w; this.h = h;
  2199. this.radiusTL = radiusTL; this.radiusTR = radiusTR;
  2200. this.radiusBR = radiusBR; this.radiusBL = radiusBL;
  2201. }).prototype.exec = function(ctx) {
  2202. var max = (w<h?w:h)/2;
  2203. var mTL=0, mTR=0, mBR=0, mBL=0;
  2204. var x = this.x, y = this.y, w = this.w, h = this.h;
  2205. var rTL = this.radiusTL, rTR = this.radiusTR, rBR = this.radiusBR, rBL = this.radiusBL;
  2206.  
  2207. if (rTL < 0) { rTL *= (mTL=-1); }
  2208. if (rTL > max) { rTL = max; }
  2209. if (rTR < 0) { rTR *= (mTR=-1); }
  2210. if (rTR > max) { rTR = max; }
  2211. if (rBR < 0) { rBR *= (mBR=-1); }
  2212. if (rBR > max) { rBR = max; }
  2213. if (rBL < 0) { rBL *= (mBL=-1); }
  2214. if (rBL > max) { rBL = max; }
  2215.  
  2216. ctx.moveTo(x+w-rTR, y);
  2217. ctx.arcTo(x+w+rTR*mTR, y-rTR*mTR, x+w, y+rTR, rTR);
  2218. ctx.lineTo(x+w, y+h-rBR);
  2219. ctx.arcTo(x+w+rBR*mBR, y+h+rBR*mBR, x+w-rBR, y+h, rBR);
  2220. ctx.lineTo(x+rBL, y+h);
  2221. ctx.arcTo(x-rBL*mBL, y+h+rBL*mBL, x, y+h-rBL, rBL);
  2222. ctx.lineTo(x, y+rTL);
  2223. ctx.arcTo(x-rTL*mTL, y-rTL*mTL, x+rTL, y, rTL);
  2224. ctx.closePath();
  2225. };
  2226.  
  2227. /**
  2228. * Graphics command object. See {{#crossLink "Graphics/drawCircle"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  2229. * @class Circle
  2230. * @constructor
  2231. * @param {Number} x
  2232. * @param {Number} y
  2233. * @param {Number} radius
  2234. **/
  2235. /**
  2236. * @property x
  2237. * @type Number
  2238. */
  2239. /**
  2240. * @property y
  2241. * @type Number
  2242. */
  2243. /**
  2244. * @property radius
  2245. * @type Number
  2246. */
  2247. /**
  2248. * Execute the Graphics command in the provided Canvas context.
  2249. * @method exec
  2250. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  2251. */
  2252. (G.Circle = function(x, y, radius) {
  2253. this.x = x; this.y = y;
  2254. this.radius = radius;
  2255. }).prototype.exec = function(ctx) { ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2); };
  2256.  
  2257. /**
  2258. * Graphics command object. See {{#crossLink "Graphics/drawEllipse"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  2259. * @class Ellipse
  2260. * @constructor
  2261. * @param {Number} x
  2262. * @param {Number} y
  2263. * @param {Number} w
  2264. * @param {Number} h
  2265. **/
  2266. /**
  2267. * @property x
  2268. * @type Number
  2269. */
  2270. /**
  2271. * @property y
  2272. * @type Number
  2273. */
  2274. /**
  2275. * @property w
  2276. * @type Number
  2277. */
  2278. /**
  2279. * @property h
  2280. * @type Number
  2281. */
  2282. /**
  2283. * Execute the Graphics command in the provided Canvas context.
  2284. * @method exec
  2285. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  2286. */
  2287. (G.Ellipse = function(x, y, w, h) {
  2288. this.x = x; this.y = y;
  2289. this.w = w; this.h = h;
  2290. }).prototype.exec = function(ctx) {
  2291. var x = this.x, y = this.y;
  2292. var w = this.w, h = this.h;
  2293.  
  2294. var k = 0.5522848;
  2295. var ox = (w / 2) * k;
  2296. var oy = (h / 2) * k;
  2297. var xe = x + w;
  2298. var ye = y + h;
  2299. var xm = x + w / 2;
  2300. var ym = y + h / 2;
  2301.  
  2302. ctx.moveTo(x, ym);
  2303. ctx.bezierCurveTo(x, ym-oy, xm-ox, y, xm, y);
  2304. ctx.bezierCurveTo(xm+ox, y, xe, ym-oy, xe, ym);
  2305. ctx.bezierCurveTo(xe, ym+oy, xm+ox, ye, xm, ye);
  2306. ctx.bezierCurveTo(xm-ox, ye, x, ym+oy, x, ym);
  2307. };
  2308.  
  2309. /**
  2310. * Graphics command object. See {{#crossLink "Graphics/drawPolyStar"}}{{/crossLink}} and {{#crossLink "Graphics/append"}}{{/crossLink}} for more information.
  2311. * @class PolyStar
  2312. * @constructor
  2313. * @param {Number} x
  2314. * @param {Number} y
  2315. * @param {Number} radius
  2316. * @param {Number} sides
  2317. * @param {Number} pointSize
  2318. * @param {Number} angle
  2319. **/
  2320. /**
  2321. * @property x
  2322. * @type Number
  2323. */
  2324. /**
  2325. * @property y
  2326. * @type Number
  2327. */
  2328. /**
  2329. * @property radius
  2330. * @type Number
  2331. */
  2332. /**
  2333. * @property sides
  2334. * @type Number
  2335. */
  2336. /**
  2337. * @property pointSize
  2338. * @type Number
  2339. */
  2340. /**
  2341. * @property angle
  2342. * @type Number
  2343. */
  2344. /**
  2345. * Execute the Graphics command in the provided Canvas context.
  2346. * @method exec
  2347. * @param {CanvasRenderingContext2D} ctx The canvas rendering context
  2348. */
  2349. (G.PolyStar = function(x, y, radius, sides, pointSize, angle) {
  2350. this.x = x; this.y = y;
  2351. this.radius = radius;
  2352. this.sides = sides;
  2353. this.pointSize = pointSize;
  2354. this.angle = angle;
  2355. }).prototype.exec = function(ctx) {
  2356. var x = this.x, y = this.y;
  2357. var radius = this.radius;
  2358. var angle = (this.angle||0)/180*Math.PI;
  2359. var sides = this.sides;
  2360. var ps = 1-(this.pointSize||0);
  2361. var a = Math.PI/sides;
  2362.  
  2363. ctx.moveTo(x+Math.cos(angle)*radius, y+Math.sin(angle)*radius);
  2364. for (var i=0; i<sides; i++) {
  2365. angle += a;
  2366. if (ps != 1) {
  2367. ctx.lineTo(x+Math.cos(angle)*radius*ps, y+Math.sin(angle)*radius*ps);
  2368. }
  2369. angle += a;
  2370. ctx.lineTo(x+Math.cos(angle)*radius, y+Math.sin(angle)*radius);
  2371. }
  2372. ctx.closePath();
  2373. };
  2374.  
  2375. // docced above.
  2376. Graphics.beginCmd = new G.BeginPath(); // so we don't have to instantiate multiple instances.
  2377.  
  2378.  
  2379. createjs.Graphics = Graphics;
  2380. }());
  2381.