summaryrefslogtreecommitdiff
path: root/animism-align/frontend/views/align/components/ticks.component.js
blob: 95a2b32506755218f0bf06e260b8e4adef662655 (plain)
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
import React, { Component } from 'react'

import { ZOOM_STEPS, ZOOM_LABEL_STEPS, ZOOM_TICK_STEPS } from '../constants'
import { timestamp } from '../../../util'

export default class Ticks extends Component {
  render() {
    let { start_ts, zoom, duration } = this.props.timeline
    duration /= 10
    const width = window.innerWidth

    let secondsPerPixel = ZOOM_STEPS[zoom] / 10 // 0.1 sec / step
    let pixelTimeDuration = 1 / secondsPerPixel // secs per pixel
    let widthTimeDuration = width / pixelTimeDuration // secs per pixel
    console.log(secondsPerPixel, pixelTimeDuration)
    console.log('width in seconds', widthTimeDuration)

    let secondsPerTick = ZOOM_LABEL_STEPS[zoom] // secs
    let pixelsPerLabel = secondsPerTick * pixelTimeDuration
    let pixelsPerTick = ZOOM_TICK_STEPS[zoom]
    console.log('pixels per label', pixelsPerLabel)

    let subdivision = secondsPerTick
    while (pixelsPerLabel < 200) {
      pixelsPerLabel *= 2
      pixelsPerTick *= 2
      subdivision *= 2
    }
    if (subdivision > 60) {

    }

    console.log('start ts', start_ts)
    let pixelOffset = (start_ts / secondsPerPixel)
    let pixelRemainder = pixelOffset % pixelsPerLabel
    let startOffset = pixelsPerLabel - pixelRemainder
    let startTiming = (pixelOffset + startOffset) * secondsPerPixel

    let labelCount = Math.ceil(width / pixelsPerLabel)
    let offset, timing, tickLabels = [], ticks = []
    for (var i = -1; i < labelCount; i++) {
      offset = i * pixelsPerLabel + startOffset - 20
      if (offset + 20 > width) continue
      timing = i * subdivision + startTiming
      if (timing > duration) {
        break
      }
      tickLabels.push(
        <div className='tickLabel' key={"tickLabel_" + i}
          style={{
            left: Math.floor(offset)
          }}>
          {timestamp(timing)}
        </div>
      )
    }

    let durationOffset = duration / secondsPerPixel - pixelOffset
    if (timing > duration) {
      tickLabels.push(
        <div className='tickLabel tickLabelTotal' key={"tickLabel_total"}
          style={{
            left: durationOffset - 20
          }}>
          {timestamp(duration, 1)}
        </div>
      )
      ticks.push(
        <div className='tick' key={"tick_total"}
          style={{
            left: Math.floor(durationOffset),
          }}
        />
      )
    }
    let tickCount = Math.ceil(width / pixelsPerTick) + 1
    for (var i = 0; i < tickCount; i += 1) {
      offset = i * pixelsPerTick + startOffset - pixelsPerLabel
      if (offset > durationOffset) {
        break
      }
      ticks.push(
        <div className='tick' key={"tick_" + i}
          style={{
            left: Math.floor(offset),
          }}
        />
      )
    }
    console.log(ticks.length)

    return (
      <div className='ticks'>
        {ticks}
        {tickLabels}
      </div>
    )
  }
}