@nivo/voronoi
Version:
3 lines (2 loc) • 10.3 kB
JavaScript
import{useMemo as n,useState as o,useRef as e,useEffect as i,useCallback as t,forwardRef as l,Fragment as r,createElement as u}from"react";import{defaultMargin as a,getRelativeCursor as d,getDistance as s,Container as h,useDimensions as c,SvgWrapper as v,ResponsiveWrapper as f}from"@nivo/core";import{scaleLinear as p}from"d3-scale";import{Delaunay as g}from"d3-delaunay";import{useTooltip as M}from"@nivo/tooltip";import{jsx as m,jsxs as b,Fragment as k}from"react/jsx-runtime";function C(){return C=Object.assign?Object.assign.bind():function(n){for(var o=1;o<arguments.length;o++){var e=arguments[o];for(var i in e)({}).hasOwnProperty.call(e,i)&&(n[i]=e[i])}return n},C.apply(null,arguments)}function y(n,o){if(null==n)return{};var e={};for(var i in n)if({}.hasOwnProperty.call(n,i)){if(-1!==o.indexOf(i))continue;e[i]=n[i]}return e}var w={xDomain:[0,1],yDomain:[0,1],layers:["links","cells","points","bounds"],enableLinks:!1,linkLineWidth:1,linkLineColor:"#bbbbbb",enableCells:!0,cellLineWidth:2,cellLineColor:"#000000",enablePoints:!0,pointSize:4,pointColor:"#666666",role:"img"},D=function(n){return[n.x,n.y]},L=a,T="cursor",P="top",R=function(n){var o=n.points,e=n.getNodePosition,i=void 0===e?D:e,t=n.margin,l=void 0===t?L:t;return o.map((function(n){var o=i(n),e=o[0],t=o[1];return[e+l.left,t+l.top]}))},W=function(n){var o=n.points,e=n.width,i=n.height,t=n.margin,l=void 0===t?L:t,r=n.debug,u=g.from(o),a=r?u.voronoi([0,0,l.left+e+l.right,l.top+i+l.bottom]):void 0;return{points:o,delaunay:u,voronoi:a}},E=function(o){var e=o.points,i=o.getNodePosition,t=void 0===i?D:i,l=o.width,r=o.height,u=o.margin,a=void 0===u?L:u,d=o.debug;return n((function(){return W({points:R({points:e,margin:a,getNodePosition:t}),width:l,height:r,margin:a,debug:d})}),[t,e,l,r,a,d])},x=function(o){var e=o.data,i=o.width,t=o.height,l=o.xDomain,r=o.yDomain,u=n((function(){return p().domain(l).range([0,i])}),[l,i]),a=n((function(){return p().domain(r).range([0,t])}),[r,t]),d=n((function(){return e.map((function(n){return{x:u(n.x),y:a(n.y),data:n}}))}),[e,u,a]);return n((function(){var n=g.from(d.map((function(n){return[n.x,n.y]}))),o=n.voronoi([0,0,i,t]);return{points:d,delaunay:n,voronoi:o}}),[d,i,t])},S=function(o){var e=o.points,i=o.delaunay,t=o.voronoi;return n((function(){return{points:e,delaunay:i,voronoi:t}}),[e,i,t])},U=function(l){var r=l.elementRef,u=l.nodes,a=l.getNodePosition,h=void 0===a?D:a,c=l.delaunay,v=l.setCurrent,f=l.margin,p=void 0===f?L:f,g=l.detectionRadius,m=void 0===g?1/0:g,b=l.isInteractive,k=void 0===b||b,C=l.onMouseEnter,y=l.onMouseMove,w=l.onMouseLeave,R=l.onMouseDown,W=l.onMouseUp,E=l.onClick,x=l.onDoubleClick,S=l.onTouchStart,U=l.onTouchMove,z=l.onTouchEnd,A=l.enableTouchCrosshair,N=void 0!==A&&A,H=l.tooltip,O=l.tooltipPosition,I=void 0===O?T:O,j=l.tooltipAnchor,F=void 0===j?P:j,B=o(null),q=B[0],G=B[1],J=e(null);i((function(){J.current=q}),[J,q]);var K=t((function(n){if(!r.current||0===u.length)return null;var o=d(r.current,n),e=o[0],i=o[1],t=c.find(e,i),l=void 0!==t?u[t]:null;if(l&&m!==1/0){var a=h(l),v=a[0],f=a[1];s(e,i,v+p.left,f+p.top)>m&&(t=null,l=null)}return null===t||null===l?null:[t,l]}),[r,c,u,h,p,m]),Q=M(),V=Q.showTooltipAt,X=Q.showTooltipFromEvent,Y=Q.hideTooltip,Z=n((function(){if(H)return"cursor"===I?function(n,o){X(H(n),o,F)}:function(n){var o=h(n),e=o[0],i=o[1];V(H(n),[e+p.left,i+p.top],F)}}),[V,X,H,I,F,h,p]),$=t((function(n){var o=K(n);if(G(o),null==v||v(o?o[1]:null),o){var e=o[1];null==Z||Z(e,n),null==C||C(o[1],n)}}),[K,G,v,Z,C]),_=t((function(n){var o=K(n);if(G(o),o){var e=o[0],i=o[1];if(null==v||v(i),null==Z||Z(i,n),J.current){var t=J.current,l=t[0],r=t[1];e!==l?null==w||w(r,n):null==y||y(i,n)}else null==C||C(i,n)}else null==v||v(null),null==Y||Y(),J.current&&(null==w||w(J.current[1],n))}),[K,G,v,J,C,y,w,Z,Y]),nn=t((function(n){G(null),null==v||v(null),Y(),w&&J.current&&w(J.current[1],n)}),[G,v,J,Y,w]),on=t((function(n){var o=K(n);G(o),o&&(null==R||R(o[1],n))}),[K,G,R]),en=t((function(n){var o=K(n);G(o),o&&(null==W||W(o[1],n))}),[K,G,W]),tn=t((function(n){var o=K(n);G(o),o&&(null==E||E(o[1],n))}),[K,G,E]),ln=t((function(n){var o=K(n);G(o),o&&(null==x||x(o[1],n))}),[K,G,x]),rn=t((function(n){var o=K(n);N&&(G(o),null==v||v(o?o[1]:null)),o&&(null==S||S(o[1],n))}),[K,G,v,N,S]),un=t((function(n){var o=K(n);N&&(G(o),null==v||v(o?o[1]:null)),o&&(null==U||U(o[1],n))}),[K,G,v,N,U]),an=t((function(n){N&&(G(null),null==v||v(null)),z&&J.current&&z(J.current[1],n)}),[N,G,v,z,J]);return{current:q,handleMouseEnter:k?$:void 0,handleMouseMove:k?_:void 0,handleMouseLeave:k?nn:void 0,handleMouseDown:k?on:void 0,handleMouseUp:k?en:void 0,handleClick:k?tn:void 0,handleDoubleClick:k?ln:void 0,handleTouchStart:k?rn:void 0,handleTouchMove:k?un:void 0,handleTouchEnd:k?an:void 0}},z=function(n){var o=n.elementRef,e=n.nodes,i=n.getNodePosition,t=n.width,l=n.height,r=n.margin,u=void 0===r?L:r,a=n.isInteractive,d=void 0===a||a,s=n.detectionRadius,h=void 0===s?1/0:s,c=n.setCurrent,v=n.onMouseEnter,f=n.onMouseMove,p=n.onMouseLeave,g=n.onMouseDown,M=n.onMouseUp,m=n.onClick,b=n.onDoubleClick,k=n.tooltip,C=n.tooltipPosition,y=void 0===C?T:C,w=n.tooltipAnchor,D=void 0===w?P:w,R=n.debug,W=E({points:e,getNodePosition:i,width:t,height:l,margin:u,debug:void 0!==R&&R}),x=W.delaunay,S=W.voronoi,z=U({elementRef:o,nodes:e,margin:u,setCurrent:c,delaunay:x,detectionRadius:h,isInteractive:d,onMouseEnter:v,onMouseMove:f,onMouseLeave:p,onMouseDown:g,onMouseUp:M,onClick:m,onDoubleClick:b,tooltip:k,tooltipPosition:y,tooltipAnchor:D}),A=z.handleMouseEnter,N=z.handleMouseMove,H=z.handleMouseLeave,O=z.handleMouseDown,I=z.handleMouseUp,j=z.handleClick,F=z.handleDoubleClick;return{delaunay:x,voronoi:S,current:z.current,handleMouseEnter:A,handleMouseMove:N,handleMouseLeave:H,handleMouseDown:O,handleMouseUp:I,handleClick:j,handleDoubleClick:F}},A=["theme"],N=function(n){var o=n.data,e=n.width,i=n.height,t=n.margin,l=n.layers,a=void 0===l?w.layers:l,d=n.xDomain,s=void 0===d?w.xDomain:d,h=n.yDomain,f=void 0===h?w.yDomain:h,p=n.enableLinks,g=void 0===p?w.enableLinks:p,M=n.linkLineWidth,b=void 0===M?w.linkLineWidth:M,k=n.linkLineColor,C=void 0===k?w.linkLineColor:k,y=n.enableCells,D=void 0===y?w.enableCells:y,L=n.cellLineWidth,T=void 0===L?w.cellLineWidth:L,P=n.cellLineColor,R=void 0===P?w.cellLineColor:P,W=n.enablePoints,E=void 0===W?w.enableCells:W,U=n.pointSize,z=void 0===U?w.pointSize:U,A=n.pointColor,N=void 0===A?w.pointColor:A,H=n.role,O=void 0===H?w.role:H,I=n.forwardedRef,j=c(e,i,t),F=j.outerWidth,B=j.outerHeight,q=j.margin,G=j.innerWidth,J=j.innerHeight,K=x({data:o,width:G,height:J,xDomain:s,yDomain:f}),Q=K.points,V=K.delaunay,X=K.voronoi,Y={links:null,cells:null,points:null,bounds:null};g&&a.includes("links")&&(Y.links=m("path",{stroke:C,strokeWidth:b,fill:"none",d:V.render()},"links")),D&&a.includes("cells")&&(Y.cells=m("path",{d:X.render(),fill:"none",stroke:R,strokeWidth:T},"cells")),E&&a.includes("points")&&(Y.points=m("path",{stroke:"none",fill:N,d:V.renderPoints(void 0,z/2)},"points")),a.includes("bounds")&&(Y.bounds=m("path",{fill:"none",stroke:R,strokeWidth:T,d:X.renderBounds()},"bounds"));var Z=S({points:Q,delaunay:V,voronoi:X});return m(v,{width:F,height:B,margin:q,role:O,ref:I,children:a.map((function(n,o){return void 0!==Y[n]?Y[n]:"function"==typeof n?m(r,{children:u(n,Z)},o):null}))})},H=l((function(n,o){var e=n.theme,i=y(n,A);return m(h,{isInteractive:!1,animate:!1,theme:e,children:m(N,C({},i,{forwardedRef:o}))})})),O=["defaultWidth","defaultHeight","onResize","debounceResize"],I=l((function(n,o){var e=n.defaultWidth,i=n.defaultHeight,t=n.onResize,l=n.debounceResize,r=y(n,O);return m(f,{defaultWidth:e,defaultHeight:i,onResize:t,debounceResize:l,children:function(n){var e=n.width,i=n.height;return m(H,C({},r,{width:e,height:i,ref:o}))}})})),j=function(o){var i=o.nodes,t=o.width,l=o.height,r=o.margin,u=void 0===r?L:r,a=o.getNodePosition,d=o.setCurrent,s=o.onMouseEnter,h=o.onMouseMove,c=o.onMouseLeave,v=o.onMouseDown,f=o.onMouseUp,p=o.onClick,g=o.onDoubleClick,M=o.onTouchStart,C=o.onTouchMove,y=o.onTouchEnd,w=o.enableTouchCrosshair,D=void 0!==w&&w,R=o.detectionRadius,W=void 0===R?1/0:R,x=o.tooltip,S=o.tooltipPosition,z=void 0===S?T:S,A=o.tooltipAnchor,N=void 0===A?P:A,H=o.debug,O=e(null),I=E({points:i,getNodePosition:a,width:t,height:l,margin:u,debug:H}),j=I.delaunay,F=I.voronoi,B=U({elementRef:O,nodes:i,delaunay:j,margin:u,detectionRadius:W,setCurrent:d,onMouseEnter:s,onMouseMove:h,onMouseLeave:c,onMouseDown:v,onMouseUp:f,onClick:p,onDoubleClick:g,onTouchStart:M,onTouchMove:C,onTouchEnd:y,enableTouchCrosshair:D,tooltip:x,tooltipPosition:z,tooltipAnchor:N}),q=B.current,G=B.handleMouseEnter,J=B.handleMouseMove,K=B.handleMouseLeave,Q=B.handleMouseDown,V=B.handleMouseUp,X=B.handleClick,Y=B.handleDoubleClick,Z=B.handleTouchStart,$=B.handleTouchMove,_=B.handleTouchEnd,nn=n((function(){if(H&&F)return F.render()}),[H,F]);return b("g",{ref:O,transform:"translate("+-u.left+","+-u.top+")",children:[H&&F&&b(k,{children:[m("path",{d:nn,stroke:"red",strokeWidth:1,opacity:.75}),W<1/0&&m("path",{stroke:"red",strokeWidth:.35,fill:"none",d:j.renderPoints(void 0,W)}),q&&m("path",{fill:"pink",opacity:.35,d:F.renderCell(q[0])})]}),m("rect",{"data-ref":"mesh-interceptor",width:u.left+t+u.right,height:u.top+l+u.bottom,fill:"red",opacity:0,style:{cursor:"auto"},onMouseEnter:G,onMouseMove:J,onMouseLeave:K,onMouseDown:Q,onMouseUp:V,onTouchStart:Z,onTouchMove:$,onTouchEnd:_,onClick:X,onDoubleClick:Y})]})},F=function(n,o){n.save(),n.globalAlpha=.75,n.beginPath(),o.render(n),n.strokeStyle="red",n.lineWidth=1,n.stroke(),n.restore()},B=function(n,o,e){n.save(),n.globalAlpha=.15,n.beginPath(),o.renderPoints(n,e),n.strokeStyle="red",n.lineWidth=1,n.stroke(),n.restore()},q=function(n,o,e){n.save(),n.globalAlpha=.35,n.beginPath(),o.renderCell(e,n),n.fillStyle="pink",n.fill(),n.restore()},G=function(n,o){var e=o.delaunay,i=o.voronoi,t=o.detectionRadius,l=o.index;F(n,i),t<1/0&&B(n,e,t),null!==l&&q(n,i,l)};export{j as Mesh,I as ResponsiveVoronoi,H as Voronoi,W as computeMesh,R as computeMeshPoints,w as defaultVoronoiProps,G as renderDebugToCanvas,B as renderDelaunayPointsToCanvas,q as renderVoronoiCellToCanvas,F as renderVoronoiToCanvas,z as useMesh,U as useMeshEvents,x as useVoronoi,S as useVoronoiLayerContext,E as useVoronoiMesh};
//# sourceMappingURL=nivo-voronoi.mjs.map