@@ -544,7 +544,7 @@ class CanvasConstructor {
544544
545545 /**
546546 * Set a color for the canvas' context.
547- * @param {string } color A canvas' color resolvable.
547+ * @param {string|CanvasGradient } color A canvas' color resolvable.
548548 * @returns {CanvasConstructor }
549549 * @chainable
550550 * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle
@@ -634,13 +634,34 @@ class CanvasConstructor {
634634 * @param {number } y0 The y axis of the coordinate of the start point.
635635 * @param {number } x1 The x axis of the coordinate of the end point.
636636 * @param {number } y1 The y axis of the coordinate of the end point.
637+ * @param {GradientStep[] } [steps=[]] The steps.
638+ * @returns {CanvasGradient }
639+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient
640+ */
641+ createLinearGradient ( x0 , y0 , x1 , y1 , steps = [ ] ) {
642+ const gradient = this . context . createLinearGradient ( x0 , y0 , x1 , y1 ) ;
643+ for ( let i = 0 ; i < steps . length ; i ++ )
644+ gradient . addColorStop ( steps [ i ] . position , steps [ i ] . color ) ;
645+
646+ return gradient ;
647+ }
648+
649+ /**
650+ * Creates a gradient along the line given by the coordinates represented by the parameters.
651+ * The coordinates are global, the second point does not rely on the position of the first and vice versa. This
652+ * method is chainable and calls setColor after creating the gradient.
653+ * @param {number } x0 The x axis of the coordinate of the start point.
654+ * @param {number } y0 The y axis of the coordinate of the start point.
655+ * @param {number } x1 The x axis of the coordinate of the end point.
656+ * @param {number } y1 The y axis of the coordinate of the end point.
657+ * @param {GradientStep[] } steps The steps.
637658 * @returns {CanvasConstructor }
638659 * @chainable
639660 * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient
640661 */
641- createLinearGradient ( x0 , y0 , x1 , y1 ) {
642- this . context . createLinearGradient ( x0 , y0 , x1 , y1 ) ;
643- return this ;
662+ printLinearGradient ( x0 , y0 , x1 , y1 , steps ) {
663+ const gradient = this . createLinearGradient ( x0 , y0 , x1 , y1 , steps ) ;
664+ return this . setColor ( gradient ) ;
644665 }
645666
646667 /**
@@ -651,13 +672,35 @@ class CanvasConstructor {
651672 * @param {number } x1 The x axis of the coordinate of the end circle.
652673 * @param {number } y1 The y axis of the coordinate of the end circle.
653674 * @param {number } r1 The radius of the end circle.
675+ * @param {GradientStep[] } [steps=[]] The steps.
676+ * @returns {CanvasGradient }
677+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createRadialGradient
678+ */
679+ createRadialGradient ( x0 , y0 , r0 , x1 , y1 , r1 , steps = [ ] ) {
680+ const gradient = this . context . createRadialGradient ( x0 , y0 , r0 , x1 , y1 , r1 ) ;
681+ for ( let i = 0 ; i < steps . length ; i ++ )
682+ gradient . addColorStop ( steps [ i ] . position , steps [ i ] . color ) ;
683+
684+ return gradient ;
685+ }
686+
687+ /**
688+ * Creates a radial gradient given by the coordinates of the two circles represented by the parameters. This
689+ * method is chainable and calls setColor after creating the gradient.
690+ * @param {number } x0 The x axis of the coordinate of the start circle.
691+ * @param {number } y0 The y axis of the coordinate of the start circle.
692+ * @param {number } r0 The radius of the start circle.
693+ * @param {number } x1 The x axis of the coordinate of the end circle.
694+ * @param {number } y1 The y axis of the coordinate of the end circle.
695+ * @param {number } r1 The radius of the end circle.
696+ * @param {GradientStep[] } steps The steps.
654697 * @returns {CanvasConstructor }
655698 * @chainable
656699 * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createRadialGradient
657700 */
658- createRadialGradient ( x0 , y0 , r0 , x1 , y1 , r1 ) {
659- this . context . createRadialGradient ( x0 , y0 , r0 , x1 , y1 , r1 ) ;
660- return this ;
701+ printRadialGradient ( x0 , y0 , r0 , x1 , y1 , r1 , steps ) {
702+ const gradient = this . createRadialGradient ( x0 , y0 , r0 , x1 , y1 , r1 , steps ) ;
703+ return this . setColor ( gradient ) ;
661704 }
662705
663706 /**
@@ -1037,6 +1080,17 @@ class CanvasConstructor {
10371080 return this ;
10381081 }
10391082
1083+ /**
1084+ * @typedef {object } GradientStep
1085+ * @property {number } position Position of the step.
1086+ * @property {string } color A colour resolvable.
1087+ */
1088+
1089+ /**
1090+ * @typedef {object } CanvasGradient
1091+ * @property {Function } addColorStop Position of the step.
1092+ */
1093+
10401094}
10411095
10421096module . exports = CanvasConstructor ;
0 commit comments