Skip to content

Commit

Permalink
improvement(chart): smooooothly transition chart length
Browse files Browse the repository at this point in the history
  • Loading branch information
WofWca committed Apr 25, 2022
1 parent 8a502ab commit be0695d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/popup/Chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,34 @@
$: stretchFactor = timeProgressionSpeed === 'soundedSpeedTime'
? soundedSpeed
: 1;
// TODO technically this is not correct, because the grid and the current output marker
// (https://github.com/WofWca/jumpcutter/blob/5e09bf841e9c94ed5f5da03dfaea862dda269788/src/popup/Chart.svelte#L424-L455)
// are still drawn in media intrinsic time, not in (media intrinsic time / soundedSpeed),
// so it's more like changing `popupChartLengthInSeconds`, like it's not respected.
$: millisPerPixel = stretchFactor * lengthSeconds * 1000 / widthPx;
$: {
if (smoothie) {
smoothie.options.millisPerPixel = millisPerPixel;
let millisPerPixelCurrValue: number | undefined;
let millisPerPixelPrevValue = 0;
let millisPerPixelLastUpdatedAt = 0;
function getTweenedMillisPerPixel() {
const millisPerPixelTweeningDuration = 50;
const tweeningPhase = 1 - Math.min(
1,
(Date.now() - millisPerPixelLastUpdatedAt) / millisPerPixelTweeningDuration
);
const diff = millisPerPixelCurrValue! - millisPerPixelPrevValue;
return millisPerPixelCurrValue! - diff * tweeningPhase;
}
function onMillisPerPixelUpdate(newVal: number) {
if (!millisPerPixelCurrValue) {
millisPerPixelCurrValue = millisPerPixel;
}
};
millisPerPixelPrevValue = getTweenedMillisPerPixel();
millisPerPixelCurrValue = millisPerPixel;
millisPerPixelLastUpdatedAt = Date.now();
}
$: onMillisPerPixelUpdate(millisPerPixel);
$: jumpPeriodMs = jumpPeriod / 100 * widthPx * millisPerPixel;
let onJumpPeriodChange: undefined | ((prevStretchFactor: number) => void);
let prevStretchFactor = stretchFactor;
Expand Down Expand Up @@ -485,6 +503,8 @@
// In theory this could also be rewritten with `maybeInsertExtrapolatedData`, but it's fine now.
(volumeThresholdSeries as any).data[1][0] = timeAtChartEdge;
smoothie.options.millisPerPixel = getTweenedMillisPerPixel();
const renderTimeBefore = (smoothie as SmoothieChartWithPrivateFields).lastRenderTimeMillis;
smoothie.render(canvasEl, timeAtChartEdge);
const renderTimeAfter = (smoothie as SmoothieChartWithPrivateFields).lastRenderTimeMillis;
Expand Down

0 comments on commit be0695d

Please sign in to comment.