Guest User

Fixed Panning

a guest
Feb 7th, 2017
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.         initialize_pan_and_zoom: function(){
  3.  
  4.             EDITOR.PIXI_APPLICATION.stage.interactive = true;
  5.  
  6.             on_wheel(EDITOR.PIXI_APPLICATION.renderer, EDITOR.PIXI_APPLICATION.stage);
  7.  
  8.             function on_wheel(renderer, stage){
  9.                 renderer.view.addEventListener("wheel", function(e) {
  10.                     //let zoom_in = e.deltaY < 0 ? true : false;
  11.                     let zoom_in = e.deltaY < 0; //simplified
  12.                     let zoom_factor;
  13.                     if (zoom_in) {
  14.                         zoom_factor = 1.1;
  15.                     } else {
  16.                         zoom_factor = (1/1.1);
  17.                     }
  18.  
  19.                     //zoom
  20.                     stage.scale.x *= zoom_factor;
  21.                     stage.scale.y *= zoom_factor;
  22.  
  23.                     //center on cursor
  24.                     let mouse_loc = renderer.plugins.interaction.eventData.data.global;
  25.                     stage.x -= (mouse_loc.x - stage.x) * (zoom_factor - 1);
  26.                     stage.y -= (mouse_loc.y - stage.y) * (zoom_factor - 1);
  27.  
  28.                     correct(renderer, stage);
  29.  
  30.                     renderer.render(stage);
  31.                     e.preventDefault();
  32.                 });
  33.             }
  34.  
  35.  
  36.             EDITOR.PIXI_APPLICATION.renderer.view.addEventListener("mousedown", function(e) {
  37.                 if(e.which == 3){
  38.                     EDITOR.CURRENT_MOUSE_LOCATION = EDITOR.PIXI_APPLICATION.renderer.plugins.interaction.eventData.data.global;
  39.                     EDITOR.PAN_LOCATION = [
  40.                         EDITOR.CURRENT_MOUSE_LOCATION.x,
  41.                         EDITOR.CURRENT_MOUSE_LOCATION.y
  42.                     ];
  43.                     EDITOR.PIXI_APPLICATION.ticker.add(apply_pan);
  44.                     e.preventDefault();
  45.                 }
  46.             });
  47.  
  48.  
  49.             EDITOR.PIXI_APPLICATION.renderer.view.addEventListener("mouseup", function(e) {
  50.                 if(e.which == 3) {
  51.                     EDITOR.CURRENT_MOUSE_LOCATION = EDITOR.PIXI_APPLICATION.renderer.plugins.interaction.eventData.data.global;
  52.                     EDITOR.PAN_LOCATION = [
  53.                         EDITOR.CURRENT_MOUSE_LOCATION.x,
  54.                         EDITOR.CURRENT_MOUSE_LOCATION.y
  55.                     ];
  56.                     EDITOR.PIXI_APPLICATION.ticker.remove(apply_pan);
  57.                     e.preventDefault();
  58.                 }
  59.             });
  60.  
  61.             function apply_pan(){
  62.                 let NEW_MOUSE_LOCATION = EDITOR.PIXI_APPLICATION.renderer.plugins.interaction.eventData.data.global;
  63.                 let LAST_PAN_LOCATION = EDITOR.PAN_LOCATION;
  64.                 let diff_x = NEW_MOUSE_LOCATION.x - LAST_PAN_LOCATION[0];
  65.                 let diff_y = NEW_MOUSE_LOCATION.y - LAST_PAN_LOCATION[1];
  66.                 EDITOR.PAN_LOCATION = [NEW_MOUSE_LOCATION.x, NEW_MOUSE_LOCATION.y];
  67.                 EDITOR.PIXI_APPLICATION.stage.x += diff_x;
  68.                 EDITOR.PIXI_APPLICATION.stage.y += diff_y;
  69.                 correct(
  70.                     EDITOR.PIXI_APPLICATION.renderer,
  71.                     EDITOR.PIXI_APPLICATION.stage
  72.                 );
  73.             }
  74.  
  75.             function correct(renderer, stage) {
  76.                 //keep in frame
  77.                 stage.x = Math.min(0, stage.x);
  78.                 stage.y = Math.min(0, stage.y);
  79.  
  80.                 //keep width in bounds
  81.                 let visible_width = (renderer.width * stage.scale.x) + stage.x;
  82.                 if (visible_width < renderer.view.width) {
  83.                     stage.x = Math.min(0, renderer.view.width - (renderer.width * stage.scale.x));
  84.                     if (stage.x == 0) {
  85.                         stage.scale.x = renderer.view.width / renderer.width;
  86.                     }
  87.                 }
  88.  
  89.                 //keep height in bounds
  90.                 let visible_height = (renderer.height * stage.scale.y) + stage.y;
  91.                 if (visible_height < renderer.view.height) {
  92.                     stage.y = Math.min(0, renderer.view.height - (renderer.height * stage.scale.y));
  93.                     if (stage.y == 0) {
  94.                         stage.scale.y = renderer.view.height / renderer.height;
  95.                     }
  96.                 }
  97.  
  98.                 //keep aspect ratio
  99.                 if (stage.scale.y != stage.scale.x) {
  100.                     stage.scale.x = Math.max(stage.scale.x, stage.scale.y);
  101.                     stage.scale.y = Math.max(stage.scale.x, stage.scale.y);
  102.                 }
  103.             }
  104.         }
Advertisement
Add Comment
Please, Sign In to add comment