React

by jatin_parmar

HTML

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

CSS

#ParentContainer {
    border: solid red 1px;
    padding: 20px;
}
section {
    display: inline-block;
    border: solid blue 1px;
}

React

class TestComponent extends React.Component {
  constructor(props) {
      super(props);
      this.state = {
          customProperties: []
      };
      this.ParentContainer=React.createRef();
  }
  _testFunction = (e) => {
      var mouseX = e.pageX;
      var mouseY = e.pageY;
      //replace this 
      //var spans = document.getElementById('ParentContainer').querySelectorAll('section');
      var spans = this.parentContainer.current.childNodes;

      var theRangeSquared = 50 * 50;
      var maxOffset = 5;
      var newCustomProperties = [];
      for (var i = 0; i < spans.length; i++) {
          var position = spans[i].getBoundingClientRect();
          var widthMod = position.width / 2;
          var heightMod = position.height / 2;
          var coordX = position.x + widthMod;
          var coordY = position.y + heightMod + window.scrollY;
          // Distance from mouse
          var dx = coordX - mouseX;
          var dy = coordY - mouseY;
          var distanceSquared = (dx * dx + dy * dy);
          var tx = 0,
              ty = 0;
          if (distanceSquared < theRangeSquared && distanceSquared !== 0) {
              // Calculate shift scale (inverse of distance)
              var shift = maxOffset * (theRangeSquared - distanceSquared) / theRangeSquared;
              var distance = Math.sqrt(distanceSquared);
              tx = shift * dx / distance;
              ty = shift * dy / distance;
          }
          newCustomProperties.push({
              x: tx,
              y: ty
          });
      }
      this.setState({
          customProperties: newCustomProperties
      });
  }
  render() {
      let counter = 0;
      const returnCustomProp = function(that) {
          counter++;
          let x = 0;
          if(that.state.customProperties[counter-1]) {
              x = that.state.customProperties[counter-1].x;
          }
          let y = 0;
          if(that.state.customProperties[counter-1]) {
              y =...