1 Star 1 Fork 0

ZQY233/原生练习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
小球动画.html 2.83 KB
一键复制 编辑 原始数据 按行查看 历史
ZQY233 提交于 2021-11-25 14:52 . 新增小球动画
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
background-color: #fff;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var can = document.getElementById("canvas")
can.width = window.innerWidth
can.height = window.innerHeight
var canvas = can.getContext("2d")
var balls = [] //存放小球
//创建对象
function ball() {
this.x = null
this.y = null
this.color = null
this.r = null
this.angle = null //小球偏移量
this.anglex = null
this.angley = null
//初始状态的小球
this.int = function(X, Y) {
this.x = X
this.y = Y
this.color = this.randomcolor()
this.r = this.randomR(10, 15)
this.angle = Math.random() * (Math.PI * 2)
this.anglex = this.randomR(5, 15) * Math.cos(this.angle)
this.angley = this.randomR(5, 15) * Math.sin(this.angle)
}
//随机颜色
this.randomcolor = function() {
return "#" + parseInt(Math.random() * 16777216).toString(16)
}
//随机数字 可控制半径或xy移动量
this.randomR = function(min, max) {
return Math.random() * max + min
}
//小球的运动及偏移量
this.move = function() {
this.x += this.anglex
this.y += this.angley
this.r -= 0.3
this.anglex *= 0.9
this.angley *= 0.9
}
}
//创建小球
function createball(X, Y) {
var count = parseInt(Math.random() * 30 + 10)
for (var i = 0; i < count; i++) {
var Ball = new ball()
Ball.int(X, Y)
balls.push(Ball)
}
}
//在canvas上绘制小球
function draw() {
for (var i = 0; i < balls.length; i++) {
var b = balls[i]
b.move()
canvas.beginPath()
canvas.arc(b.x, b.y, b.r, 0, Math.PI * 2, true)
canvas.fillStyle = b.color
canvas.fill()
canvas.closePath()
}
}
//移除小球
function remove() {
for (var i = 0; i < balls.length; i++) {
var b = balls[i]
if (b.r < 0.3) {
balls.splice(i, 1)
i--
}
}
}
//计时器
time()
function time() {
canvas.clearRect(0, 0, can.width, can.height)
draw()
remove()
window.requestAnimationFrame(time)
}
can.onmouseup = function(e) {
var x1 = e.pageX
var y1 = e.pageY
createball(x1, y1)
}
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zqylzcwcxy/native-practice.git
[email protected]:zqylzcwcxy/native-practice.git
zqylzcwcxy
native-practice
原生练习
master

搜索帮助