josescxavier Posted September 28, 2017 Share Posted September 28, 2017 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. 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'); Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted September 28, 2017 Share Posted September 28, 2017 May be d3 is stopping this event. I dont have any guarantees on d3 usage with pixi, look in their code please. Quote Link to comment Share on other sites More sharing options...
josescxavier Posted September 29, 2017 Author Share Posted September 29, 2017 20 hours ago, ivan.popelyshev said: May be d3 is stopping this event. I dont have any guarantees on d3 usage with pixi, look in their code please. Will take a look Quote Link to comment Share on other sites More sharing options...
NITWIT Posted October 3, 2017 Share Posted October 3, 2017 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. Quote Link to comment Share on other sites More sharing options...
josescxavier Posted October 11, 2017 Author Share Posted October 11, 2017 When start the drag action I remove the zoom handler until the drag stop and it works great Quote selection.on(".zoom", null); ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted October 11, 2017 Share Posted October 11, 2017 Congrats! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.