- {@html JSON.stringify(flattenObject([scrollyVideoState]))
+ {@html JSON.stringify(flattenObject(scrollyVideoState))
.replace(/[{}"]/g, '')
.split(',')
.join('
')}
diff --git a/src/components/ScrollyVideo/js/ScrollyVideo.d.ts b/src/components/ScrollyVideo/js/ScrollyVideo.d.ts
index 9692bfee..78ceb82a 100644
--- a/src/components/ScrollyVideo/js/ScrollyVideo.d.ts
+++ b/src/components/ScrollyVideo/js/ScrollyVideo.d.ts
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-empty-object-type */
-/* eslint-disable @typescript-eslint/no-explicit-any */
+
export default ScrollyVideo;
/**
* ____ _ _ __ ___ _
@@ -29,8 +29,8 @@ declare class ScrollyVideo {
debug,
autoplay,
}: {
- src?: any;
- scrollyVideoContainer: any;
+ src?: string;
+ scrollyVideoContainer: string | HTMLDivElement | undefined;
objectFit?: string;
sticky?: boolean;
full?: boolean;
@@ -45,7 +45,7 @@ declare class ScrollyVideo {
autoplay?: boolean;
});
container: Element;
- src: any;
+ src: string;
transitionSpeed: number;
frameThreshold: number;
useWebCodecs: boolean;
@@ -63,11 +63,11 @@ declare class ScrollyVideo {
targetTime: number;
canvas: HTMLCanvasElement;
context: CanvasRenderingContext2D;
- frames: any[];
+ frames: ImageBitmap[] | null;
frameRate: number;
currentFrameNumber: number;
- updateScrollPercentage: (jump: any) => void;
- targetScrollPosition: any;
+ updateScrollPercentage: (jump: boolean) => void;
+ targetScrollPosition: number | null;
resize: () => void;
/**
* Sets the currentTime of the video as a specified percentage of its total duration.
@@ -78,13 +78,13 @@ declare class ScrollyVideo {
* - transitionSpeed: number - Defines the speed of the transition when `jump` is false. Represents the duration of the transition in milliseconds. Default is 8.
* - easing: (progress: number) => number - A function that defines the easing curve for the transition. It takes the progress ratio (a number between 0 and 1) as an argument and returns the eased value, affecting the playback speed during the transition.
*/
- setVideoPercentage(percentage: any, options?: {}): void;
+ setVideoPercentage(percentage: number, options?: {}): void;
/**
* Sets the style of the video or canvas to "cover" it's container
*
* @param el
*/
- setCoverStyle(el: any): void;
+ setCoverStyle(el: string): void;
/**
* Uses webCodecs to decode the video into frames
*/
@@ -94,7 +94,7 @@ declare class ScrollyVideo {
*
* @param frameNum
*/
- paintCanvasFrame(frameNum: any): void;
+ paintCanvasFrame(frameNum: number): void;
/**
* Transitions the video or the canvas to the proper frame.
*
@@ -108,9 +108,9 @@ declare class ScrollyVideo {
transitionSpeed,
easing,
}: {
- jump: any;
+ jump: boolean;
transitionSpeed?: number;
- easing?: any;
+ easing?: (progress: number) => number;
}): void;
transitioningRaf: number;
/**
@@ -122,13 +122,13 @@ declare class ScrollyVideo {
* - transitionSpeed: number - Defines the speed of the transition when `jump` is false. Represents the duration of the transition in milliseconds. Default is 8.
* - easing: (progress: number) => number - A function that defines the easing curve for the transition. It takes the progress ratio (a number between 0 and 1) as an argument and returns the eased value, affecting the playback speed during the transition.
*/
- setTargetTimePercent(percentage: any, options?: {}): void;
+ setTargetTimePercent(percentage: number, options?: {}): void;
/**
* Simulate trackScroll programmatically (scrolls on page by percentage of video)
*
* @param percentage
*/
- setScrollPercent(percentage: any): void;
+ setScrollPercent(percentage: number): void;
/**
* Call to destroy this ScrollyVideo object
*/
diff --git a/src/components/ScrollyVideo/js/ScrollyVideo.js b/src/components/ScrollyVideo/js/ScrollyVideo.js
index 044a0c71..0f74727a 100644
--- a/src/components/ScrollyVideo/js/ScrollyVideo.js
+++ b/src/components/ScrollyVideo/js/ScrollyVideo.js
@@ -75,6 +75,17 @@ class ScrollyVideo {
this.video.pause();
this.video.load();
+ this.video.addEventListener(
+ 'canplaythrough',
+ () => {
+ this.onReady();
+ if (this.autoplay && !this.useWebCodecs) {
+ this.autoplayScroll();
+ }
+ },
+ { once: true }
+ );
+
// Start the video percentage at 0
this.videoPercentage = 0;
@@ -190,7 +201,6 @@ class ScrollyVideo {
this.updateScrollPercentage(true);
this.totalTime = this.video.duration;
this.setCoverStyle(this.canvas || this.video);
- if (this.autoplay) this.autoplayScroll();
},
{ once: true }
);
@@ -201,7 +211,6 @@ class ScrollyVideo {
this.setTargetTimePercent(0, { jump: true });
this.totalTime = this.video.duration;
this.setCoverStyle(this.canvas || this.video);
- if (this.autoplay) this.autoplayScroll();
},
{ once: true }
);
@@ -372,6 +381,8 @@ class ScrollyVideo {
// Paint our first frame
this.paintCanvasFrame(Math.floor(this.currentTime * this.frameRate));
this.onReady();
+
+ if (this.autoplay) this.autoplayScroll();
}
/**
@@ -456,6 +467,10 @@ class ScrollyVideo {
this.currentTime >= this.targetTime
: this.currentTime <= this.targetTime;
+ if (scrollyVideoState.isAutoPlaying) {
+ scrollyVideoState.autoplayProgress = this.currentTime / this.totalTime;
+ }
+
// If we are already close enough to our target, pause the video and return.
// This is the base case of the recursive function
if (
diff --git a/src/components/ScrollyVideo/js/state.svelte.ts b/src/components/ScrollyVideo/js/state.svelte.ts
index cbe89594..1ace7509 100644
--- a/src/components/ScrollyVideo/js/state.svelte.ts
+++ b/src/components/ScrollyVideo/js/state.svelte.ts
@@ -12,7 +12,7 @@ type FramesData = {
totalFrames: number;
};
-type ScrollyVideoState = {
+export type ScrollyVideoState = {
generalData: GeneralData;
usingWebCodecs: boolean;
framesData: FramesData;
diff --git a/src/components/ScrollyVideo/js/utils.d.ts b/src/components/ScrollyVideo/js/utils.d.ts
index ce0992c9..1a38d614 100644
--- a/src/components/ScrollyVideo/js/utils.d.ts
+++ b/src/components/ScrollyVideo/js/utils.d.ts
@@ -1,6 +1,25 @@
-export function debounce(func: any, delay?: number): (...args: any[]) => void;
+import type { ScrollyVideoState } from './types';
+
+type FlattenedScrollyVideoState = {
+ src: string;
+ videoPercentage: number;
+ frameRate: number;
+ currentTime: number;
+ totalTime: number;
+ usingWebCodecs: boolean;
+ codec: string;
+ currentFrame: number;
+ totalFrames: number;
+ isAutoPlaying: boolean;
+ autoplayProgress: number;
+};
+
+export function debounce