html canvas 临时画布6,HTML5/Canvas创建多层画布的梦幻泡沫动画

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

/* jshint esversion: 6 */

((main) => {

this.requestAnimationFrame = (() => {

return window.requestAnimationFrame ||

window.webkitRequestAnimationFrame ||

window.mozRequestAnimationFrame ||

window.oRequestAnimationFrame ||

window.msRequestAnimationFrame ||

function(callback) {

window.setTimeout(callback, 1000 / 60);

};

})();

main(this, document, Vector2);

})((window, document, v2, undefined) => {

'use strict';

const PI = Math.PI,

TAU = PI * 2;

const APP_DEFAULTS = {

particleCount: 600,

particleColor: 'rgba(200,200,230,0.5)'

};

class Particle {

constructor(size, speed, context, bounds) {

this.size = size;

this.ctx = context;

this.bounds = bounds;

this.position = new v2();

this.position.randomize(bounds);

this.velocity = new v2(0, speed);

this.velocity.y -= Math.random();

}

reset() {

this.position.y = this.bounds.y + this.size;

this.position.x = Math.random() * this.bounds.x;

}

update() {

this.position.add(this.velocity);

if (this.position.y < -this.size) {

this.reset();

}

}

}

class App {

constructor() {

this.setup();

this.getCanvas();

this.resize();

this.populate();

this.render();

}

setup() {

let self = this;

self.props = Object.assign({}, APP_DEFAULTS);

self.dimensions = new v2();

window.onresize = () => {

self.resize();

};

}

getCanvas() {

this.canvas = {

back: document.querySelector('.back'),

mid: document.querySelector('.mid'),

front: document.querySelector('.front')

};

this.ctx = {

back: this.canvas.back.getContext('2d'),

mid: this.canvas.mid.getContext('2d'),

front: this.canvas.front.getContext('2d')

};

}

resize() {

for (var c in this.canvas) {

this.canvas[c].width = this.dimensions.x = window.innerWidth;

this.canvas[c].height = this.dimensions.y = window.innerHeight;

}

}

populate() {

this.particles = [];

for (let i = 0; i < this.props.particleCount; i++) {

let pCtx = i < 300 ? this.ctx.back : i < 500 ? this.ctx.mid : this.ctx.front,

size = i < 300 ? 5 : i < 500 ? 8 : 12,

speed = i < 300 ? -0.5 : i < 500 ? -1 : -2,

particle = new Particle(size, speed, pCtx, this.dimensions);

this.particles.push(particle);

}

}

render() {

let self = this;

self.draw();

window.requestAnimationFrame(self.render.bind(self));

}

draw() {

for (var c in this.ctx) {

this.ctx[c].clearRect(0, 0, this.dimensions.x, this.dimensions.y);

}

for (let i = 0, len = this.particles.length; i < len; i++) {

let p = this.particles[i];

p.update();

p.ctx.beginPath();

p.ctx.fillStyle = this.props.particleColor;

p.ctx.arc(p.position.x, p.position.y, p.size, 0, TAU);

p.ctx.fill();

p.ctx.closePath();

}

}

}

window.onload = () => {

let app = new App();

};

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值