Skip to content

Commit 5cec222

Browse files
committed
simple use ways with canvas
1 parent f8e22f9 commit 5cec222

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

HTML5Test/canvas/base_use.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>base use</title>
8+
<script src="./common/base_use.js"></script>
9+
</head>
10+
<body>
11+
<canvas id="container"></canvas>
12+
<script>
13+
let canvas = document.querySelector('#container')
14+
let ctx = canvas.getContext('2d')
15+
ctx.fillRect(25,25,100,100)
16+
ctx.clearRect(45,45,60,60)
17+
ctx.strokeRect(50,50,50,50)
18+
</script>
19+
</body>
20+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Basic Path Function</title>
8+
</head>
9+
<body>
10+
<canvas id="container"></canvas>
11+
<script>
12+
let canvas = document.querySelector('#container')
13+
let ctx = canvas.getContext('2d')
14+
/**
15+
* 首先 需要创建路径起始点
16+
* 然后你使用画图命令去画出路径
17+
* 之后你把路径封闭
18+
* 一旦路径生成,可以听过描边或填充路径区域来渲染
19+
*/
20+
// beginPath 新建一条路径
21+
// closePath 闭合路径之后图形绘制命令又重新指向到上下文中
22+
// stroke 使用线条来绘制轮廓
23+
// fill 使用填充来生成图形
24+
25+
// 开始路径 => 描绘路径 => 关闭路径
26+
// 三角形
27+
ctx.beginPath()
28+
ctx.moveTo(75,50)
29+
ctx.lineTo(100,75)
30+
ctx.lineTo(100,25)
31+
ctx.fill()
32+
33+
34+
</script>
35+
</body>
36+
</html>

HTML5Test/canvas/moveTo_function.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>simple moveTo demo</title>
8+
</head>
9+
<body>
10+
<canvas id="container"></canvas>
11+
<script>
12+
let canvas = document.querySelector('#container')
13+
let ctx = canvas.getContext('2d')
14+
ctx.beginPath()
15+
ctx.arc(75 ,75 ,50 ,0 ,Math.PI*2 ,true) //绘制
16+
ctx.moveTo(110,75);
17+
ctx.arc(75,75,35,0,Math.PI,false); // 口(顺时针)
18+
ctx.moveTo(65,65);
19+
ctx.arc(60,65,5,0,Math.PI*2,true); // 左眼
20+
ctx.moveTo(95,65);
21+
ctx.arc(90,65,5,0,Math.PI*2,true); // 右眼
22+
ctx.stroke();
23+
24+
// lineTo 是线
25+
26+
27+
// arc(x, y, radius, startAngle, endAngle, anticlockwise)
28+
// 画一个以(x,y)为圆心的以radius为半径的圆弧(圆),从startAngle开始到endAngle结束,按照anticlockwise给定的方向(默认为顺时针)来生成。
29+
// arcTo(x1, y1, x2, y2, radius)
30+
// 根据给定的控制点和半径画一段圆弧,再以直线连接两个控制点。
31+
</script>
32+
</body>
33+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>simple moveTo demo</title>
8+
</head>
9+
<body>
10+
<canvas id="container"></canvas>
11+
<script>
12+
let canvas = document.querySelector('#container')
13+
let ctx = canvas.getContext('2d')
14+
for(let i = 0 ; i < 4 ; i++){
15+
for(let j = 0 ; j < 3 ; j++){
16+
ctx.beginPath()
17+
let x = 25 + j * 50; //X坐标值
18+
let y = 25 + i * 50; //y坐标值
19+
let radius = 20 //圆弧半径
20+
let startAngle = 0 // 开始点
21+
let endAngle = Math.PI + (Math.PI * j) /2 ;//结束点
22+
let anticlockwise = i % 2 == 0 ? false : true ; //顺时针或逆时针
23+
24+
ctx.arc(x ,y ,radius ,startAngle ,endAngle ,anticlockwise);
25+
if(i > 1){
26+
ctx.fill();
27+
}else{
28+
ctx.stroke();
29+
}
30+
}
31+
}
32+
</script>
33+
</body>
34+
</html>

0 commit comments

Comments
 (0)