博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
帆布小球碰壁效果
阅读量:7091 次
发布时间:2019-06-28

本文共 1220 字,大约阅读时间需要 4 分钟。

<!doctype html>

<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>帆布小球碰壁效果</title>

<style>

canvas {

border:2px dotted #ccc;
}
</style>
</head>
<body>
<canvas id="canvas" width="300" height="450"></canvas>
<script type="text/javascript">
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var raf;
var ball = {
x: 100,
y: 100,
vx: 5,
vy: 2,
radius: 25,
color: 'blue',
draw: function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
};
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ball.draw();
ball.x += ball.vx;
ball.y += ball.vy;
if (ball.y + ball.vy > canvas.height ||
ball.y + ball.vy < 0) {
ball.vy = -ball.vy;
}
if (ball.x + ball.vx > canvas.width ||
ball.x + ball.vx < 0) {
ball.vx = -ball.vx;
}
raf = window.requestAnimationFrame(draw);
}
canvas.addEventListener('mouseover', function(e) {
raf = window.requestAnimationFrame(draw);
});
canvas.addEventListener("mouseout", function(e) {
window.cancelAnimationFrame(raf);
});
ball.draw();
</script>
</body>
</html>

转载于:https://www.cnblogs.com/web-chuanfa/p/9131377.html

你可能感兴趣的文章
D2 日报 2019年5月19日
查看>>
浅谈async/await
查看>>
Flutter杂症( flutter packages pub run build_runner build )
查看>>
LeetCode集锦(二) - reverse integer
查看>>
Java开发者职业生涯要看的200+本书
查看>>
JavaScript 中的 JSON
查看>>
DDD与面向对象设计
查看>>
Remove.bg 免費圖片去背線上工具,5 秒輕鬆幫人物去背景,連我阿嬤都會去背!- TechMoon 科技月球...
查看>>
JavaScript基础知识-(对象)
查看>>
tail: 输出文件的末尾部分
查看>>
小猿圈web前端开发面试需要注意哪些?
查看>>
java之映射
查看>>
Docopt命令行库
查看>>
阿里云数据管理DMS企业版发布年度重大更新 多项功能全面升级
查看>>
BCH(比特币现金):比特币最成功补丁,价值第一的中国区块链项目
查看>>
laravel 多数据库操作
查看>>
小网客博客
查看>>
为python添加tab自动补全功能
查看>>
用Node.js 写web框架(三)
查看>>
强行重置Mysql的账号密码
查看>>