HTML 5 Canvas组件绘制太极图案

开发 前端
实现原理主要是利用HTML5的Canvas组件提供的path函数功能来绘制圆,首先绘制两个半圆,分别为黑色和白色,组成一个圆,绘制完成以后再分别绘制一个黑色和白色的圆在绘制好的黑白圆之内,半径恰好是黑白大圆一半。

一、实现思路:

实现原理主要是利用HTML5的Canvas组件提供的path函数功能来绘制圆,首先绘制两个半圆,分别为黑色和白色,组成一个圆,绘制完成以后再分别绘制一个黑色和白色的圆在绘制好的黑白圆之内,半径恰好是黑白大圆一半。 ***在绘制好的两个黑白小圆内分别填充白色和黑色的点,半径大小为10pixel左右。

二、程序效果如下:

 

HTML5 Canvas组件绘制太极图案

 

三、关键程序解析:

绘制半圆的程序,其中200,200表示开始绘制圆心点坐标,第三个参数150表示绘制圆的半径

第四个参数表示开始角度,第五个参数表示结束角度,***一个参数表示是否为顺时针或者逆时针

绘制白色半圆的代码如下:

  1. ctx.fillStyle="#fff";    
  2. ctx.beginPath();     
  3. ctx.arc(200, 200, 150, 1.5*Math.PI, Math.PI/2, false);    
  4. ctx.closePath();    
  5. ctx.fill(); 

绘制黑色半圆的代码如下:31358.cn

  1. ctx.fillStyle="#000";    
  2. ctx.beginPath();     
  3. ctx.arc(200, 200, 150, Math.PI/2, 1.5*Math.PI, false);    
  4. ctx.closePath();    
  5. ctx.fill(); 

在太极图案中添加文字的代码使用了透明处理,Canvas全局透明度设置函数

如下:

  1. // set transparency value      
  2. ctx.globalAlpha = 0.2; 

绘制文字的代码如下:

  1. // Draw semi transparent text    
  2. ctx.fillStyle = "#f00";    
  3. ctx.font = "32pt Arial";    
  4. ctx.fillText("Hello", 220, 200);    
  5. ctx.fillText("Canvas", 100, 250);  

程序完全JavaScript代码如下:

  1. window.onload = function() {    
  2.     var cvs = document.getElementById("canvas-path");    
  3.     ctx = cvs.getContext("2d");    
  4.     // Create circle, radius = 150    
  5.     // start point(x, y), radius, start angle, end angle, boolean antiClockWise    
  6.     ctx.fillStyle="#fff";    
  7.     ctx.beginPath();     
  8.     ctx.arc(200, 200, 150, 1.5*Math.PI, Math.PI/2, false);    
  9.     ctx.closePath();    
  10.     ctx.fill();    
  11.     ctx.fillStyle="#000";    
  12.     ctx.beginPath();     
  13.     ctx.arc(200, 200, 150, Math.PI/2, 1.5*Math.PI, false);    
  14.     ctx.closePath();    
  15.     ctx.fill();    
  16.     // draw sub circle    
  17.     // start point(x, y), radius, start angle, end angle, boolean antiClockWise    
  18.     ctx.fillStyle="#000";    
  19.     ctx.beginPath();     
  20.     ctx.arc(200, 275, 75, 0, Math.PI*2, false);     
  21.     ctx.closePath();    
  22.     ctx.fill();    
  23.     ctx.fillStyle="#fff";    
  24.     ctx.beginPath();     
  25.     ctx.arc(200, 125, 75, 0, Math.PI*2, false);    
  26.     ctx.closePath();    
  27.     ctx.fill();    
  28.     // fill black and white point    
  29.     ctx.fillStyle="#fff";    
  30.     ctx.beginPath();     
  31.     ctx.arc(200, 275, 10, 0, Math.PI*2, false);     
  32.     ctx.closePath();    
  33.     ctx.fill();    
  34.     ctx.fillStyle="#000";    
  35.     ctx.beginPath();     
  36.     ctx.arc(200, 125, 10, 0, Math.PI*2, false);    
  37.     ctx.closePath();    
  38.     ctx.fill();    
  39.     // set transparency value      
  40.     ctx.globalAlpha = 0.2;       
  41.     // Draw semi transparent text    
  42.     ctx.fillStyle = "#f00";    
  43.     ctx.font = "32pt Arial";    
  44.     ctx.fillText("Hello", 220, 200);    
  45.     ctx.fillText("Canvas", 100, 250);    
  46.     ctx.globalAlpha = 1.0;     
  47.     ctx.shadowOffsetX = 2;      
  48.     ctx.shadowOffsetY = 2;      
  49.     ctx.shadowBlur = 2;      
  50.     ctx.shadowColor = "rgba(0, 0, 0, 0.5)";      
  51.     ctx.fillStyle = "#000";    
  52.     ctx.font = "20px Times New Roman";    
  53.     ctx.fillText("-created by gloomyfish", 100, 30);    
  54. }; 

原文:http://www.31358.cn/html5_study/919.html

【编辑推荐】

  1. HTML 5 Canvas(画布)教程之图像处理
  2. HTML 5新特性Canvas入门秘籍
  3. 使用JavaScript和Canvas开发游戏之使用Canvas
  4. 使用JavaScript和Canvas开发游戏之认识Canvas
  5. 15个不可思议的HTML 5 Canvas应用欣赏
责任编辑:陈贻新 来源: 31358.cn
相关推荐

2011-12-29 14:22:40

Java

2012-02-24 15:28:36

ibmdw

2012-06-04 10:16:18

HTML5

2012-06-12 09:53:14

HTML5

2012-09-24 13:49:13

HTML5CanvasJS

2022-06-16 10:33:14

代码AI

2021-01-06 10:05:09

鸿蒙HarmonyOSCanvas

2011-11-09 10:05:26

HTML 5

2012-05-09 09:41:58

HTML5

2021-01-04 11:10:14

鸿蒙HarmonyOSCanvas

2011-11-25 14:20:57

HTML 5

2012-05-29 09:57:10

HTML5

2017-07-05 16:22:09

HTML5canvas动态

2015-10-08 08:48:44

HTML5canvas动画

2012-08-30 10:18:09

HTML5CanvasHTML5实例

2011-07-21 15:34:36

iPhone HTML5 Canvas

2012-05-07 14:25:16

HTML5

2011-07-18 13:35:14

HTML 5

2012-05-09 12:18:14

HTML5Canvas

2016-01-20 10:11:56

华丽CanvasHTML5
点赞
收藏

51CTO技术栈公众号