HTML
<canvas> 元素的 width 属性用于设置 canvas 的宽度(像素)。以下是语法 -
<canvas width="pixels_val">
其中,pixels_val 是以像素为单位的宽度。下面我们来看一个实现 <canvas> 元素 width 属性的示例 -
示例
<!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>输出

在上例中,我们创建了一个 canvas 并使用了 JavaScript -
<script>
var c = document.getElementById("newCanvas");
var context = c.getContext("2d");
context.fillStyle = "#FF5655";
context.fillRect(100, 50, 60, 100);
</script>在此之前,我们已经设置了 canvas id 以及宽度和高度 -
<canvas id="newCanvas" width="400" height="200" style="border:3px dashed yellow"> HTML5 canvas tag isn't supported by your browser. </canvas>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP