2 Star 18 Fork 4

JackWhh/flow-cat-public

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Flower.js 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
JackWhh 提交于 2022-11-18 18:03 . init
const electron = require('@electron/remote')
const workAreaSize = electron.screen.getPrimaryDisplay().workAreaSize
class Flower {
constructor(url) {
this._img = document.createElement('img')
this._img.src = `img/flower/${url}`
this._img.width = Math.random() * 40 + 5
this.x = 100
this.y = 80
this.rotaion = 0
this.speedX = (Math.random()*6 + 5)
this.speedY = (Math.random()*6 + 9)
this.speedRotation = (Math.random() + 1) * (Math.random() > 0.5 ? 1 : -1)
this.move()
}
get img() {
return this._img;
}
move() {
this.speedX = (this.speedX - 0.1) > 1 ? (this.speedX - 0.1): this.speedX
this.x += this.speedX
this.speedY = (this.speedY - 0.3) > 1 ? (this.speedY - 0.5) :this.speedY
if (this.x > (workAreaSize.width/4)) {
this.speedY = -(Math.random()*2)
}
this.y += this.speedY
this.rotaion += this.speedRotation
this.img.style.transform = `translate(-${this.x}px, -${this.y}px) rotate(${this.rotaion}deg)`
if (this.y <= 50 || Math.abs(this.x+50) >= (workAreaSize.width*0.5) || Math.abs(this.y+50) >= (workAreaSize.height*0.5)) {
if (this._img.parentNode) {
this._img.parentNode.removeChild(this._img)
}
}else {
// 以屏幕刷新频率执行
requestAnimationFrame(() => {
this.move()
})
}
}
}
module.exports = Flower;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jack_whh/flow-cat-public.git
git@gitee.com:jack_whh/flow-cat-public.git
jack_whh
flow-cat-public
flow-cat-public
main

搜索帮助