code0n

controlling css properties in tweenjs

Recommended Posts

https://github.com/tweenjs/tween.js/

Whats the exact problem you're having? The top of the above linked readme shows DOM manipulations, the following snippet makes very minimal changes to that code from the readme.

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>
</head>
<body>
  <script>
  var box = document.createElement('div');
    box.style.setProperty('background-color', '#008800');
    box.style.setProperty('width', '100px');
    box.style.setProperty('height', '100px');
    box.style.setProperty('border-radius', '0px')
    document.body.appendChild(box);

    // Setup the animation loop.
    function animate(time) {
      requestAnimationFrame(animate);
      TWEEN.update(time);
    }
    requestAnimationFrame(animate);

    var coords = { x: 0 }; // Start at (0, 0)
    var tween = new TWEEN.Tween(coords) // Create a new tween that modifies 'coords'.
      .to({ x: 100 }, 10000) // Move to (300, 200) in 1 second.
      .easing(TWEEN.Easing.Quadratic.Out) // Use an easing function to make the animation smooth.
      .onUpdate(function() { // Called after tween.js updates 'coords'.
        // Move 'box' to the position described by 'coords' with a CSS translation.
        // box.style.setProperty('transform', 'translate(' + coords.x + 'px, ' + coords.y + 'px)');
        box.style.setProperty('border-radius', coords.x + 'px')
      })
      .start(); // Start the tween immediately.
  </script>
</body>

I added the border radius, changed the object from 2d to 1d (border radius is a single value) and updated the update function to alter the border radius property (I also upped the time from the example to 10s which makes it easier to see the change over time).

I have limited understanding of Tween.js, but it literally just handles the rate of change over time, you then handle the application of that change.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.