一些关于画布的基础知识,一些小的效果在以后会写一些
我写在一起了,可以分别来练一练
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>canvas基础知识掌握</title>
<style type="text/css">
body {
background-color: red;
}
* {
margin: 0px;
padding: 0px;
}
#can {
background-color: black;
}
</style>
</head>
<body>
<canvas id="can"></canvas>
<script type="text/javascript">
var canvas = document.getElementById('can')
var c = canvas.getContext('2d') //通过这句话,我们获取了画布的绘图环境
//设置画布的宽和高和背景颜色
canvas.width = window.innerWidth
canvas.height = window.innerHeight
canvas.style.backgroundColor = 'black'
//一:画线
//1,普通实线段c.moveTo() c.lineTo()
c.beginPath()
c.moveTo(100, 100)
c.lineTo(400, 400)
c.lineTo(500, 234)
c.lineTo(133, 400)
c.lineTo(567, 711)
c.strokeStyle = 'white'
c.closePath()
c.stroke()
//2,虚线 setlineDash()
c.beginPath()
c.moveTo(200, 200)
c.lineTo(250, 250)
c.lineWidth = '5'
c.lineCap = 'round'
c.setLineDash([2, 10]) //里面传入一个数组,数组第一位是虚线颗粒的长度
//第二位是虚线颗粒的距离
c.strokeStyle = 'red'
c.stroke()
c.closePath()
//二:画矩形
//1,实心矩形c.fillRect()
c.beginPath()
c.fillStyle = 'navajowhite'
c.fillRect(0, 0, 30, 20)
c.closePath()
//2,画空心
// c.beginPath()
c.strokeStyle = 'white'
c.strokeRect(50,50,100,200)
// c.closePath()
//三:文本
c.fillStyle = 'white'
c.font = '30px 微软雅黑'
c.fillText('dajfak',200,300)
//四:画弧
c.arc(600,600,100,0,2*Math.PI,true)
c.stroke()
//路径的起始和结束
c.beginPath()
c.closePath()
</script>
</body>
</html>