josescxavier

Zoom and Drag

Recommended Posts

Hi,

My goal is to create a app where I can add points to an image. During the process I'll need to zoom in/out the image to accuratly place the points. Right now I can move the image(pink grid), zoom in/out, and the bunny maintain it's position on the grid. I can move the bunny clicking on it and it still works great.

ChakAY0.png

I was able to do most of it with the following code. However after add a container, (bunnies) I lost the onDragMove function on the bunny. I tried to set up a jsfiddle without success. When I try to drag the bunny the this.dragging variable is undefined.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Hello World</title>
</head>
<body id='pixiCanvas'>
  <script src="pixi.js"></script>
  <script src="https://d3js.org/d3.v4.js"></script>
  <script type="text/javascript">

    var app = new PIXI.Application(800, 600, {backgroundColor : 0x1099bb});
    document.body.appendChild(app.view);

    // create a texture from an image path
    var texture = PIXI.Texture.fromImage('assets/bunny.png');
    var textureGrid = PIXI.Texture.fromImage('grid.png');

    var bunny_x = 100;
    var bunny_y = 100;

    // Scale mode for pixelation
    texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;

    var grid = new PIXI.Sprite(textureGrid);
    app.stage.addChild(grid);

    var zoom_handler = d3.zoom().scaleExtent([1, 8]).on("zoom", zoom_actions);
    
    function zoom_actions() {
        if(bunny.dragging == false){
            console.log("zoom: ",bunny_x,bunny_y)
            grid.scale.set(d3.event.transform.k)
            grid.position.set(d3.event.transform.x,d3.event.transform.y)
            bunnies.scale.set(d3.event.transform.k)
            bunnies.position.set(d3.event.transform.x,d3.event.transform.y)
        }
    }

    var pixiCanvas = d3.select('#pixiCanvas');
    pixiCanvas.call(zoom_handler);

    var bunny = new PIXI.Sprite(texture);
    bunny.interactive = true;
    bunny.buttonMode = true;
    bunny.anchor.set(0.5);
    bunny.scale.set(1);
    bunny.x = bunny_x;
    bunny.y = bunny_y;

    bunny
        .on('pointerdown', onDragStart)
        .on('pointerup', onDragEnd)
        .on('pointerupoutside', onDragEnd)
        .on('pointermove', onDragMove);

    bunny.click = function() {
        console.log("bunny.click");
        this.x += 5;
    }

    var bunnies = new PIXI.Container();
    bunnies.addChild(bunny);
    // add it to the stage
    app.stage.addChild(bunnies);

    function onDragStart(event) {
        console.log("onDragStart: ",bunny_x,bunny_y);
        this.data = event.data;
        this.alpha = 0.5;
        this.dragging = true;
    }

    function onDragEnd() {
        console.log("onDragEnd: ",bunny_x,bunny_y);
        this.alpha = 1;
        this.dragging = false;
        this.data = null;
    }

    function onDragMove() {
        console.log("onDragMove: ",bunny_x,bunny_y);
        if (this.dragging) {
            console.log("dragging: ",bunny_x,bunny_y);
            var newPosition = this.data.getLocalPosition(this.parent);
            this.x = newPosition.x;
            bunny_x = newPosition.x;
            this.y = newPosition.y;
            bunny_y = newPosition.y;
        }
    }

  </script>
</body>
</html>

If you want to help me set up a jsfiddle here are all the links with the images:

var texture = PIXI.Texture.fromImage('https://s26.postimg.org/heslqn655/bunny.png');
var textureGrid = PIXI.Texture.fromImage('https://s26.postimg.org/nr7r0h97d/grid.png');

 

Share this post


Link to post
Share on other sites

I'm a newb, so don't listen to me.

But I'd try attaching your mouse event handlers to bunnies.bunny instead of just bunny? Maybe the mouse events aren't triggering because it's just hitting the container instead of the bunny sprite.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.