代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas绘制五星红旗</title>
</head>
<body>
<canvas id="canvas" width="600" height="400"></canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// 绘制背景
var width=canvas.width;
var height=width*2/3;
ctx.fillStyle="red";
ctx.fillRect(0,0,width,height); var maxR=0.15,minR=0.05; //0.15大五角星的半径,0.05小五角星的半径
var maxX=0.25,maxY=0.25; //大五角星的位置
var minX=[0.50,0.60,0.60,0.50]; //小五角星的X坐标
var minY=[0.10,0.20,0.35,0.45]; //小五角星的Y坐标
var ox=height*maxX,oy=height*maxY; //大五角星的中心坐标
drawStar(ctx,ox,oy,height*maxR,"#ff0",0); //绘制五角星
for (var idx = 0; idx < 4; idx++) {
var sx = minX[idx] * height, sy = minY[idx] * height;
var theta = Math.atan((oy - sy)/(ox - sx));
drawStar(ctx,sx, sy, height * minR, "#ff0",-Math.PI/2+theta);
} /*五角星的坐标为(sx,sy),半径为radius,rotate为0时一个顶点在对称轴上*/
/*rotate:绕对称轴旋转rotate弧度*/
function drawStar(ctx,sx,sy,radius,color,rotate){
ctx.save();
ctx.fillStyle=color;
ctx.translate(sx,sy); //移动坐标原点
ctx.rotate(Math.PI+rotate); //Math.PI等于圆周率3.14
ctx.beginPath();
//360度分成5份,2/5*PI,但底下是PI/5*4
var dig=Math.PI/5*4;
for(var i=0;i<5;i++){ //画五角星的五条长边
var x=Math.sin(i*dig); //点的x坐标
var y=Math.cos(i*dig); //点的y坐标
ctx.lineTo(x*radius,y*radius);
}
ctx.closePath();
ctx.strokeStyle = "red"
ctx.stroke();
ctx.fill();
ctx.restore();
}
</script>
</body>
</html>

效果

http://www.360doc.com/content/16/0809/10/10978981_581855158.shtml

最新文章

  1. c#之第三课
  2. [LeetCode] Serialize and Deserialize Binary Tree
  3. Linux文件打包压缩、解压缩、备份命令使用方法(转载)
  4. 学习BFC
  5. Android监听SD卡文件变化
  6. localstorage本地存储
  7. OC - 26.CAAnimationGroup
  8. iOS指纹识别
  9. Android Studio入门到精通
  10. Xcode的小标记旁边的文件的名称的作用
  11. Visual Studio 2010/2013 查看DLL接口(函数)
  12. C#中的系统时间获取问题
  13. ListView使用的时候遇到的一些问题
  14. getMemory的经典例子
  15. vue里v-for下的key的作用
  16. 用IDEA时,类/方法提示&quot;class/method **** is never used&quot;
  17. Java -- JDBC 学习--通过 ResultSet 执行查询操作
  18. 【Social listening实操】从社交媒体传播和文本挖掘角度解读《欢乐颂2》
  19. Django的路由层详情
  20. Fiddler工具使用介绍二

热门文章

  1. Pku1236 Network of Schools
  2. SourceGenerator入门指北
  3. three.js cannon.js物理引擎地形生成器和使用指针锁定控件
  4. Ansible自动化运维工具的使用
  5. 找出10000内的素数 CSP
  6. 阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性公网IP是独立的公网IP资源,可以绑定到阿里云专有网络VPC类型的ECS、NAT网关、私网负载均衡SLB上,并可以动态解绑,实现公网IP和ECS、NAT网关、SLB的解耦,满足灵活管理的要求。阿里云弹性公网IP那些事 阿里云云栖号 6月1日 弹性络VPC类型的E
  7. 实用 nginx.conf 用法大全
  8. 丢包 ICMP
  9. 回归(Regression)
  10. Oracle数据库查询锁表及解锁