HTML canvas rect() 方法
HTML canvas 的 rect() 方法用于创建矩形。<canvas> 元素允许您使用 JavaScript 在网页上绘制图形。每个 canvas 都有两个元素来描述 canvas 的高和宽,即 height 和 width。
语法如下 −
context.fillRect(p,q,width,height);
上述中,
- p: 矩形左上角的 x 坐标
- q: 矩形左上角的 y 坐标
- width: 矩形的宽度
- height: 矩形的高度
现在我们来看一个示例来实现 canvas 的 rect() 方法 −
示例
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="450" height="350" style="border:2px solid blue;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(100, 60, 200, 200);
ctx.fillStyle = "orange";
ctx.fill();
ctx.beginPath();
ctx.rect(110, 90, 180, 120);
ctx.fillStyle = "yellow";
ctx.fill();
</script>
</body>
</html>输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
JavaScript
PHP