{"id":542,"date":"2018-08-18T07:16:10","date_gmt":"2018-08-18T14:16:10","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=3252"},"modified":"2018-08-18T07:16:10","modified_gmt":"2018-08-18T14:16:10","slug":"javascript-smooth-quadratic-bezier","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=542","title":{"rendered":"JavaScript Smooth Quadratic Bezier"},"content":{"rendered":"<p>Being able to draw smooth lines that connect arbitrary points is something that I find myself needing very frequently. This is a port of an old snippet that does just that. By averaging control points of a quadratic bezier curve we ensure that our resulting Bezier curves are always smooth.<\/p>\n<p><iframe height='400' scrolling='no' title='Bezier Skin' src='\/\/codepen.io\/ZevanRosser\/embed\/wEwvdx\/?height=400&#038;theme-id=5318&#038;default-tab=result&#038;embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen <a href='https:\/\/codepen.io\/ZevanRosser\/pen\/wEwvdx\/'>Bezier Skin<\/a> by Zevan Rosser (<a href='https:\/\/codepen.io\/ZevanRosser'>@ZevanRosser<\/a>) on <a href='https:\/\/codepen.io'>CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<p>The key can be seen here with the `bezierSkin` function. It draws either a closed or open curve.<\/p>\n<pre lang=\"javascript\" line=\"1\">\r\n\/\/ array of xy coords, closed boolean\r\nfunction bezierSkin(bez, closed = true) {\r\n  var avg = calcAvgs(bez), \r\n      leng = bez.length,\r\n      i, n;\r\n\r\n  if (closed) {\r\n    c.moveTo(avg[0], avg[1]);\r\n    for (i = 2; i < leng; i += 2) {\r\n      n = i + 1;\r\n      c.quadraticCurveTo(bez[i], bez[n], avg[i], avg[n]);\r\n    }\r\n    c.quadraticCurveTo(bez[0], bez[1], avg[0], avg[1]);\r\n  } else {\r\n    c.moveTo(bez[0], bez[1]);\r\n    c.lineTo(avg[0], avg[1]);\r\n    for (i = 2; i < leng - 2; i += 2) {\r\n      n = i + 1;\r\n      c.quadraticCurveTo(bez[i], bez[n], avg[i], avg[n]);\r\n    }\r\n    c.lineTo(bez[leng - 2], bez[leng - 1]);\r\n  }\r\n}\r\n\r\n\r\n\/\/ create anchor points by averaging the control points\r\nfunction calcAvgs(p) {\r\n  var avg = [],\r\n      leng = p.length, prev;\r\n  for (var i = 2; i < leng; i++) {\r\n    prev = i - 2;\r\n    avg.push((p[prev] + p[i]) \/ 2);\r\n  }\r\n  \/\/ close\r\n  avg.push((p[0] + p[leng - 2]) \/ 2);\r\n  avg.push((p[1] + p[leng - 1]) \/ 2);\r\n  return avg;\r\n}\r\n<\/pre>\n<p>The control points are then averaged to ensure that the curve contains no sharp angles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Being able to draw smooth lines that connect arbitrary points is something that I find myself needing very frequently. This is a port of an old snippet that does just that. By averaging control points of a quadratic bezier curve we ensure that our resulting Bezier curves are always smooth. See the Pen Bezier Skin [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,30,33,114,161,160,121],"tags":[117,162,186,163,134],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-arrays","category-bezier","category-graphics","category-graphics-algorithms","category-html5","category-javascript","category-math","tag-bezier","tag-canvas","tag-graphics","tag-html5","tag-javascript"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=542"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}