Drawing a portion of an ellipse programmatically with a Bezier or an Elliptical path — SVG and raphael.js -
i'm trying draw bezier curve surrounding ellipse given margin :

i want achieve programmatically, if changes ellipse size, curve follow it.
at moment i've made function :
function bezierpathtoprounded(ellipse, margin) { var box = ellipse.paper.getbbox(); var leftx = box.x - margin; var rightx = box.x + margin + box.width; var y = box.y + box.height/2 - margin; var p = "m "+ leftx + ", "+ y + " c " //could relative + ( box.x - margin + (box.width/15) ) + ", " + ( box.y + margin - (box.height/4.5) ) + " " + ( box.x + margin + box.width - (box.width/15) ) + ", " + ( box.y + margin - (box.height/4.5) ) + " " + rightx +", " + y; return p; } but can't figure out how calculate direction points values work ellipse :
- box.width/15
- box.height/4.5
there fiddle example.
i've read stackoverflow question , tried same on example, still can't figure out simple solution, remains random...
edit
now i'm trying elliptical arc, result worser bezier path :

there function i'm using. if remove margin follows ellipse... matter how may follow ellipse margin ?
function borderpath(ellipse, margin, flag) { var flag = flag == undefined ? 1 : 0; var box = ellipse.paper.getbbox(); var leftx = box.x - margin; var rightx = box.x + margin + box.width; var y = box.y + box.height/2; y += (flag == 1) ? -margin : margin; var rx = box.width/2 + margin; var ry = box.height/2; var p = "m "+ leftx + ", "+ y + " " + rx + " " + ry + " 0 0 "+ flag +" " + rightx +", " + y; return p; } see updated fiddle here.
really sorry awful colors, example purpose.
if want bezier curves, you'll have decide how "wrong" want look. bezier curves cannot represent circular (and extension, elliptical) curves, can close, in same way polygon can, higher precision using fewer sections.
i describe both circle-approximation , curve offsetting using bezier curves in primer on bezier curves, http://pomax.github.io/bezierinfo/#circles_cubic , http://pomax.github.io/bezierinfo/#offsetting respectively, if you're coding scratch particularly offsetting overkill if need describe in example.
instead, i'd recommend firing inkscape or illustrator, turning on grid overlay, , drawing bezier curve around ellipse. make good, check coordinates are, , use reliable-enough information implementing in canvas program. don't need mathematically rigidly correct, long people don't go "that looks wrong", should fine.
Comments
Post a Comment