Jump to content

Easing on mouse movement


SpaceWorm
 Share

Recommended Posts

I've got a small app that so far loads a rectangular texture into an object which moves based on the current position of the mouse; if the mouse moves toward it, it gets closer and vice-versa.  I wanted to use an easing function to delay and smooth out the movement a bit.  It's a quart out function, so the object should initially move quickly toward the mouse and then slowly just at the end.  However, for some reason, it seems like movement is just happening 1:1 and just following the mouse movements without any easing.  I'm new to PIXI(and, honestly js in general), so I'm not sure if there's something basic I'm missing here that explains where I'm going wrong.  My code is attached below.  Any help is appreciated, thanks!

        let app;
        let mouseX;
        let mouseY;
        let resetting = false;

        let appContainer;
        
        let navBoxes = [];

        class NavBox extends PIXI.Sprite{
            constructor(startX, startY, texture, name, text, endpoint){
                super(texture);
                this.anchor.set(0.5);

                this.startX = startX;
                this.startY = startY;
                this.targetX = startX;
                this.targetY = startY;
                this.bX = startX;
                this.bY = startY;
                this.x = startX;
                this.y = startY;

                this.moveDelta = 0;

                this.name = name;
                this.text = text;
                this.endpoint = endpoint;
            }

            setTargets(tarX, tarY){
                this.moveDelta = 0;

                this.bX = this.x;
                this.bY = this.y;

                this.targetX = tarX;
                this.targetY = tarY;
            }

            move(delta){
                if(this.moveDelta < 2000){
                    this.moveDelta += delta;
                }
                else{
                    this.moveDelta = 2000;
                }

                this.x += (this.targetX - this.x) * (1 - Math.pow(this.moveDelta/2000, 4));
                this.y += (this.targetY - this.y) * (1 - Math.pow(this.moveDelta/2000, 4));
            }

            reset(){
                this.setTargets(this.startX, this.startY);
            }
        }

        window.onload = function () {
            app = new PIXI.Application(
                {
                    width: 1000,
                    height: 300,
                    backgroundColor: 0xAAAAAA
                });

            document.querySelector("#headerDiv").appendChild(app.view);

            app.loader.baseUrl = "img"
            app.loader
                .add("test", "testBtn.png");
            app.loader.onComplete.add(doneLoading);
            app.loader.load();

            
        };

        function doneLoading(){
            appContainer = new PIXI.Container();
            appContainer.interactive = true;
            app.stage.addChild(appContainer);
            appContainer.on('mousemove', mouseMoved);

            let tempBox = new NavBox(150, 50, app.loader.resources.test.texture, "testButton", "Some Text", "someendpoint");
            navBoxes.push(tempBox);
            appContainer.addChild(tempBox);
            app.ticker.add(gameLoop);
        }

        function mouseMoved(event){
            mouseX = event.data.global.x;
            mouseY = event.data.global.y;
            
            navBoxes.forEach(function(box){
                if(mouseX < app.view.width && mouseX > 0 && mouseY > 0 & mouseY < app.view.height){
                    resetting = false;
                    box.setTargets(box.startX - (mouseX - app.view.width/2)/3, box.startY - (mouseY - app.view.height/2)/4);
                }
                else{
                    if(resetting){}
                    else{
                        resetting = true;
                        box.reset();
                    }
                    
                }
            });
            
        }

        function gameLoop(delta){
            navBoxes.forEach(function(box){
                if(box.targetX !== undefined && box.targetY !== undefined){
                    box.move(delta);
                }
            });
            
        }

 

Link to comment
Share on other sites

yes, its in "renderer.view". If you dont know where renderer is, or ticker, or any other thing you would make if you wrote it from scratch - here is the article: https://github.com/pixijs/pixi.js/wiki/v5-Custom-Application-GameLoop

You can also pass existing canvas to renderer/application options.

Edited by ivan.popelyshev
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...