<commit_before>import PelagosClient from "../../lib/pelagosClient";

const url = 'https://storage.googleapis.com/vizzuality-staging/random/'
const DAY_MS = 24 * 60 * 60 * 1000;

class CanvasLayer {
  constructor(position, options, map) {
    this.map = map;
    this.data = {};
    this.position = position;
    this.minDate = Date.now();
    this.tileSize = new google.maps.Size(256, 256);
    this.options = _.extend({}, this.defaults, this.options || {});
    this.map.overlayMapTypes.insertAt(this.position, this);
    this.visible = true;
    this.filters = {}
  }

  hide() {
    this.visible = false;
    this.map.overlayMapTypes.removeAt(this.position);
  }

  show() {
    this.visible = true;
    this.map.overlayMapTypes.insertAt(this.position, this);
  }

  isVisible() {
    return this.visible;
  }

  applyFilters(filters) {
    if (filters.timeline) {
      this.filters.timeline = filters.timeline;
    }
    this.show();
  }

  resetData() {
    this.data = {};
  }

  _getCanvas(coord, zoom, ownerDocument) {
    // create canvas and reset style
    var canvas = ownerDocument.createElement('canvas');
    canvas.style.border = 'none';
    canvas.style.margin = '0';
    canvas.style.padding = '0';
    canvas.id = `${zoom},${coord.x},${coord.y}`;

    // prepare canvas and context sizes
    var ctx = canvas.getContext('2d');
    ctx.width = canvas.width = this.tileSize.width;
    ctx.height = canvas.height = this.tileSize.height;

    canvas.ctx = ctx;
    return canvas;
  }

  drawFrame(pos, zoom) {
    let canvasKeys = Object.keys(this.data);
    for (let i = 0, length = canvasKeys.length; i < length; i++) {
      if (this.data[canvasKeys[i]] && this.data[canvasKeys[i]][pos]) {
        let data = this.data[canvasKeys[i]][pos];
        this.drawTile2Canvas(canvasKeys[i], data, false);
      }
    }
  }

  drawTile2Canvas(idCanvas, data, accumulative) {
    let canvas = document.getElementById(idCanvas);
    if (!accumulative) {
      canvas.ctx.clearRect(0, 0, canvas.width, canvas.height);
    }
    let size = this.map.getZoom() > 6
      ? 3
      : 2 || 1;
    for (let j = 0, lengthData = data.latitude.length; j < lengthData; j++) {
      const weight = data.weight[j];
      if (!weight)
        continue;
      if (weight > 0.9)
        canvas.ctx.fillStyle = 'rgb(255,255,240)';
      else if (weight > 0.05)
        canvas.ctx.fillStyle = 'rgb(10,200,200)';
      else
        canvas.ctx.fillStyle = 'rgb(0,255,242)';
      canvas.ctx.fillRect(~~data.x[j], ~~data.y[j], size, size);
    }
  }

  regenerate() {
    let canvasKeys = Object.keys(this.data);
    for (let i = 0, length = canvasKeys.length; i < length; i++) {
      let times = Object.keys(this.data[canvasKeys[i]]);
      for (let j = 0, lengthTime = times.length; j < lengthTime; j++) {
        this.drawTile2Canvas(canvasKeys[i], this.data[canvasKeys[i]][times[j]], true);
      }<commit_msg>FIX: avoid animating non-existing canvas<commit_after>import PelagosClient from "../../lib/pelagosClient";

const url = 'https://storage.googleapis.com/vizzuality-staging/random/'
const DAY_MS = 24 * 60 * 60 * 1000;

class CanvasLayer {
  constructor(position, options, map) {
    this.map = map;
    this.data = {};
    this.position = position;
    this.minDate = Date.now();
    this.tileSize = new google.maps.Size(256, 256);
    this.options = _.extend({}, this.defaults, this.options || {});
    this.map.overlayMapTypes.insertAt(this.position, this);
    this.visible = true;
    this.filters = {}
  }

  hide() {
    this.visible = false;
    this.map.overlayMapTypes.removeAt(this.position);
  }

  show() {
    this.visible = true;
    this.map.overlayMapTypes.insertAt(this.position, this);
  }

  isVisible() {
    return this.visible;
  }

  applyFilters(filters) {
    if (filters.timeline) {
      this.filters.timeline = filters.timeline;
    }
    this.show();
  }

  resetData() {
    this.data = {};
  }

  _getCanvas(coord, zoom, ownerDocument) {
    // create canvas and reset style
    var canvas = ownerDocument.createElement('canvas');
    canvas.style.border = 'none';
    canvas.style.margin = '0';
    canvas.style.padding = '0';
    canvas.id = `${zoom},${coord.x},${coord.y}`;

    // prepare canvas and context sizes
    var ctx = canvas.getContext('2d');
    ctx.width = canvas.width = this.tileSize.width;
    ctx.height = canvas.height = this.tileSize.height;

    canvas.ctx = ctx;
    return canvas;
  }

  drawFrame(pos, zoom) {
    let canvasKeys = Object.keys(this.data);
    for (let i = 0, length = canvasKeys.length; i < length; i++) {
      if (this.data[canvasKeys[i]] && this.data[canvasKeys[i]][pos]) {
        let data = this.data[canvasKeys[i]][pos];
        this.drawTile2Canvas(canvasKeys[i], data, false);
      }
    }
  }

  drawTile2Canvas(idCanvas, data, accumulative) {
    let canvas = document.getElementById(idCanvas);
    if (!canvas) return;
    if (!accumulative) {
      canvas.ctx.clearRect(0, 0, canvas.width, canvas.height);
    }
    let size = this.map.getZoom() > 6
      ? 3
      : 2 || 1;
    for (let j = 0, lengthData = data.latitude.length; j < lengthData; j++) {
      const weight = data.weight[j];
      if (!weight)
        continue;
      if (weight > 0.9)
        canvas.ctx.fillStyle = 'rgb(255,255,240)';
      else if (weight > 0.05)
        canvas.ctx.fillStyle = 'rgb(10,200,200)';
      else
        canvas.ctx.fillStyle = 'rgb(0,255,242)';
      canvas.ctx.fillRect(~~data.x[j], ~~data.y[j], size, size);
    }
  }

  regenerate() {
    let canvasKeys = Object.keys(this.data);
    for (let i = 0, length = canvasKeys.length; i < length; i++) {
      let times = Object.keys(this.data[canvasKeys[i]]);
      for (let j = 0, lengthTime = times.length; j < lengthTime; j++) {
        this.drawTile2Canvas(canvasKeys[i], this.data[canvasKeys[i]][times[j]], true);
      }
{'fraction_non_alphanumeric': 0.1611471491976784, 'fraction_numerical': 0.020826220553089792, 'mean_word_length': 2.792233009708738, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '1677320', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1969, 'n_words': 403}