Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

HTML canvas strokeStyle Property


คุณสมบัติ strokeStyle ใน HTML canvas ใช้ตั้งค่าสี gradient หรือ pattern สำหรับ stroke องค์ประกอบ <canvas> อนุญาตให้คุณวาดกราฟิกบนหน้าเว็บด้วย JavaScript ทุก canvas มี 2 องค์ประกอบอธิบายความสูงและความกว้าง คือ height และ width

Syntax ดังนี้:

context.strokeStyle = color | gradient | pattern;

ค่าด้านบนรวมถึง:

  • color — สี stroke ของการวาด
  • gradient — Linear หรือ radial gradient object สร้าง gradient stroke
  • pattern — Pattern object สร้าง pattern stroke

มาดูตัวอย่างการใช้ strokeStyle property ของ canvas:

ตัวอย่าง

<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="550" height="400" style="border: 2px solid orange;"></canvas>
<script>
  var c = document.getElementById("newCanvas");
  var ctx = c.getContext("2d");
  var grad = ctx.createLinearGradient(150, 0, 280, 0);
  grad.addColorStop("0", "orange");
  grad.addColorStop("0.5", "blue");
  grad.addColorStop("1.0", "green");
  ctx.strokeStyle = grad;
  ctx.lineWidth = 20;
  ctx.strokeRect(150, 120, 240, 100);
</script>
</body>
</html>

ผลลัพธ์

HTML canvas strokeStyle Property