@@ -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,33 @@ 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+ // Not HTML API
663+ Object . defineProperty ( this , "lineDash" , {
664+ get : function ( ) {
665+ return this . ctx . lineDash ;
666+ } ,
667+ set : function ( value ) {
668+ this . ctx . lineDash = value ;
669+ setLineDash . call ( this ) ;
670+ }
671+ } ) ;
672+
644673 // Not HTML API
645674 Object . defineProperty ( this , "ignoreClearRect" , {
646675 get : function ( ) {
@@ -652,6 +681,32 @@ import {
652681 } ) ;
653682 } ;
654683
684+ /**
685+ * Sets the line dash pattern used when stroking lines.
686+ * @name setLineDash
687+ * @function
688+ * @description It uses an array of values that specify alternating lengths of lines and gaps which describe the pattern.
689+ */
690+ Context2D . prototype . setLineDash = function ( dashArray ) {
691+ this . lineDash = dashArray ;
692+ } ;
693+
694+ /**
695+ * gets the current line dash pattern.
696+ * @name getLineDash
697+ * @function
698+ * @returns {Array } An Array of numbers that specify distances to alternately draw a line and a gap (in coordinate space units). If the number, when setting the elements, is odd, the elements of the array get copied and concatenated. For example, setting the line dash to [5, 15, 25] will result in getting back [5, 15, 25, 5, 15, 25].
699+ */
700+ Context2D . prototype . getLineDash = function ( ) {
701+ if ( this . lineDash . length % 2 ) {
702+ // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getLineDash#return_value
703+ return this . lineDash . concat ( this . lineDash ) ;
704+ } else {
705+ // The copied value is returned to prevent contamination from outside.
706+ return this . lineDash . slice ( ) ;
707+ }
708+ } ;
709+
655710 Context2D . prototype . fill = function ( ) {
656711 pathPreProcess . call ( this , "fill" , false ) ;
657712 } ;
@@ -1105,6 +1160,8 @@ import {
11051160 this . lineCap = this . ctx . lineCap ;
11061161 this . lineWidth = this . ctx . lineWidth ;
11071162 this . lineJoin = this . ctx . lineJoin ;
1163+ this . lineDash = this . ctx . lineDash ;
1164+ this . lineDashOffset = this . ctx . lineDashOffset ;
11081165 }
11091166 } ;
11101167
@@ -2383,4 +2440,32 @@ import {
23832440 Math . round ( maxy - miny )
23842441 ) ;
23852442 } ;
2443+
2444+ var getPrevLineDashValue = function ( lineDash , lineDashOffset ) {
2445+ return JSON . stringify ( {
2446+ lineDash : lineDash ,
2447+ lineDashOffset : lineDashOffset
2448+ } ) ;
2449+ } ;
2450+
2451+ var setLineDash = function ( ) {
2452+ // Avoid unnecessary line dash declarations.
2453+ if (
2454+ ! this . prevLineDash &&
2455+ ! this . ctx . lineDash . length &&
2456+ ! this . ctx . lineDashOffset
2457+ ) {
2458+ return ;
2459+ }
2460+
2461+ // Avoid unnecessary line dash declarations.
2462+ const nextLineDash = getPrevLineDashValue (
2463+ this . ctx . lineDash ,
2464+ this . ctx . lineDashOffset
2465+ ) ;
2466+ if ( this . prevLineDash !== nextLineDash ) {
2467+ this . pdf . setLineDash ( this . ctx . lineDash , this . ctx . lineDashOffset ) ;
2468+ this . prevLineDash = nextLineDash ;
2469+ }
2470+ } ;
23862471} ) ( jsPDF . API ) ;
0 commit comments