搜索文档
首页
HTML/CSS
JavaScript
服务端开发
Java教程
移动端
数据库
当前位置:
首页
HTML/CSS
HTML5基础教程
HTML5 图形
源代码
清空
点击运行
<canvas id="ex1" width="500" height="170" style="border: 1px solid #cccccc;"> HTML5 Canvas not supported </canvas> <script> var x = 0; var y = 15; var speed = 5; function animate() { reqAnimFrame = (window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { var self = this, start, finish; return window.setTimeout(function() { start = +new Date(); callback(start); finish = +new Date(); self.timeout = 1000/60 - (finish - start); }, self.timeout); }); reqAnimFrame(animate); x += speed; if(x <= 0 || x >= 475){ speed = -speed; } draw(); } function draw() { var canvas = document.getElementById("ex1"); var context = canvas.getContext("2d"); context.clearRect(0, 0, 500, 170); context.fillStyle = "#ff00ff"; context.fillRect(x, y, 25, 25); } animate(); </script>
运行结果