一、绘制圆形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#cav{
border: 1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="cav" width="600px" height="300px"></canvas>
<script>
let cav = document.getElementById('cav');
let c = cav.getContext('2d');
c.beginPath();
c.arc(150,150,100,0,Math.PI*3/2,false)
c.fillStyle = 'blue'
c.fill()
c.closePath();
</script>
</body>
</html>
二、绘制三角形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#cav{
border: 1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="cav" width="600px" height="300px"></canvas>
<script>
let cav = document.getElementById('cav');
let c = cav.getContext('2d');
c.beginPath();
c.moveTo(10,10)
c.lineTo(100,150)
c.lineTo(200,10)
c.lineTo(10,10)
c.strokeStyle = 'red'
c.lineWidth = "5"
c.fillStyle = 'blue'
c.fill();
c.closePath();
c.stroke();
c.beginPath();
c.moveTo(210,10)
c.lineTo(250,150)
c.lineTo(300,10)
c.lineTo(210,10)
c.strokeStyle = 'blue'
c.lineWidth = "5"
c.fillStyle = 'yellow'
c.fill();
c.closePath();
c.stroke();
</script>
</body>
</html>
三、 绘制矩形
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#cav{
border: 1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="cav" width="600px" height="300px"></canvas>
<script>
let cav = document.getElementById('cav');
let c = cav.getContext('2d');
c.fillStyle='red'
c.fillRect(20,20,200,100)
c.lineWidth = '10'
c.strokeStyle = "pink";
c.strokeRect(20,20,200,100)
cav.onmousedown = function(e){
let event = e||window.event;
console.log(event.clientX);
console.log(event.clientY);
c.clearRect(event.clientX,event.clientY,300,200)
}
</script>
</body>
</html>