陪伴而非守候
2024-01-18 14:41:22
使用 ,createLinearGradient我們可以根據 中的停止值創建顏色漸變和顏色混合addColorStop。var gradient = ctx.createLinearGradient(0, 0, 0, 400);
gradient.addColorStop(0, 'blue');
gradient.addColorStop(1, 'red');我們是否可以避免顏色混合并根據停止值具有不同的顏色而不使用混合或漸變。createLinearGradient也許這對于任何其他 API都是不可能的?fillRect我知道我們可以使用兩個不同的和來創建它fillStyle。
1 回答

達令說
TA貢獻1821條經驗 獲得超6個贊
我不是 100% 確定你的意思,但如果你想要兩種不同顏色的清晰停止,你可以簡單地將兩種顏色設置在相同的停止位置:
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>
添加回答
舉報
0/150
提交
取消