代码拉取完成,页面将自动刷新
<!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>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。