Skip to content

Instantly share code, notes, and snippets.

@markknol
Last active August 16, 2021 11:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markknol/5c5d48655ebac555a6eec41792acdfb6 to your computer and use it in GitHub Desktop.
Save markknol/5c5d48655ebac555a6eec41792acdfb6 to your computer and use it in GitHub Desktop.
Create oval / circle with bezierCurveTo in Haxe / Javascript

How to draw a circle or oval with Haxe/JavaScript

public static function oval(g:Graphics, w:Float, h:Float, cx:Float = 0, cy:Float = 0):Graphics {
	var lx = cx - w * .5;
	var rx = cx + w * .5;
	var ty = cy - h * .5;
	var by = cy + h * .5;

	var magic = 0.551915024494; 
	var xmagic = magic * w * 0.5;
	var ymagic = h * magic * 0.5;

	g.moveTo(cx, ty);
	g.bezierCurveTo(cx + xmagic, ty, rx, cy - ymagic, rx, cy);
	g.bezierCurveTo(rx, cy + ymagic, cx + xmagic, by, cx, by);
	g.bezierCurveTo(cx - xmagic, by, lx, cy + ymagic, lx, cy);
	g.bezierCurveTo(lx, cy - ymagic, cx - xmagic, ty, cx, ty);

	return g;
}

sources
Magic value http://spencermortensen.com/articles/bezier-circle/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment