Jump to content

Search In
  • More options...
Find results that contain...
Find results in...

ModifiersPlugin helper functions

Recommended Posts

Just wanted to share some nice functions I came across that can be used with the ModifiersPlugin.


Doing a normal modulus calculation restarts the value at 0, which may not be what you want.

500 % 500 // => 0

This function will allow you to do a modulus operation for a min/max range.

wrap(500, -100, 500); // => -100

function wrap(value, min, max) {
  var v = value - min;
  var r = max - min;

  return ((r + v % r) % r) + min;

And this is a modified version of that function that will make the modulus value "yoyo".


mirroredWrap(600, -100, 500); // => 400

function mirroredWrap(value, min, max) {
  var v  = value - min;
  var r1 = max - min;
  var r2 = r1 * 2;
  v = (r2 + v % r2) % r2;
  return v > r1 ? (r2 - v) + min : v + min;

With the first wrap function you can do some interesting stuff, like making an object appear in two different places, kind of like in those old asteroid games.



And with the mirroredWrap, you can do stuff like creating multiple bounces with a single tween.

See the Pen mRJeNX by osublake (@osublake) on CodePen



  • Like 12

Share this post

Link to post
Share on other sites

Slick stuff, Blake! Thanks for sharing. 

  • Like 1

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.

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.


  • Recently Browsing   0 members

    No registered users viewing this page.