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?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var gradient;
function init() {
canvas.width = window.innerWidth
canvas.height = window.innerHeight
gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, "rgb(255, 255, 255)");
gradient.addColorStop(1, "rgb(0, 0, 0)");
canvas.style.backgroundColor = gradient;
ctx.beginPath();
ctx.rect(20, 20, 1000, 1000);
ctx.fillStyle = gradient;
ctx.fill();
}
window.onload = init();
</script>
</html>