代码:

<!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 绘制新西兰国旗的更多相关文章

  1. HTML5 Canvas 绘制澳大利亚国旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  2. HTML5 Canvas 绘制英国国旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  3. 学习笔记:HTML5 Canvas绘制简单图形

    HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...

  4. 使用 HTML5 Canvas 绘制出惊艳的水滴效果

    HTML5 在不久前正式成为推荐标准,标志着全新的 Web 时代已经来临.在众多 HTML5 特性中,Canvas 元素用于在网页上绘制图形,该元素标签强大之处在于可以直接在 HTML 上进行图形操作 ...

  5. 使用html5 canvas绘制图片

    注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...

  6. 使用html5 canvas绘制圆形或弧线

    注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...

  7. html5 Canvas绘制图形入门详解

    html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome. ...

  8. 解决html5 canvas 绘制字体、图片与图形模糊问题

    html5 canvas 绘制字体.图片与图形模糊问题 发生情况 多出现在高dpi设备,这意味着每平方英寸有更多的像素,如手机,平板电脑.当然很多高端台式电脑也有高分辨率高dpi的显示器. canva ...

  9. 使用html5 Canvas绘制线条(直线、折线等)

    使用html5 Canvas绘制直线所需的CanvasRenderingContext2D对象的主要属性和方法(有"()"者为方法)如下: 属性或方法 基本描述 strokeSty ...

随机推荐

  1. Python之协程的实现

    1.Python里面一般用gevent实现协程协程, 而协程就是在等待的时候切换去做别的操作,相当于将一个线程分块,充分利用资源 (1)低级版协程的实现 import gevent def test1 ...

  2. C#控制台程序读取项目中文件路径

    //使用appdomain获取当前应用程序集的执行目录 string dir = AppDomain.CurrentDomain.BaseDirectory; //使用path获取当前应用程序集的执行 ...

  3. spring boot redis代码与配置

    import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Co ...

  4. AngularJs MVC 详解

    为什么在前端也需要MVC 1.代码规模越来越大,切分职责是大势所趋 2.为了复用 3.为了后期维护方便 MVC的目的是为了模块化和复用 前端实现MVC的困难 1.操作DOM必须等整个页面加载完 2.多 ...

  5. hdu 4096 判断路径

    思路:将每个关系当成一条有向边,查询时就判断之间存在路径. #include<iostream> #include<cstdio> #include<cstring> ...

  6. linux系统——日志文件系统及性能分析

    Linux日志文件系统及性能分析 日志文件系统可以在系统发生断电或者其它系统故障时保证整体数据的完整性,Linux是目前支持日志文件系统最多的操作系统之一,本文重点研究了Linux常用的日志文件系统: ...

  7. hdoj 1175 连连看

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  9. div两侧的boder断开 消失 奇怪

    原文发布时间为:2009-11-06 -- 来源于本人的百度文章 [由搬家工具导入] 解决方法: 设定外层DIV的宽度即可,如 width:99% ========================== ...

  10. Cryptography I 学习笔记 --- 零碎

    1. KDF(密钥推导函数,key derivation function),根据用户输入的一个初始密钥来生成一系列的后续密钥.可以使用PRF来生成 2. 可以用salt与slow hash func ...