search
HomeWeb Front-endH5 TutorialHTML5 Canvas绘制五星红旗_html5教程技巧

Canvas 绘图 API 都没有定义在 元素本身上,而是定义在通过画布的 getContext() 方法获得的一个“绘图环境”对象上。

Canvas API 也使用了路径的表示法。但是,路径由一系列的方法调用来定义,而不是描述为字母和数字的字符串,比如调用 beginPath() 和 arc() 方法。

一旦定义了路径,其他的方法,如 fill(),都是对此路径操作。绘图环境的各种属性,比如 fillStyle,说明了这些操作如何使用。

使用Canvas画中国国旗,代码:

XML/HTML Code复制内容到剪贴板
  1. nbsp;HTML>  
  2. html>  
  3.     head>  
  4.         meta charset="UTF-8">  
  5.         title>中国标准国旗title>  
  6.     head>  
  7.     body>  
  8.         canvas id="canvas" width="600" height="400">canvas>  
  9.             
  10.         script type="text/javascript">  
  11.             // 使用HTML5绘制标准五星红旗   
  12.             var canvas = document.getElementById("canvas");   
  13.             var context = canvas.getContext('2d');   
  14.             var width = canvas.width;   
  15.             var height = width * 2 / 3;   
  16.             var w = width / 30;//小网格的宽   
  17.             context.fillStyle = "red";   
  18.             context.fillRect(0, 0, width, height);   
  19.             var maxR = 0.15, minR = 0.05;//   
  20.             var maxX = 0.25, maxY = 0.25;//大五星的位置   
  21.             var minX = [0.50, 0.60, 0.60, 0.50];   
  22.             var minY = [0.10, 0.20, 0.35, 0.45];   
  23.             // 画大 ☆   
  24.             var ox = height * maxX, oy = height * maxY;   
  25.             create5star(context, ox, oy, height * maxR, "#ff0", 0);//绘制五角星   
  26.             // 画小 ★   
  27.             for (var idx = 0; idx  4; idx++) {   
  28.                 var sx = minX[idx] * height, sy = minY[idx] * height;   
  29.                 var theta = Math.atan((oy - sy) / (ox - sx));   
  30.                 create5star(context, sx, sy, height * minR, "#ff0", -Math.PI / 2 + theta);   
  31.             }   
  32.             //辅助线   
  33.             context.moveTo(0, height / 2)   
  34.             context.lineTo(width, height / 2);   
  35.             context.stroke();   
  36.             context.moveTo(width / 2, 0);   
  37.             context.lineTo(width / 2, height);   
  38.             context.stroke();   
  39.             //画网格,竖线   
  40.             for (var j = 0; j  15; j++) {   
  41.                 context.moveTo(j * w, 0);   
  42.                 context.lineTo(j * w, height / 2);   
  43.                 context.stroke();   
  44.             }   
  45.             //画网格,横线   
  46.             for (var j = 0; j  10; j++) {   
  47.                 context.moveTo(0, j * w);   
  48.                 context.lineTo(width / 2, j * w);   
  49.                 context.stroke();   
  50.             }   
  51.             //画大圆   
  52.             context.beginPath();   
  53.             context.arc(ox, oy, maxR * height, 0, Math.PI * 2, false);   
  54.             context.closePath();   
  55.             context.stroke();   
  56.             // 画小圆   
  57.             for (var idx = 0; idx  4; idx++) {   
  58.                 context.beginPath();   
  59.                 var sx = minX[idx] * height, sy = minY[idx] * height;   
  60.                 context.arc(sx, sy, height * minR, 0, Math.PI * 2, false);   
  61.                 context.closePath();   
  62.                 context.stroke();   
  63.             }   
  64.             //大圆中心与小圆中心连接线   
  65.             for (var idx = 0; idx  4; idx++) {   
  66.                 context.moveTo(ox, oy);   
  67.                 var sx = minX[idx] * height, sy = minY[idx] * height;   
  68.                 context.lineTo(sx, sy);   
  69.                 context.stroke();   
  70.             }   
  71.             //绘制五角星   
  72.             /**   
  73.              * 创建一个五角星形状. 该五角星的中心坐标为(sx,sy),中心到顶点的距离为radius,rotate=0时一个顶点在对称轴上   
  74.              * rotate:绕对称轴旋转rotate弧度   
  75.              */   
  76.             function create5star(context, sx, sy, radius, color, rotato){   
  77.                 context.save();   
  78.                 context.fillStyle = color;   
  79.                 context.translate(sx, sy);//移动坐标原点   
  80.                 context.rotate(Math.PI + rotato);//旋转   
  81.                 context.beginPath();//创建路径   
  82.                 var x = Math.sin(0);   
  83.                 var y = Math.cos(0);   
  84.                 var dig = Math.PI / 5 * 4;   
  85.                 for (var i = 0; i  5; i++) {//画五角星的五条边   
  86.                     var x = Math.sin(i * dig);   
  87.                     var y = Math.cos(i * dig);   
  88.                     context.lineTo(x * radius, y * radius);   
  89.                 }   
  90.                 context.closePath();   
  91.                 context.stroke();   
  92.                 context.fill();   
  93.                 context.restore();   
  94.             }   
  95.         script>  
  96.     body>  
  97. html>  

以上就是本文的全部内容,希望对大家的学习有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

HTML5 Features: The Core of H5HTML5 Features: The Core of H5May 04, 2025 am 12:05 AM

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

H5: Exploring the Latest Version of HTMLH5: Exploring the Latest Version of HTMLMay 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Beyond Basics: Advanced Techniques in H5 CodeBeyond Basics: Advanced Techniques in H5 CodeMay 02, 2025 am 12:03 AM

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software