Canvas学习

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas绘图</title>
</head>
<body>
<canvas id="canvas" style="border:1px solid #aaa display:block margin:50px auto;">
	当前浏览器不支持Canvas,请更换浏览器后再试。//这句话在浏览器不支持时出现!
</canvas>
	<script type="text/javascript">
	var tangram=[
	    {p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"#caff67"},     
	    {p:[{x:0,y:0},{x:400,y:400},{x:0,y:800}],color:"#67becf"}, 
	    {p:[{x:800,y:0},{x:800,y:400},{x:600,y:600},{x:600,y:200}],color:"#ef3d61"}, 
	    {p:[{x:600,y:200},{x:600,y:600},{x:400,y:400}],color:"#f9f51a"}, 
	    {p:[{x:400,y:400},{x:600,y:600},{x:400,y:800},{x:200,y:600}],color:"#a594c0"}, 
	    {p:[{x:200,y:600},{x:400,y:800},{x:0,y:800}],color:"#fa8ecc"}, 
	    {p:[{x:800,y:400},{x:800,y:800},{x:400,y:800}],color:"#f6ca29"}
		]
	window.onload=function(){
		var canvas = document.getElementById("canvas");
		canvas.width=800;
		canvas.height=800;
		var context=canvas.getContext("2d");
		for(var i=0;i<tangram.length;i++){
			draw(tangram[i],context)
		}
	}
	
	function draw(piece,context){
		//beginPath、closePath来标注路径的范围
		context.beginPath();
		context.moveTo(piece.p[0].x,piece.p[0].y);
		for(var i=0;i<piece.p.length;i++){
			context.lineTo(piece.p[i].x,piece.p[i].y);
		}
		context.closePath();
		context.fillStyle = piece.color;
		context.fill();//为封闭图形填充颜色
		
		context.strokeStyle="black";//设置外边框
		context.lineWidth=3;
		context.stroke();
	}

	</script>
</body>

</html>


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>DrawArc</title>

</head>

<body>

<canvas id="canvas" style="border:1px solid #aaa display:block margin:50px auto;">

当前浏览器不支持Canvas,请更换浏览器后再试。//这句话在浏览器不支持时出现!

</canvas>

<script type="text/javascript">


window.onload=function(){

var canvas=document.getElementById("canvas");

canvas.width=1024;

canvas.height=768;

var context = canvas.getContext("2d");

context.lineWidth=5;

context.strokeStyle="#005588";

for(var i=0;i<10;i++){

context.beginPath();

context.arc(50+i*100,60,40,0,2*Math.PI*(i+1)/10);

context.closePath();

//如果路径没有封闭的话会自动封闭路径(头尾相连

context.stroke();

}

for(var i=0;i<10;i++){

context.beginPath();

context.arc(50+i*100,180,40,0,2*Math.PI*(i+1)/10);

//context.closePath();

//遇到beginPath知道要重新规划路径

context.stroke();

}

for(var i=0;i<10;i++){

context.beginPath();

context.arc(50+i*100,300,40,0,2*Math.PI*(i+1)/10,true);

context.closePath();

//遇到beginPath知道要重新规划路径

context.stroke();

}

for(var i=0;i<10;i++){

context.beginPath();

context.arc(50+i*100,420,40,0,2*Math.PI*(i+1)/10,true);

//context.closePath();

//遇到beginPath知道要重新规划路径

context.stroke();

}

context.fillStyle="#005588"

for(var i=0;i<10;i++){

context.beginPath();

context.arc(50+i*100,540,40,0,2*Math.PI*(i+1)/10);

context.closePath();

context.fill();

}

for(var i=0;i<10;i++){

context.beginPath();

context.arc(50+i*100,660,40,0,2*Math.PI*(i+1)/10);

context.fill();

//fill填充是自动收尾相连

}

}

/**

window.onload=function(){

var canvas=document.getElementById("canvas");

canvas.width=1024;

canvas.height=768;

var context = canvas.getContext("2d");

context.lineWidth=5;

context.strokeStyle="#005588"

context.arc(300,300,200,0,1.5*Math.PI);

context.stroke();

}*/

</script>

</body>

</html>


你可能感兴趣的:(canvas,html5,前端开发)