HTML5 Canvas旋转动画的2个代码例子(一个旋转的太极图效果)


Posted in HTML / CSS onApril 10, 2014

效果图:
HTML5 Canvas旋转动画的2个代码例子(一个旋转的太极图效果)

方法一:

复制代码
代码如下:
<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="500" height="500">your browser does not support the canvas tag</canvas>
<script type="text/javascript">
var deg = 0;
var r = 30;
var rl = 100;
function drawTaiji() {
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var colorA = "rgb(0, 0, 0)";
var colorB = "red";

var px =Math.sin(deg)*r;
var py =Math.cos(deg)*r;
context.clearRect(0, 0, 300, 300);
context.beginPath();
context.fillStyle = colorA;
context.arc(rl, rl, 60, 0.5 * Math.PI +deg, 1.5 * Math.PI +deg, true);
context.closePath();
context.fill();
context.fillStyle = colorB;
context.beginPath();
context.arc(rl, rl, 60, 1.5* Math.PI +deg, 0.5 * Math.PI +deg, true);
context.closePath();
context.fill();
context.fillStyle = colorB;
context.beginPath();
context.arc(rl+px, rl-py, 30, 0.5 * Math.PI + deg, 1.5 * Math.PI + deg, true);
context.closePath();
context.fill();
context.fillStyle = colorA;
context.beginPath();
context.arc(rl-px, rl+py, 30, 1.5 * Math.PI + deg, 0.5 * Math.PI + deg, true);
context.closePath();
context.fill();
context.fillStyle = colorA;
context.beginPath();
context.arc(rl+px, rl-py, 8, 0, 2 * Math.PI, true);
context.closePath();
context.fill();
context.fillStyle = colorB;
context.beginPath();
context.arc(rl-px, rl+py, 8, 0, 2 * Math.PI, true);
context.closePath();
context.fill();
deg +=0.1;
}
setInterval(drawTaiji, 100);
</script> </p> <p></body>
</html>

方法二:

复制代码
代码如下:

<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="500" height="500" >your browser does not support the canvas tag </canvas>
<script type="text/javascript">
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
var angle = 0;
var count = 360;
var clrA = '#000';
var clrB = 'red';

function taiji(x, y, radius, angle, wise) {
angleangle = angle || 0;
wisewise = wise ? 1 : -1;
ctx.save();
ctx.translate(x, y);
ctx.rotate(angle);
ctx.fillStyle = clrA;
ctx.beginPath();
ctx.arc(0, 0, radius, 0, Math.PI, true);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = clrB;
ctx.arc(0, 0, radius, 0, Math.PI, false);
ctx.fill();
ctx.fillStyle = clrB;
ctx.beginPath();
ctx.arc(wise * -0.5 * radius, 0, radius / 2, 0, Math.PI * 2, true);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = clrA;
ctx.arc(wise * +0.5 * radius, 0, radius / 2, 0, Math.PI * 2, false);
ctx.arc(wise * -0.5 * radius, 0, radius / 10, 0, Math.PI * 2, true);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = clrB;
ctx.arc(wise * +0.5 * radius, 0, radius / 10, 0, Math.PI * 2, true);
ctx.fill();
ctx.restore();
}

loop = setInterval(function () {
beginTag = true;
ctx.clearRect(0, 0, canvas.width, canvas.height);
taiji(200, 200, 50, Math.PI * (angle / count) * 2, true);
//taiji(350, 350, 50, Math.PI * ((count - angle) / count) * 2, false);
angle = (angle + 5) % count;
}, 50);
</script> </p> <p></body>
</html>


