SVGDrawing.js revision a89ad754cce3cfc8aee71760e10217b54020360d
var SHAPE = "svgShape",
Y_LANG = Y.Lang,
AttributeLite = Y.AttributeLite,
PluginHost = Y.Plugin.Host,
SVGShape,
SVGCircle,
SVGRect,
SVGPath,
SVGEllipse,
DOCUMENT = Y.config.doc;
function SVGDrawing(){}
SVGDrawing.prototype = {
/**
* Draws a bezier curve.
*
* @method curveTo
* @param {Number} cp1x x-coordinate for the first control point.
* @param {Number} cp1y y-coordinate for the first control point.
* @param {Number} cp2x x-coordinate for the second control point.
* @param {Number} cp2y y-coordinate for the second control point.
* @param {Number} x x-coordinate for the end point.
* @param {Number} y y-coordinate for the end point.
*/
curveTo: function(cp1x, cp1y, cp2x, cp2y, x, y) {
var pathArrayLen,
currentArray,
hiX,
loX,
hiY,
loY;
if(this._pathType !== "C")
{
this._pathType = "C";
currentArray = ["C"];
this._pathArray.push(currentArray);
}
else
{
currentArray = this._pathArray[Math.max(0, this._pathArray.length - 1)];
if(!currentArray)
{
currentArray = [];
this._pathArray.push(currentArray);
}
}
pathArrayLen = this._pathArray.length - 1;
this._pathArray[pathArrayLen] = this._pathArray[pathArrayLen].concat([Math.round(cp1x), Math.round(cp1y), Math.round(cp2x) , Math.round(cp2y), x, y]);
hiX = Math.max(x, Math.max(cp1x, cp2x));
hiY = Math.max(y, Math.max(cp1y, cp2y));
loX = Math.min(x, Math.min(cp1x, cp2x));
loY = Math.min(y, Math.min(cp1y, cp2y));
this._trackSize(hiX, hiY);
this._trackSize(loX, loY);
},
/**
* Draws a quadratic bezier curve.
*
* @method quadraticCurveTo
* @param {Number} cpx x-coordinate for the control point.
* @param {Number} cpy y-coordinate for the control point.
* @param {Number} x x-coordinate for the end point.
* @param {Number} y y-coordinate for the end point.
*/
quadraticCurveTo: function(cpx, cpy, x, y) {
var pathArrayLen,
currentArray,
hiX,
loX,
hiY,
loY;
if(this._pathType !== "Q")
{
this._pathType = "Q";
currentArray = ["Q"];
this._pathArray.push(currentArray);
}
else
{
currentArray = this._pathArray[Math.max(0, this._pathArray.length - 1)];
if(!currentArray)
{
currentArray = [];
this._pathArray.push(currentArray);
}
}
pathArrayLen = this._pathArray.length - 1;
this._pathArray[pathArrayLen] = this._pathArray[pathArrayLen].concat([Math.round(cpx), Math.round(cpy), Math.round(x), Math.round(y)]);
hiX = Math.max(x, cpx);
hiY = Math.max(y, cpy);
loX = Math.min(x, cpx);
loY = Math.min(y, cpy);
this._trackSize(hiX, hiY);
this._trackSize(loX, loY);
},
/**
* Draws a rectangle.
*
* @method drawRect
* @param {Number} x x-coordinate
* @param {Number} y y-coordinate
* @param {Number} w width
* @param {Number} h height
*/
drawRect: function(x, y, w, h) {
this.moveTo(x, y);
this.lineTo(x + w, y);
this.lineTo(x + w, y + h);
this.lineTo(x, y + h);
this.lineTo(x, y);
},
/**
* Draws a rectangle with rounded corners.
*
* @method drawRect
* @param {Number} x x-coordinate
* @param {Number} y y-coordinate
* @param {Number} w width
* @param {Number} h height
* @param {Number} ew width of the ellipse used to draw the rounded corners
* @param {Number} eh height of the ellipse used to draw the rounded corners
*/
drawRoundRect: function(x, y, w, h, ew, eh) {
this.moveTo(x, y + eh);
this.lineTo(x, y + h - eh);
this.quadraticCurveTo(x, y + h, x + ew, y + h);
this.lineTo(x + w - ew, y + h);
this.quadraticCurveTo(x + w, y + h, x + w, y + h - eh);
this.lineTo(x + w, y + eh);
this.quadraticCurveTo(x + w, y, x + w - ew, y);
this.lineTo(x + ew, y);
this.quadraticCurveTo(x, y, x, y + eh);
},
/**
* Draws a wedge.
*
* @param {Number} x x-coordinate of the wedge's center point
* @param {Number} y y-coordinate of the wedge's center point
* @param {Number} startAngle starting angle in degrees
* @param {Number} arc sweep of the wedge. Negative values draw clockwise.
* @param {Number} radius radius of wedge. If [optional] yRadius is defined, then radius is the x radius.
* @param {Number} yRadius [optional] y radius for wedge.
*/
drawWedge: function(x, y, startAngle, arc, radius, yRadius)
{
this._drawingComplete = false;
this.path = this._getWedgePath({x:x, y:y, startAngle:startAngle, arc:arc, radius:radius, yRadius:yRadius});
},
/**
* Generates a path string for a wedge shape
*
* @method _getWedgePath
* @param {Object} config attributes used to create the path
* @return String
* @private
*/
_getWedgePath: function(config)
{
var x = config.x,
y = config.y,
startAngle = config.startAngle,
arc = config.arc,
radius = config.radius,
yRadius = config.yRadius || radius,
segs,
segAngle,
theta,
angle,
angleMid,
ax,
ay,
bx,
by,
cx,
cy,
i = 0,
diameter = radius * 2,
path = ' M' + x + ', ' + y;
// limit sweep to reasonable numbers
if(Math.abs(arc) > 360)
{
arc = 360;
}
// First we calculate how many segments are needed
// for a smooth arc.
segs = Math.ceil(Math.abs(arc) / 45);
// Now calculate the sweep of each segment.
segAngle = arc / segs;
// The math requires radians rather than degrees. To convert from degrees
// use the formula (degrees/180)*Math.PI to get radians.
theta = -(segAngle / 180) * Math.PI;
// convert angle startAngle to radians
angle = (startAngle / 180) * Math.PI;
if(segs > 0)
{
// draw a line from the center to the start of the curve
ax = x + Math.cos(startAngle / 180 * Math.PI) * radius;
ay = y + Math.sin(startAngle / 180 * Math.PI) * yRadius;
path += " L" + Math.round(ax) + ", " + Math.round(ay);
path += " Q";
for(; i < segs; ++i)
{
angle += theta;
angleMid = angle - (theta / 2);
bx = x + Math.cos(angle) * radius;
by = y + Math.sin(angle) * yRadius;
cx = x + Math.cos(angleMid) * (radius / Math.cos(theta / 2));
cy = y + Math.sin(angleMid) * (yRadius / Math.cos(theta / 2));
path += Math.round(cx) + " " + Math.round(cy) + " " + Math.round(bx) + " " + Math.round(by) + " ";
}
path += ' L' + x + ", " + y;
}
this._trackSize(diameter, diameter);
return path;
},
/**
* Draws a line segment using the current line style from the current drawing position to the specified x and y coordinates.
*
* @method lineTo
* @param {Number} point1 x-coordinate for the end point.
* @param {Number} point2 y-coordinate for the end point.
*/
lineTo: function(point1, point2, etc) {
var args = arguments,
i,
len,
pathArrayLen,
currentArray;
this._pathArray = this._pathArray || [];
if (typeof point1 === 'string' || typeof point1 === 'number') {
args = [[point1, point2]];
}
len = args.length;
this._shapeType = "path";
if(this._pathType !== "L")
{
this._pathType = "L";
currentArray = ['L'];
this._pathArray.push(currentArray);
}
else
{
currentArray = this._pathArray[Math.max(0, this._pathArray.length - 1)];
if(!currentArray)
{
currentArray = [];
this._pathArray.push(currentArray);
}
}
pathArrayLen = this._pathArray.length - 1;
for (i = 0; i < len; ++i) {
this._pathArray[pathArrayLen] = this._pathArray[pathArrayLen].concat([args[i][0], args[i][1]]);
this._trackSize.apply(this, args[i]);
}
},
/**
* Moves the current drawing position to specified x and y coordinates.
*
* @method moveTo
* @param {Number} x x-coordinate for the end point.
* @param {Number} y y-coordinate for the end point.
*/
moveTo: function(x, y) {
var pathArrayLen,
currentArray;
this._pathArray = this._pathArray || [];
if(this._pathType != "M")
{
this._pathType = "M";
currentArray = ["M"];
this._pathArray.push(currentArray);
}
else
{
currentArray = this._pathArray[Math.max(0, this._pathArray.length - 1)];
if(!currentArray)
{
currentArray = [];
this._pathArray.push(currentArray);
}
}
pathArrayLen = this._pathArray.length - 1;
this._pathArray[pathArrayLen] = this._pathArray[pathArrayLen].concat([x, y]);
this._trackSize(x, y);
},
/**
* Completes a drawing operation.
*
* @method end
*/
end: function() {
this._draw();
},
/**
* Sets the size of the graphics object.
*
* @method setSize
* @param w {Number} width to set for the instance.
* @param h {Number} height to set for the instance.
*/
setSize: function(w, h) {
var node;
if(this.get("autoSize"))
{
node = this.get("node");
if(w > node.getAttribute("width"))
{
node.setAttribute("width", w);
}
if(h > node.getAttribute("height"))
{
node.setAttribute("height", h);
}
}
},
/**
* Updates the size of the graphics object
*
* @method _trackSize
* @param {Number} w width
* @param {Number} h height
* @private
*/
_trackSize: function(w, h) {
var node = this.get("node");
if (w > this._right) {
this._right = w;
}
if(w < this._left)
{
this._left = w;
}
if (h < this._top)
{
this._top = h;
}
if (h > this._bottom)
{
this._bottom = h;
}
this._width = this._right - this._left;
this._height = this._bottom - this._top;
node.style.left = this._left + "px";
node.style.top = this._top + "px";
this.setSize(this._width, this._height);
}
};
Y.SVGDrawing = SVGDrawing;