1、线性渐变:
<style>canvas {width: 500px;height: 300px;border: 1px solid;}</style></head><body><canvas id="can" width="500px" height="300px"></canvas><script>var canvas = document.getElementById("can");var ctx = canvas.getContext("2d");ctx.beginPath();//线性渐变的起始点是坐标系的原点,而不是矩形的原点var bg = ctx.createLinearGradient(0,0,200,0);//线性渐变,设两个点bg.addColorStop(0,"white");//只有0~1bg.addColorStop(0.5,"blue");bg.addColorStop(1,"black");ctx.fillStyle = bg;ctx.fillRect(0,0,200,100);</script></body>
