代码
提交代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>慕课网Wiki</title> <style> #imooc{ border:1px solid #ccc; } </style> </head> <body> <canvas id="imooc">您的浏览器不支持 HTML5 canvas 标签</canvas> <script> const canvas = document.getElementById('imooc'); const ctx = canvas.getContext('2d'); ctx.rect(10,10, 200,100) let lg = ctx.createLinearGradient(0,0, 200,0) // 1. 创建渐变线 lg.addColorStop(0, "#f00") // 2. 设定关键点 lg.addColorStop(0.5, "#fff") // 2. 设定关键点 lg.addColorStop(1, "#000") // 2. 设定关键点 ctx.fillStyle = lg; // 3. 填充应用渐变线 ctx.fill(); </script> </body>
运行结果