Skip to content

Commit be0695d

Browse files
committed
improvement(chart): smooooothly transition chart length
1 parent 8a502ab commit be0695d

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

Diff for: src/popup/Chart.svelte

+24-4
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,34 @@
4141
$: stretchFactor = timeProgressionSpeed === 'soundedSpeedTime'
4242
? soundedSpeed
4343
: 1;
44+
4445
// TODO technically this is not correct, because the grid and the current output marker
4546
// (https://github.com/WofWca/jumpcutter/blob/5e09bf841e9c94ed5f5da03dfaea862dda269788/src/popup/Chart.svelte#L424-L455)
4647
// are still drawn in media intrinsic time, not in (media intrinsic time / soundedSpeed),
4748
// so it's more like changing `popupChartLengthInSeconds`, like it's not respected.
4849
$: millisPerPixel = stretchFactor * lengthSeconds * 1000 / widthPx;
49-
$: {
50-
if (smoothie) {
51-
smoothie.options.millisPerPixel = millisPerPixel;
50+
let millisPerPixelCurrValue: number | undefined;
51+
let millisPerPixelPrevValue = 0;
52+
let millisPerPixelLastUpdatedAt = 0;
53+
function getTweenedMillisPerPixel() {
54+
const millisPerPixelTweeningDuration = 50;
55+
const tweeningPhase = 1 - Math.min(
56+
1,
57+
(Date.now() - millisPerPixelLastUpdatedAt) / millisPerPixelTweeningDuration
58+
);
59+
const diff = millisPerPixelCurrValue! - millisPerPixelPrevValue;
60+
return millisPerPixelCurrValue! - diff * tweeningPhase;
61+
}
62+
function onMillisPerPixelUpdate(newVal: number) {
63+
if (!millisPerPixelCurrValue) {
64+
millisPerPixelCurrValue = millisPerPixel;
5265
}
53-
};
66+
millisPerPixelPrevValue = getTweenedMillisPerPixel();
67+
millisPerPixelCurrValue = millisPerPixel;
68+
millisPerPixelLastUpdatedAt = Date.now();
69+
}
70+
$: onMillisPerPixelUpdate(millisPerPixel);
71+
5472
$: jumpPeriodMs = jumpPeriod / 100 * widthPx * millisPerPixel;
5573
let onJumpPeriodChange: undefined | ((prevStretchFactor: number) => void);
5674
let prevStretchFactor = stretchFactor;
@@ -485,6 +503,8 @@
485503
// In theory this could also be rewritten with `maybeInsertExtrapolatedData`, but it's fine now.
486504
(volumeThresholdSeries as any).data[1][0] = timeAtChartEdge;
487505
506+
smoothie.options.millisPerPixel = getTweenedMillisPerPixel();
507+
488508
const renderTimeBefore = (smoothie as SmoothieChartWithPrivateFields).lastRenderTimeMillis;
489509
smoothie.render(canvasEl, timeAtChartEdge);
490510
const renderTimeAfter = (smoothie as SmoothieChartWithPrivateFields).lastRenderTimeMillis;

0 commit comments

Comments
 (0)