Skip to content

Commit

Permalink
feat: add public method - addMoreFrames
Browse files Browse the repository at this point in the history
  • Loading branch information
kimhongyeon committed Aug 7, 2024
1 parent a5a159f commit acb4bf7
Showing 1 changed file with 79 additions and 33 deletions.
112 changes: 79 additions & 33 deletions src/lib/FastImageSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class FastImageSequence {
private initialized: boolean = false;
private posterImage: HTMLImageElement | undefined;
private timeFrameVisible: number = 0;
private time: number = 0;

/**
* Creates an instance of FastImageSequence.
Expand Down Expand Up @@ -120,15 +121,12 @@ export class FastImageSequence {
display: 'block',
});

this.container.appendChild(this.canvas);

this.resizeObserver = new ResizeObserver(() => {
this.clearCanvas = true;
if (this.lastFrameDrawn < 0 && this.posterImage) {
this.drawImage(this.posterImage);
}
});
this.resizeObserver.observe(this.canvas);

this.mutationObserver = new MutationObserver(() => {
if (!this.container.isConnected) {
Expand All @@ -138,37 +136,10 @@ export class FastImageSequence {
});
this.mutationObserver.observe(container, {childList: true});

// init all frames
this.frames = Array.from({length: this.options.frames}, (_, index) => new Frame(index));
this.log = this.options.showDebugInfo ? console.log : () => {
};

// init all input sources
const sources = this.options.src instanceof Array ? this.options.src : [this.options.src];
this.sources = sources.map((src, index) => {
if (src.tarURL !== undefined) {
return new ImageSourceTar(this, index, src);
} else if (src.imageURL !== undefined) {
return new ImageSourceFetch(this, index, src);
} else {
return new ImageSource(this, index, src);
}
});

this.loadResources().then(() => {
this.initialized = true;

this.log('Frames', this.frames);
this.log('Options', this.options);

if (this.options.showDebugInfo) {
this.logElement = createLogElement();
this.container.appendChild(this.logElement);
this.tick(() => this.logDebugStatus(this.logElement as HTMLDivElement));
}

this.drawingLoop(-1);
});
this.struct();
}

/**
Expand Down Expand Up @@ -261,6 +232,22 @@ export class FastImageSequence {
this.speed = 0;
}

/**
* Add more frames to the image sequence.
* @param {number} moreFrames - The number of frames to add.
*/
public addMoreFrames(moreFrames: number) {
if (moreFrames < 0) {
throw new Error('FastImageSequence: moreFrames must be greater than or equal to 0');
}

if (moreFrames === 0) {
return;
}

this.struct(moreFrames);
}

/**
* Get the image of a specific frame.
* @param {number} index - The index of the frame.
Expand Down Expand Up @@ -298,8 +285,9 @@ export class FastImageSequence {

/**
* Destruct the FastImageSequence instance.
* @param {boolean} [isAddMoreFrames=false] - Whether the destruct is called to add more frames.
*/
public destruct() {
public destruct(isAddMoreFrames: boolean = false) {
if (this.destructed) {
return;
}
Expand All @@ -313,7 +301,7 @@ export class FastImageSequence {
this.mutationObserver.disconnect();

this.container.removeChild(this.canvas);
if (this.logElement) {
if (this.logElement && !isAddMoreFrames) {
this.container.removeChild(this.logElement);
this.logElement = undefined;
}
Expand All @@ -335,6 +323,62 @@ export class FastImageSequence {
this.clearCanvas = true;
}

/**
* Struct the FastImageSequence instance.
* If moreFrames is provided, it will add more frames to the sequence. Otherwise, it will initialize the sequence.
* @param moreFrames
*/
private struct(moreFrames?: number) {
if (moreFrames !== undefined) {
this.options.frames += moreFrames;
this.destruct();
}

this.container.appendChild(this.canvas);
this.resizeObserver.observe(this.canvas);
this.mutationObserver.observe(this.container, {childList: true});

// init all frames
this.frames = Array.from({length: this.options.frames}, (_, index) => new Frame(index));

// init all input sources
const sources = this.options.src instanceof Array ? this.options.src : [this.options.src];
this.sources = sources.map((src, index) => {
if (src.tarURL !== undefined) {
return new ImageSourceTar(this, index, src);
} else if (src.imageURL !== undefined) {
return new ImageSourceFetch(this, index, src);
} else {
return new ImageSource(this, index, src);
}
});

if (moreFrames !== undefined) {
this.destructed = false;
}

this.loadResources().then(() => {
if (moreFrames === undefined) {
this.initialized = true;
}

this.log('Frames', this.frames);
this.log('Options', this.options);

if (this.options.showDebugInfo && moreFrames === undefined) {
this.logElement = createLogElement();
this.container.appendChild(this.logElement);
this.tick(() => this.logDebugStatus(this.logElement as HTMLDivElement));
}

if (moreFrames !== undefined) {
this.drawingLoop(this.time);
} else {
this.drawingLoop(-1);
}
});
}

private setLoadingPriority() {
const priorityIndex = this.index;// this.wrapIndex(Math.min(this.spread / 2 - 2, (this.frame - this.prevFrame) * (dt * 60)) + this.frame);
this.frames.forEach((image) => {
Expand Down Expand Up @@ -379,6 +423,8 @@ export class FastImageSequence {
return;
}

this.time = time;

time /= 1000;

const dt = this.initialized ? this.startTime < 0 ? 1 / 60 : Math.min(time - this.startTime, 1 / 30) : 0;
Expand Down

0 comments on commit acb4bf7

Please sign in to comment.