-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas长方形.html
49 lines (44 loc) · 1.41 KB
/
canvas长方形.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
data: [5, 24, 36, 4, 10, 20, 36,10, 21, 20, 36,10, 24]
data: [5, 24, 36, 4, 10, 20, 36,10, 21, 20, 36,10, 24]
data: [5, 24, 36, 4, 10, 20, 36,10, 21, 20, 36,10, 24]
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>封装绘制矩形方法</title>
</head>
<body>
<div id="canvas-warp">
<canvas id="canvas" style="border: 1px solid #aaaaaa; display: block; margin: 50px auto;">
你的浏览器居然不支持Canvas?!赶快换一个吧!!
</canvas>
</div>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 600;
var context = canvas.getContext("2d");
drawRect(context, 150, 50, 50, 50, "red", 5, "blue");
drawRect(context, 250, 50, 50, 50, "green", 5, "red");
drawRect(context, 350, 50, 50, 50, "yellow", 15, "black");
}
function drawRect(cxt, x, y, width, height, fillColor, borderWidth, borderColor){
cxt.beginPath();
cxt.moveTo(x, y);
cxt.lineTo(x + width, y);
cxt.lineTo(x + width, y + height);
cxt.lineTo(x, y + height);
cxt.lineTo(x, y);
cxt.closePath();
cxt.lineWidth = borderWidth;
cxt.strokeStyle = borderColor;
cxt.fillStyle = fillColor;
cxt.fill();
cxt.stroke();
}
</script>
</body>
</html>
</body>
</html>