Is there a way to create lines/curves/shapes with SVG/canvas with gradient fills? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a way to create lines/curves/shapes with SVG/canvas with gradient fills?

In CSS it is easy to create gradients with linear-gradient and radial-gradient but is there a similiar way to create shapes/curves with gradient fills in SVG/Canvas? If yes, then how can I do this?

23rd Mar 2020, 4:06 PM
Jalaj Kumar
Jalaj Kumar - avatar
1 Answer
+ 2
First create your gradient, than assign to a style and draw a shape, for example as follows: let context = canvas.getContext('2d'); let gradient = context.createLinearGradient( 0,0,0,120); gradient.addColorStop(0, '#0000FF'); gradient.addColorStop(0.5, '#00FF00'); gradient.addColorStop(1, '#FF0000'); context.fillStyle = gradient; context.fillRect(30, 25, 350, 200);
23rd Mar 2020, 5:07 PM
JaScript
JaScript - avatar