A
A
A person from Kazakhstan2020-05-07 06:43:22
JavaScript
A person from Kazakhstan, 2020-05-07 06:43:22

How to complete this effect?

I'm doing some animation - movement of the circle under the mouse
When the mouse hits under the objects, mix-blend-mode + svg clip-path occurs
but the circle does not reach the borders of the svg
Maybe clientXthis is not the method to use
How to finish this effect ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-05-07
@LenovoId

Did you mean this?

const svg = document.querySelector('#svg');
const item = svg.querySelector('#rect');

const size = svg.getBoundingClientRect();
const viewBox = svg
  .getAttribute('viewBox')
  .split(/\s+/g)
  .map(entry => Number(entry));

const normalize = (min, max, value) => {
  return (value - min) / (max - min);
};

const lerp = (min, max, value) => {
  return (1 - value) * min + value * max;
};

const map = (minSource, maxSource, minDestination, maxDestination, value) => {
  return lerp(minDestination, maxDestination, normalize(minSource, maxSource, value));
};

svg.addEventListener('mousemove', event => {
  item.setAttribute(
    'cx',
    map(0, size.width, viewBox[0], viewBox[2], event.offsetX)
  );
  item.setAttribute(
    'cy',
    map(0, size.height, viewBox[1], viewBox[3], event.offsetY)
  );
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question