คุณสมบัติ fillStyle() ของผ้าใบ HTML ใช้เพื่อกำหนดสี ไล่ระดับสี หรือรูปแบบสำหรับภาพวาด ค่าเริ่มต้นคือ #000000
ต่อไปนี้เป็นไวยากรณ์ -
ctx.fillStyle=color|gradient|pattern;
ด้านบน ค่า ได้แก่ −
- สี: สีเติมของภาพวาด ซึ่งเป็นสี CSS
- การไล่ระดับสี: วัตถุไล่ระดับเชิงเส้นหรือแนวรัศมีเพื่อเติมรูปวาด
- รูปแบบ: รูปแบบวัตถุที่จะเติมภาพวาด
ให้เราดูตัวอย่างการใช้คุณสมบัติ fillStyle() ของผ้าใบ -
ตัวอย่าง
<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="500" height="350" style="border:2px solid orange;">
</canvas>
<script>
var c = document.getElementById("newCanvas");
var ctx = c.getContext("2d");
var newGrad =ctx.createLinearGradient(0, 0, 130, 0);
newGrad.addColorStop(0, "blue");
newGrad.addColorStop(0.8, "green");
ctx.fillStyle = newGrad;
ctx.fillRect(0, 0, 500, 350);
</script>
</body>
</html> ผลลัพธ์
