Heim Web-Frontend H5-Tutorial HTML5 Canvas绘制五星红旗_html5教程技巧

HTML5 Canvas绘制五星红旗_html5教程技巧

May 30, 2016 pm 05:07 PM
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>  

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

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Tabellenrahmen in HTML Tabellenrahmen in HTML Sep 04, 2024 pm 04:49 PM

Anleitung zum Tabellenrahmen in HTML. Hier besprechen wir verschiedene Möglichkeiten zum Definieren von Tabellenrändern anhand von Beispielen für den Tabellenrand in HTML.

HTML-Rand links HTML-Rand links Sep 04, 2024 pm 04:48 PM

Anleitung zum HTML-Rand links. Hier besprechen wir einen kurzen Überblick über HTML margin-left und seine Beispiele sowie seine Code-Implementierung.

Verschachtelte Tabelle in HTML Verschachtelte Tabelle in HTML Sep 04, 2024 pm 04:49 PM

Dies ist eine Anleitung für verschachtelte Tabellen in HTML. Hier diskutieren wir anhand der entsprechenden Beispiele, wie man eine Tabelle innerhalb der Tabelle erstellt.

HTML-Tabellenlayout HTML-Tabellenlayout Sep 04, 2024 pm 04:54 PM

Leitfaden zum HTML-Tabellenlayout. Hier besprechen wir die Werte des HTML-Tabellenlayouts zusammen mit den Beispielen und Ausgaben im Detail.

HTML-Eingabeplatzhalter HTML-Eingabeplatzhalter Sep 04, 2024 pm 04:54 PM

Leitfaden für HTML-Eingabeplatzhalter. Hier besprechen wir die Beispiele für HTML-Eingabeplatzhalter zusammen mit den Codes und Ausgaben.

HTML-geordnete Liste HTML-geordnete Liste Sep 04, 2024 pm 04:43 PM

Leitfaden zur HTML-geordneten Liste. Hier besprechen wir auch die Einführung von HTML-geordneten Listen und Typen sowie deren Beispiele

Text in HTML verschieben Text in HTML verschieben Sep 04, 2024 pm 04:45 PM

Anleitung zum Verschieben von Text in HTML. Hier besprechen wir eine Einführung, wie Marquee-Tags funktionieren, mit Syntax und Beispielen für die Implementierung.

HTML-Onclick-Button HTML-Onclick-Button Sep 04, 2024 pm 04:49 PM

Anleitung zum HTML-OnClick-Button. Hier diskutieren wir deren Einführung, Funktionsweise, Beispiele und Onclick-Events in verschiedenen Veranstaltungen.

See all articles