d3-curve-circlecorners
Version:
A curve function that can be passed to the d3.js line.curve and area.curve functions
3 lines (2 loc) • 1.87 kB
JavaScript
// https://github.com/carpiediem/d3-curve-circlecorners#readme v0.1.5 Copyright 2022 Ryan SL Carpenter
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).circleCorners=e()}(this,(function(){"use strict";function t(t,e){this._context=t,this._radius=e||1}function e(t,e,i){const n=Math.atan2(t.y-e.y,t.x-e.x);return{x:t.x-i*Math.cos(n),y:t.y-i*Math.sin(n)}}return t.prototype={areaStart:function(){this._line=0},areaEnd:function(){this._line=NaN},lineStart:function(){this._prev={x:NaN,y:NaN},this._vert={x:NaN,y:NaN},this._point=0},lineEnd:function(){1===this._point?this._context.moveTo(this._vert.x,this._vert.y):this._context.lineTo(this._vert.x,this._vert.y),(this._line||0!==this._line&&3===this._point)&&this._context.closePath(),this._line=1-this._line},point:function(t,i){switch(t=+t,i=+i,this._point){case 0:this._point=1;break;case 1:this._point=2,this._line?this._context.lineTo(this._vert.x,this._vert.y):this._context.moveTo(this._vert.x,this._vert.y);break;default:return void function(t,i,n){const r=Math.abs(Math.atan2(t._vert.y-t._prev.y,t._vert.x-t._prev.x)-Math.atan2(t._vert.y-n,t._vert.x-i)),_=r>Math.PI?2*Math.PI-r:r,h=Math.min(Math.sqrt(Math.pow(t._vert.x-t._prev.x,2)+Math.pow(t._vert.y-t._prev.y,2)),Math.sqrt(Math.pow(t._vert.x-i,2)+Math.pow(t._vert.y-n,2))),s=Math.min(t._radius,h*Math.tan(_/2)),o=_?Math.abs(s/Math.tan(_/2)):0,a=(t._vert.x-t._prev.x)*(t._vert.y-n)-(t._vert.x-i)*(t._vert.y-t._prev.y)<0?1:0,v=e(t._vert,t._prev,o),x=e(t._vert,{x:i,y:n},o);t._context._+=`L${v.x},${v.y}A${s},${s},0,0,${a},${x.x},${x.y}`,t._prev=x,t._vert={x:i,y:n}}(this,t,i)}this._prev=this._vert,this._vert={x:t,y:i}}},function e(i){function n(e){return new t(e,i)}return n.radius=function(t){return e(+t)},n}(0)}));