Canvas记录01
实现基本绘图
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
<style
>
*{
margin
:0;
padding
:0;
}
#canvas
{
background
:yellowgreen
;
}
</style
>
</head
>
<body
>
<canvas id
="canvas" width
=700 height
=600></canvas
>
<script
>
const canvas
= document
.querySelector('#canvas')
canvas
.width
= window
.innerWidth
canvas
.height
= window
.innerHeight
const ctx
= canvas
.getContext('2d')
ctx
.fillStyle
= 'red'
ctx
.fillRect(50,50,400,200)
ctx
.lineWidth
= 40;
ctx
.strokeRect(50,50,400,200)
ctx
.clearRect(50,50,400,200)
</script
>
</body
>
</html
>
实现路径图形
*** 绘制路径图形
* 1直线 lineTo(x,y)
* 2圆弧 arc(X,Y,半径,开始弧度,结束弧度,方向)
* 3切线圆弧 arcTo(X1,Y1,X2,Y2,半径)
* 二次贝塞尔曲线 quadraticCurveTo(cpx1,cpy1,x1,y1)
* 三次贝塞尔曲线 bezierCurveTo(cpx1,cpy1,cpx2,cpy2,x1,y1)
* 矩形 rect(x,y, w,h)**
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
<style
>
*{
margin
:0;
padding
:0;
}
#canvas
{
background
:yellowgreen
;
}
</style
>
</head
>
<body
>
<canvas id
="canvas" width
=700 height
=600></canvas
>
<script
>
const canvas
= document
.querySelector('#canvas')
canvas
.width
= window
.innerWidth
canvas
.height
= window
.innerHeight
const ctx
= canvas
.getContext('2d')
ctx
.lineWidth
= 10
ctx
.strokeStyle
='gray'
ctx
.beginPath()
ctx
.rect(50,50,300,100)
ctx
.rect(50,350,300,100)
ctx
.stroke()
</script
>
</body
>
</html
>
小案例01 — 机器人
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
<style
>
*{
margin
:0;
padding
:0;
}
#canvas
{
background
:yellowgreen
;
}
</style
>
</head
>
<body
>
<canvas id
="canvas" ></canvas
>
<script
>
const canvas
= document
.querySelector('#canvas')
canvas
.width
= window
.innerWidth
canvas
.height
= window
.innerHeight
const ctx
= canvas
.getContext('2d')
ctx
.fillStyle
='red'
ctx
.lineWidth
= 40
ctx
.fillRect(50,250,400,200)
ctx
.strokeRect(50,250,400,200)
ctx
.clearRect(50,300,400,60)
ctx
.beginPath()
ctx
.moveTo(150,400)
ctx
.lineTo(350,400)
ctx
.stroke()
ctx
.beginPath()
ctx
.arc(
150,330,
20,
0,Math
.PI * 2
)
ctx
.fill()
ctx
.beginPath()
ctx
.arc(
350,340,
20,
0,Math
.PI,
true
)
ctx
.fill()
ctx
.beginPath()
ctx
.moveTo(50,50)
ctx
.bezierCurveTo(
150,50,
150,250,
250,250
)
ctx
.stroke()
ctx
.moveTo(450,50)
ctx
.bezierCurveTo(
350,50,
350,250,
250,250
)
ctx
.stroke()
</script
>
</body
>
</html
>
输出图形
小案例02 — 水滴
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
<style
>
*{
margin
:0;
padding
:0;
}
#canvas
{
background
:yellowgreen
;
}
</style
>
</head
>
<body
>
<canvas id
="canvas" ></canvas
>
<script
>
const canvas
= document
.querySelector('#canvas')
canvas
.width
= window
.innerWidth
canvas
.height
= window
.innerHeight
const ctx
= canvas
.getContext('2d')
ctx
.translate(300,300)
ctx
.fillStyle
= 'blue'
ctx
.beginPath()
ctx
.moveTo(0,0)
ctx
.quadraticCurveTo(50,-50,50,-100)
ctx
.arc(0,-100,50,0,Math
.PI,true)
ctx
.quadraticCurveTo(-50,-50,0,0)
ctx
.fill()
</script
>
</body
>
</html
>
输出图形