diff --git a/editor/canvg/canvg.js b/editor/canvg/canvg.js index b3d2083f..3d447227 100644 --- a/editor/canvg/canvg.js +++ b/editor/canvg/canvg.js @@ -165,11 +165,19 @@ if(!window.console) { return that.value.indexOf('url(') == 0 }, - getGradient: function(e) { - var grad = this.getDefinition(); - if (grad != null && grad.createGradient) { - return grad.createGradient(svg.ctx, e); + getFillStyle: function(e) { + var def = this.getDefinition(); + + // gradient + if (def != null && def.createGradient) { + return def.createGradient(svg.ctx, e); } + + // pattern + if (def != null && def.createPattern) { + return def.createPattern(svg.ctx, e); + } + return null; } } @@ -278,6 +286,10 @@ if(!window.console) { svg.Point = function(x, y) { this.x = x; this.y = y; + + this.angleTo = function(p) { + return Math.atan2(p.y - this.y, p.x - this.x); + } } svg.CreatePoint = function(s) { var a = svg.ToNumberArray(s); @@ -516,16 +528,19 @@ if(!window.console) { this.children[i].render(ctx); } } + + this.addChild = function(childNode, create) { + var child = childNode; + if (create) child = svg.CreateElement(childNode); + child.parent = this; + this.children.push(child); + } - if (node != null) { + if (node != null && node.nodeType == 1) { //ELEMENT_NODE // add children for (var i=0; i 1) { + rx *= Math.sqrt(l); + ry *= Math.sqrt(l); + } // cx', cy' var s = (largeArcFlag == sweepFlag ? -1 : 1) * Math.sqrt( ((Math.pow(rx,2)*Math.pow(ry,2))-(Math.pow(rx,2)*Math.pow(currp.y,2))-(Math.pow(ry,2)*Math.pow(currp.x,2))) / @@ -1127,15 +1184,30 @@ if(!window.console) { ); // vector magnitude var m = function(v) { return Math.sqrt(Math.pow(v[0],2) + Math.pow(v[1],2)); } + // ratio between two vectors + var r = function(u, v) { return (u[0]*v[0]+u[1]*v[1]) / (m(u)*m(v)) } // angle between two vectors - var a = function(u, v) { return (u[0]*v[1] < u[1]*v[0] ? -1 : 1) * Math.acos((u[0]*v[0]+u[1]*v[1]) / (m(u)*m(v))); } + var a = function(u, v) { return (u[0]*v[1] < u[1]*v[0] ? -1 : 1) * Math.acos(r(u,v)); } // initial angle var a1 = a([1,0], [(currp.x-cpp.x)/rx,(currp.y-cpp.y)/ry]); // angle delta - var ad = a([(currp.x-cpp.x)/rx,(currp.y-cpp.y)/ry], [(-currp.x-cpp.x)/rx,(-currp.y-cpp.y)/ry]); + var u = [(currp.x-cpp.x)/rx,(currp.y-cpp.y)/ry]; + var v = [(-currp.x-cpp.x)/rx,(-currp.y-cpp.y)/ry]; + var ad = a(u, v); + if (r(u,v) <= -1) ad = Math.PI; + if (r(u,v) >= 1) ad = 0; + if (sweepFlag == 0 && ad > 0) ad = ad - 2 * Math.PI; if (sweepFlag == 1 && ad < 0) ad = ad + 2 * Math.PI; + // for markers + var halfWay = new svg.Point( + centp.x - rx * Math.cos((a1 + ad) / 2), + centp.y - ry * Math.sin((a1 + ad) / 2) + ); + pp.addMarkerAngle(halfWay, (a1 + ad) / 2 + (sweepFlag == 0 ? 1 : -1) * Math.PI / 2); + pp.addMarkerAngle(cp, ad + (sweepFlag == 0 ? 1 : -1) * Math.PI / 2); + bb.addPoint(cp.x, cp.y); // TODO: this is too naive, make it better if (ctx != null) { var r = rx > ry ? rx : ry; @@ -1160,24 +1232,52 @@ if(!window.console) { return bb; } - this.getEndPoint = function() { - return this.PathParser.current; - } - - this.getEndAngle = function() { - return this.PathParser.angle(); + this.getMarkers = function() { + var points = this.PathParser.getMarkerPoints(); + var angles = this.PathParser.getMarkerAngles(); + + var markers = []; + for (var i=0; i