html鼠标交互,HTML5 Canvas紫雨(可鼠标交互)

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

let particles = [];

function generateParticle(e = performance.now()) {

let p = new Particle();

p.pos.x = random(-0.01, 1.01) * width;

p.pos.y = random(-height_half) - p.r;

if(random() < 0.06) {

let side = random([ 0, 1 ]);

p.pos.x = side * width;

p.pos.y = map(sin(e * 0.001), -1, 1, 0.3, 0.8) * height;

p.acc.x = -(side || -1) * random(3, 7);

p.acc.y = random(-5, -10);

}

particles.push(p);

}

function draw(e) {

if(particles.length < 4000) {

for(let i = 0; i < 9; i++) {

generateParticle(e);

}

}

beginPath();

let gravity = createVector(0, 0.06);

let mouseDownMult = mouseDown ? 1.4 : 0.8;

let maxDist = width * 0.5;

// isPreviewEmbed = () => true;

let mouse = isPreviewEmbed() ? (

mouseIn = e < 2000,

(() => {

let time = e * 0.001 + PI;

let w = width * 0.1;

return createVector(

cos(time) * w + width_half,

sin(time) * w + height * 0.3

)

})()

) : mousePos;

for(let i = particles.length - 1; i >= 0; i--) {

let n = particles[i];

if(n.pos.y - n.r > height) {

particles.splice(i, 1);

generateParticle(e);

continue;

}

if(mouseIn) {

let diff = mouse.copy().sub(n.pos);

let dist = diff.mag();

if(dist < maxDist) {

let limit = mouseDownMult * n.ml * (maxDist - dist) * 0.01;

n.acc.add(diff.limit(limit));

}

}

n.acc.add(gravity);

n.draw();

}

fill(hsl(260, 100, 50));

}

class Particle {

constructor() {

this.pos = createVector();

this.vel = createVector();

this.acc = createVector();

this.r = random(2, 8);

this.fr = map(this.r, 2, 8, 0.997, 0.99);

this.ml = map(this.r, 2, 8, 0.4, 0.05);

}

draw() {

this.vel.add(this.acc).mult(this.fr);

this.acc.set(0, 0);

this.pos.add(this.vel);

rect(this.pos.x, this.pos.y, this.r);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值