html5 canvas画五角星(美国队长)

画一波五角星: 美国队长图标

原理:  (1)根据五角星的顶点,外顶点5个,内顶点5个,分成内外圆

(2)由三角函数可表示出每个顶点的位置

(3)利用canvas的lineTo()接口画图

上代码:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3
 4 <head>
 5     <meta charset="UTF-8">
 6     <title>Captain America</title>
 7     <style type="text/css">
 8     #canvas {
 9         display: block;
10         margin: 50px auto;
11     }
12     </style>
13 </head>
14
15 <body>
16     <canvas id="canvas"></canvas>
17     <script>
18     window.onload = function() {
19         var canvas = document.getElementById("canvas");
20         canvas.width = 800;
21         canvas.height = 600;
22
23         var context = canvas.getContext("2d");
24         // 四个圆
25         drawArc(context, 300, 300, 120, 0, Math.PI * 2, false, "#dd5870");
26         drawArc(context, 300, 300, 100, 0, Math.PI * 2, false, "#e0dedf");
27         drawArc(context, 300, 300, 80, 0, Math.PI * 2, false, "#dd5870");
28         drawArc(context, 300, 300, 60, 0, Math.PI * 2, false, "#2773d3");
29         drawStar(context, 20, 60, 300, 300, 0, "#dedce1"); // 五角星
30     }
31
32     function drawStar(ctx, r, R, x, y, changeDeg, fillColor) {
33         //绘制星星的路径,changeDeg:表示五角星旋转的角度
34         ctx.beginPath(); //开始路径
35         for (var i = 0; i < 5; i++) {
36             ctx.lineTo(Math.cos((18 + i * 72 - changeDeg) / 180 * Math.PI) * R + x, -Math.sin((18 + i * 72 - changeDeg) / 180 * Math.PI) * R + y);
37             ctx.lineTo(Math.cos((54 + i * 72 - changeDeg) / 180 * Math.PI) * r + x, -Math.sin((54 + i * 72 - changeDeg) / 180 * Math.PI) * r + y);
38         }
39         ctx.closePath() //结束路径
40         ctx.fillStyle = fillColor;
41         ctx.fill();
42     }
43
44     function drawArc(ctx, x, y, r, stratAngel, endAngel, flag, fillColor) {
45         ctx.beginPath();
46         ctx.arc(x, y, r, stratAngel, endAngel, flag);
47         ctx.fillStyle = fillColor;
48         ctx.fill();
49         ctx.closePath();
50     }
51     </script>
52 </body>
53 </html>

结果:

时间: 2024-10-09 07:27:55

html5 canvas画五角星(美国队长)的相关文章

HTML5 Canvas 画钟表

画钟表是2D画图的老生常谈,我也不能免俗弄了一个.代码如下: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>钟表</title> </head> <body onloa

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()"> &

html5 canvas画饼

1. [图片] lxdpie.jpg ?2. [文件] lqdpie.html ~ 801B     下载(7) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="ru"><head>  <title></title>  <met

html5 canvas 画hello ketty

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>Html5 - 小猫咪</title> <link rel="stylesheet" type="text/css" href="css/common/reset.css"/> &

Html5 canvas 画带箭头的线

 var canvas=document.getElementById("canvas");         var context=canvas.getContext("2d");         function Line(x1,y1,x2,y2){             this.x1=x1;             this.y1=y1;             this.x2=x2;             this.y2=y2;         }  

html5 canvas 画时钟

<!DOCTYPE HTML> <html> <body> <canvas id="myCanvas" width="400" height="400" style="border:1px solid"></canvas> <script type="text/javascript">     // get the canvas    

html5——canvas画直线

<html> <head> <title>canvas demo</title> </head> <body> <canvas id="mycanvas" width="500px" height="500px" ></canvas> <script type="text/javascript"> var mycanva

html5 canvas绘制圆形印章,以及与页面交互

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>HTML5 Canvas画印章</title> 6 <script type="text/javascript" src="../JQmain/jquery-2.2.0.min.js"></script> 7 &l

html5学习(一)--canvas画时钟

利用空余时间学习一下html5. 1 <!doctype html> 2 <html> 3 <head></head> 4 <body> 5 <canvas id="clock" width="500" height="500"></canvas> 6 <script> 7 var clock=document.getElementById('cloc