Skip to content

Commit

Permalink
Merge remote-tracking branch 'phet-core-history-copy/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Dec 17, 2024
2 parents 0d79268 + 89db1c0 commit 7619a26
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions js/MipmapElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2022-2024, University of Colorado Boulder

import asyncLoader from './asyncLoader.js';
import phetCore from './phetCore.js';

/**
* Size and raster data for levels in a mipmap. See also type Mipmap in Imageable.ts. Defined in phet-core instead of
* scenery because it is loaded upstream and should not have any downstream dependencies such as scenery.
*
* @author Sam Reid (PhET Interactive Simulations)
*/
export default class MipmapElement {
public readonly width: number;
public readonly height: number;
public readonly url: string;
public readonly img: HTMLImageElement;
public readonly canvas: HTMLCanvasElement;

public constructor( width: number, height: number, url: string, lock = true ) {

this.width = width;
this.height = height;
this.url = url;

this.img = new Image(); // eslint-disable-line phet/no-html-constructors
this.img.src = this.url; // trigger the loading of the image for its level

this.canvas = document.createElement( 'canvas' );
this.canvas.width = this.width;
this.canvas.height = this.height;

const context = this.canvas.getContext( '2d' )!;
const unlock = lock ? asyncLoader.createLock( this.img ) : null;
this.img.onload = () => {
context.drawImage( this.img, 0, 0 );
unlock && unlock();
};
}
}

phetCore.register( 'MipmapElement', MipmapElement );

0 comments on commit 7619a26

Please sign in to comment.