Html5+JavaScript 在Canvas上绘制五星红旗,具体思路如下图所示:

绘制思路在上图中已有说明,具体代码如下:

 <script type="text/javascript">

         //绘制五角星,其中一点垂直向上,相隔的点相连,即可绘制。
function drawStar(ctx,starCenterX,starCenterY,starRadius) {
var aX = starCenterX;
var aY = starCenterY - starRadius;
var bX = starCenterX - Math.cos(18 * Math.PI / 180) * starRadius;
var bY = starCenterY - Math.sin(18 * Math.PI / 180) * starRadius;
var cX = starCenterX - Math.cos(54 * Math.PI / 180) * starRadius;
var cY = starCenterY + Math.sin(54 * Math.PI / 180) * starRadius;
var dX = starCenterX + Math.cos(54 * Math.PI / 180) * starRadius;
var dY = starCenterY + Math.sin(54 * Math.PI / 180) * starRadius;
var eX = starCenterX + Math.cos(18 * Math.PI / 180) * starRadius;
var eY = starCenterY - Math.sin(18 * Math.PI / 180) * starRadius;
ctx.beginPath();
ctx.fillStyle = "yellow";
ctx.moveTo(aX, aY);
ctx.lineTo(cX, cY);
ctx.lineTo(eX, eY);
ctx.lineTo(bX, bY);
ctx.lineTo(dX, dY);
ctx.lineTo(aX, aY);
ctx.fill();
ctx.closePath();
} window.onload = function () {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
var width = 300*1.5;
var height = 200*1.5;
var sX = 50; //其实坐标
var sY = 50; //其实坐标
var step = 10*1.5;
//绘制矩形
ctx.beginPath();
ctx.lineWidth = 1;
ctx.fillStyle = "red";
ctx.fillRect(sX, sY, width, height);
ctx.closePath();
//绘制大五角星
var bigStarCenterX = sX + 5 * step;
var bigStarCenterY = sY + 5 * step;
var bigStarRadius = (height * 3 / 10) / 2; //外接圆直径为旗高3/10,半径要再除以2
drawStar(ctx, bigStarCenterX, bigStarCenterY, bigStarRadius);
//绘制小五角星
var smallStarRadius = (height * 1 / 10) / 2; //外接圆直径为旗高1/10,半径要再除以2 var smallStarCenterX_1 = sX + 10 * step;
var smallStarCenterY_1 = sY + 2 * step;
drawStar(ctx, smallStarCenterX_1, smallStarCenterY_1, smallStarRadius);
var smallStarCenterX_2 = sX + 12 * step;
var smallStarCenterY_2 = sY + 4 * step;
drawStar(ctx, smallStarCenterX_2, smallStarCenterY_2, smallStarRadius);
var smallStarCenterX_3 = sX + 12 * step;
var smallStarCenterY_3 = sY + 7 * step;
drawStar(ctx, smallStarCenterX_3, smallStarCenterY_3, smallStarRadius);
var smallStarCenterX_4 = sX + 10 * step;
var smallStarCenterY_4 = sY + 9 * step;
drawStar(ctx, smallStarCenterX_4, smallStarCenterY_4, smallStarRadius);
};
</script>

最新文章

  1. Telnet弱口令猜解【Python脚本】
  2. 【深入Java虚拟机】之二:Java垃圾回收机制
  3. Dapper试用
  4. C# Windows Forms 事件处理顺序
  5. (转)基于企业级证书的IOS应用打包升级功能介绍
  6. 似懂非懂的Comparable与Comparator
  7. Mac 上安装 GCC
  8. react入门(一)
  9. 8天入门docker系列 —— 第三天 使用aspnetcore小案例熟悉对镜像的操控
  10. C# 检测证书是否安装、 安装证书
  11. 原生ajax写法
  12. ThreadPoolExecutor线程池的分析和使用
  13. Java并发-ConcurrentModificationException原因源码分析与解决办法
  14. Error: insufficient funds for gas * price + value
  15. linux驱动程序:控制发光二极管
  16. C#读取Mysql blob字段 (转帖)
  17. 过滤器将获取到的内容注入到servlet的request中
  18. linux内存管理之vmalloc函数分析
  19. yii2:多条件多where条件下碰到between时,between语句如何处理呢?
  20. elasticsearch中filter执行原理深度剖析(bitset机制与caching机制)

热门文章

  1. z-stack组网过程
  2. SQL Server中的Merge关键字
  3. 前端用Webpact打包React后端Node+Express实现简单留言版
  4. Unity性能优化之 Draw Call原理&lt;转&gt;
  5. BZOJ2763 [JLOI2011]飞行路线(SPFA + DP)
  6. java-并发-高级并发对象2
  7. WPF menu
  8. Python for Infomatics 第13章 网页服务三(译)
  9. C++ activemq CMS 学习笔记.
  10. windows环境下搭建vue+webpack的开发环境