canvas教程

canvas学习之圆周运动,canvas学习圆周

字号+ 作者:H5之家 来源:H5之家 2016-06-11 18:00 我要评论( )

canvas学习之圆周运动,canvas学习圆周。canvas学习之圆周运动,canvas学习圆周 html部分 ......bodycanvas id=

canvas学习之圆周运动,canvas学习圆周

html部分

...... <body> <canvas ></canvas> <!-- 给动画添加控制按钮 --> <div>     <button>Start</button>     <button>Stop</button> </div> ......

圆周运动
相关知识:三角函数
方法:将形状放在周长上角度为0弧度的位置,该位置位于右手边,在每次动画循环中,只需增加位于圆周上的形状的角度,就可以使形状沿着圆周运动。
需要解决的问题:
如何计算位于圆周上形状的(x,y)坐标值
解决:需要知道三角形邻边和对边的长度,分别代表x,y的位置

var angle = 45; var adjRatio = Math.cos(angle*(Math.PI/180)); // 余弦-斜边-邻边 var oppRatio = Math.sin(angle*(Math.PI/180)); // 正弦-对边-斜边 var radius = 50; var x = radius * adjRatio; var y = radius * oppRatio;

位置:定义shape类,并向其添加属性,

var shape = function(x,y,canvasWidth,canvasHeight){ this.x = x; this.y = y; this.width = width; this.height = height; this.radius = Math.random()*30; // 介于0~30之间的随机半径 this.angle = 0; // 起始的角度值 }

计算位于圆周上当前角度的形状多对应的x,y的值,
圆周通过半径定义
注意:形状对象中定义的点(x,y)现在引用的是圆周的中心---形状围绕它旋转的点,而不是起点

var x = temshape.x+(temshape.radius*Math.cos(temshape.angle*(Math.PI/180))); var y = temshape.y+(temshape.radius*Math.sin(temshape.angle*(Math.PI/180))); temshape.angle+=5; // 增加旋转的角度 if(temshape.angle>360){ temshape.angle=0; }

将新的x,y变量添加到fillRect中

context.fillRect(x,y,temshape.width,temshape.height)   // 画矩形

---------------------------完整代码--------------------------------

<script> function draw1(id){ var myCanvas = $('#myCanvas'); var context = myCanvas.get(0).getContext('2d'); var canvasWidth = myCanvas.width(); var canvasHeight = myCanvas.height(); var startButton = $('#startAnimation'); var stopButton = $('#stopAnimation'); var playAnimation = true; startButton.hide(); startButton.click(function(){ $(this).hide(); stopButton.show(); playAnimation = true; animate(); }) stopButton.click(function(){ $(this).hide(); startButton.show(); playAnimation = false; }) var shape = function(x,y,canvasWidth,canvasHeight){ this.x = x; this.y = y; this.width = width; this.height = height; this.radius = Math.random()*30; // 介于0~30之间的随机半径 this.angle = 0; // 起始的角度值 } var shapes = new Array();         for(var i = 0;i<10;i++){          var x = Math.random()*250;             var y = Math.random()*250;             var width = height = Math.random()*50;             shapes.push(new shape(x,y,width,height));         } function animate(){ context.clearRect(0,0,canvasWidth,canvasHeight); // 擦除 var shapesLength = shapes.length; for(var i=0;i<shapesLength;i++){ var temshape = shapes[i]; var x = temshape.x+(temshape.radius*Math.cos(temshape.angle*(Math.PI/180))); var y = temshape.y+(temshape.radius*Math.sin(temshape.angle*(Math.PI/180))); temshape.angle+=5; if(temshape.angle>360){ temshape.angle=0; } context.fillRect(x,y,temshape.width,temshape.height) // 画矩形 }; if(playAnimation){ setTimeout(animate,33); } }animate(); }draw1('#myCanvas'); </script>

上图,矩形做圆周运动。

canvas学习之圆周运动,canvas学习圆周 html部分 ......bodycanvas /canvas!-- 给动画添加控制按钮 --divbuttonart_confoot">

相关文章

相关搜索: html5

今日最新相关阅读:

相关频道: HTML/CSS  HTML5  Javascript  jQuery  AJax教程  前端代码  正则表达式  Flex教程  WEB前端教程  

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • html5canvas核心技术图形、动画与游戏开发源码

    html5canvas核心技术图形、动画与游戏开发源码

    2017-05-02 17:42

  • 打印html5中Canvas的方法

    打印html5中Canvas的方法

    2017-05-01 15:03

  • HTML5+Canvas调用手机拍照功能实现图片上传(下)

    HTML5+Canvas调用手机拍照功能实现图片上传(下)

    2017-04-30 17:00

  • 学习慕课网canvas倒计时实例笔记

    学习慕课网canvas倒计时实例笔记

    2017-04-30 14:01

网友点评