@@ -69,7 +69,7 @@ const COMMANDS = [
6969 'bezierCurveTo' , 'fillText' , 'strokeText' , 'setLineDash' , 'drawImage' ,
7070 'putImageData' , 'clip' , 'save' , 'restore' , 'translate' ,
7171 'rotate' , 'scale' , 'transform' , 'setTransform' , 'resetTransform' ,
72- 'set' , 'clear' , 'sleep' ,
72+ 'set' , 'clear' , 'sleep' , 'fillPolygon' , 'strokePolygon' ,
7373] ;
7474
7575
@@ -361,6 +361,12 @@ class CanvasModel extends DOMWidgetModel {
361361 case 'strokeLine' :
362362 this . strokeLine ( args , buffers ) ;
363363 break ;
364+ case 'fillPolygon' :
365+ this . fillPolygon ( args , buffers ) ;
366+ break ;
367+ case 'strokePolygon' :
368+ this . strokePolygon ( args , buffers ) ;
369+ break ;
364370 case 'fillPath' :
365371 await this . fillPath ( args , buffers ) ;
366372 break ;
@@ -494,6 +500,34 @@ class CanvasModel extends DOMWidgetModel {
494500 this . ctx . closePath ( ) ;
495501 }
496502
503+ protected fillPolygon ( args : any [ ] , buffers : any ) {
504+ this . ctx . beginPath ( ) ;
505+ const points = getArg ( args [ 0 ] , buffers ) ;
506+
507+ // Move to the first point, then create lines between points
508+ this . ctx . moveTo ( points . getItem ( 0 ) , points . getItem ( 1 ) ) ;
509+ for ( let idx = 2 ; idx < points . length ; idx += 2 ) {
510+ this . ctx . lineTo ( points . getItem ( idx ) , points . getItem ( idx + 1 ) ) ;
511+ }
512+
513+ this . ctx . closePath ( ) ;
514+ this . ctx . fill ( ) ;
515+ }
516+
517+ protected strokePolygon ( args : any [ ] , buffers : any ) {
518+ this . ctx . beginPath ( ) ;
519+ const points = getArg ( args [ 0 ] , buffers ) ;
520+
521+ // Move to the first point, then create lines between points
522+ this . ctx . moveTo ( points . getItem ( 0 ) , points . getItem ( 1 ) ) ;
523+ for ( let idx = 2 ; idx < points . length ; idx += 2 ) {
524+ this . ctx . lineTo ( points . getItem ( idx ) , points . getItem ( idx + 1 ) ) ;
525+ }
526+
527+ this . ctx . closePath ( ) ;
528+ this . ctx . stroke ( ) ;
529+ }
530+
497531 protected async fillPath ( args : any [ ] , buffers : any ) {
498532 const [ serializedPath ] = args ;
499533
@@ -668,6 +702,28 @@ class RoughCanvasModel extends CanvasModel {
668702 this . roughCanvas . arc ( x , y , ellipseSize , ellipseSize , start , end , false , this . getRoughStrokeStyle ( ) ) ;
669703 }
670704
705+ protected fillPolygon ( args : any [ ] , buffers : any ) {
706+ const points = getArg ( args [ 0 ] , buffers ) ;
707+
708+ const polygon : [ number , number ] [ ] = [ ] ;
709+ for ( let idx = 0 ; idx < points . length ; idx += 2 ) {
710+ polygon . push ( [ points . getItem ( idx ) , points . getItem ( idx + 1 ) ] ) ;
711+ }
712+
713+ this . roughCanvas . polygon ( polygon , this . getRoughFillStyle ( ) ) ;
714+ }
715+
716+ protected strokePolygon ( args : any [ ] , buffers : any ) {
717+ const points = getArg ( args [ 0 ] , buffers ) ;
718+
719+ const polygon : [ number , number ] [ ] = [ ] ;
720+ for ( let idx = 0 ; idx < points . length ; idx += 2 ) {
721+ polygon . push ( [ points . getItem ( idx ) , points . getItem ( idx + 1 ) ] ) ;
722+ }
723+
724+ this . roughCanvas . polygon ( polygon , this . getRoughStrokeStyle ( ) ) ;
725+ }
726+
671727 protected async setAttr ( attr : number , value : any ) {
672728 if ( RoughCanvasModel . ROUGH_ATTRS [ attr ] ) {
673729 ( this as any ) [ RoughCanvasModel . ROUGH_ATTRS [ attr ] ] = value ;
0 commit comments