Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- initialize_pan_and_zoom: function(){
- EDITOR.PIXI_APPLICATION.stage.interactive = true;
- on_wheel(EDITOR.PIXI_APPLICATION.renderer, EDITOR.PIXI_APPLICATION.stage);
- function on_wheel(renderer, stage){
- renderer.view.addEventListener("wheel", function(e) {
- //let zoom_in = e.deltaY < 0 ? true : false;
- let zoom_in = e.deltaY < 0; //simplified
- let zoom_factor;
- if (zoom_in) {
- zoom_factor = 1.1;
- } else {
- zoom_factor = (1/1.1);
- }
- //zoom
- stage.scale.x *= zoom_factor;
- stage.scale.y *= zoom_factor;
- //center on cursor
- let mouse_loc = renderer.plugins.interaction.eventData.data.global;
- stage.x -= (mouse_loc.x - stage.x) * (zoom_factor - 1);
- stage.y -= (mouse_loc.y - stage.y) * (zoom_factor - 1);
- correct(renderer, stage);
- renderer.render(stage);
- e.preventDefault();
- });
- }
- EDITOR.PIXI_APPLICATION.renderer.view.addEventListener("mousedown", function(e) {
- if(e.which == 3){
- EDITOR.CURRENT_MOUSE_LOCATION = EDITOR.PIXI_APPLICATION.renderer.plugins.interaction.eventData.data.global;
- EDITOR.PAN_LOCATION = [
- EDITOR.CURRENT_MOUSE_LOCATION.x,
- EDITOR.CURRENT_MOUSE_LOCATION.y
- ];
- EDITOR.PIXI_APPLICATION.ticker.add(apply_pan);
- e.preventDefault();
- }
- });
- EDITOR.PIXI_APPLICATION.renderer.view.addEventListener("mouseup", function(e) {
- if(e.which == 3) {
- EDITOR.CURRENT_MOUSE_LOCATION = EDITOR.PIXI_APPLICATION.renderer.plugins.interaction.eventData.data.global;
- EDITOR.PAN_LOCATION = [
- EDITOR.CURRENT_MOUSE_LOCATION.x,
- EDITOR.CURRENT_MOUSE_LOCATION.y
- ];
- EDITOR.PIXI_APPLICATION.ticker.remove(apply_pan);
- e.preventDefault();
- }
- });
- function apply_pan(){
- let NEW_MOUSE_LOCATION = EDITOR.PIXI_APPLICATION.renderer.plugins.interaction.eventData.data.global;
- let LAST_PAN_LOCATION = EDITOR.PAN_LOCATION;
- let diff_x = NEW_MOUSE_LOCATION.x - LAST_PAN_LOCATION[0];
- let diff_y = NEW_MOUSE_LOCATION.y - LAST_PAN_LOCATION[1];
- EDITOR.PAN_LOCATION = [NEW_MOUSE_LOCATION.x, NEW_MOUSE_LOCATION.y];
- EDITOR.PIXI_APPLICATION.stage.x += diff_x;
- EDITOR.PIXI_APPLICATION.stage.y += diff_y;
- correct(
- EDITOR.PIXI_APPLICATION.renderer,
- EDITOR.PIXI_APPLICATION.stage
- );
- }
- function correct(renderer, stage) {
- //keep in frame
- stage.x = Math.min(0, stage.x);
- stage.y = Math.min(0, stage.y);
- //keep width in bounds
- let visible_width = (renderer.width * stage.scale.x) + stage.x;
- if (visible_width < renderer.view.width) {
- stage.x = Math.min(0, renderer.view.width - (renderer.width * stage.scale.x));
- if (stage.x == 0) {
- stage.scale.x = renderer.view.width / renderer.width;
- }
- }
- //keep height in bounds
- let visible_height = (renderer.height * stage.scale.y) + stage.y;
- if (visible_height < renderer.view.height) {
- stage.y = Math.min(0, renderer.view.height - (renderer.height * stage.scale.y));
- if (stage.y == 0) {
- stage.scale.y = renderer.view.height / renderer.height;
- }
- }
- //keep aspect ratio
- if (stage.scale.y != stage.scale.x) {
- stage.scale.x = Math.max(stage.scale.x, stage.scale.y);
- stage.scale.y = Math.max(stage.scale.x, stage.scale.y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment