小球在屏幕上随机移动
2014-10-12 21:48
239 查看
<html> <head> <style type="text/css"> .canvas{ background:#000; } </style> </head> <body> <canvas class="canvas" ></canvas> <script type="text/javascript"> var S = { start: function(){ Drawing.init(); var d = new Dot(5,20); Drawing.loop(function (){ d.move(); }); } }; Dot = function(x,y){ this.x = x; this.y = y; this.z = 25; this.h =1; }; Dot.prototype = { move: function(){ this.x = Math.random() * window.innerWidth ; this.y = Math.random() * window.innerHeight ; Drawing.drawDot(this); } }; Drawing = (function(){ var canvas,context,animatefn, requestFrame = window.requestAnimateFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimateFrame || window.msRequestAnimateFrame || function(callback){ window.setTimeout(callback,1000/60); }; return { init: function(){ canvas = document.querySelector('.canvas'); context = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; }, drawDot: function(dot){ context.fillStyle = '#fff'; context.beginPath(); context.arc(dot.x ,dot.y ,dot.z,0,2 * Math.PI,true); context.closePath(); context.fill(); }, clearAnimate: function(){ context.clearRect(0,0,canvas.width,canvas.height); }, loop: function(fn){ animatefn = !animatefn ? fn : animatefn; this.clearAnimate(); animatefn(); requestFrame.call(window,this.loop.bind(this)); } } }()); S.start(); </script> </body> </html>
相关文章推荐
- 自定义随机颜色的圆形小球view随手指移动
- 手指在屏幕上滑动,红色的小球始终跟随手指移动(自定义控件)
- iOS中点击移动,使得手机屏幕的颜色随机变化
- Python实现鼠标在屏幕随机移动操作【Windows/Linux】
- 手指在屏幕上滑动,红色的小球始终跟随手指移动。
- HTML5实现随机生成小球并向上移动
- 小球随机出现在屏幕
- [面向对象的案例]在canvas画布内实现小球的随机移动
- 从零开始学AS3游戏开发【二】 随机移动的敌人
- 小球移动问题
- 移动 Ubuntu16.04 桌面左侧的启动器到屏幕底部
- Android移动开发-Android屏幕手势检测的实现
- 移动小球
- 限制窗口在屏幕范围内移动(不能移到屏幕外)
- 如何让图片自适应手机等移动设备屏幕大小
- 跟随屏幕移动的图像
- 小球跟随鼠标移动效果
- 源码推荐(8.28):视频美化,可向任意方向移动的粘性小球
- HTML5 移动页面自适应手机屏幕四类方法
- EasyX实现小球的移动