Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
reindernijhoff committed May 27, 2024
1 parent e731cfa commit 94d5ffd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/lib/FastImageSequence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Frame from "./Frame.js";
import {createLogElement, logToScreen} from "./LogToScreen.js";
import ImageSource, {type ImageSourceOptions, INPUT_SRC} from "./ImageSource.js";
import ImageSource, {type ImageSourceOptions, INPUT_CODE, INPUT_SRC} from "./ImageSource.js";
import ImageSourceTar from "./ImageSourceTar.js";
import ImageSourceFetch from "./ImageSourceFetch.js";

Expand Down Expand Up @@ -493,11 +493,11 @@ export class FastImageSequence {

private logDebugStatus(output: HTMLDivElement) {
const formatPercentage = (n: number) => `${Math.abs(n * 100).toFixed(1).padStart(5, ' ')}%`;
let debugInfo = `${this.options.name} - frames: ${this.frames.length}, wrap: ${this.options.loop}, objectFit: ${this.options.objectFit}\n- loadProgress ${formatPercentage(this.loadProgress)}, last frame drawn ${this.lastFrameDrawn}/${this.index}\n`;
let debugInfo = `${this.options.name} - frames: ${this.frames.length}, wrap: ${this.options.loop}, objectFit: ${this.options.objectFit}\n loadProgress ${formatPercentage(this.loadProgress)}, last frame drawn ${this.lastFrameDrawn}/${this.index}\n`;

for (const source of this.sources) {
const {progress, numLoading, numLoaded, maxLoaded} = source.getLoadStatus();
debugInfo += `- ${source.type === INPUT_SRC ? `image` : ` tar`} ${formatPercentage(progress)}, numLoading: ${numLoading}, numLoaded: ${numLoaded}/${maxLoaded}${source.options.useWorker ? ', use worker' : ''}\n`;
debugInfo += ` src[${source.index}] ${source.type === INPUT_SRC ? `image:` : source.type === INPUT_CODE ? `code: ` : `tar: `} ${formatPercentage(progress)}, numLoading: ${numLoading}, numLoaded: ${numLoaded}/${maxLoaded}${source.options.useWorker ? ', use worker' : ''}\n`;
}
logToScreen(output, debugInfo);
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib/ImageElement.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import ImageSource, {type ImageSourceType, INPUT_SRC} from "./ImageSource.js";
import ImageSource from "./ImageSource.js";
import type Frame from "./Frame.js";

export default class ImageElement {
public available: boolean = true;
public loading: boolean = false;
public type: ImageSourceType = INPUT_SRC;
public frame: Frame;

private _image: CanvasImageSource | undefined;
Expand All @@ -13,7 +12,6 @@ export default class ImageElement {
constructor(context: ImageSource, frame: Frame) {
this.context = context;
this.frame = frame;
this.type = this.context.type;
}

public get image(): CanvasImageSource | undefined {
Expand Down
9 changes: 6 additions & 3 deletions src/lib/ImageSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import ImageElement from "./ImageElement.js";

export const INPUT_SRC = 0;
export const INPUT_TAR = 1;
export const INPUT_CODE = 2;

export type ImageSourceType = typeof INPUT_SRC | typeof INPUT_TAR;
export type ImageSourceType = typeof INPUT_SRC | typeof INPUT_TAR | typeof INPUT_CODE;

/**
* @typedef ImageSourceOptions
Expand Down Expand Up @@ -45,7 +46,6 @@ export default class ImageSource {

public options: ImageSourceOptions;
public index: number = -0;
public type: ImageSourceType;
public initialized: boolean = false;

protected context: FastImageSequence;
Expand All @@ -54,13 +54,16 @@ export default class ImageSource {
this.context = context;
this.index = index;
this.options = {...ImageSource.defaultOptions, ...options};
this.type = this.options.tarURL !== undefined ? INPUT_TAR : INPUT_SRC;

this.options.maxCachedImages = clamp(Math.floor(this.options.maxCachedImages), 1, this.context.options.frames);

this.context.frames.forEach(frame => frame.images[index] = new ImageElement(this, frame));
}

public get type() {
return INPUT_CODE;
}

protected get images(): ImageElement[] {
return this.context.frames.map(frame => frame.images[this.index] as ImageElement);
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/ImageSourceFetch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import ImageElement from "./ImageElement.js";
import {getImageFetchWorker, releaseImageFetchWorker} from "./ImageFetch.js";
import {loadImage} from "./DownloadFile.js";
import ImageSource from "./ImageSource.js";
import ImageSource, {INPUT_SRC} from "./ImageSource.js";

export default class ImageSourceFetch extends ImageSource {
public override get type() {
return INPUT_SRC;
}

public override getImageURL(index: number): string | undefined {
return this.options.imageURL ? new URL(this.options.imageURL(index), window.location.href).href : undefined;
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/ImageSourceTar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ImageSource from "./ImageSource.js";
import ImageSource, {INPUT_TAR} from "./ImageSource.js";
import {downloadFile} from "./DownloadFile.js";
import Tarball from "./Tarball.js";
import type ImageElement from "./ImageElement.js";
Expand All @@ -7,6 +7,10 @@ export default class ImageSourceTar extends ImageSource {
public tarball: Tarball | undefined;
private tarLoadProgress: number = 0;

public override get type() {
return INPUT_TAR;
}

public override async loadResources() {
if (this.options.tarURL !== undefined) {
const data = await downloadFile(this.options.tarURL, (progress) => {
Expand Down

0 comments on commit 94d5ffd

Please sign in to comment.