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