<commit_before>import * as Hammer from 'hammerjs';

// TODO: Clean up.
export function pinchZoom(el: HTMLElement) {
  let mc = new Hammer.Manager(el);
  let pinch = new Hammer.Pinch();
  let pan = new Hammer.Pan();

  pinch.recognizeWith(pan);
  mc.add([pinch, pan]);

  let adjustScale = 1;
  let adjustDeltaX = 0;
  let adjustDeltaY = 0;

  let currentScale = 0;
  let currentDeltaX = 0;
  let currentDeltaY = 0;
  let panDelay = 0;

  mc.on('pan pinch', ev => {
    if (ev.type === 'pan' && panDelay >= Date.now()) return;
    currentScale = Math.min(Math.max(1, adjustScale * ev.scale), 2);
    currentDeltaX = adjustDeltaX + (ev.deltaX / currentScale);
    currentDeltaY = adjustDeltaY + (ev.deltaY / currentScale);

    let limitX = el.clientWidth * (currentScale - 1) / 4;
    if (currentDeltaX < -limitX) currentDeltaX = -limitX;
    if (currentDeltaX > limitX) currentDeltaX = limitX;

    let limitY = el.clientHeight * (currentScale - 1) / 4;
    if (currentDeltaY < -limitY) currentDeltaY = -limitY;
    if (currentDeltaY > limitY) currentDeltaY = limitY;

    el.style.transform = `scale(${currentScale}) translate(${currentDeltaX}px,${currentDeltaY}px)`;
  });

  mc.on('panend pinchend', ev => {
      adjustScale = currentScale;
      adjustDeltaX = currentDeltaX;
      adjustDeltaY = currentDeltaY;
      if (ev.type === 'pinchend') panDelay = Date.now() + 200;
  });
}<commit_msg>[Client] Fix pinch zoom rounding errors.<commit_after>import * as Hammer from 'hammerjs';

// TODO: Clean up. Zoom out when changing page. Perhaps zoom to image width/height rather than container?
export function pinchZoom(el: HTMLElement) {
  let mc = new Hammer.Manager(el);
  let pinch = new Hammer.Pinch();
  let pan = new Hammer.Pan();

  pinch.recognizeWith(pan);
  mc.add([pinch, pan]);

  let adjustScale = 1;
  let adjustDeltaX = 0;
  let adjustDeltaY = 0;

  let currentScale = 0;
  let currentDeltaX = 0;
  let currentDeltaY = 0;
  let panDelay = 0;

  mc.on('pan pinch', ev => {
    if (ev.type === 'pan' && panDelay >= Date.now()) return;
    currentScale = Math.min(Math.max(1, adjustScale * ev.scale), 2);
    currentDeltaX = Math.round(adjustDeltaX + (ev.deltaX / currentScale));
    currentDeltaY = Math.round(adjustDeltaY + (ev.deltaY / currentScale));

    let limitX = Math.round(el.clientWidth * (currentScale - 1) / 4);
    if (currentDeltaX < -limitX) currentDeltaX = -limitX;
    if (currentDeltaX > limitX) currentDeltaX = limitX;

    let limitY = Math.round(el.clientHeight * (currentScale - 1) / 4);
    if (currentDeltaY < -limitY) currentDeltaY = -limitY;
    if (currentDeltaY > limitY) currentDeltaY = limitY;

    el.style.transform = `scale(${currentScale}) translate(${currentDeltaX}px,${currentDeltaY}px)`;
  });

  mc.on('panend pinchend', ev => {
      adjustScale = currentScale;
      adjustDeltaX = currentDeltaX;
      adjustDeltaY = currentDeltaY;
      if (ev.type === 'pinchend') panDelay = Date.now() + 200;
  });
}
{'fraction_non_alphanumeric': 0.14121621621621622, 'fraction_numerical': 0.010810810810810811, 'mean_word_length': 3.513719512195122, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7581887', 'n_tokens_mistral': 1056, 'n_tokens_neox': 985, 'n_words': 258}