<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>渐变</title>
<style>
* {
margin: 0;
padding: 0;
}

ul {
list-style: none;
}

canvas {
border: solid 1px #000;
display: block;
margin: 100px auto;
}
</style>
</head>
<body>
<canvas></canvas>
<script>
var canvas = document.querySelector('canvas');

canvas.width = 600;
canvas.height = 400;

//获取画笔
var ctx = canvas.getContext('2d');

//线性渐变
// var gradient = ctx.createLinearGradient(0,0,canvas.width,canvas.height);
var gradient = ctx.createLinearGradient(100,100,500,300);

//设置颜色
gradient.addColorStop(0, 'red');
gradient.addColorStop(0.5, 'yellow');
gradient.addColorStop(1, 'blue');

//修改填充样式
ctx.fillStyle = gradient;
//
ctx.fillRect(0,0,canvas.width, canvas.height);

</script>
</body>
</html>