canvas設置漸變
阿新 • • 發佈:2019-02-09
hit var alt creat dial radi rec rgb document
canvas設置漸變
方法
createLinearGradient(x1, y1, x2, y2) 線性漸變 createRadialGradient(x1, y1, r1, x2, y2, r2) 輻射漸變
線性漸變
const canvas = document.getElementById(‘canvas‘); const ctx = canvas.getContext(‘2d‘); var lingrad = ctx.createLinearGradient(0, 0, 0, 150); lingrad.addColorStop(0, ‘orange‘); lingrad.addColorStop(0.5, ‘red‘); lingrad.addColorStop(1, ‘pink‘); ctx.fillStyle = lingrad; ctx.fillRect(10, 10, 130, 130);
徑向漸變
var radgrad = ctx.createRadialGradient(90, 90, 20, 100, 100, 50); radgrad.addColorStop(0, ‘white‘); radgrad.addColorStop(0.6, ‘rgba(255, 165, 0, 0.8)‘); radgrad.addColorStop(0.8, ‘rgba(255, 165, 0, 0.3)‘); radgrad.addColorStop(1, ‘rgba(255, 165, 0, 0)‘); ctx.fillStyle = radgrad; ctx.fillRect(0, 0, 150, 150);
canvas設置漸變