From 58ba42d917bfb1542c590143b25b8bd66bdb0269 Mon Sep 17 00:00:00 2001 From: Princiya Date: Sun, 19 Nov 2017 14:03:32 +0530 Subject: [PATCH] remove manual tick --- src/js/viz.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/js/viz.js b/src/js/viz.js index fc53139..8fef09f 100644 --- a/src/js/viz.js +++ b/src/js/viz.js @@ -9,6 +9,7 @@ const viz = { chargeStrength: -100, tickCount: 100, canvasColor: 'white', + alphaStart: 1, alphaTargetStart: 0.1, alphaTargetStop: 0, @@ -47,28 +48,33 @@ const viz = { this.registerSimulationForces(); } else { this.simulation.nodes(this.nodes); + this.resetAlpha(); } this.registerLinkForce(); - this.manualTick(); }, - manualTick() { - this.simulation.alphaTarget(this.alphaTargetStart); - for (let i = 0; i < this.tickCount; i++) { - this.simulation.tick(); + resetAlpha() { + const alpha = this.simulation.alpha(); + const alphaRounded = Math.round((1 - alpha) * 100); + if (alphaRounded === 100) { + this.simulation.alpha(this.alphaStart); + this.restartSimulation(); } - this.stopSimulation(); }, - restartSimulation() { + resetAlphaTarget() { this.simulation.alphaTarget(this.alphaTargetStart); - this.simulation.restart(); + this.restartSimulation(); }, - stopSimulation() { + stopAlphaTarget() { this.simulation.alphaTarget(this.alphaTargetStop); }, + restartSimulation() { + this.simulation.restart(); + }, + registerLinkForce() { const linkForce = d3.forceLink(this.links); linkForce.id((d) => { @@ -403,7 +409,7 @@ const viz = { dragStart() { if (!d3.event.active) { - this.restartSimulation(); + this.resetAlphaTarget(); } d3.event.subject.shadow = true; d3.event.subject.fx = d3.event.subject.x; @@ -419,7 +425,7 @@ const viz = { dragEnd() { if (!d3.event.active) { - this.stopSimulation(); + this.stopAlphaTarget(); } d3.event.subject.x = d3.event.subject.fx; d3.event.subject.y = d3.event.subject.fy;