Browse Source

optimize: 调用方式

shiqi.li 6 years ago
parent
commit
85e469de41
3 changed files with 17 additions and 33 deletions
  1. 15 25
      js/tpanorama.js
  2. 1 4
      views/index.html
  3. 1 4
      views/page1.html

+ 15 - 25
js/tpanorama.js

@@ -30,16 +30,15 @@
 
 
 
 
     function tpanorama(opt) {
     function tpanorama(opt) {
-        this.config(opt);
+        this.render(opt);
     }
     }
 
 
     tpanorama.prototype = {
     tpanorama.prototype = {
         constructor: this,
         constructor: this,
         def: {},
         def: {},
-        config: function (opt) {
+        render: function (opt) {
             this.def = extend(options, opt, true);
             this.def = extend(options, opt, true);
-        },
-        init: function () {
+            document.getElementById(this.def.container).innerHTML = '';
             _lables = [];
             _lables = [];
             initContainer(this.def.container);
             initContainer(this.def.container);
             initCamera();
             initCamera();
@@ -47,27 +46,18 @@
             makePanorama(this.def.pRadius, this.def.widthSegments, this.def.heightSegments, this.def.url);
             makePanorama(this.def.pRadius, this.def.widthSegments, this.def.heightSegments, this.def.url);
             initRenderer();
             initRenderer();
             initLable(this.def.lables, this.def.showlable);
             initLable(this.def.lables, this.def.showlable);
-            var that = this;
-            if (_count1 == 1) {
-                _container.addEventListener('mousedown', onDocumentMouseDown, false);
-                _container.addEventListener('mousemove', onDocumentMouseMove, false);
-                _container.addEventListener('mouseup', onDocumentMouseUp, false);
-                _container.addEventListener('mousewheel', function (e) {
-                    var then = that;
-                    onDocumentMouseWheel(e, then.def.minFocalLength, then.def.maxFocalLength);
-                }, false);
-                _container.addEventListener('DOMMouseScroll', function (e) {
-                    onDocumentMouseWheel(e, then.def.minFocalLength, then.def.maxFocalLength);
-                }, false);
-                _container.addEventListener('click', onDocumentMouseClick, false);
-                global.addEventListener('resize', onWindowResize, false);
-
-                animate();
-            }
-            _count1++;
-        },
-        clean: function () {
-            document.getElementById(this.def.container).innerHTML = '';
+            _container.addEventListener('mousedown', onDocumentMouseDown, false);
+            _container.addEventListener('mousemove', onDocumentMouseMove, false);
+            _container.addEventListener('mouseup', onDocumentMouseUp, false);
+            _container.addEventListener('mousewheel', (e) => {
+                onDocumentMouseWheel(e, this.def.minFocalLength, this.def.maxFocalLength);
+            }, false);
+            _container.addEventListener('DOMMouseScroll', (e) => {
+                onDocumentMouseWheel(e, this.def.minFocalLength, this.def.maxFocalLength);
+            }, false);
+            _container.addEventListener('click', onDocumentMouseClick, false);
+            global.addEventListener('resize', onWindowResize, false);
+            animate();
         }
         }
     }
     }
 
 

+ 1 - 4
views/index.html

@@ -48,7 +48,6 @@
                 showlable: 'show' // show,click
                 showlable: 'show' // show,click
             }
             }
             tp = new tpanorama(opt);
             tp = new tpanorama(opt);
-            tp.init();
         }
         }
 
 
         function changeImg(name) {
         function changeImg(name) {
@@ -63,9 +62,7 @@
                 opt.lables = [{ position: { lon: 48.96, lat: -20.16 }, text: '樱花' }]
                 opt.lables = [{ position: { lon: 48.96, lat: -20.16 }, text: '樱花' }]
             }
             }
             opt.url = 'img/' + name + '.png';
             opt.url = 'img/' + name + '.png';
-            tp.clean();
-            tp.config(opt);
-            tp.init();
+            tp.render(opt);
         }
         }
 
 
     </script>
     </script>

+ 1 - 4
views/page1.html

@@ -48,7 +48,6 @@
                 showlable: 'show' // show,click
                 showlable: 'show' // show,click
             }
             }
             tp = new tpanorama(opt);
             tp = new tpanorama(opt);
-            tp.init();
         }
         }
 
 
         function changeImg(name) {
         function changeImg(name) {
@@ -63,9 +62,7 @@
                 opt.lables = [{ position: { lon: 48.96, lat: -20.16 }, text: '樱花' }]
                 opt.lables = [{ position: { lon: 48.96, lat: -20.16 }, text: '樱花' }]
             }
             }
             opt.url = 'img/' + name + '.png';
             opt.url = 'img/' + name + '.png';
-            tp.clean();
-            tp.config(opt);
-            tp.init();
+            tp.render(opt);
         }
         }
 
 
     </script>
     </script>