1+ import { loopAccess , rad } from "./util" ;
2+
13export interface Point {
24 // Cartesian coordinates (starting at [0,0] in the bottom left).
35 x : number ;
@@ -50,16 +52,11 @@ export interface RenderOptions {
5052 boundingBox ?: boolean ;
5153}
5254
53- // Safe array access at any index using a modulo operation that will always be positive.
54- const loopAccess = < T > ( arr : T [ ] ) => ( i : number ) : T => {
55- return arr [ ( ( i % arr . length ) + arr . length ) % arr . length ] ;
56- } ;
57-
5855// Translates a point's [x,y] cartesian coordinates into values relative to the viewport.
5956// Translates the angle from degrees to radians and moves the start angle a half rotation.
6057const cleanupPoint = ( point : Point , opt : RenderOptions ) : InternalPoint => {
6158 const handles = point . handles || { angle : 0 , out : 0 , in : 0 } ;
62- handles . angle = Math . PI + ( 2 * Math . PI * handles . angle ) / 360 ;
59+ handles . angle = Math . PI + rad ( handles . angle ) ;
6360 return {
6461 x : point . x ,
6562 y : opt . height - point . y ,
@@ -68,7 +65,7 @@ const cleanupPoint = (point: Point, opt: RenderOptions): InternalPoint => {
6865} ;
6966
7067// Renders a closed shape made up of the input points.
71- const renderClosed = ( p : Point [ ] , opt : RenderOptions ) : string => {
68+ export const renderClosed = ( p : Point [ ] , opt : RenderOptions ) : string => {
7269 const points = p . map ( ( point ) => cleanupPoint ( point , opt ) ) ;
7370
7471 // Compute guides from input point data.
@@ -119,7 +116,7 @@ const renderClosed = (p: Point[], opt: RenderOptions): string => {
119116 if ( opt . boundingBox ) {
120117 guides += `
121118 <rect x="0" y="0" width="${ opt . width } " height="${ opt . height } " fill="none"
122- stroke="${ color } " stroke-width="${ 2 * size } " stroke-dasharray="${ 2 * size } " />` ;
119+ stroke="${ color } " stroke-width="${ 2 * size } " stroke-dasharray="${ 2 * size } " />` ;
123120 }
124121
125122 // Points and handles.
@@ -132,7 +129,7 @@ const renderClosed = (p: Point[], opt: RenderOptions): string => {
132129 <line x1="${ x } " y1="${ y } " x2="${ hands . x1 } " y2="${ hands . y1 } "
133130 stroke-width="${ size } " stroke="${ color } " />
134131 <line x1="${ nextPoint . x } " y1="${ nextPoint . y } " x2="${ hands . x2 } " y2="${ hands . y2 } "
135- stroke-width="${ size } " stroke="${ color } " stroke-dasharray="${ 2 * size } " />
132+ stroke-width="${ size } " stroke="${ color } " stroke-dasharray="${ 2 * size } " />
136133 <circle cx="${ hands . x1 } " cy="${ hands . y1 } " r="${ size } "
137134 fill="${ color } " />
138135 <circle cx="${ hands . x2 } " cy="${ hands . y2 } " r="${ size } "
@@ -142,7 +139,7 @@ const renderClosed = (p: Point[], opt: RenderOptions): string => {
142139 }
143140
144141 const stroke = opt . stroke || ( opt . guides ? "black" : "none" ) ;
145- const strokeWidth = opt . strokeWidth || ( opt . guides ? 1 : 0 ) ;
142+ const strokeWidth = opt . strokeWidth || ( opt . guides ? 1 : 0 ) ;
146143
147144 return `
148145 <svg
@@ -163,22 +160,3 @@ const renderClosed = (p: Point[], opt: RenderOptions): string => {
163160 </svg>
164161 ` . replace ( / \s + / g, " " ) ;
165162} ;
166-
167- console . log (
168- renderClosed (
169- [
170- { x : 700 , y : 200 , handles : { angle : - 135 , out : 80 , in : 80 } } ,
171- { x : 300 , y : 200 , handles : { angle : 135 , out : 80 , in : 80 } } ,
172- { x : 300 , y : 600 , handles : { angle : 45 , out : 80 , in : 80 } } ,
173- { x : 700 , y : 600 , handles : { angle : - 45 , out : 80 , in : 80 } } ,
174- ] ,
175- {
176- width : 1000 ,
177- height : 800 ,
178- fill : "pink" ,
179- stroke : "red" ,
180- strokeWidth : 2 ,
181- guides : true ,
182- } ,
183- ) ,
184- ) ;
0 commit comments