Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: ae63b74277d7437ef11cc15894f71da0c94744d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const digits = /-?\d+\.?\d*/g;

export function extractNum(string) {
  return string.replace(/\D/g,'');
}

export function getTracked(trackedAndTrackerEls) {
  const sorted = [...trackedAndTrackerEls]
    .sort((a, b) => a.id < b.id || extractNum(a.id) <= extractNum(b.id));

  const groups = [];
  const els = sorted.slice();

  while (els.length) {
    groups.push([els.pop(), els.pop()]);
  }

  return groups;
}

export function track(targetEl, trackingEl, transformMtx) {
  let x = targetEl.getAttributeNS(null, 'x');
  let y = targetEl.getAttributeNS(null, 'y');
  let ptBefore = new DOMPoint(x, y);
  let ptAfter = ptBefore.matrixTransform(transformMtx);
  trackingEl.setAttributeNS(null, 'x', ptAfter.x);
  trackingEl.setAttributeNS(null, 'y', ptAfter.y);
}

export default function getComputedTransformMatrix(el) {
  const matrixSequence = getComputedStyle(el).transform.match(digits),
    identityMatrix = '';

  return new DOMMatrix(matrixSequence || identityMatrix);
}