第25章 canvas绘制圆形

本文介绍了一个简单的HTML页面,该页面使用JavaScript来绘制一个紫色的笑脸。笑脸由多个圆形组成,包括脸部、眼睛和嘴巴等特征。通过`<canvas>`元素进行绘图,并利用`JavaScript`的`CanvasRenderingContext2D`接口实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="25.js"></script>
<style type="text/css">
body{ margin:0; padding:0;}
</style>
</head>
<body onLoad="draw('canvas')">
<canvas id="canvas" width='400' height="300"></canvas>
</body>
</html>

 

js

// JavaScript Document
function draw(id){
	var context = document.getElementById(id).getContext('2d');
	context.beginPath();
	context.strokeStyle = "rgb(0,0,0)";
	context.arc(100,100,100,0,2*Math.PI,true);
	context.closePath();
	context.fillStyle = 'rgb(200,0,200)';
	context.fill();    
	
	context.beginPath();
	context.arc(50,75,25,0,2*Math.PI,true);
	context.fillStyle = 'rgb(0,0,0)';
	context.fill();
	
	context.beginPath();
	context.arc(150,75,25,0,2*Math.PI,true);
	context.fillStyle = 'rgb(0,0,0)';
	context.fill();
	
	context.beginPath();
	context.arc(150,75,25,0,2*Math.PI,true);
	context.fillStyle = 'rgb(0,0,0)';
	context.fill();
	
	context.beginPath();
	context.arc(100,125,10,0,2*Math.PI,true);
	context.fillStyle = 'rgb(0,0,0)';
	context.fill();
	
	context.beginPath();
	context.strokeStyle = "rgb(0,0,0)";
	context.lineWidth = 5;
	context.arc(100,150,25,Math.PI/6,5*Math.PI/6,false);
	
	context.stroke();
}

 

效果图:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值