<commit_before>'use strict'
import Vect4 from './math/Vect4'
import Mat4 from './math/Mat4'

export default class BrowserSurfaceView {
  /**
   *
   * @param {BrowserSurface} browserSurface
   * @returns {BrowserSurfaceView}
   */
  static create (browserSurface) {
    const canvas = this._createCanvas()
    const context2d = canvas.getContext('2d')
    const browserSurfaceView = new BrowserSurfaceView(canvas, context2d, browserSurface)
    canvas.view = browserSurfaceView
    return browserSurfaceView
  }

  /**
   * @returns {HTMLCanvasElement}
   * @private
   */
  static _createCanvas () {
    const canvas = document.createElement('canvas')
    canvas.style.left = '0px'
    canvas.style.top = '0px'
    canvas.style.position = 'absolute'
    canvas.style.zIndex = 0
    return canvas
  }

  enableMouseListeners () {
    this.canvas.addEventListener('mouseenter', this._mouseEnterListener, true)
    this.canvas.addEventListener('mouseleave', this._mouseLeaveListener, true)
    // other mouse listeners are set in the browser pointer class
  }

  disableMouseListeners () {
    this.canvas.removeEventListener('mouseenter', this._mouseEnterListener, true)
    this.canvas.removeEventListener('mouseleave', this._mouseLeaveListener, true)
  }

  /**
   * @private
   * @param canvas
   * @param context2d
   * @param browserSurface
   */
  constructor (canvas, context2d, browserSurface) {
    this.canvas = canvas
    this.context2d = context2d
    this.browserSurface = browserSurface
    this._drawListeners = []

    const browserPointer = this.browserSurface.browserSeat.browserPointer
    const browserSession = this.browserSurface.browserSession
    this._mouseEnterListener = browserSession.eventSource((event) => {
      event.preventDefault()
      browserPointer.onMouseEnter(event)
    })
    this._mouseLeaveListener = browserSession.eventSource((event) => {
      event.preventDefault()
      browserPointer.onMouseLeave(event)
    })

    this._destroyPromise = new Promise((resolve) => {
      this._destroyResolve = resolve
    })
  }

  draw (sourceCanvas) {
    this.canvas.width = sourceCanvas.width
    this.canvas.height = sourceCanvas.height
    this.context2d.drawImage(sourceCanvas, 0, 0)
    this._drawListeners.forEach(listener => {
      listener(this)
    })
  }

  addDrawListener (listener) {
    this._drawListeners.push(listener)
  }

  removeDrawListener (listener) {
    const index = this._drawListeners.indexOf(listener)
    if (index > -1) {
      this._drawListeners.splice(index, 1)
    }
  }

  /**
   * @param {Point} canvasPoint
   * @return {Point}
   */
  toSurfaceSpace (canvasPoint) {
    const surfaceWidth = this.browserSurface.size.w
    const surfaceHeight = this.browserSurface.size.h
    const canvasWidth = this.canvas.width
    const canvasHeight = this.canvas.height

    if (canvasWidth === surfaceWidth && canvasHeight === surfaceHeight) {
      return canvasPoint
    }

    const scalarVector = Vect4.create(surfaceWidth / canvasWidth, surfaceHeight / canvasHeight, 1, 1)

    return Mat4.scalarVector(scalarVector).timesPoint(canvasPoint)
  }

  unfade () {
    this._unfade(0.08)
  }

  _unfade (opacity) {
    if (opacity < 1) {
      window.requestAnimationFrame(() => {
        this._unfade(opacity)
      })
    }
    this.canvas.style.opacity = opacity
    this.canvas.style.filter = 'alpha(opacity=' + opacity * 100 + ')'
    opacity *= 1.1
  }

  fade () {
    this._fade(1)
  }

  _fade (opacity) {
    if (opacity > 0.1) {
      window.requestAnimationFrame(() => {
        this._fade(opacity)
      })
    }
    this.canvas.style.opacity = opacity
    this.canvas.style.filter = 'alpha(opacity=' + opacity * 100 + ')'
    opacity *= 0.8
  }

  destroy () {
    this._destroyResolve()
  }

  onDestroy () {
    return this._destroyPromise
  }

}<commit_msg>remove empty line<commit_after>'use strict'
import Vect4 from './math/Vect4'
import Mat4 from './math/Mat4'

