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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
import { h, Component } from 'preact'
import { clamp, norm } from '../util/math'
const initialState = {
dir: '/',
start: null,
end: null,
cursor: null,
selection: null,
width: 0,
ratio: 0,
loading: true
}
export default class Timeline extends Component {
state = { ...initialState }
constructor() {
super()
this.handleMouseDown = this.handleMouseDown.bind(this)
this.handleMouseMove = this.handleMouseMove.bind(this)
this.handleMouseUp = this.handleMouseUp.bind(this)
this.computeOffset = this.computeOffset.bind(this)
}
componentDidMount() {
const { sequence } = this.props
window.addEventListener('resize', this.computeOffset)
window.addEventListener('mousemove', this.handleMouseMove)
window.addEventListener('mouseup', this.handleMouseUp)
this.computeOffset()
if (sequence) {
this.reset()
}
}
componentDidUpdate(prevProps) {
const { sequence } = this.props
if (sequence !== prevProps.sequence) {
this.reset()
}
}
componentWillUnmount(){
window.removeEventListener('resize', this.computeOffset)
window.removeEventListener('mousemove', this.handleMouseMove)
window.removeEventListener('mouseup', this.handleMouseUp)
}
reset(){
const { sequence } = this.props
if (!sequence || !sequence.length) return
const len = sequence.length - 1
const start = { frame: sequence[0], i: 0 }
const end = { frame: sequence[len], i: len }
const width = Math.sqrt(clamp(sequence.length / 15000, 0.2, 1.0)) * Math.min(window.innerWidth - 40, 1000)
const ratio = width / sequence.length
setTimeout(() => this.computeOffset())
this.setState({
...initialState,
width,
ratio,
start,
end,
})
}
computeOffset(){
if (this.ref) {
this.offset = this.ref.getBoundingClientRect()
}
}
computeFrame(e){
const { sequence } = this.props
if (!sequence || !sequence.length) return null
let x = (e.pageX - this.offset.left) / this.offset.width
let y = (e.pageY - this.offset.top) / this.offset.height
if (this.state.dragging) {
x = clamp(x, 0, 1)
y = clamp(y, 0, 1)
}
if (0 <= x && x <= 1 && 0 <= y && y <= 1) {
const index = Math.floor(x * (sequence.length-1))
return { frame: sequence[index], i: index }
}
return null
}
handleMouseDown(e) {
this.setState({ dragging: true })
const frame = this.computeFrame(e)
if (frame) {
this.props.onPick && this.props.onPick(frame)
this.props.onSelect && this.props.onSelect({ start: frame, end: frame })
this.setState({
start: frame,
end: frame,
})
}
}
handleMouseMove(e) {
const frame = this.computeFrame(e)
if (frame) {
this.props.onCursor && this.props.onCursor(frame)
if (this.state.dragging) {
let start = this.state.start
let end = frame
this.setState({
cursor: frame,
end: frame,
})
if (this.props.onSelect) {
if (end.i < start.i) {
[start, end] = [end, start]
}
this.props.onSelect({ start, end })
}
} else {
this.setState({
cursor: frame
})
}
}
}
handleMouseUp(e) {
if (!this.state.dragging) return
let { start, end } = this.state
if (end.i < start.i) {
[start, end] = [end, start]
}
this.props.onSelect && this.props.onSelect({ start, end })
this.setState({ dragging: false })
}
render() {
const { sequence } = this.props
const { loading, start, end, cursor, width, ratio } = this.state
return (
<div
className='timeline'
style={{ width }}
ref={ref => this.ref = ref}
onMouseDown={this.handleMouseDown}
>
{ratio && start && end && this.renderSelection(start, end, ratio)}
{ratio && cursor && this.renderCursor(cursor, ratio)}
</div>
)
}
renderCursor(cursor, ratio){
const left = cursor.i * ratio
return (
<div key='cursor' className='cursor' style={{ left }} />
)
}
renderSelection(start, end, ratio){
const left = Math.min(start.i, end.i) * ratio
const width = Math.max(Math.abs(start.i - end.i) * ratio, 1)
return (
<div key='selection' className='selection' style={{ left, width }} />
)
}
}
|