@@ -51,6 +51,8 @@ import {
5151 this . currentPoint = ctx . currentPoint || new Point ( ) ;
5252 this . miterLimit = ctx . miterLimit || 10.0 ;
5353 this . lastPoint = ctx . lastPoint || new Point ( ) ;
54+ this . lineDashOffset = ctx . lineDashOffset || 0.0 ;
55+ this . lineDash = ctx . lineDash || [ ] ;
5456
5557 this . ignoreClearRect =
5658 typeof ctx . ignoreClearRect === "boolean" ? ctx . ignoreClearRect : true ;
@@ -641,6 +643,32 @@ import {
641643 }
642644 } ) ;
643645
646+ /**
647+ * A float specifying the amount of the line dash offset. The default value is 0.0.
648+ *
649+ * @name lineDashOffset
650+ * @default 0.0
651+ */
652+ Object . defineProperty ( this , "lineDashOffset" , {
653+ get : function ( ) {
654+ return this . ctx . lineDashOffset ;
655+ } ,
656+ set : function ( value ) {
657+ this . ctx . lineDashOffset = value ;
658+ setLineDash . call ( this ) ;
659+ }
660+ } ) ;
661+
662+ Object . defineProperty ( this , "lineDash" , {
663+ get : function ( ) {
664+ return this . ctx . lineDash ;
665+ } ,
666+ set : function ( value ) {
667+ this . ctx . lineDash = value ;
668+ setLineDash . call ( this ) ;
669+ }
670+ } ) ;
671+
644672 // Not HTML API
645673 Object . defineProperty ( this , "ignoreClearRect" , {
646674 get : function ( ) {
@@ -652,6 +680,16 @@ import {
652680 } ) ;
653681 } ;
654682
683+ /**
684+ * Sets the line dash pattern used when stroking lines.
685+ * @name setLineDash
686+ * @function
687+ * @description It uses an array of values that specify alternating lengths of lines and gaps which describe the pattern.
688+ */
689+ Context2D . prototype . setLineDash = function ( dashArray ) {
690+ this . lineDash = dashArray ;
691+ } ;
692+
655693 Context2D . prototype . fill = function ( ) {
656694 pathPreProcess . call ( this , "fill" , false ) ;
657695 } ;
@@ -1105,6 +1143,8 @@ import {
11051143 this . lineCap = this . ctx . lineCap ;
11061144 this . lineWidth = this . ctx . lineWidth ;
11071145 this . lineJoin = this . ctx . lineJoin ;
1146+ this . lineDash = this . ctx . lineDash ;
1147+ this . lineDashOffset = this . ctx . lineDashOffset ;
11081148 }
11091149 } ;
11101150
@@ -2383,4 +2423,32 @@ import {
23832423 Math . round ( maxy - miny )
23842424 ) ;
23852425 } ;
2426+
2427+ var getPrevLineDashValue = function ( lineDash , lineDashOffset ) {
2428+ return JSON . stringify ( {
2429+ lineDash : lineDash ,
2430+ lineDashOffset : lineDashOffset
2431+ } ) ;
2432+ } ;
2433+
2434+ var setLineDash = function ( ) {
2435+ // Avoid unnecessary line dash declarations.
2436+ if (
2437+ ! this . prevLineDash &&
2438+ ! this . ctx . lineDash . length &&
2439+ ! this . ctx . lineDashOffset
2440+ ) {
2441+ return ;
2442+ }
2443+
2444+ // Avoid unnecessary line dash declarations.
2445+ const nextLineDash = getPrevLineDashValue (
2446+ this . ctx . lineDash ,
2447+ this . ctx . lineDashOffset
2448+ ) ;
2449+ if ( this . prevLineDash !== nextLineDash ) {
2450+ this . pdf . setLineDash ( this . ctx . lineDash , this . ctx . lineDashOffset ) ;
2451+ this . prevLineDash = nextLineDash ;
2452+ }
2453+ } ;
23862454} ) ( jsPDF . API ) ;
0 commit comments