@@ -58,6 +58,7 @@ function newEvyGo() {
5858 // advanced canvas
5959 poly,
6060 ellipse,
61+ curve,
6162 stroke,
6263 fill,
6364 dash,
@@ -99,6 +100,7 @@ function needsCanvas(f) {
99100 f . clear ||
100101 f . poly ||
101102 f . ellipse ||
103+ f . curve ||
102104 f . stroke ||
103105 f . fill ||
104106 f . dash ||
@@ -562,6 +564,71 @@ function ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle) {
562564 stroke && ctx . stroke ( )
563565}
564566
567+ // curve is exported to evy go/wasm.
568+ // curve draws connected curve segments encoded as string
569+ // representing 2 dimensional array, e.g.:
570+ // "1 2 3 4,5 6" => [[1,2,3,4], [5,6]]
571+ // the curve segments here are 1 2 3 4 and 5 6
572+ //
573+ // curve segments are interpreted differently depending on their
574+ // **length**:
575+ // 2: endX, endY - Line from current position end position x, y
576+ // 4: controlX, controlY, endX, endY: quadratic bezier curve, see
577+ // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/quadraticCurveTo
578+ // 5: control1X, control1Y, control2X, control2Y, radius: arcTo, see
579+ // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arcTo
580+ // 6: control1X, control1Y, control2X, control2Y, endX, endY: bezierCurveTo, see
581+ // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/bezierCurveTo
582+ //
583+ // This notation is very dense and is intended as escape hatch for advanced graphics.
584+ function curve ( ptr , len ) {
585+ const s = memToString ( ptr , len )
586+ // parse "1 2 3 4,5 6" into [[1,2,3,4], [5,6]]:
587+ const curves = s . split ( "," ) . map ( ( s ) => s . split ( " " ) . map ( Number ) )
588+
589+ const { x, y, ctx, fill, stroke } = canvas
590+
591+ ctx . beginPath ( )
592+ ctx . moveTo ( x , y )
593+
594+ for ( const curve of courves ) {
595+ switch ( curve . length ) {
596+ case 2 :
597+ ctx . lineTo ( transformX ( curve [ 0 ] ) , transformY ( curve [ 1 ] ) )
598+ break
599+ case 4 :
600+ ctx . quadraticCurveTo (
601+ transformX ( curve [ 0 ] ) , // controlPoint.x
602+ transformY ( curve [ 1 ] ) , // controlPoint.y
603+ transformX ( curve [ 2 ] ) , // endPoint.x
604+ transformY ( curve [ 3 ] ) // endPoint.y
605+ )
606+ break
607+ case 5 :
608+ ctx . arcTo (
609+ transformX ( curve [ 0 ] ) , // controlPoint1.x
610+ transformY ( curve [ 1 ] ) , // controlPoint1.y
611+ transformX ( curve [ 2 ] ) , // controlPoint2.x
612+ transformY ( curve [ 3 ] ) , // controlPoint1.y
613+ transformX ( curve [ 4 ] ) // radius
614+ )
615+ break
616+ case 6 :
617+ ctx . bezierCurveTo (
618+ transformX ( curve [ 0 ] ) , // controlPoint1.x
619+ transformY ( curve [ 1 ] ) , // controlPoint1.y
620+ transformX ( curve [ 2 ] ) , // controlPoint2.x
621+ transformY ( curve [ 3 ] ) , // controlPoint1.y
622+ transformX ( curve [ 4 ] ) , // endPoint.x
623+ transformY ( curve [ 5 ] ) // endPoint.y
624+ )
625+ break
626+ }
627+ }
628+ fill && ctx . fill ( )
629+ stroke && ctx . stroke ( )
630+ }
631+
565632// stroke is exported to evy go/wasm.
566633function stroke ( ptr , len ) {
567634 const s = memToString ( ptr , len )
0 commit comments