js数学 atan2 cos sin
2015-07-07 21:15
621 查看
1.给出相对坐标中心o,p1到o的距离l,p1到x轴的角度angle(0~2PI)
left:x0+Math.cos(angle) top: y0+Math.sin(angle)
2.给出相对坐标中心o,p1坐标,求p1关于x轴的夹角
//angle大于-Math.PI,小于Math.PI // (-∏,∏) var angle=astan2(y1-y0,x1-x0);
案例:
用canvas写一个摆动角在60度到120度之间的循环动画,要求长方体随着摆动跟着转动。
/* 知识点: 摆线转过的角度等于长方体自身旋转的角度 已知angle,求x,y */ var canvas,contxt; canvas=document.getElementById('myCanvas'); contxt=canvas.getContext("2d"); var drawWall=function(){ contxt.save(); contxt.strokeStyle="#000"; contxt.lineWidth=5; contxt.beginPath(); contxt.moveTo(100,0); contxt.lineTo(500,0) contxt.stroke(); contxt.closePath(); contxt.restore(); }; var drawLine=function(x,y){ contxt.save(); contxt.strokeStyle="#000"; contxt.lineWidth=2; contxt.beginPath(); contxt.moveTo(300,0); contxt.lineTo(300+x,y) contxt.stroke(); contxt.closePath(); contxt.restore(); }; var drawRect=function(x,y,angle){ contxt.save(); contxt.fillStyle="#f00"; contxt.translate(300+x,y); contxt.rotate(angle+Math.PI/2); contxt.rect(-100,-50,200,100); contxt.fill(); contxt.restore(); }; var angle,increase,width,height,x,y,radius; angle=Math.PI/3; increase=Math.PI/3/200; width=200; height=100; radius=300; var onframe=function(){ contxt.clearRect(0,0,800,600); drawWall(); x=radius*Math.cos(angle); y=radius*Math.sin(angle); drawLine(x,y); drawRect(x,y,angle); if(angle>(Math.PI/3*2)){ increase=-increase; }else if(angle<Math.PI/3){ increase=-increase; } angle+=increase; window.requestAnimationFrame(onframe); }; onframe();
相关文章推荐
- 【js】 流式布局 页面
- Chrome控制台 JS调试的一些小技巧
- javascript内置顶层函数
- javascript数租
- js中的各种难题(1)
- javascript对文档对象的内容、属性、样式的操作
- JSP整理总结--9大内置对象、4个作用域
- JSon 简单例子
- JAWR【一个java项目的javascript和CSS集成和压缩工具】
- JSP整理总结--JSP指令、动作标签
- javascript笔记02:严格模式的特定要求
- JavaScript学习笔记
- 完美的js URLEncode函数
- js 增删改查
- 高性能javascript小结
- 枚举做JSP下拉选框的数据源
- 【转载】JavaScript 经典实例收集整理
- JavaScript之this用法详解
- js window对象
- 【翻译】使用Sencha Ext JS 6打造通用应用程序