<!DOCTYPE html5><html lang="en"><head> <meta charset="UTF-8"> <title>CSS3-Canvas畫布(漸變)</title> <script> window.onload=function () { var canvas=document.getElementById("canvas");//獲取canvas對象 var ctx=canvas.getContext("2d"); //建立二維的繪圖上下文對象 var gra=ctx.createLinearGradient(0,0,200,200);// 建立漸變對象 而且規定漸變填充的方向 //座標(0,0)到(200,200) gra.addColorStop(0,"lightpink"); gra.addColorStop(0.5,"lightblue"); gra.addColorStop(1,"lightgreen"); ctx.fillStyle=gra;//gra漸變對象 一般做爲 fillStyle或者strokeStyle的屬性值 ctx.fillRect(50,50,200,200); //開始座標爲(50,50) 寬度爲200 有填充的矩形 } </script></head><body><h2>Canvas畫布(漸變)</h2><canvas id="canvas" width="500" height="500" style="border:1px solid red "> 瀏覽器不支持canvas</canvas></body></html>