2024-10-15 07:48:12
1、canvas绘制矩形
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>canvas绘制矩形</title>
<script type="text/javascript" src="canvas2.js"></script>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body onload="draw('canvas')">
<canvas id="canvas" width="400" height="300"></canvas>
</body>
</html>
js:
/**
* Created by winson on 2016/9/11.
*/
function draw(id) {
var canvas = document.getElementById(id);//用getElementById获取到canvas对象
var context = canvas.getContext('2d');//取得上下文
context.fillStyle = "green";//绘制背景的颜色
context.strokeStyle = "#fff";//绘制边框的颜色
context.lineWidth = 5; //设置画笔宽度
context.fillRect(0, 0, 400, 300);//绘制
context.strokeRect(50, 50, 180, 120);
}