# resource/imageasset (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/resource/imageasset/

Image asset.

## ImageAsset extends Asset

Image asset.
- When loading, receives the file bytes and builds both the image element and an ImageBitmap decoded off the main thread. (createImageBitmap)
  So texImage2D does not decode the image again on the main thread when it is first drawn. (The decoded image is closed after the first upload)
  Only formats whose ImageBitmap result is identical to uploading the image element, down to the pixel, are pre-decoded. (DecodedImageStore)
- SceneManager uploads the loaded images to the GPU while the scene loads (the loading screen). (Graphic.prepareLoadedImages)
- If the bytes cannot be received (a file:// address, another origin without CORS), the address is put into the image element directly as before.
  In that case getImage().src is that address, and it is a blob: address if the bytes were received. (The asset path is getAssetPath)

Example: Load an image file and check its size

```js
import { ImageAsset } from "./build/vanilla.js";

const imageAsset = new ImageAsset();
await imageAsset.load("examples/assets/spritesheet.png");
const imageSize = imageAsset.getImageSize();
console.log(imageSize.x, imageSize.y, imageAsset.isLoaded());
```

- `image = null` (field, since 0.0.8): Image element. (an empty element is made at construction and replaced with a new one on every load)
  - returns HTMLImageElement
- `constructor()` (constructor, since 0.0.8): Constructor.
- `async load(assetPath)` (method, since 0.0.8): Loads the asset asynchronously.
  - `assetPath` string: Image file path. (does nothing if already loaded)
  - returns Promise<void>: A promise resolved when the image is loaded. (rejects if it cannot be loaded)
- `async loadFromBytes(assetPath, bytes)` (method, since 0.4.0): Loads from bytes already held. (Images the browser decodes such as png, jpeg, webp, replaces with the new bytes even if already loaded)
  - Throws an exception if it cannot decode. (Same as load)
  - `assetPath` string: Name used for failure notices and getAssetPath.
  - `bytes` ArrayBuffer | ArrayBufferView | Blob: Bytes of the image file. (used as is if a Blob, otherwise wrapped in a Blob)
  - returns Promise<void>: A promise resolved when the image is decoded. (rejects if it cannot be decoded)

  Example: Load an image from bytes already fetched

  ```js
  import { ImageAsset } from "./build/vanilla.js";
  
  const response = await fetch("assets/icons/icon.png");
  const bytes = await response.arrayBuffer();
  const imageAsset = new ImageAsset();
  await imageAsset.loadFromBytes("icon.png", bytes);
  const imageSize = imageAsset.getImageSize();
  console.log(imageAsset.getAssetPath(), imageSize.x, imageSize.y);
  ```

- `unload()` (method, since 0.4.0): Unloads the asset. (Closes the pre-decoded image if it has not been uploaded yet)
- `getImage()` (method, since 0.0.9): Returns the image.
  - returns HTMLImageElement: Image element.
- `getImageSize()` (method, since 0.0.8): Returns the image size.
  - returns Vector2: Image size. (pixels, 0, 0 before loading)
