Share Posted December 27, 2021 See the Codepen. Obviously when u click, obj animates to the value target had on click. But if I wanted obj to animate to whatever value target would have at the end of the 1 second duration, how would you approach that? My initial thought is doing a lerp, but I wanted to see if GSAP has some clever functionality for this case first. I think FLIP can do something similar? Code from the Codepen: const target = { x: 0, y : 0 } const obj = { x: 0, y: 0 } const targetEl = document.querySelector('.js-target') const objEl = document.querySelector('.js-obj') objEl.textContent = `x: ${obj.x}, y: ${obj.y}` function tick() { target.x = gsap.utils.wrap(0, 1000, target.x + 1) target.y = gsap.utils.wrap(0, 1000, target.y + 1) targetEl.textContent = `x: ${target.x}, y: ${target.y}` } gsap.ticker.add(tick) document.addEventListener('click', () => { gsap.to(obj, { duration: 1, x: target.x, y: target.y, onUpdate: () => { objEl.textContent = `x: ${obj.x}, y: ${obj.y}` } }) }) See the Pen caafcd930a74495c2c83fa0b50d98a9c by ReGGae (@ReGGae) on CodePen Link to comment Share on other sites More sharing options...
Share Posted December 27, 2021 I don't think there is an easy way to figure that out. One problem is that you are updating everything inside a ticker function, but how often that updates is going to vary based refresh rate of your monitor and fps. If your monitor runs at 60fps and mine runs at 120fps, we're going to have wildly different target values after 1 second. I had to add in extra calculation here to normalize the framerate difference when doing lerp, but it's probably not 100% accurate. See the Pen WNNNBpo by GreenSock (@GreenSock) on CodePen Link to comment Share on other sites More sharing options...
Author Share Posted December 27, 2021 1 hour ago, OSUblake said: I don't think there is an easy way to figure that out. One problem is that you are updating everything inside a ticker function, but how often that updates is going to vary based refresh rate of your monitor and fps. If your monitor runs at 60fps and mine runs at 120fps, we're going to have wildly different target values after 1 second. I had to add in extra calculation here to normalize the framerate difference when doing lerp, but it's probably not 100% accurate. I tried lerping towards the target on click instead but since im wrapping it didnt work as I wanted, it probs would if I didnt do that tho, but would been hard getting an exact duration tho. In the end I achieved what I wanted doing some math in a shader (since its webgl). If u want to see, towards the end of the clip attached u can see that the grid item now snaps into place when opening/closing even if the grid is moving. which I struggled with using pure JS. grid.mp4 1 Link to comment Share on other sites More sharing options...
Share Posted December 27, 2021 I may not understand your goal correctly, but here's a different approach: See the Pen dca0f2b784afa72903898206f27315c4 by GreenSock (@GreenSock) on CodePen 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now