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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
<template>
<div class="touch-none" :width="width" :height="height"
@wheel.exact.prevent="onWheel" @wheel.shift.exact.prevent="onShiftWheel"
@pointerdown.prevent="onPointerDown" @pointermove.prevent="onPointerMove" @pointerup.prevent="onPointerUp"
@pointercancel.prevent="onPointerUp" @pointerout.prevent="onPointerUp" @pointerleave.prevent="onPointerUp"
ref="rootRef">
<svg :viewBox="`0 0 ${width} ${height}`">
<line stroke="yellow" stroke-width="2px" x1="50%" y1="0%" x2="50%" y2="100%"/>
<template v-for="n in ticks" :key="n">
<line v-if="n == minDate || n == maxDate"
:x1="-4" y1="0"
:x2="4" y2="0"
stroke="yellow" stroke-width="8px" :style="tickStyles(n)" class="animate-fadein"/>
<line v-else
:x1="-8" y1="0"
:x2="8" y2="0"
stroke="yellow" stroke-width="1px" :style="tickStyles(n)" class="animate-fadein"/>
</template>
<text fill="#606060" v-for="n in ticks" :key="n"
:x="width/2 + 20" y="0"
text-anchor="start" dominant-baseline="middle" :style="tickLabelStyles(n)" class="text-sm animate-fadein">
{{n}}
</text>
</svg>
</div>
</template>
<script setup lang="ts">
import {ref, onMounted, nextTick} from 'vue';
// Refs
const rootRef = ref(null as HTMLElement | null);
// Props
const props = defineProps({
width: {type: Number, required: true},
height: {type: Number, required: true},
});
// Vars
const minDate = -1000; // Lowest date that gets marked
const maxDate = 1000;
const startDate = ref(0); // Lowest date on displayed timeline
const endDate = ref(0);
const scales = [200, 50, 10, 1, 0.2]; // The timeline get divided into units of scales[0], then scales[1], etc
let scaleIdx = 0; // Index of current scale in 'scales'
const ticks = ref(null); // Holds date value for each tick
const SCROLL_SHIFT_CHG = 0.2; // Proportion of timeline length to shift by upon scroll
const ZOOM_RATIO = 1.5; // When zooming out, the timeline length gets multiplied by this ratio
const MIN_TICK_SEP = 30; // Smallest px separation between ticks
const MIN_LAST_TICKS = 3; // When at smallest scale, don't zoom further if less than this many ticks would result
let PAD_UNITS = 0.5; // Amount of extra scale units to add before/after min/max date
// For initialisation
function initTicks(): number[] {
// Find smallest usable scale
for (let i = 0; i < scales.length; i++){
let len = maxDate - minDate + (PAD_UNITS * scales[i]) * 2;
if (props.height * (scales[i] / len) > MIN_TICK_SEP){
scaleIdx = i;
} else {
break;
}
}
// Set start/end date
let extraPad = PAD_UNITS * scales[scaleIdx];
startDate.value = minDate - extraPad;
endDate.value = maxDate + extraPad;
// Get tick values
let newTicks = [];
let next = minDate;
while (next <= maxDate){
newTicks.push(next);
next += scales[scaleIdx];
}
ticks.value = newTicks;
//
updateTicks();
}
onMounted(() => nextTick(initTicks));
// Adds extra ticks outside the visible area (which can transition in upon shift/zoom),
// and adds/removes ticks upon a scale change
function updateTicks(){
let len = endDate.value - startDate.value;
let shiftChg = len * SCROLL_SHIFT_CHG;
let scaleChg = len * (ZOOM_RATIO - 1) / 2;
let scale = scales[scaleIdx];
// Get ticks in new range, and add hidden ticks that might transition in on a shift action
let tempTicks = [];
let next = Math.ceil((Math.max(minDate, startDate.value - shiftChg) - minDate) / scale);
let last = Math.floor((Math.min(maxDate, endDate.value + shiftChg) - minDate) / scale);
while (next <= last){
tempTicks.push(minDate + next * scale);
next++;
}
// Get hidden ticks that might transition in on a zoom action
let tempTicks2 = [];
let tempTicks3 = [];
if (scaleIdx > 0){
scale = scales[scaleIdx-1];
let first = Math.ceil((Math.max(minDate, startDate.value - scaleChg) - minDate) / scale);
while ((minDate + first * scale) < tempTicks[0]){
tempTicks2.push(minDate + first * scale);
first++;
}
let last = Math.floor((Math.min(maxDate, endDate.value + scaleChg) - minDate) / scale);
let next = Math.floor((tempTicks[tempTicks.length - 1] - minDate) / scale) + 1;
while (next <= last){
tempTicks3.push(minDate + next * scale);
next++;
}
}
//
ticks.value = [].concat(tempTicks2, tempTicks, tempTicks3);
}
// Performs a shift action
function shiftTimeline(n: number){
let len = endDate.value - startDate.value;
let extraPad = PAD_UNITS * scales[scaleIdx]
let paddedMinDate = minDate - extraPad;
let paddedMaxDate = maxDate + extraPad;
let chg = len * n;
if (startDate.value + chg < paddedMinDate){
if (startDate.value == paddedMinDate){
console.log('Reached minDate limit')
return;
}
chg = paddedMinDate - startDate.value;
startDate.value = paddedMinDate;
endDate.value += chg;
} else if (endDate.value + chg > paddedMaxDate){
if (endDate.value == paddedMaxDate){
console.log('Reached maxDate limit')
return;
}
chg = paddedMaxDate - endDate.value;
endDate.value = paddedMaxDate;
startDate.value += chg;
} else {
startDate.value += chg;
endDate.value += chg;
}
updateTicks();
}
// Performs a zoom action
function zoomTimeline(frac: number){
let oldLen = endDate.value - startDate.value;
let newLen = oldLen * frac;
let extraPad = PAD_UNITS * scales[scaleIdx]
let paddedMinDate = minDate - extraPad;
let paddedMaxDate = maxDate + extraPad;
// Get new bounds
let newStart: number;
let newEnd: number;
if (pointerY == null){
let lenChg = newLen - oldLen
newStart = startDate.value - lenChg / 2;
newEnd = endDate.value + lenChg / 2;
} else {
let y = 0; // Element-relative pointerY
if (rootRef.value != null){ // Can become null during dev-server hot-reload for some reason
let rect = rootRef.value.getBoundingClientRect();
y = pointerY - rect.top;
}
let zoomCenter = startDate.value + (y / props.height) * oldLen;
newStart = zoomCenter - (zoomCenter - startDate.value) * frac;
newEnd = zoomCenter + (endDate.value - zoomCenter) * frac;
}
if (newStart < paddedMinDate){
newEnd += paddedMinDate - newStart;
newStart = paddedMinDate;
if (newEnd > paddedMaxDate){
if (startDate.value == paddedMinDate && endDate.value == paddedMaxDate){
console.log('Reached upper scale limit');
return;
} else {
newEnd = paddedMaxDate;
}
}
} else if (newEnd > paddedMaxDate){
newStart -= newEnd - paddedMaxDate;
newEnd = paddedMaxDate;
if (newStart < paddedMinDate){
if (startDate.value == paddedMinDate && endDate.value == paddedMaxDate){
console.log('Reached upper scale limit');
return;
} else {
newStart = paddedMinDate;
}
}
}
// Possibly change the scale
newLen = newEnd - newStart;
let tickDiff = props.height * (scales[scaleIdx] / newLen);
if (tickDiff < MIN_TICK_SEP){
if (scaleIdx == 0){
console.log('INFO: Reached zoom out limit');
return;
} else {
scaleIdx--;
}
} else {
if (scaleIdx < scales.length - 1){
if (tickDiff > MIN_TICK_SEP * scales[scaleIdx] / scales[scaleIdx + 1]){
scaleIdx++;
}
} else {
if (newLen / tickDiff < MIN_LAST_TICKS){
console.log('INFO: Reached zoom in limit');
return;
}
}
}
//
startDate.value = newStart;
endDate.value = newEnd;
updateTicks();
}
// For mouse/etc handling
let pointerX = null; // Stores pointer position (used for pointer-centered zooming)
let pointerY = null;
const ptrEvtCache = []; // Holds last captured PointerEvent for each pointerId (used for pinch-zoom)
let lastPinchY = -1; // Holds last y-distance between two pointers that are down
let dragDiffY = 0; // Holds accumlated change in pointer's y-coordinate while dragging
let dragHandler = 0; // Set by a setTimeout() to a handler for pointer dragging
function onPointerDown(evt: PointerEvent){
ptrEvtCache.push(evt);
// Update stored cursor position
pointerX = evt.clientX;
pointerY = evt.clientY;
}
function onPointerMove(evt: PointerEvent){
// Update event cache
const index = ptrEvtCache.findIndex((e) => e.pointerId == evt.pointerId);
ptrEvtCache[index] = evt;
//
if (ptrEvtCache.length == 1){
// Handle pointer dragging
dragDiffY += evt.clientY - pointerY;
if (dragHandler == 0){
dragHandler = setTimeout(() => {
if (Math.abs(dragDiffY) > 2){
shiftTimeline(-dragDiffY / props.height);
dragDiffY = 0;
}
dragHandler = 0;
}, 50);
}
} else if (ptrEvtCache.length == 2){
// Handle pinch-zoom
const pinchY = Math.abs(ptrEvtCache[0].clientY - ptrEvtCache[1].clientY);
if (lastPinchY > 0){
if (pinchY > lastPinchY){
console.log('Pinching out, zooming in');
//TODO: implement pinch-zooming
} else if (pinchY < lastPinchY){
console.log('Pinching in, zooming out');
//TODO: implement pinch-zooming
}
}
lastPinchY = pinchY;
}
// Update stored cursor position
pointerX = evt.clientX;
pointerY = evt.clientY;
}
function onPointerUp(evt: PointerEvent){
// Remove from event cache
const index = ptrEvtCache.findIndex((e) => e.pointerId == evt.pointerId);
ptrEvtCache.splice(index, 1);
// Reset other vars
if (ptrEvtCache.length < 2){
lastPinchY = -1;
}
dragDiffY = 0;
}
function onWheel(evt: WheelEvent){
if (evt.deltaY > 0){
shiftTimeline(SCROLL_SHIFT_CHG);
} else {
shiftTimeline(-SCROLL_SHIFT_CHG);
}
}
function onShiftWheel(evt: WheelEvent){
if (evt.deltaY > 0){
zoomTimeline(ZOOM_RATIO);
} else {
zoomTimeline(1/ZOOM_RATIO);
}
}
// Styles
function tickStyles(tick: number){
let y = (tick - startDate.value) / (endDate.value - startDate.value) * props.height;
let scaleX = 1;
if (scaleIdx > 0 && tick % scales[scaleIdx-1] == 0){ // If the tick exists on the scale directly above this one
scaleX = 2;
}
return {
transform: `translate(${props.width/2}px, ${y}px) scale(${scaleX})`,
transitionProperty: 'transform, opacity',
transitionDuration: '300ms',
transitionTimingFunction: 'ease-out',
opacity: (y >= 0 && y <= props.height) ? 1 : 0,
}
}
function tickLabelStyles(tick: number){
let y = (tick - startDate.value) / (endDate.value - startDate.value) * props.height;
let fontHeight = 10;
return {
transform: `translate(0, ${y}px)`,
transitionProperty: 'transform, opacity',
transitionDuration: '300ms',
transitionTimingFunction: 'ease-out',
opacity: (y >= fontHeight && y <= props.height - fontHeight) ? 1 : 0,
}
}
</script>
<style>
.animate-fadein {
animation-name: fadein;
animation-duration: 300ms;
animation-timing-function: ease-in;
}
@keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>
|