Forum Discussion

RussLickteig's avatar
RussLickteig
Community Member
5 days ago

Center point is off when I use JavaScript for animation creation.

Hey everyone.  If I use the typical animations SPIN to animate a circle the circle rotates perfectly centered as it should.  If I use the new AI tool in SL to create a custom animation with javaScript the circle no longer rotates from it's center point.  WHY?  I have tried several times to get the AI assistant to rotate from it's center and so on.  Any ideas or script that does this properly?  

4 Replies

  • Nedim's avatar
    Nedim
    Community Member

    I’m not sure which code you used, but attached is a .story file that compares different options and approaches:

    Example 1:
    Object is rotating with the Default Spin Entrance Animation

    Example 2:
    Object is rotating using a JavaScript API object reference and the Web Animations API

    Example 3:
    Object is rotating using a JavaScript API object reference with the rotation property and update() function

  • RussLickteig's avatar
    RussLickteig
    Community Member
    /*
      JS - Locked Centered Continuous Slow Rotation Animation for Oval 1
      Animation Details:
      - Oval 1 rotates 360 degrees in 5 seconds.
      - Rotation is locked around its center.
      - Animation repeats indefinitely.
    */
    
    const oval = object('5nl2b3u6wAP'); // Oval 1 object ID
    
    // --- Initial Setup ---
    oval.style.opacity = 1; // Ensure the oval is visible
    oval.style.transformOrigin = '50% 50%'; // Lock the rotation point to the absolute center
    
    // --- Continuous Rotation Animation ---
    oval.animate(
      [
        { rotate: '0deg' },          // Start at 0 degrees
        { rotate: '360deg' }         // Complete a full rotation
      ],
      {
        duration: 5000,              // 5 seconds per rotation
        iterations: Infinity,        // Infinite repetitions
        easing: 'linear'             // Smooth, continuous rotation
      }
    );

     

  • RussLickteig's avatar
    RussLickteig
    Community Member

    Rotates just fine with this script from ChatGPT.  

     

    if (obj) {
    
    const rotationSpeed = 360 / 10000; // 10 seconds per rotation
    
    let lastTime = performance.now();
    let rotation = 0;
    
    // Capture Storyline’s original transform
    const baseTransform = obj.style.transform || "";
    
    function rotateGraphic(currentTime) {
    const deltaTime = currentTime - lastTime;
    lastTime = currentTime;
    
    rotation += deltaTime * rotationSpeed;
    
    obj.style.transform = `${baseTransform} rotate(${rotation}deg)`;
    
    requestAnimationFrame(rotateGraphic);
    }
    
    requestAnimationFrame(rotateGraphic);
    }