Alternating Blocks

October 24, 2021

This is a simple pair of blocks that transition between 0 and 1 opacity. I came up with a few ways to do it.

First, using absolute value like this will only ever produce 0 or 1:

const toggle = () = {
  setLeftOpacity(Math.abs(leftOpacity - 1))
  setRightOpacity(Math.abs(rightOpacity - 1))
}

And second, using a swap function:

const toggle = () = {
  let temp = leftOpacity;
  setLeftOpacity(rightOpacity)
  setRightOpacity(temp)
}

And finally, there’s conditionals:

const toggle = () = {
  setLeftOpacity(leftOpacity == 1 ? 0 : 1);
  setRightOpacity(rightOpacity == 1 ? 0 : 1);
}