vanilladocs
vanillapunch.com
API / resource/imageasset

resource/imageasset

이미지 애셋. (원본 resource/imageasset.js)

  • 원본은 HTMLImageElement 에 src 를 넣고 onload 를 기다린다. 러스트 판은 엔진에게 바이트를 부탁하고, 다 오면 처음 get_image 할 때 풀어서 Image 로 만든다. (Image::load_from_memory 의 형식, 웹은 받을 때 브라우저가 푼 그림을 손잡이에서 쓴다) AssetLoader 로 받은 것은 다 받았을 때 이미 풀어 둔 그림(손잡이의 get_decoded_image)을 그대로 쓴다. (풀지 못했으면 손잡이가 실패다)
  • load, load_from_bytes 는 원본처럼 await 할 수 있는 읽기 손잡이를 돌려준다. await 는 원본 img.onload 처럼 풀기까지 끝나야 끝나고, 풀지 못하면 실패(원본 onerror 의 reject)로 끝난다. 푼 그림은 손잡이에 담겨 get_image 가 다시 풀지 않는다.

Image asset. (the original resource/imageasset.js)

  • The original sets src on an HTMLImageElement and waits for onload. The Rust version asks the engine for the bytes, and once they arrive decodes them into an Image on the first get_image. (formats of Image::load_from_memory, on web it uses the image the browser decoded on receipt from the handle) For ones received by AssetLoader, the image already decoded when everything arrived (the handle's get_decoded_image) is used as is. (if it could not be decoded, the handle fails)
  • load, load_from_bytes return a load handle that can be awaited as in the original. Like the original img.onload, await finishes only once decoding finishes, and if it cannot be decoded it finishes in failure (the reject of the original onerror). The decoded image is kept on the handle so get_image does not decode it again.

ImageAssetstruct traits Default, Asset

짝Counterpart: vanilla.js ImageAsset소스source

이미지 애셋.

  • 바이트가 다 오면 처음 get_image 할 때 풀어 Image 로 든다. (PNG, JPEG 같은 Image::load_from_memory 의 형식)

Image asset.

  • Once the bytes arrive, decodes them on the first get_image and holds an Image. (formats of Image::load_from_memory such as PNG, JPEG)
예제Example저장소의 PNG 를 읽어 크기 보기Read a PNG from the repository and check its size
use vanilla::{Engine, EngineConfiguration, ImageAsset, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut image_asset = ImageAsset::new();
let load_handle = image_asset.load(&engine, "examples/assets/atlas/icons.png");
assert!(load_handle.is_loaded());
let image = image_asset.get_image().expect("decoded image");
assert_eq!(image.get_width(), 128);
assert_eq!(image_asset.get_image_size(), Vector2::create(128.0, 128.0));
생성자constructorpub fn new() -> ImageAsset#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

반환ReturnsImageAsset 요청 전의 빈 이미지 애셋.Empty image asset before a request.

짝Counterpart: vanilla.js ImageAsset.constructor

메서드methodpub fn load(&mut self, engine: &Engine, asset_path: &str) -> AssetLoadHandle#0.3.2 부터since 0.3.2소스source

읽기 요청. (원본 load, await 할 수 있는 읽기 손잡이를 돌려준다. 풀기까지 끝나야 끝난다)

  • 이미 요청했으면 다시 하지 않고 처음 손잡이를 돌려준다.

Requests a read. (the original load, returns a load handle that can be awaited. Finishes only once decoding finishes)

  • If already requested, does not request again and returns the first handle.
engine&Engine
바이트를 부탁할 엔진.Engine to ask for the bytes.
asset_path&str
애셋 경로. (데스크탑은 애셋 폴더 기준, 웹은 asset_base_url 기준, data: 주소도 된다)Asset path. (relative to the asset folder on desktop, to asset_base_url on the web; data: URLs also work)

반환ReturnsAssetLoadHandle 읽기 손잡이. (데스크탑은 그 자리에서 바이트가 읽혀 있다. 못 읽으면 실패로 끝난다)Load handle. (on desktop the bytes are read on the spot. Finishes in failure if it cannot be read)

짝Counterpart: vanilla.js ImageAsset.load

메서드methodpub fn load_from_bytes(&mut self, asset_path: &str, bytes: Vec<u8>) -> AssetLoadHandle#0.3.2 부터since 0.3.2소스source

이미 가진 바이트로 읽기. (원본 loadFromBytes, await 할 수 있는 읽기 손잡이를 돌려준다. 풀지 못하면 실패로 끝난다)

  • 앞서 푼 그림은 버리고 이 바이트로 다시 푼다.

Reads from bytes already in hand. (the original loadFromBytes, returns a load handle that can be awaited. Finishes in failure if it cannot be decoded)

  • Discards the previously decoded image and decodes again from these bytes.
asset_path&str
적어 둘 경로. (알림 글에 쓴다)Path to record. (used in notice text)
bytesVec<u8>
그림 파일 바이트.Image file bytes.

반환ReturnsAssetLoadHandle 바이트가 다 읽힌 읽기 손잡이.Load handle whose bytes are fully read.

짝Counterpart: vanilla.js ImageAsset.loadFromBytes

예제Example파일 바이트로 읽기와 깨진 바이트Read from file bytes, and broken bytes
use vanilla::ImageAsset;

let png_bytes = std::fs::read("examples/assets/atlas/icons.png").expect("png file");
let mut image_asset = ImageAsset::new();
image_asset.load_from_bytes("icons.png", png_bytes);
assert_eq!(image_asset.get_image_size().x, 128.0);
image_asset.load_from_bytes("broken.png", vec![0, 1, 2, 3]);
assert!(image_asset.get_image().is_none());
assert_eq!(image_asset.get_image_size().x, 0.0);
메서드methodpub fn unload(&mut self)#0.3.2 부터since 0.3.2소스source

내리기.

Unloads.

짝Counterpart: vanilla.js ImageAsset.unload

메서드methodpub fn get_image(&mut self) -> Option<Rc<Image>>#0.3.2 부터since 0.3.2소스source

이미지. (다 읽히기 전이거나 풀기에 실패하면 None)

  • 처음 부를 때 한 번 풀고 그 뒤로는 푼 것을 돌려준다. 풀지 못하면 표준 에러에 까닭을 찍고 다시 풀지 않는다.

Image. (None before it is fully read or if decoding fails)

  • Decodes once on the first call and returns the decoded one after that. If it cannot be decoded, prints the reason to standard error and does not decode again.

반환ReturnsOption<Rc<Image>> 푼 이미지. 요청 전이거나 읽는 중이거나 읽기나 풀기에 실패했으면 None.Decoded image. None before a request, while reading, or if reading or decoding failed.

짝Counterpart: vanilla.js ImageAsset.getImage

메서드methodpub fn get_image_size(&mut self) -> Vector2#0.3.2 부터since 0.3.2소스source

이미지 크기. (픽셀, 아직 없으면 0)

Image size. (pixels, 0 if not yet)

반환ReturnsVector2 이미지의 가로와 세로. (픽셀) 이미지가 없으면 (0, 0).Width and height of the image. (pixels) (0, 0) if there is no image.

짝Counterpart: vanilla.js ImageAsset.getImageSize