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 ...
随机推荐
- Android简单的BaseExpandableList使用
1.Activity package com.example.administrator.mystudent.ExpandableListView; import android.app.Expand ...
- js判断时间是否过期
var myDate=new Date(); myDate.setFullYear(2014,2,1); //2014年3月1日 //注意:表示月份的参数介于 0 到 11 之间.也就是说,如果希望把 ...
- 项目记事【StreamAPI】:使用 StreamAPI 简化对 Collection 的操作
最近项目里有这么一段代码,我在做 code-review 的时候,觉得可以使用 Java8 StreamAPI 简化一下. 这里先看一下代码(不是源码,一些敏感信息被我用其他类替代了): privat ...
- httpClient get方式抓取数据
/* * 爬取网页信息 */ private static String pickData(String url) { CloseableHttpClient ht ...
- [NOI2008][bzoj1061] 志愿者招募 [费用流+巧妙的建图]
题面 传送门 思路 引入:网络流? 看到这道题,第一想法是用一个dp来完成决策 但是,显然这道题的数据并不允许我们进行dp,尤其是有10000种志愿者的情况下 那么我们就要想别的办法来解决: 贪心?这 ...
- [ZJOI2012][bzoj 2816] 网络 network [LCT]
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2816 思路: 第一个条件看完暂时还没什么想法 看完第二个,发现每一个颜色都是一个森林 进而想 ...
- 关于可图化序列的一点结论 NEU 1429
Graphic Sequence A graphic sequence is a sequence of numbers which can be the degree sequence of som ...
- Java众神之路(4)-关键字(下)
21.float float是Java原始类型. float变量可以存储单精度浮点值. 示例: float ratio = 0.1f; float diameter = 6.15f; 注释: 使用此关 ...
- nginx和php安装文件
#!/usr/bin/env bash echo "=============START=====================" ## php echo '[php]yum i ...
- Python之访问set
dict的作用是建立一组 key 和一组 value 的映射关系,dict的key是不能重复的. 有的时候,我们只想要 dict 的 key,不关心 key 对应的 value,目的就是保证这个集合的 ...