Zoom and Drag

Recommended Posts


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.


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>
  <meta charset="utf-8">
  <title>Hello World</title>
<body id='pixiCanvas'>
  <script src="pixi.js"></script>
  <script src=""></script>
  <script type="text/javascript">

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

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

    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)

    var pixiCanvas ='#pixiCanvas');;

    var bunny = new PIXI.Sprite(texture);
    bunny.interactive = true;
    bunny.buttonMode = true;
    bunny.x = bunny_x;
    bunny.y = bunny_y;

        .on('pointerdown', onDragStart)
        .on('pointerup', onDragEnd)
        .on('pointerupoutside', onDragEnd)
        .on('pointermove', onDragMove); = function() {
        this.x += 5;

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

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

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

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


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

var texture = PIXI.Texture.fromImage('');
var textureGrid = PIXI.Texture.fromImage('');


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.