HTML5 Canvas 绘制新西兰国旗

代码:
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>新西兰国旗</title>
</head>
<body onload="draw()">
<canvas id="myCanvus" width="480px" height="240px" style="border:1px dashed black;">
出现文字表示你的浏览器不支持HTML5
</canvas>
</body>
</html>
<script type="text/javascript">
<!--
function draw(){
var canvas=document.getElementById("myCanvus");
var canvasWidth=240;
var canvasHeight=120;
var context=canvas.getContext("2d");
context.fillStyle = "white";
context.fillRect(0, 0, canvasWidth*2, canvasHeight*2);
// 先画角上蓝色方块
context.fillStyle = "blue";
context.fillRect(0, 0, 100, 40);
context.fillRect(140, 0, 100, 40);
context.fillRect(0, 80, 100, 40);
context.fillRect(140, 80, 100, 40);
// 用旋转的白色长条去盖上蓝色方块,覆盖完出现八个三角块
context.save();
context.translate(120,60);
context.rotate(getRad(26.56));
context.fillStyle = "white";
context.fillRect(-300, -12, 600, 24);
context.restore();
context.save();
context.translate(120,60);
context.rotate(getRad(-26.56));
context.fillStyle = "white";
context.fillRect(-300, -12, 600, 24);
context.restore();
// 四个红条
context.save();
context.translate(120,60);
context.rotate(getRad(26.56));
context.fillStyle = "red";
context.fillRect(-300, 0, 300, 8);
context.restore();
context.save();
context.translate(120,60);
context.rotate(getRad(-26.56));
context.fillStyle = "red";
context.fillRect(-300, 0, 300, 8);
context.restore();
context.save();
context.translate(120,60);
context.rotate(getRad(-26.56));
context.fillStyle = "red";
context.fillRect(0, -8, 300, 8);
context.restore();
context.save();
context.translate(120,60);
context.rotate(getRad(26.56));
context.fillStyle = "red";
context.fillRect(0, -8, 300, 8);
context.restore();
// 用一个白条去把中间部分的多余红边去掉
context.fillStyle = "white";
context.fillRect(0, 40, 240, 40);
// 画中间的红色十字
context.fillStyle = "red";
context.fillRect(0, 48, 240, 24);
context.fillRect(108, 0, 24, 120);
// 用蓝色方块覆盖掉多出来的部分
context.fillStyle = "blue";
context.fillRect(0, canvasHeight, canvasWidth*2, canvasHeight);
context.fillRect(canvasWidth, 0, canvasWidth, canvasHeight);
// 逐个画南十字星的四颗星
// 第一颗星
var x=360;
var y=45;
context.save();
var r=14;
context.translate(x-r,y-r);
context.strokeStyle = "white";
context.fillStyle = "white";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
context.save();
r=10;
context.translate(x-r,y-r);
context.strokeStyle = "red";
context.fillStyle = "red";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
// 第二颗星
var x=360;
var y=200;
context.save();
var r=14;
context.translate(x-r,y-r);
context.strokeStyle = "white";
context.fillStyle = "white";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
context.save();
r=10;
context.translate(x-r,y-r);
context.strokeStyle = "red";
context.fillStyle = "red";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
// 第三颗星
var x=300;
var y=110;
context.save();
var r=14;
context.translate(x-r,y-r);
context.strokeStyle = "white";
context.fillStyle = "white";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
context.save();
r=10;
context.translate(x-r,y-r);
context.strokeStyle = "red";
context.fillStyle = "red";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
// 第四颗星
var x=420;
var y=100;
context.save();
var r=14;
context.translate(x-r,y-r);
context.strokeStyle = "white";
context.fillStyle = "white";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
context.save();
r=10;
context.translate(x-r,y-r);
context.strokeStyle = "red";
context.fillStyle = "red";
context.beginPath();
context.moveTo(r, 0);
context.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r);
context.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r);
context.lineTo(r, 0);
context.fill();
context.stroke();
context.closePath();
context.restore();
}
function getRad(degree){
return degree/180*Math.PI;
}
//-->
</script>
HTML5 Canvas 绘制新西兰国旗的更多相关文章
- HTML5 Canvas 绘制澳大利亚国旗
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- HTML5 Canvas 绘制英国国旗
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- 学习笔记:HTML5 Canvas绘制简单图形
HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...
- 使用 HTML5 Canvas 绘制出惊艳的水滴效果
HTML5 在不久前正式成为推荐标准,标志着全新的 Web 时代已经来临.在众多 HTML5 特性中,Canvas 元素用于在网页上绘制图形,该元素标签强大之处在于可以直接在 HTML 上进行图形操作 ...
- 使用html5 canvas绘制图片
注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...
- 使用html5 canvas绘制圆形或弧线
注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...
- html5 Canvas绘制图形入门详解
html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome. ...
- 解决html5 canvas 绘制字体、图片与图形模糊问题
html5 canvas 绘制字体.图片与图形模糊问题 发生情况 多出现在高dpi设备,这意味着每平方英寸有更多的像素,如手机,平板电脑.当然很多高端台式电脑也有高分辨率高dpi的显示器. canva ...
- 使用html5 Canvas绘制线条(直线、折线等)
使用html5 Canvas绘制直线所需的CanvasRenderingContext2D对象的主要属性和方法(有"()"者为方法)如下: 属性或方法 基本描述 strokeSty ...
随机推荐
- spring bean自动注入
使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...
- DOM的相关概念
[前面的话]DOM全称是Document Object Model,即文档对象模型.我们常说的html文档其实就是一个DOM树,DOM操作就是在内存中找到DOM树上我们想要的DOM对象,对它的属性进行 ...
- 【转】BehaviorDesigner学习
BehaviorDesigner学习 行为树: 行为树设计师插件是一个专门为unity设计的AI插件. 通过继承Behavior中的Task下的四个节点,可以创建自己的行为树节点. 行为树中的自定 ...
- CentOS下安装netcat
CentOS下安装netcat 使用zookeeper过程中,需要监控集群状态.在使用四字命令时(echo conf | nc localhost 2181),报出如下错误:-bash: netcat ...
- 【Luogu】P4172水管局长(LCT)
题目链接 有个结论是x到y的路径上最长边权值等于最小生成树上最长边权值,于是问题转化为最小生成树. 再考虑把问题反过来,删边变成加边. 于是变成动态维护最小生成树,LCT可以做到. #include& ...
- linux系统——hosts文件修改
1. 关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) Linux 的/etc/hosts是 ...
- 邻面合并(merging)
邻面合并(merging) 题目描述 给定一个N×MN×M的网格,每个格子上写有0或1.现在用一些长方形覆盖其中写有1的格子,长方形的每条边都要与坐标轴平行.要求:每个写着1的格子都要被覆盖,长方形不 ...
- java运行时间计算
long startTime = System.currentTimeMillis(); //获取开始时间 doSomething(); //测试的代码段 long endTime = System. ...
- Bzoj1195 [HNOI2006]最短母串 [AC自动机]
Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1304 Solved: 439 Description 给定n个字符串(S1,S2,„,Sn),要求找 ...
- Hibernate的merge与update方法的区别
今天做了个测试,写了个测试用例来看看merge与update时控制台打印出来的日志有什么不一样.实体bean很简单,就id和name两个字段,接下来分别给出以下几种测试情形的控制台日志内容: 1. 数 ...