File size: 3,678 Bytes
7e111c4
 
 
 
 
 
 
 
 
 
 
9d7eb99
7e111c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df087e
 
 
7e111c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const each = require('licia/each')
const contain = require('licia/contain')
const toArr = require('licia/toArr')

module.exports = function (eruda) {
  const { evalCss } = eruda.util

  class Touches extends eruda.Tool {
    constructor() {
      super()
      this.name = 'touches'
      this.displayName = 'タッチ'
      this._style = evalCss(require('./style.scss'))
      this._touches = []
      this._isRunning = false
      this._updateTouches = (e) => {
        const touches = []
        const changedTouches = toArr(e.changedTouches)
        each(e.touches, (touch) => {
          touches.push({
            clientX: touch.clientX,
            clientY: touch.clientY,
            changed: contain(changedTouches, touch),
          })
        })
        this._touches = touches
      }
    }
    init($el, container) {
      super.init($el, container)
      $el.html('<canvas></canvas>')
      this._initCanvas()
      this._bindEvent()
    }
    show() {
      super.show()
      this._start()
    }
    hide() {
      super.hide()
      this._stop()
    }
    destroy() {
      super.destroy()
      evalCss.remove(this._style)
      window.removeEventListener('touchstart', this._updateTouches)
      window.removeEventListener('touchmove', this._updateTouches)
      window.removeEventListener('touchend', this._updateTouches)
      window.removeEventListener('touchcancel', this._updateTouches)
    }
    _initCanvas() {
      const canvas = (this._canvas = this._$el.find('canvas').get(0))
      const ctx = (this._ctx = this._canvas.getContext('2d'))
      const winWidth = window.innerWidth
      const winHeight = window.innerHeight
      canvas.width = 800
      canvas.height = 800 * (winHeight / winWidth)
      ctx.textBaseline = 'middle'
    }
    _start() {
      if (this._isRunning) return
      this._isRunning = true
      const self = this
      function loop() {
        if (!self._isRunning) return
        self._update()
        requestAnimationFrame(loop)
      }
      loop()
    }
    _stop() {
      this._isRunning = false
    }
    _update() {
      const ctx = this._ctx
      const touches = this._touches
      const canvas = this._canvas
      const { width, height } = canvas
      const { innerWidth, innerHeight } = window
      ctx.clearRect(0, 0, width, height)

      const curTheme = evalCss.getCurTheme()

      if (touches.length === 0) {
        ctx.fillStyle = curTheme.foreground
        ctx.font = 'bold 50px Helvetica,Arial,sans-serif'
        const text = '画青をタッチしてください'
        const textWidth = ctx.measureText(text).width
        ctx.fillText(text, (width - textWidth) / 2, height / 2)
        return
      }

      ctx.font = 'bold 50px Helvetica,Arial,sans-serif'

      each(touches, (touch) => {
        const { clientX, clientY, changed } = touch
        const x = (clientX / innerWidth) * width
        const y = (clientY / innerHeight) * height
        if (changed) {
          ctx.fillStyle = curTheme.consoleErrorForeground
        } else {
          ctx.fillStyle = curTheme.accent
        }
        ctx.fillText(
          `${Math.round(clientX)}, ${Math.round(clientY)}`,
          x + 50,
          y
        )
        ctx.beginPath()
        ctx.arc(x, y, 25, 0, Math.PI * 2, true)
        ctx.closePath()
        ctx.fill()
      })
    }
    _bindEvent() {
      window.addEventListener('touchstart', this._updateTouches, true)
      window.addEventListener('touchmove', this._updateTouches, true)
      window.addEventListener('touchend', this._updateTouches, true)
      window.addEventListener('touchcancel', this._updateTouches, true)
    }
  }

  return new Touches()
}