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="380px" height="200px" style="border:1px dashed black;">
出现文字表示你的浏览器不支持HTML5
</canvas>
</body>
</html>
<script type="text/javascript">
<!--
function draw(){
var canvas=document.getElementById("myCanvus");
var canvasWidth=380;
var canvasHeight=200;
var context=canvas.getContext("2d");
context.fillStyle = "white";
context.fillRect(0, 0, canvasWidth, canvasHeight);
var i;
// 13个横条
for(i=0;i<13;i++){
if(i % 2==0){
context.fillStyle = "red";
context.fillRect(0, canvasHeight/13*i, canvasWidth, canvasHeight/13);
}else{
context.fillStyle = "white";
context.fillRect(0, canvasHeight/13*i, canvasWidth, canvasHeight/13);
}
}
// 左上角蓝底
var D=canvasWidth/5*2;
var C=canvasHeight/13*7;
context.fillStyle = "blue";
context.fillRect(0, 0, D, C);
// 画星星
var x;
var y;
var j=0;
context.strokeStyle = "white";
context.fillStyle = "white"
for(i=0;i<6;i++){
for(j=0;j<5;j++){
x=D/12+2*i*D/12;
y=C/10+2*j*C/10;
context.save();
var r=5;
context.translate(x-r,y-r);
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();
}
}
for(i=0;i<5;i++){
for(j=0;j<4;j++){
x=D/6+2*i*D/12;
y=C/5+2*j*C/10;
context.save();
var r=5;
context.translate(x-r,y-r);
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>
关于美国国旗如何画请参考:https://wenku.baidu.com/view/d6eb1d40b4daa58da1114a32.html
HTML5 Canvas 绘制星条旗的更多相关文章
- 学习笔记: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 ...
- html5 canvas绘制环形进度条,环形渐变色仪表图
html5 canvas绘制环形进度条,环形渐变色仪表图 在绘制圆环前,我们需要知道canvas arc() 方 ...
- 怎样用JavaScript和HTML5 Canvas绘制图表
原文:https://code.tutsplus.com/zh-...原作:John Negoita翻译:Stypstive 在这篇教程中,我将展示用JavaScript和canvas作为手段,在饼状 ...
随机推荐
- [oldboy-django][2深入django]初始Form组件
http://www.cnblogs.com/wupeiqi/articles/6144178.html 1 初始Form组件 # Form验证(初始Form组件验证) - 问题: - 无法记住上次提 ...
- re.search 与 re.match的区别
search ⇒ find something anywhere in the string and return a match object. match ⇒ find something at ...
- PAT1026
要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tick,即“时 ...
- c++ 中pair类模板的用法详解
pair: 头文件:#include<utility> 类模板:template <class T1, class T2> struct pair 参数:T1是第一个值的数据类 ...
- How to modify a compiled Android application (.apk file)
Today I’d like to share with you my findings about how an existing .apk file can be modified. An .ap ...
- Newton 插值法
定义 $f(x)$ 关于 $x_0, x_1, \dots, x_k$ 的 $k$ 阶均差(差商)记做 $ f [x_0, x_1, \dots, x_k] $,均差是递归定义的,有两种等价定义 \b ...
- [USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)
题目链接 Solution 水水的套路题. 可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) . 然后 \(DAG\) 上的点权值就是 \(scc\) 的大小. ...
- 《插件》一个比较好用的 chrome浏览器的json格式化插件
插件名: JSON-Handle 下载地址: http://jsonhandle.sinaapp.com/ 插件下载后,在浏览器输入:chrome://extensions/ 将下 ...
- nodeJS学习(9)--- nodeJS模块:exports vs module.exports
模块简介: 通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ 这些核心模块被编译成二进制文件,可以 require('模块名') ...
- Java数据结构-------List
三种List:ArrayList,Vector,LinkedList 类继承关系图 ArrayList和Vector通过数组实现,几乎使用了相同的算法:区别是ArrayList不是线程安全的,Vect ...