Skip to content

Commit

Permalink
Fix some performances issues #20
Browse files Browse the repository at this point in the history
  • Loading branch information
jefBinomed committed Oct 16, 2018
1 parent dd74215 commit 6ad0e00
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 41 deletions.
50 changes: 30 additions & 20 deletions public/computePositionsWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,38 @@ const MINIMUM_DISTANCE = 100;
const MAXIMUM_DISTANCE = 500;
const SUN_RADIUS = 200;
const planets = [];
/*
// Generate randoms planets for tests
const numPlanets = 50;
for (let i = 0; i < numPlanets; i++){
planets.push({
id: `id${i}`,
name: `name${i}`,
url: "https://pbs.twimg.com/profile_images/973550404456861696/3GMoz0SV_400x400.jpg",
radius: 30 + ((i % 2) === 0 ? -1 * Math.random() * 10 : Math.random() * 10),
distance: 10 + Math.random() * 350,
collision: false,
iterations: 0,
speed: (300 + Math.random() * 100),
init: true,
// Change datas
angle: 0,
score: 0,
x : 0,
y : 0
});
/*
const numPlanets = 800;
let countPlanets = 0;
//for (let i = 0; i < numPlanets; i++){
const arrowCreate = ()=>{
const i = countPlanets;
if (countPlanets >= numPlanets){
return;
}
planets.push({
id: `id${i}`,
name: `name${i}`,
url: "https://pbs.twimg.com/profile_images/973550404456861696/3GMoz0SV_400x400.jpg",
radius: 30 + ((i % 2) === 0 ? -1 * Math.random() * 10 : Math.random() * 10),
distance: 10 + Math.random() * 350,
collision: false,
iterations: 0,
speed: (300 + Math.random() * 100),
init: true,
// Change datas
angle: 0,
score: 0,
x : 0,
y : 0
});
countPlanets++;
setTimeout(arrowCreate,100)
}
setTimeout(arrowCreate,100)
}
//}
*/
let continueLoop = true;
let time = 0;
Expand Down
18 changes: 10 additions & 8 deletions src/components/Galaxy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import ExplosionHelper from '../utils/canvas/ExplosionHelper.js';
export default {
name: 'Galaxy',
props: {
planets: Array
},
data() {
return {
// This is the CanvasRenderingContext that children will draw to.
Expand Down Expand Up @@ -78,19 +75,24 @@ export default {
this.drawSun();
// We take a copy to show planets
const planets = this.planets.slice();
for (let planet of planets){
if (!planet.collision){
this.planetHelper.drawPlanet(planet);
if (this.planets) {
const planets = this.planets.slice();
for (let planet of planets){
if (!planet.collision){
this.planetHelper.drawPlanet(planet);
}
}
this.explosionHelper.renderExplosions();
}
this.explosionHelper.renderExplosions();
this.context.restore();
window.requestAnimationFrame(this.animate.bind(this));
},
explodedPlanet(planet){
this.explosionHelper.explose(planet);
},
setPlanets(planets){
this.planets = planets;
}
},
Expand Down
8 changes: 4 additions & 4 deletions src/utils/canvas/ExplosionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class Explosion {

update() {
for (var i = 0; i < this.particles.length; i++) {
this.particles[i].update();
if (this.particles[i].r < .5) {
this.particles.splice(i, 1)
}
this.particles[i].update();
if (this.particles[i].r < .5) {
this.particles.splice(i, 1)
}
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/views/Countdown.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div id="countdown-container">
<Galaxy ref="galaxy" v-bind:planets="planets"></Galaxy>
<Galaxy ref="galaxy"></Galaxy>
<section id="animation-galaxy"
v-if="countDownStart"
>
Expand Down Expand Up @@ -39,7 +39,6 @@ export default {
data() {
return {
scores : [],
planets: [],
worker: null,
audioPlayer: null,
countDownFinish: false,
Expand Down Expand Up @@ -92,13 +91,13 @@ export default {
});
}, 1000);
this.worker.onmessage = function(e) {
this.worker.onmessage = function workerMessage(e) {
const data = e.data;
switch(data.type){
case 'planets':
this.planets.length = 0;
this.planets.push(...data.data);
data.data.forEach(planet => {
const firebaseBatch = firestore.batch();
data.data.forEach((planet) => {
if (planet.init && planet.collision){
this.$refs['galaxy'].explodedPlanet(planet);
// console.debug('Will Notify destruction of planet : ', planet);
Expand All @@ -111,10 +110,14 @@ export default {
x: 0,
y: 0
}};
firestore.collection("planets").doc(`${planet.id}`).set(planet);
const planetUpdateRef = firestore.collection("planets").doc(`${planet.id}`);
firebaseBatch.set(planetUpdateRef, planet);
}
})
this.scores = this.planets.slice(0,5);
});
firebaseBatch.commit();
// Hack Vue databinding because it cause issue of performance else (change detection mencanism)
this.$refs['galaxy'].setPlanets(data.data);
this.scores = data.data.slice(0,5);
break;
}
}.bind(this);
Expand Down

0 comments on commit 6ad0e00

Please sign in to comment.