HTML / CSS 相关文章推荐
全面总结使用CSS实现水平垂直居中效果的方法
Mar 10 HTML / CSS
CSS3简单实现照片墙
Dec 12 HTML / CSS
使用CSS3和Checkbox实现JQuery的一些效果
Aug 03 HTML / CSS
CSS3 实现footer 固定在底部(无论页面多高始终在底部)
Oct 15 HTML / CSS
HTML5地理定位与第三方工具百度地图的应用
Nov 17 HTML / CSS
HTML5上传文件显示进度的实现代码
Aug 30 HTML / CSS
HTML5中5个简单实用的API
Apr 28 HTML / CSS
HTML5 Canvas 旋转风车绘制
Aug 18 HTML / CSS
html5视频媒体标签video的使用方法及完整参数说明详解
Sep 27 HTML / CSS
浅谈Html5页面打开app的一些思考
Mar 30 HTML / CSS
HTML5 直播疯狂点赞动画实现代码 附源码
Apr 14 HTML / CSS
CSS中实现动画效果-附案例
Feb 28 HTML / CSS
使用javascript和HTML5 Canvas画的四渐变色播放按钮效果
Apr 10 #HTML / CSS
HTML5的自定义属性data-*详细介绍和JS操作实例
Apr 10 #HTML / CSS
让ie浏览器成为支持html5的浏览器的解决方法(使用html5shiv)
Apr 08 #HTML / CSS
为你的html5网页添加音效示例
Apr 03 #HTML / CSS
使用jquery实现HTML5响应式导航菜单教程
Apr 02 #HTML / CSS
简单html5代码获取地理位置
Mar 31 #HTML / CSS
HTML5注册页面示例代码
Mar 27 #HTML / CSS
You might like
《APMServ 5.1.2》使用图解
2006/10/23 PHP
抓取YAHOO股票报价的类
2009/05/15 PHP
php抓取页面与代码解析 推荐
2010/07/23 PHP
php 判断是否是中文/英文/数字示例代码
2013/09/30 PHP
jquery load事件(callback/data)使用方法及注意事项
2013/02/06 Javascript
jQuery插件datalist实现很好看的input下拉列表
2015/07/14 Javascript
AngularJS 实现按需异步加载实例代码
2015/10/18 Javascript
jQuery实现鼠标选文字发新浪微博的方法
2016/04/02 Javascript
分享ES6的7个实用技巧
2018/01/18 Javascript
JS浅拷贝和深拷贝原理与实现方法分析
2019/02/28 Javascript
使用 Vue 实现一个虚拟列表的方法
2019/08/20 Javascript
微信小程序自定义组件components(代码详解)
2019/10/21 Javascript
Vue 实现CLI 3.0 + momentjs + lodash打包时优化
2019/11/13 Javascript
javascript实现雪花飘落效果
2020/08/19 Javascript
Java 生成随机字符的示例代码
2021/01/13 Javascript
jQuery使用hide()、toggle()函数实现相机品牌展示隐藏功能
2021/01/29 jQuery
通过python下载FTP上的文件夹的实现代码
2013/02/10 Python
python多线程编程方式分析示例详解
2013/12/06 Python
简单谈谈Python中的几种常见的数据类型
2017/02/10 Python
Python+Selenium自动化实现分页(pagination)处理
2017/03/31 Python
Python 多进程并发操作中进程池Pool的实例
2017/11/01 Python
python判断一个集合是否为另一个集合的子集方法
2018/05/04 Python
详解pandas DataFrame的查询方法(loc,iloc,at,iat,ix的用法和区别)
2019/08/02 Python
利用pyecharts实现地图可视化的例子
2019/08/12 Python
Python多线程实现支付模拟请求过程解析
2020/04/21 Python
浅谈keras.callbacks设置模型保存策略
2020/06/18 Python
HTML5适合的情人节礼物有纪念日期功能
2021/01/25 HTML / CSS
英国历史最悠久的DJ设备供应商:DJ Finance、DJ Warehouse、The DJ Shop
2019/09/04 全球购物
美国工业用品采购网站:Zoro.com
2020/10/27 全球购物
后勤管理员岗位职责
2014/08/27 职场文书
群众路线教育实践活动的心得体会
2014/09/03 职场文书
政府领导干部个人对照检查材料思想汇报
2014/09/24 职场文书
检讨书模板大全
2015/05/07 职场文书
环保证明
2015/06/23 职场文书
回复函范文
2015/07/14 职场文书
Pyhton模块和包相关知识总结
2021/05/12 Python