export default class BrowserSurfaceView {
  /**
   *
   * @param {BrowserSurface} browserSurface
   * @returns {BrowserSurfaceView}
   */
  static create (browserSurface) {
    const canvas = this._createCanvas()
    const context2d = canvas.getContext('2d')
    const browserSurfaceView = new BrowserSurfaceView(canvas, context2d, browserSurface)
    canvas.view = browserSurfaceView
    return browserSurfaceView
  }

  /**
   * @returns {HTMLCanvasElement}
   * @private
   */
  static _createCanvas () {
    const canvas = document.createElement('canvas')
    canvas.style.left = '0px'
    canvas.style.top = '0px'
    canvas.style.position = 'absolute'
    canvas.style.zIndex = 0
    return canvas
  }

  enableMouseListeners () {
    this.canvas.addEventListener('mouseenter', this._mouseEnterListener, true)
    this.canvas.addEventListener('mouseleave', this._mouseLeaveListener, true)
    // other mouse listeners are set in the browser pointer class
  }

  disableMouseListeners () {
    this.canvas.removeEventListener('mouseenter', this._mouseEnterListener, true)
    this.canvas.removeEventListener('mouseleave', this._mouseLeaveListener, true)
  }

  /**
   * @private
   * @param canvas
   * @param context2d
   * @param browserSurface
   */
  constructor (canvas, context2d, browserSurface) {
    this.canvas = canvas
    this.context2d = context2d
    this.browserSurface = browserSurface
    this._drawListeners = []

    const browserPointer = this.browserSurface.browserSeat.browserPointer
    const browserSession = this.browserSurface.browserSession
    this._mouseEnterListener = browserSession.eventSource((event) => {
      event.preventDefault()
      browserPointer.onMouseEnter(event)
    })
    this._mouseLeaveListener = browserSession.eventSource((event) => {
      event.preventDefault()
      browserPointer.onMouseLeave(event)
    })

    this._destroyPromise = new Promise((resolve) => {
      this._destroyResolve = resolve
    })
  }

  draw (sourceCanvas) {
    this.canvas.width = sourceCanvas.width
    this.canvas.height = sourceCanvas.height
    this.context2d.drawImage(sourceCanvas, 0, 0)
    this._drawListeners.forEach(listener => {
      listener(this)
    })
  }

  addDrawListener (listener) {
    this._drawListeners.push(listener)
  }

  removeDrawListener (listener) {
    const index = this._drawListeners.indexOf(listener)
    if (index > -1) {
      this._drawListeners.splice(index, 1)
    }
  }

  /**
   * @param {Point} canvasPoint
   * @return {Point}
   */
  toSurfaceSpace (canvasPoint) {
    const surfaceWidth = this.browserSurface.size.w
    const surfaceHeight = this.browserSurface.size.h
    const canvasWidth = this.canvas.width
    const canvasHeight = this.canvas.height

    if (canvasWidth === surfaceWidth && canvasHeight === surfaceHeight) {
      return canvasPoint
    }

    const scalarVector = Vect4.create(surfaceWidth / canvasWidth, surfaceHeight / canvasHeight, 1, 1)

    return Mat4.scalarVector(scalarVector).timesPoint(canvasPoint)
  }

  unfade () {
    this._unfade(0.08)
  }

  _unfade (opacity) {
    if (opacity < 1) {
      window.requestAnimationFrame(() => {
        this._unfade(opacity)
      })
    }
    this.canvas.style.opacity = opacity
    this.canvas.style.filter = 'alpha(opacity=' + opacity * 100 + ')'
    opacity *= 1.1
  }

  fade () {
    this._fade(1)
  }

  _fade (opacity) {
    if (opacity > 0.1) {
      window.requestAnimationFrame(() => {
        this._fade(opacity)
      })
    }
    this.canvas.style.opacity = opacity
    this.canvas.style.filter = 'alpha(opacity=' + opacity * 100 + ')'
    opacity *= 0.8
  }

  destroy () {
    this._destroyResolve()
  }

  onDestroy () {
    return this._destroyPromise
  }
}
{'fraction_non_alphanumeric': 0.11581001951854261, 'fraction_numerical': 0.01040988939492518, 'mean_word_length': 3.159090909090909, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '4871063', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2252, 'n_words': 434}