Thuộc tính width của phần tử
<canvas width="pixels_val">
Ở trên, pixel_val là chiều rộng được tính bằng pixel. Bây giờ chúng ta hãy xem một ví dụ để triển khai thuộc tính width của phần tử
Ví dụ
<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="400" height="200" style="border:3px dashed yellow">
HTML5 canvas tag isn't supported by your browser.
</canvas>
<script>
var c = document.getElementById("newCanvas");
var context = c.getContext("2d");
context.fillStyle = "#FF5655";
context.fillRect(100, 50, 60, 100);
</script>
</body>
</html> Đầu ra
Trong ví dụ trên, chúng tôi đã tạo canvas và sử dụng JavaScript -
<script>
var c = document.getElementById("newCanvas");
var context = c.getContext("2d");
context.fillStyle = "#FF5655";
context.fillRect(100, 50, 60, 100);
</script> Trước đó, chúng tôi đã đặt id canvas với chiều rộng và chiều cao -
<canvas id="newCanvas" width="400" height="200" style="border:3px dashed yellow"> HTML5 canvas tag isn't supported by your browser. </canvas>