Jump to content

Zoom and Drag


josescxavier
 Share

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');

 

Link to comment
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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...