1 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
我不是 100% 確定你的意思,但如果你想要兩種不同顏色的清晰停止,你可以簡(jiǎn)單地將兩種顏色設(shè)置在相同的停止位置:
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const grad = ctx.createLinearGradient(0,0,0,150);
// implicit from -Infinity "green"
grad.addColorStop(0.33, "green");
grad.addColorStop(0.33, "yellow"); // same stop as previous color
grad.addColorStop(0.66, "yellow");
grad.addColorStop(0.66, "red"); // same stop as previous color
// implicit to +Infinity "red"
ctx.fillStyle = grad;
ctx.fillRect(0,0,300,150);
<canvas></canvas>
添加回答
舉報(bào)