DEV Community

Discussion on: A JavaScript interview question asked at Facebook

 
vince_tblt profile image
Vincent Thibault

Wow interesting I didn’t know about it!
With future options it’s gonna be a real game-changer!

Thread Thread
 
richardeschloss profile image
Richard Schloss • Edited

After giving this a bit more thought...I promise not to beat it death but...you led me on the right track and I created fully working code now (code review welcomed!). The one minor thing missed was that translateX alone won't always move the object to the right. I.e., if the object is rotated 45deg, the translateX will move along the rotated X axis, not to the right of the screen by the requested amount (i.e., 200px diagonally would mean sqrt((200)2 / 2) = 141 px to the right instead of 200px). The extra parameters need to be provided to the transformation matrix. So, they were really also testing us a bit on computational geometry, more than just the animation. With a few tweaks to the code, regardless of API used, the following code should still work regardless of initial transform:

function animate(elm, milliseconds, distance) {
  console.warn(
    'Running an "animate" method that should be named "moveRightOnly"'
  );
  return new Promise((resolve, reject) => {
    // Allow animation to be chained
    if (distance < 0 || milliseconds < 0) {
      reject(
        new Error(
          "Feeling negative? Really? This method only moves things to the right. Sorry!"
        )
      );
    }

    // I think the Matrix API is neat, but the only reason I'm doing it this long-drawn out way is:
    // 1. MDN warns of the API being non-standard, not production-ready
    // 2. ES features make it easy enough to do without it
    let computedStyle = getComputedStyle(elm).getPropertyValue("transform");
    let newStyle
    if (computedStyle === "none") {
      elm.style.transform = "translate(0px, 0px)";
      computedStyle = getComputedStyle(elm).getPropertyValue("transform");
    }

    const params = computedStyle
    .match(/\((.+)\)/)[1]
    .split(/,\s*/)
    .map(s => parseFloat(s));   
    if (computedStyle.includes('matrix3d')) {
      params[12] += distance
      newStyle = `matrix3d(${params.join(', ')})`
    } else {
      params[4] += distance
      newStyle = `matrix(${params.join(', ')})`
    }

    elm.animate(
      [
        {
          transform: computedStyle
        },
        {
          transform: newStyle
        }
      ],
      {
        duration: milliseconds,
        fill: "forwards"
      }
    ).onfinish = resolve;
  });
}

Updated working examples are here: JSFiddle

(Alternative, using the MatrixAPI, is here. The translateSelf method had to be replaced with setMatrixValue)

--> If the "test box" lines up with the "goal box" it passes.

I think this code addresses the key concerns that were brought up. I'd be damn impressed if someone could do this in a 15-minute phone screen! (and I'm pretty sure Facebook would be impressed by the questions you brought up!)

Thread Thread
 
vince_tblt profile image
Vincent Thibault

Awesome ! You did a really great job !
The only problem I see : they failed on 3D initial parameter (translateZ(1px)) :

  • The webanimation failed because you handle a 3D matrix as a 2D matrix, making it failed.
  • The matrix version failed because it reset the 3D matrix to a 2D matrix.

So you need to make use of matrix.is2D (or check for /matrix3d/ to change the algorithm used but its seems quite painful to do.

Here is a solution of the problem without the need of parsing the matrix data (just generate a 2D translation matrix and multiply it to the current matrix), with this solution you don't need to care about the 2D-3D stuff : jsfiddle.net/vthibault/9Lqyphkm/

Thread Thread
 
richardeschloss profile image
Richard Schloss

I updated to include 3d, and also to account for when the animation ends, in case it's desired to chain the animation calls. I still prefer to directly set the parameters instead of performing the multiply operation (which can be a lot of multiply-add operations, and a potential source rounding errors if floating points are used).