<commit_before>/* eslint-env browser */

'use strict';

const electron = require('electron');

const BACKGROUND_COLOUR = 'rgba(255, 255, 255, 1)';

const canvas  = document.createElement('canvas');
const context = canvas.getContext('2d');

canvas.width  = window.innerWidth * 0.95;
canvas.height = window.innerHeight * 0.95;

document.body.appendChild(canvas);

const drawText = function drawText(text, x, y, colour) {
  context.fillStyle = colour;
  context.font = 'normal 36px monospace';
  context.fillText(text, x, y);
};

const displayText = function displayText(text, x, y, alpha) {
  drawText(text, x, y, `rgba(0, 0, 0, ${alpha})`);
};

const clearText = function clearText(text, x, y) {
  drawText(text, x, y, BACKGROUND_COLOUR);
};

const fadeText = function fadeText(
  text, x, y, orientation, initialAlpha, step
) {
  const chars = text.split('');

  const alphas = [];

  for (let i = 0; i < chars.length; i++) {
    alphas.push(initialAlpha);
  }

  const interval = setInterval(function fade() {
    chars.forEach(function fadeChar(char, i) {
      let xOffset = 0;
      let yOffset = 0;

      if (orientation) {
        xOffset = 36 * i;
      } else {
        yOffset = 36 * i;
      }

      clearText(char, x + xOffset, y + yOffset);
      displayText(char, x + xOffset, y + yOffset, alphas[i]);

      alphas[i] += step;

      if (alphas[i] > 1 || alphas[i] < 0) {
        clearInterval(interval);
      }
    });
  }, 50);
};

const fadeInText = function fadeInText(text, x, y, orientation) {
  fadeText(text, x, y, orientation, 0, 0.05);
};

const fadeOutText = function fadeOutText(text, x, y, orientation) {
  fadeText(text, x, y, orientation, 1, -0.05);
};

electron.ipcRenderer.on('clear', function clearWords() {
  context.clearRect(0, 0, canvas.width, canvas.height);
});

electron.ipcRenderer.on('display-words', function displayWords(event, words) {
  const orientations = [ Math.random() > 0.5 ? 0 : 1 ];

  for (let i = 1; i < words.length; i++) {
    orientations.push((orientations[i - 1] + 1) % 2);
  }

  let i = 0;
  let word;

  const interval = setInterval(function displayWord() {
    const x = 0;
    const y = canvas.height / 2;
    const orientation = orientations[i];

    const prevWord = word;

    if (prevWord) {
      fadeOutText(prevWord, x, y, orientations[i - 1]);
    }

    word = words[i];

    if (!word) {
      return clearInterval(interval);
    }

    fadeInText(word, x, y, orientation);

    i++;

  }, 2000);
});<commit_msg>Crossfade words keeping the common letter<commit_after>/* eslint-env browser */

'use strict';

const electron = require('electron');

const BACKGROUND_COLOUR = 'rgba(255, 255, 255, 1)';
const CHAR_WIDTH        = 36;

const canvas  = document.createElement('canvas');
const context = canvas.getContext('2d');

canvas.width  = window.innerWidth * 0.95;
canvas.height = window.innerHeight * 0.95;

document.body.appendChild(canvas);

const drawText = function drawText(text, x, y, colour) {
  context.fillStyle = colour;
  context.font = `normal ${CHAR_WIDTH}px monospace`;
  context.fillText(text, x, y);
};

const displayText = function displayText(text, x, y, alpha) {
  drawText(text, x, y, `rgba(0, 0, 0, ${alpha})`);
};

const clearText = function clearText(text, x, y) {
  drawText(text, x, y, BACKGROUND_COLOUR);
};

const fadeText = function fadeText(
  text, x, y, orientation, initialAlpha, step
) {
  const chars = text.split('');

  const alphas = [];

  for (let i = 0; i < chars.length; i++) {
    alphas.push(initialAlpha);
  }

  const interval = setInterval(function fade() {
    chars.forEach(function fadeChar(char, i) {
      let xOffset = 0;
      let yOffset = 0;

      if (orientation) {
        xOffset = CHAR_WIDTH * i;
      } else {
        yOffset = CHAR_WIDTH * i;
      }

      clearText(char, x + xOffset, y + yOffset);
      displayText(char, x + xOffset, y + yOffset, alphas[i]);

      alphas[i] += step;

      if (alphas[i] > 1 || alphas[i] < 0) {
        clearInterval(interval);
      }
    });
  }, 50);
};

const fadeInText = function fadeInText(text, x, y, orientation) {
  fadeText(text, x, y, orientation, 0, 0.05);
};

const fadeOutText = function fadeOutText(text, x, y, orientation) {
  fadeText(text, x, y, orientation, 1, -0.05);
};

electron.ipcRenderer.on('clear', function clearWords() {
  context.clearRect(0, 0, canvas.width, canvas.height);
});

electron.ipcRenderer.on('display-words', function displayWords(event, words) {
  const orientations = [ Math.random() > 0.5 ? 0 : 1 ];

  for (let i = 1; i < words.length; i++) {
    orientations.push((orientations[i - 1] + 1) % 2);
  }

  let i = 0;
  let word;

  let x = canvas.width / 2;
  let y = canvas.height / 2;

  const interval = setInterval(function displayWord() {
    const orientation = orientations[i];

    let prevWord = word;

    word = words[i];

    if (!word) {
      return clearInterval(interval);
    }

    if (prevWord) {
      let firstMatchingIndexInPrevWord = -1;
      let firstMatchingIndexInWord = -1;

      // TODO: Get a random intersection, not the first one every time...
      word.split('').forEach(function findMatchingIndex(char, j) {
        if (firstMatchingIndexInPrevWord >= 0) {
          return;
        }

        firstMatchingIndexInPrevWord = prevWord.indexOf(char);

        if (firstMatchingIndexInPrevWord >= 0) {
          firstMatchingIndexInWord = j;
        }
      });

      prevWord = `${prevWord.substr(0, firstMatchingIndexInPrevWord)} ${prevWord.substr(firstMatchingIndexInPrevWord + 1)}`;
      word     = `${word.substr(0, firstMatchingIndexInWord)} ${word.substr(firstMatchingIndexInWord + 1)}`;

      fadeOutText(prevWord, x, y, orientations[i - 1]);

      if (orientation) {
        x -= CHAR_WIDTH * firstMatchingIndexInWord;
        y += CHAR_WIDTH * firstMatchingIndexInPrevWord;
      } else {
        x += CHAR_WIDTH * firstMatchingIndexInPrevWord;
        y -= CHAR_WIDTH * firstMatchingIndexInWord;
      }
    }

    fadeInText(word, x, y, orientation);

    i++;
  }, 2000);
});
{'fraction_non_alphanumeric': 0.14743801652892563, 'fraction_numerical': 0.020165289256198347, 'mean_word_length': 3.1331967213114753, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5570844', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2004, 'n_words': 519}