*If you don't want my story go to the bottom marked with Bold for a short answer.
The person who posted this question might have long forgotten about this question, but for anyone to come here, this is my solution. I will be using gsap3 in my answer as that is the current. I was working on a scroll-based animation when I came across this problem. What I wanted to do in my animations as you might see in the code is have an entry animation on a div based on a scroll. The problem is the following CSS
transform: translate(314.18px, 244.9px) rotate(295deg)
should have different x and y coordinates depending on the screen size. So for example, if the user started on a full screen and then resized the window that would distort the animation, so I would like to update the coordinates on window resize. To solve the problem, I set the x and y coordinates to be returned values of different functions width() and height() hoping the gsap.to function will store the function as its value in the key-value pair but as mentioned in the above answer not only does it store variable values once but it turns out it also stores the returned value of functions rather than the functions themselves, so I tried something else. That is to put all the animation in one function and call it every time the window is resized. The before and after code looked like the following.
Before the solution
var vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
function updateDimention() {
vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
vh = Math.max(document.documentElement.clienthHeight || 0, window.innerHeight || 0)
}
window.onresize=updateDimention;
let height = function(){return vh};
let width = function(){return vw};
gsap.registerPlugin(ScrollTrigger, MotionPathPlugin);
const entryAnim = gsap.timeline(
{
scrollTrigger:{
scrub:true,
pin:true,
trigger:".main",
start:"top top",
end:"+=6000px",
},
// yoyo:true,
}
);
entryAnim.pause()
entryAnim.to(
".pic1",{
motionPath:{
path:[{x:0,y:0,},{x:0.155*width(),y:0.2*height()},{x:0.18*width(),y:0.25*height()},{x:0.23*width(),y:0.395*height()}],
// type:"cubic",
autorotate:true,
},
}).to(
".pic2",{
motionPath:{
path:[{x:0.18*width(),y:0,},{x:0.15*width(),y:0.21*height()},{x:0*width(),y:0.40*height()},{x:-0.000503*width(),y:0.75*height()}],
// type:"cubic",
autorotate:true,
},
}).to(
".pic3",{
motionPath:{
path:[{x:-0.15*width(),y:-0.10,},{x:-0.10*width(),y:-0.5*height()},{x:0*width(),y:0*height()},{x:0.4245*width(),y:0.324*height()}],
// type:"cubic",
autorotate:false,
},
})
const rotationAnim = gsap.timeline(
{
scrollTrigger:{
trigger:".main",
start:"top top",
end:"+=6000px",
pin:true,
scrub:true
},
// yoyo:true,
}
);
rotationAnim.pause()
rotationAnim.to(".pic1",{
rotation:295,
}).to(
".pic2",{
rotation:329,
}
).to(".pic3",{
rotation:48,
})
After the solution
var vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
function updateDimention() {
vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
vh = Math.max(document.documentElement.clienthHeight || 0, window.innerHeight || 0)
}
let height = function(){return vh};
let width = function(){return vw};
gsap.registerPlugin(ScrollTrigger, MotionPathPlugin);
function Animation(){
const entryAnim = gsap.timeline(
{
scrollTrigger:{
scrub:true,
pin:true,
trigger:".main",
start:"top top",
end:"+=6000px",
},
// yoyo:true,
}
);
entryAnim.pause()
entryAnim.to(
".pic1",{
motionPath:{
path:[{x:0,y:0,},{x:0.155*width(),y:0.2*height()},{x:0.18*width(),y:0.25*height()},{x:0.23*width(),y:0.395*height()}],
// type:"cubic",
autorotate:true,
},
}).to(
".pic2",{
motionPath:{
path:[{x:0.18*width(),y:0,},{x:0.15*width(),y:0.21*height()},{x:0*width(),y:0.40*height()},{x:-0.000503*width(),y:0.75*height()}],
// type:"cubic",
autorotate:true,
},
}).to(
".pic3",{
motionPath:{
path:[{x:-0.15*width(),y:-0.10,},{x:-0.10*width(),y:-0.5*height()},{x:0*width(),y:0*height()},{x:0.4245*width(),y:0.324*height()}],
// type:"cubic",
autorotate:false,
},
})
const rotationAnim = gsap.timeline(
{
scrollTrigger:{
trigger:".main",
start:"top top",
end:"+=6000px",
pin:true,
scrub:true
},
// yoyo:true,
}
);
rotationAnim.pause()
rotationAnim.to(".pic1",{
rotation:295,
}).to(
".pic2",{
rotation:329,
}
).to(".pic3",{
rotation:48,
})
}
Animation()
function updateAnimation (){
updateDimention();
Animation();
}
window.onresize=updateAnimation;
In short
so as a conclusion what you want to do is the following,
function Animation(){
//in here put all your gsap related animations
}
//then down here call the Animation function every time values are changed so that
//the gap animation can be redefined. In this case I am going to do it in window resize.
window.onresize = Animation;