Sign in to follow this  
Noturnoo

Smooth Follow Cursor

Recommended Posts

Hi, Pixrs :)
I'm new to PixiJS and would like to ask a question about how to do a delay on mouse move using Pixi (on the element that has the Displacement filter), I removed a part of the example code that is on the official PixiJS website. I created a div (#follow) to show how effect I wish.
Anyway, I would like the element to follow the mouse in a smooth way.

Code is in Codepen:

Thanks

Share this post


Link to post
Share on other sites

It looks like simple LERP (linear interpolation)

speed = 0.1;

dt = speed; // fixed step
dt = 1.0 - Math.exp(1.0 - dt, delta); // if you have a delta time in frame.

const position = sprite.position;
const target = renderer.plugins.interaction.mouse.global;

if (Math.abs(position.x - target.x) + Math.abs(position.y -target.y) < 1) {
    position.copy(target);
}
else
{
    position.x = position.x + (target.x - position.x) * dt;
    position.y = position.y + (target.y - position.y) * dt;
}

I didnt test it. Please try to use it and fix it, and if you fail, I'll help.

Also, not sure about formulaes, I'm doing it by my math sense.

Share this post


Link to post
Share on other sites

Hello Ivan, first of all thanks for helping, I really am beginner and I do not know what I should do, I added this at the end of the code, and it did not work:

 

app.ticker.add(function(delta) {

var speed = 0.1;

var dt = speed; // fixed step
var dt = 1.0 - Math.exp(1.0 - dt, delta); // if you have a delta time in frame.

 const position = circ.position;
const target = app.plugins.interaction.mouse.global;
   
if (Math.abs(position.x - target.x) + Math.abs(position.y -target.y) < 1) {
    position.copy(target);
}
else
{
    position.x = position.x + (target.x - position.x) * dt;
    position.y = position.y + (target.y - position.y) * dt;
}

});

that was it?

Share this post


Link to post
Share on other sites

Still did not work :(
Sprite simply loses position

 


app.ticker.add(function(delta) {

   var speed = 0.8;
   var delta = 1;

var dt = speed; // fixed step
var dt = 1.0 - Math.exp(1.0 - dt, delta); // if you have a delta time in frame.

 const position = displacementSprite.position;
const target = app.renderer.plugins.interaction.mouse.global;
   
   
   console.log(target)
   
if (Math.abs(position.x - target.x) + Math.abs(position.y -target.y) < 1) {
    position.copy(target);
}
else
{
    position.x = position.x + (target.x - position.x) * dt;
    position.y = position.y + (target.y - position.y) * dt;
}
});

 

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.