canvas基础一

一些关于画布的基础知识,一些小的效果在以后会写一些
我写在一起了,可以分别来练一练
在这里插入图片描述

<!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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值