Ok, I think I have my first contribution! The getBBox() problem is
solved. The patch below to pathDimensions function does the trick. I
have tested successfully in IE7, Ch3, FF3, Sf4. I am not a JS guru so
hope you guys can perfect it.
Issues:
1. In pathDimensions function, always the first point in the path was
passed to curve dim as x1,y1 where curveDim takes
(x1,y1,c1x,c1y,c2x,c2y,x2,y2). This has to be changed to x2,y2 of the
previous curve.
2. curveDim function calculation is wrong I guess. I have replaced
that by determining the right points by using the formula to draw a
cubic bezier from this link
http://www.paultondeur.com/2008/03/09/drawing-a-cubic-bezier-curve-using-actionscript-3/
Here is the changed function,
var pathDimensions = cacher(function (path) {
if (!path) {
return {x: 0, y: 0, width: 0, height: 0};
}
path = path2curve(path);
var x = 0,
y = 0,
X = [],
Y = [],
x0 = 0,
y0 = 0;
;
for (var i = 0, ii = path[length]; i < ii; i++) {
if (path[i][0] == "M") {
x = path[i][1];
y = path[i][2];
X[push](x);
Y[push](y);
} else {
//var dim = curveDim(x, y, path[i][1], path[i][2], path
[i][3], path[i][4], path[i][5], path[i][6]);
var x0, y0;
if(x0 == 0 && y0 == 0){
x0 = x;
y0 = y;
}else{
x0 = path[i-1][5];
y0 = path[i-1][6];
}
var dim = _curveDim(x0, y0, path[i][1], path[i][2],
path[i][3], path[i][4], path[i][5], path[i][6]);
X = X[concat](dim.min.x, dim.max.x);
Y = Y[concat](dim.min.y, dim.max.y);
}
}
var xmin = mmin[apply](0, X),
ymin = mmin[apply](0, Y);
return {
x: xmin,
y: ymin,
width: mmax[apply](0, X) - xmin,
height: mmax[apply](0, Y) - ymin
};
function _curveDim(x1,y1,c1x,c1y,c2x,c2y,x2,y2) {
var xPosArr = new Array(), yPosArr = new Array(), u;
for (u = 0; u <= 1; u += 1/100) {
xPosArr[Math.floor(u*100)] = Math.pow(u,3)*(x2+3*(c1x-c2x)-
x1)+3*Math.pow(u,2)*(x1-2*c1x+c2x)+3*u*(c1x-x1)+x1;
yPosArr[Math.floor(u*100)] = Math.pow(u,3)*(y2+3*(c1y-c2y)-
y1)+3*Math.pow(u,2)*(y1-2*c1y+c2y)+3*u*(c1y-y1)+y1;
}
xPosArr.sort(_sortNum);
yPosArr.sort(_sortNum);
var xArr = [x1,xPosArr[0],xPosArr[xPosArr.length-2],x2];
xArr.sort(_sortNum);
var yArr = [y1,yPosArr[0],yPosArr[yPosArr.length-2],y2];
yArr.sort(_sortNum);
return {min:{x:xArr[0],y:yArr[0]},max:{x:xArr[xArr.length-1], y:yArr
[yArr.length-1]}}
}
function _sortNum(a,b){return a - b;}
}