From ceb6f13b9f696a040242d292e9191cf1cb6a1afc Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Mon, 23 Mar 2015 22:27:07 -0400
Subject: [PATCH] animation loop methods

---
 js/models/ThreeModel.js    | 26 +++++++++++++++++++++++++-
 js/threeViews/ThreeView.js |  6 +++---
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/js/models/ThreeModel.js b/js/models/ThreeModel.js
index 567dbb49..96ebdeae 100644
--- a/js/models/ThreeModel.js
+++ b/js/models/ThreeModel.js
@@ -14,6 +14,9 @@ function ThreeModel(){
     var parts = [];
     var basePlane = [];
 
+    var animationLoopRunning = false;
+    var stopAnimationFlag = false;
+
     initialize();
 
     function initialize(){
@@ -95,15 +98,36 @@ function ThreeModel(){
         return objectToRemove;
     }
 
+    function startAnimationLoop(){
+        if (animationLoopRunning) return;
+        stopAnimationFlag = false;
+        animationLoopRunning = true;
+        console.log("animation started");
+        _loop();
+    }
+
+    function stopAnimationLoop(){
+        if (!animationLoopRunning) return;
+        stopAnimationFlag = true;
+    }
+
+    function _loop(){
+        if (stopAnimationFlag) return console.log("animation stopped");
+        render();
+        requestAnimationFrame(_loop);
+    }
+
     function render(){
+        if (animationLoopRunning) return;
         renderer.render(scene, camera);
     }
 
     return {//return public properties/methods
         render: render,
+        startAnimationLoop: startAnimationLoop,
+        stopAnimationLoop: stopAnimationLoop,
         sceneRemove: sceneRemove,
         sceneAdd: sceneAdd,
-//        scene: scene,
         domElement: renderer.domElement,
         camera: camera,
         cells: cells,
diff --git a/js/threeViews/ThreeView.js b/js/threeViews/ThreeView.js
index 0087c29b..e2d74f77 100644
--- a/js/threeViews/ThreeView.js
+++ b/js/threeViews/ThreeView.js
@@ -40,7 +40,7 @@ ThreeView = Backbone.View.extend({
         this.$el.append(this.model.domElement);//render only once
 
         this.model.render();
-        this._animate();
+//        this._animate();
     },
 
     ////////////////////////////////////////////////////////////////////////////////
@@ -48,8 +48,8 @@ ThreeView = Backbone.View.extend({
     ////////////////////////////////////////////////////////////////////////////////
 
     _animate: function(){
-        requestAnimationFrame(this._animate);
-        this.controls.update();
+//        requestAnimationFrame(this._animate);
+//        this.controls.update();
     },
 
     _setControlsEnabled: function(){
-- 
GitLab