html5 图片反弹,HTML5/Pixi.js彩虹粒子反弹动画

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

let windowWidth = window.innerWidth;

let windowHeight = window.innerHeight;

const textures = [];

const sparkles = [];

let nSparkles = 0;

let toLaunch = 0;

const spread = 200;

const startX = -200;

const startY = windowHeight - 50;

const maxD = Math.sqrt((startX - startX+spread)*(startX - startX+spread) + (startY - startY+spread)*(startY - startY+spread));

// init stage

const renderer = PIXI.autoDetectRenderer(windowWidth, windowHeight, {

backgroundColor : 0x111111

});

document.body.appendChild(renderer.view);

const stage = new PIXI.Container();

const container = new PIXI.Container();

stage.addChild(container);

container.x = 0;

container.y = 0;

const initTextures = () => {

for (var i = 0; i < 10; i++) {

textures.push(PIXI.Texture.fromImage('https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/rp-' + i + '.png'));

}

}

class Spark {

constructor(texture, scale) {

this.sprite = PIXI.Sprite.from(texture);

this.vel = { x: 0, y: 0 };

this.sprite.x = -20;

this.sprite.y = -20;

this.sprite.width = scale * 15;

this.sprite.height = scale * 15;

this.rebounded = false;

container.addChild(this.sprite);

}

update() {

this.sprite.x += this.vel.x;

this.sprite.y += this.vel.y;

if (this.sprite.x > windowWidth/2 && !this.rebounded) {

this.rebounded = true;

this.vel.y = Math.random();

this.vel.x = Math.random()*-1;

launchRebounds(this.sprite.x, this.sprite.y, this.sprite.texture);

}

if (this.rebounded) {

this.sprite.alpha -= 0.004;

}

}

}

const initSparkles = () => {

const n = windowWidth*7;

for (var i = 0; i < n; i++) {

const sparkle = new Spark(textures[0], Math.random());

sparkles.push(sparkle);

}

nSparkles = n;

}

const getSparkle = () => {

toLaunch++;

if (toLaunch === nSparkles-1) toLaunch = 0;

return sparkles[toLaunch];

}

const launchSparkle = () => {

const sparkle = getSparkle();

const x = startX + Math.random() * spread;

const y = startY + Math.random() * spread;

const d = Math.sqrt((startX - x)*(startX - x) + (startY - y)*(startY - y));

sparkle.sprite.texture = textures[Math.floor(d/maxD*10)];

sparkle.vel.x = 0.9 + Math.random() * 0.2;

sparkle.vel.y = -0.9 + Math.random() * -0.2;

sparkle.sprite.x = x;

sparkle.sprite.y = y;

sparkle.rebounded = false;

sparkle.sprite.alpha = 0.5+Math.random()*0.5;

}

const launchRebounds = (x, y, texture) => {

for (var i = 0; i < 3; i++) {

const sparkle = getSparkle();

sparkle.sprite.texture = texture;

sparkle.vel.x = Math.random()*-1;

sparkle.vel.y = Math.random()*1;

sparkle.sprite.x = x;

sparkle.sprite.y = y;

sparkle.rebounded = true;

sparkle.sprite.alpha = 0.5+Math.random()*0.5;

}

}

const animate = () => {

for (var s = 0; s < 3; s ++) {

launchSparkle();

}

for (var i = 0; i < nSparkles; i++) {

sparkles[i].update();

}

renderer.render(stage);

requestAnimationFrame(animate);

}

const resize = () => {

windowWidth = window.innerWidth;

windowHeight = window.innerHeight;

renderer.view.style.width = windowWidth + 'px';

renderer.view.style.height = windowHeight + 'px';

renderer.resize(windowWidth, windowHeight)

};

initTextures();

initSparkles();

requestAnimationFrame(animate);

window.addEventListener('resize', resize);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR in static/leaflet/libs/pixi/4.8.7/pixi.js static/leaflet/libs/pixi/4.8.7/pixi.js from Terser plugin是指在使用Terser插件时出现了一个错误。具体来说,这个错误是指在压缩和混淆JavaScript代码的过程中,Terser插件无法处理static/leaflet/libs/pixi/4.8.7/pixi.js文件。这可能是由于文件路径错误、文件损坏或版本不兼容等原因造成的。要解决这个问题,可以尝试检查文件路径是否正确,确保文件存在并且没有损坏。另外,还可以尝试升级或降级Terser插件的版本,以解决与pixi.js文件的兼容性问题。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [Leaflet.Pixi:用于Leaflet.js的功能完善的Pixi.js渲染引擎适配器。 [状态=不完整]](https://download.csdn.net/download/weixin_42165490/18455348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [91.(leaflet篇)leaflet态势标绘-进攻方向绘制.rar](https://download.csdn.net/download/m0_60387551/86154209)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值