var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight,
mousePos =
{x: 400,
y:
300}, // tạo ra vải canvas = document.createElement ('vải'), context = canvas.getContext ('2d') , hạt = [], tên lửa = [], MAX_PARTICLES = 400, Colorcode = 0; // init $ (document) .ready (function () {document.body.appendChild (vải); canvas.width = SCREEN_WIDTH; canvas. height = SCREEN_HEIGHT; setInterval (ra mắt, 800); setInterval (loop, 1000-1050);}); // vị trí cập nhật chuột $ (document) .mousemove (function (e) {e.preventDefault (); mousePos = {x : e.clientX, y: e.clientY};}); // khởi động nhiều tên lửa !!! $ (document) .mousedown (function (e) {for (var i = 0; i <5; i ++) {launchFrom (Math.random () * SCREEN_WIDTH * 2/3 + SCREEN_WIDTH / 6);}}); chức năng khởi động () {launchFrom (mousePos.x);} function launchFrom (x) {if (rockets.length <10) { var = tên lửa Rocket mới (x); rocket.explosionColor = Math.floor (Math.random () * 360/10) * 10; rocket.vel.y = Math.random () * -3 - 4; rocket.vel .x = Math.random () * 6-3; rocket.size = 8; rocket.shrink = 0,999; rocket.gravity = 0,01; rockets.push (tên lửa);}} chức năng loop () {// cập nhật kích thước màn hình if (SCREEN_WIDTH = window.innerWidth!) {canvas.width = SCREEN_WIDTH = window.innerWidth;} if (SCREEN_HEIGHT = window.innerHeight!) {canvas.height = SCREEN_HEIGHT = window.innerHeight;} // rõ ràng vải context.fillStyle = "rgba (0, 0, 0, 0.05)"; context.fillRect (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); var existingRockets = []; for (var i = 0; i <rockets.length; i ++) {// cập nhật và đưa ra tên lửa [i] .update (); rocket [i] .render (context); // tính toán khoảng cách với Pythagoras var khoảng cách = Math.sqrt (Math.pow (mousePos.x - tên lửa [ i] .pos.x, 2) + Math.pow (mousePos.y - tên lửa [i] .pos.y, 2)); // cơ hội ngẫu nhiên 1% nếu tên lửa là ở trên giữa var randomChance = tên lửa [i ] .pos.y <(SCREEN_HEIGHT * 2/3)? (Math.random () * 100 <= 1): false; / * quy tắc Explosion - 80% của màn hình - đi xuống - gần với chuột - 1% cơ hội bùng nổ ngẫu nhiên * / if (tên lửa [i] .pos. y <SCREEN_HEIGHT / 5 || rocket [i] .vel.y> = 0 || khoảng cách <50 || randomChance) {tên lửa [i] .explode ();} else {existingRockets.push (tên lửa [i]); }} rocket = existingRockets; var existingParticles = []; for (var i = 0; i <particles.length; i ++) {hạt [i] .update (); // vẽ lại và lưu các hạt có thể được trả lại nếu (hạt [i] .exists ()) {hạt [i] .render (context); existingParticles.push (hạt [i]);}} // cập nhật mảng với các hạt hiện có - hạt cũ cần được thu gom rác thải hạt = existingParticles; trong khi (particles.length> MAX_PARTICLES) {particles.shift ();}} chức năng Particle (pos) {this.pos = {x: pos? pos.x: 0, y: pos? pos.y: 0}; this.vel = {x: 0, y: 0}; this.shrink = .97; this.size = 2; this.resistance = 1; this.gravity = 0; this.flick = false; this.alpha = 1; this.fade = 0; this.color = 0;} Particle.prototype.update = function () {// áp dụng kháng this.vel.x * = this.resistance; this.vel. y * = this.resistance; // trọng lực xuống this.vel.y + = this.gravity; // vị trí cập nhật dựa trên tốc độ this.pos.x + = this.vel.x; this.pos.y + = này .vel.y; // co this.size * = this.shrink; // phai ra this.alpha - = this.fade;}; = function Particle.prototype.render (c) {if (! this.exists ( )) {return;} c.save (); c.globalCompositeOperation = 'nhẹ'; var x = this.pos.x, y = this.pos.y, r = this.size / 2; var Gradient = c. createRadialGradient (x, y, 0,1, x, y, r); gradient.addColorStop (0,1, "rgba (255.255.255," + this.alpha + ")"); gradient.addColorStop (0,8, "HSLA (" + này. màu + ", 100%, 50%," + this.alpha + ")"); gradient.addColorStop (1, "HSLA (" + this.color + ", 100%, 50%, 0,1)"); c .fillStyle = gradient; c.beginPath (); c.arc (this.pos.x, this.pos.y, this.flick? Math.random () * this.size: this.size, 0, Math.PI * 2, true); c.closePath (); c.fill (); c.restore ();}; Particle.prototype.exists = function () {return this.alpha> = 0.1 && this.size> = 1;}; chức năng Rocket (x) {Particle.apply (this, [{x: x, y: SCREEN_HEIGHT}]); this.explosionColor = 0;} Rocket.prototype = new Particle (); Rocket.prototype.constructor = Rocket; Rocket.prototype.explode = function () {var count = Math.random () * 10 + 80; for (var i = 0 ; i <count; i ++) {var hạt = Particle mới (this.pos); góc var = Math.random () * Math.PI * 2; // chước hiệu ứng 3D bằng cách sử dụng cosin và đặt hạt hơn ở giữa var tốc độ = Math.cos (Math.random () * Math.PI / 2) * 15; particle.vel.x = Math.cos (góc) * Tốc độ; particle.vel.y = Math.sin (góc) * Tốc độ ; particle.size = 10; particle.gravity = 0,2; particle.resistance = 0,92; particle.shrink = Math.random () * 0,05 + 0,93; particle.flick = true; particle.color = this.explosionColor; particles.push (hạt);}}; Rocket.prototype.render = function (c) {nếu {(this.exists ()) return;} c.save (); c.globalCompositeOperation = 'nhẹ'; var x = này. pos.x, y = this.pos.y, r = this.size / 2; var Gradient = c.createRadialGradient (x, y, 0,1, x, y, r); gradient.addColorStop (0,1, "rgba (255 , 255, 255, "+ this.alpha +") "); gradient.addColorStop (1," rgba (0, 0, 0, "+ this.alpha +") "); c.fillStyle = gradient; c. beginPath (); c.arc (this.pos.x, this.pos.y, this.flick? Math.random () * this.size / 2 + this.size / 2: this.size, 0, Math.PI * 2, true); c.closePath (); c.fill (); c.restore () ;};
đang được dịch, vui lòng đợi..
