Skip to content

Instantly share code, notes, and snippets.

@zackbloom
Created October 4, 2011 18:13
Show Gist options
  • Select an option

  • Save zackbloom/1262366 to your computer and use it in GitHub Desktop.

Select an option

Save zackbloom/1262366 to your computer and use it in GitHub Desktop.
Rounded Polygon
$DRAW.rounded_poly = (cxt, points, radius=15) ->
edges = []
for i in [0...points.length]
c_pnt = points[i]
n_pnt = points[(i + 1) % points.length]
edges.push
x1: c_pnt.x
y1: c_pnt.y
x2: n_pnt.x
y2: n_pnt.y
do cxt.beginPath
segments = []
for c_edge in edges
[ang, rad] = $DRAW.cart_to_polar c_edge
rad = Math.min radius, rad / 2
segments.push
x1: c_edge.x1 + Math.cos(ang) * rad
y1: c_edge.y1 - Math.sin(ang) * rad
x2: c_edge.x2 - Math.cos(ang) * rad
y2: c_edge.y2 + Math.sin(ang) * rad
cxt.moveTo segments.first().x1, segments.first().y1
for i in [0...segments.length]
e = edges[i]
c = segments[i]
n = segments[(i + 1) % segments.length]
cxt.lineTo c.x2, c.y2
cxt.quadraticCurveTo e.x2, e.y2, n.x1, n.y1
do cxt.closePath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment