-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'phet-core-history-copy/main'
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |