javascript+HTML5的Canvas实现Lab单车动画效果
网络编程 2021-07-04 21:48www.168986.cn编程入门
这篇文章主要介绍了javascript+HTML5的Canvas实现Lab单车动画效果,涉及javascript使用Canvas配合数学运算实现自行车动画效果,需要的朋友可以参考下
本文实例讲述了javascript+HTML5的Canvas实现Lab单车动画效果。分享给大家供大家参考。具体如下
这里运用HTML5实现的单车动画,运行一下效果你就明白了,不过不要在IE8下测试,没效果的。请在支持最新HTML3和CSS3的浏览器下测试,祝您好运。
运行效果截图如下
具体代码如下
<html> <head> <title>Canvas Lab单车动画,HTML5动画</title> </head> <script type="text/javascript"> var framecounter = 800;//为了完成作业,无耻了点,在这里设置变量…… var direction = "left"; function drawBikeBody () { var canvas = document.getElementById('diagonal'); var context = canvas.getContext('2d'); context.lineWidth = 3; context.beginPath(); context.moveTo(0,0); context.lineTo(150,0); context.lineTo(100,100); context.closePath(); context.moveTo(100,100); context.lineTo(200,75);//后轮轴承点 context.lineTo(150,0); //狼蚁网站SEO优化那个一个脚踏 context.moveTo(100,100); context.lineTo(110,120); context.lineTo(120,120); context.moveTo(110,120); context.lineTo(100,120); //另一个脚踏 context.moveTo(100,100); context.lineTo(90,80); context.lineTo(100,80); context.moveTo(90,80); context.lineTo(80,80); //座包 context.moveTo(150,0); context.lineTo(160,-20); context.lineTo(175,-20); context.moveTo(160,-20); context.lineTo(135,-20); //扶手 context.moveTo(0,0); context.lineTo(15,-30); context.lineTo(10,-35); context.lineTo(20,-40); context.lineTo(40,-40); //前轮轴承 context.moveTo(0,0); context.lineTo(-35.5,75);//前轮轴承点 //前轮 context.moveTo(75-35.5,75);//去掉这个目测不行啊,貌似context.arc()方法里有lineTo context.arc(-35.5,75,75,0,Math.PI2,true); //后轮 context.moveTo(200+75,75); context.arc(200,75,75,0,Math.PI2,true); } function drawForcePiece1(){ var canvas = document.getElementById('diagonal'); var context = canvas.getContext('2d'); context.beginPath(); for(var i = framecounter; i < framecounter+360; i += 20) { context.moveTo(-35.5,75); x = Math.cos(Math.PI / 180 i) 75 + (-35.5); y = Math.sin(Math.PI / 180 i) 75 + 75; context.lineTo(x,y); } } function drawForcePiece2(){ var canvas = document.getElementById('diagonal'); var context = canvas.getContext('2d'); context.beginPath(); for(var i = framecounter; i < framecounter+360; i += 20) { context.moveTo(200,75); x = Math.cos(Math.PI / 180 i) 75 + 200; y = Math.sin(Math.PI / 180 i) 75 + 75; context.lineTo(x,y); } } function drawBike(){ var canvas = document.getElementById('diagonal'); var context = canvas.getContext('2d'); context.clearRect(0, 0, 800, 600); context.save(); context.translate(framecounter,300); drawBikeBody(); context.stroke(); context.restore(); context.save(); context.translate(framecounter,300); drawForcePiece1(); context.stroke(); context.restore(); context.save(); context.translate(framecounter,300); drawForcePiece2(); context.stroke(); context.restore(); if (direction==="left") { framecounter--; }else{ framecounter++; } } function move() { var interal = setInterval(function () { drawBike(); }, 10); } window.addEventListener("load",move,true); </script> <body> <canvas id="diagonal" width="800" height="600" style="border:1px dashed">Please update your brower to the newest version!</canvas> <br> <button id="reset" onclick="framecounter=800;">Reset</button> <button id="left" onclick="direction='left'">Go Left</button> <button id="right" onclick="direction='ture'">Go Right</button> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程