Skip to content
Advertisement

Assign a linearGradient to a canvas background color

I have created a gradient, and wish to make it the background color of the canvas, but the canvas doesn’t work with it. the rectangle which I drew to ensure the validity of the gradient works just fine however. What is wrong here, can you simply not make a background color a gradient?

Do I have to resort to drawing a rectangle over the entirety of the canvas instead?

JavaScript

Advertisement

Answer

You are mixing CSS style gradients with canvas gradients, they are two different things:

You can do the same linear gradient trick both ways, draw a rectangle that is the full size of the canvas or apply the gradient via css, below are samples just pick your favorite.

I added a few circles and lines to show something more in the canvas

JavaScript
JavaScript

JavaScript
JavaScript
JavaScript
Advertisement