CanvasShape.js revision 828c58761d90445b8b9d20a82d85dc1479317f71
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Base class for creating shapes.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @class CanvasShape
828c58761d90445b8b9d20a82d85dc1479317f71Tripp var host = this,
828c58761d90445b8b9d20a82d85dc1479317f71Tripp host._eventPrefix = host.constructor.EVENT_PREFIX || host.constructor.NAME;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp // Need to initPlugins manually, to handle constructor parsing, static Plug parsing
828c58761d90445b8b9d20a82d85dc1479317f71TrippCanvasShape.prototype = Y.merge(Y.CanvasDrawing.prototype, {
828c58761d90445b8b9d20a82d85dc1479317f71Tripp init: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Initializes the shape
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method _initialize
828c58761d90445b8b9d20a82d85dc1479317f71Tripp var host = this;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp host.get("stroke");
828c58761d90445b8b9d20a82d85dc1479317f71Tripp host.get("fill");
828c58761d90445b8b9d20a82d85dc1479317f71Tripp node.setAttribute("width", host.get("width"));
828c58761d90445b8b9d20a82d85dc1479317f71Tripp node.setAttribute("height", host.get("height"));
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Add a class name to each node.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method addClass
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {String} className the class name to add to the node's class attribute
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Removes a class name from each node.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method removeClass
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {String} className the class name to remove from the node's class attribute
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Gets the current position of the node in page coordinates.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method getXY
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @return Array The XY position of the shape.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp getXY: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Set the position of the shape in page coordinates, regardless of how the node is positioned.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method setXY
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Array} Contains X & Y values for new position (coordinates are page-based)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Determines whether the node is an ancestor of another HTML element in the DOM hierarchy.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method contains
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {CanvasShape | HTMLElement} needle The possible node or descendent
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @return Boolean Whether or not this shape is the needle or its ancestor.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Test if the supplied node matches the supplied selector.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method test
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {String} selector The CSS selector to test against.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @return Boolean Wheter or not the shape matches the selector.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp //return Y.Selector.test(this.node, selector);
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Compares nodes to determine if they match.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Node instances can be compared to each other and/or HTMLElements.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method compareTo
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {HTMLElement | Node} refNode The reference node to compare to the node.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @return {Boolean} True if the nodes match, false if they do not.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Value function for fill attribute
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method _getDefaultFill
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @return Object
828c58761d90445b8b9d20a82d85dc1479317f71Tripp _getDefaultFill: function() {
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Value function for stroke attribute
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method _getDefaultStroke
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @return Object
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Left edge of the path
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Right edge of the path
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Top edge of the path
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Bottom edge of the path
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Creates the dom node for the shape.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @return HTMLElement
828c58761d90445b8b9d20a82d85dc1479317f71Tripp if(type.indexOf('mouse') > -1 || type.indexOf('click') > -1)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp return true;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp return false;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Adds a stroke to the shape node.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method _strokeChangeHandler
828c58761d90445b8b9d20a82d85dc1479317f71Tripp this._miterlimit = null;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp this._dashstyle = (dashstyle && Y.Lang.isArray(dashstyle) && dashstyle.length > 1) ? dashstyle : null;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp set: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp var host = this,
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Adds a fill to the shape node.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method _fillChangeHandler
828c58761d90445b8b9d20a82d85dc1479317f71Tripp this._fillColor = null;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Applies translate transformation.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method translate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} x The x-coordinate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} y The y-coordinate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @protected
828c58761d90445b8b9d20a82d85dc1479317f71Tripp translate: function(x, y)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp var translate = "translate(" + x + "px, " + y + "px)";
828c58761d90445b8b9d20a82d85dc1479317f71Tripp this._updateTransform("translate", /translate\(.*\)/, translate);
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Applies a skew to the x-coordinate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method skewX:q
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} x x-coordinate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp skewX: function(x)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Applies a skew to the x-coordinate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method skewX:q
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} x x-coordinate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp skewY: function(y)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Applies a rotation.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method rotate
828c58761d90445b8b9d20a82d85dc1479317f71Tripp this._updateTransform("rotate", /rotate\(.*\)/, rotate);
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * An array of x, y values which indicates the transformOrigin in which to rotate the shape. Valid values range between 0 and 1 representing a
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * fraction of the shape's corresponding bounding box dimension. The default value is [0.5, 0.5].
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute transformOrigin
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Array
828c58761d90445b8b9d20a82d85dc1479317f71Tripp _transformOrigin: function(x, y)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp node.style.MozTransformOrigin = (100 * x) + "% " + (100 * y) + "%";
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Applies a scale transform
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method scale
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} val
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Applies a matrix transformation
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method matrix
828c58761d90445b8b9d20a82d85dc1479317f71Tripp matrix: function(a, b, c, d, e, f)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp transform = node.style.MozTransform || node.style.webkitTransform || node.style.msTransform || node.style.OTransform,
828c58761d90445b8b9d20a82d85dc1479317f71Tripp transformOrigin = (100 * transformOrigin[0]) + "% " + (100 * transformOrigin[1]) + "%";
828c58761d90445b8b9d20a82d85dc1479317f71Tripp _draw: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Completes a shape or drawing
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method _paint
828c58761d90445b8b9d20a82d85dc1479317f71Tripp _paint: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp for(; i < len; ++i)
828c58761d90445b8b9d20a82d85dc1479317f71Tripp args.unshift(this._xcoords[i] - this._left, this._ycoords[i] - this._top);
828c58761d90445b8b9d20a82d85dc1479317f71Tripp if (this._stroke) {
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Draws a dashed line between two points.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method _drawDashedLine
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} xStart The x position of the start of the line
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} yStart The y position of the start of the line
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} xEnd The x position of the end of the line
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @param {Number} yEnd The y position of the end of the line
828c58761d90445b8b9d20a82d85dc1479317f71Tripp delta = Math.sqrt(Math.pow(xDelta, 2) + Math.pow(yDelta, 2)),
828c58761d90445b8b9d20a82d85dc1479317f71Tripp segmentCount = Math.floor(Math.abs(delta / segmentLength)),
828c58761d90445b8b9d20a82d85dc1479317f71Tripp context.lineTo(xCurrent + Math.cos(radians) * dashsize, yCurrent + Math.sin(radians) * dashsize);
828c58761d90445b8b9d20a82d85dc1479317f71Tripp delta = Math.sqrt((xEnd - xCurrent) * (xEnd - xCurrent) + (yEnd - yCurrent) * (yEnd - yCurrent));
828c58761d90445b8b9d20a82d85dc1479317f71Tripp context.lineTo(xCurrent + Math.cos(radians) * dashsize, yCurrent + Math.sin(radians) * dashsize);
828c58761d90445b8b9d20a82d85dc1479317f71Tripp context.lineTo(xCurrent + Math.cos(radians) * delta, yCurrent + Math.sin(radians) * delta);
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Clears the graphics object.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @method clear
828c58761d90445b8b9d20a82d85dc1479317f71Tripp clear: function() {
828c58761d90445b8b9d20a82d85dc1479317f71Tripp return this;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * An array of x, y values which indicates the transformOrigin in which to rotate the shape. Valid values range between 0 and 1 representing a
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * fraction of the shape's corresponding bounding box dimension. The default value is [0.5, 0.5].
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute transformOrigin
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Array
828c58761d90445b8b9d20a82d85dc1479317f71Tripp valueFn: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * The rotation (in degrees) of the shape.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute rotation
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Number
828c58761d90445b8b9d20a82d85dc1479317f71Tripp getter: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp return this._rotation;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Dom node of the shape
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute node
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type HTMLElement
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @readOnly
828c58761d90445b8b9d20a82d85dc1479317f71Tripp getter: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp return this.node;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Unique id for class instance.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute id
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type String
828c58761d90445b8b9d20a82d85dc1479317f71Tripp valueFn: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp return Y.guid();
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute width
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute height
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * The x-coordinate for the shape.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * The x-coordinate for the shape.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Indicates whether the shape is visible.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute visible
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Boolean
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Contains information about the fill of the shape.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * <dt>color</dt><dd>The color of the fill.</dd>
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the fill. The default value is 1.</dd>
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute fill
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Object
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Contains information about the stroke of the shape.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * <dt>color</dt><dd>The color of the stroke.</dd>
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * <dt>weight</dt><dd>Number that indicates the width of the stroke.</dd>
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * <dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the stroke. The default value is 1.</dd>
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * <dt>dashstyle</dt>Indicates whether to draw a dashed stroke. When set to "none", a solid stroke is drawn. When set to an array, the first index indicates the
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * length of the dash. The second index indicates the length of gap.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute stroke
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Object
828c58761d90445b8b9d20a82d85dc1479317f71Tripp var tmpl = this.get("stroke") || this._getDefaultStroke();
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Indicates whether or not the instance will size itself based on its contents.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute autoSize
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Boolean
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Determines whether the instance will receive mouse events.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute pointerEvents
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type string
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * Reference to the container Graphic.
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @attribute graphic
828c58761d90445b8b9d20a82d85dc1479317f71Tripp * @type Graphic
828c58761d90445b8b9d20a82d85dc1479317f71Tripp getter: function()
828c58761d90445b8b9d20a82d85dc1479317f71Tripp return this._graphic;
828c58761d90445b8b9d20a82d85dc1479317f71Tripp//Straightup augment, no wrapper functions