Make Objects Follow Your Mouse Cursor in Storyline 360 with JavaScript & GSAP
February 26, 2024
In this video tutorial, we will use Javascript and a bit of GSAP to make an Articulate Storyline 360 slide object follow the mouse cursor’s position.
We will use getBoundingClientRect() to get the position of our slide in the viewport, then make sure the satellite moves only when the mouse is within the bounds of the slide.
varplayer=GetPlayer();
varsatellite= document.querySelector("[data-acc-text='satellite']");
varoffsetX=30;
varoffsetY=30;
//this will execute each time we move the mouse
document.addEventListener('mousemove', function(event){
//select the slide
varslide= document.getElementById('slide');
//find out what distance it is from the edges of the viewport
varslideRect=slide.getBoundingClientRect();
//declare two variables, and use them to store the position of the mouse
varmouseX=event.clientX;
varmouseY=event.clientY;
//check if the mouse is moving ONLY within the slide
if(mouseX>=slideRect.left&&mouseX<=slideRect.right&&mouseY>=slideRect.top&&mouseY<=slideRect.bottom ){
//adjust the coordinates based on the position and size of the slide
varadjustedX=mouseX-slideRect.left+offsetX;
varadjustedY=mouseY-slideRect.top+offsetY;
//ensure that the mouse doesn't go beyond the right and bottom edge of the slide
adjustedX= Math.max(0, Math.min(slideRect.right, adjustedX));
adjustedY= Math.max(0, Math.min(slideRect.bottom, adjustedY));
//setting the text variables so we can see them update in storyline
player.SetVar("SL_mouseX", adjustedX);
player.SetVar("SL_mouseY", adjustedY);
//animate the satellite
gsap.to(satellite, {x:adjustedX, y:adjustedY});
};
});