vanilladocs
vanillapunch.com
API / resource/spriteatlas

resource/spriteatlas

찾을 아틀라스 목록. (SpriteAtlas.findFrame 이 앞에서부터 찾는다)

List of atlases to search. (SpriteAtlas.findFrame searches from the front)

SpriteAtlasextends Object

짝Counterpart: vanilla-engine SpriteAtlas

스프라이트 아틀라스. (쪽 그림 몇 장에 모은 그림을 이름으로 꺼낸다)

  • 같은 쪽 그림을 쓰는 스프라이트는 엔진이 묶어 한 번에 그린다. (Graphic.setBatchingEnabled)
  • 읽는 JSON 꼴 둘: 1. tools/atlas.cjs 가 만든 것. { images: { "이름.png": { x, y, width, height, page } }, meta: { imageFileName, pages: [{ imageFileName }] } } page 가 없으면 0 쪽, meta.pages 가 없으면 meta.imageFileName 한 장이다. 2. 쪽 경로와 [쪽, x, y, 가로, 세로] 표. { pagePaths: ["경로"], frames: { "이름": [쪽, x, y, 가로, 세로] } }
  • 그림 이름은 확장자를 뗀 이름으로 찾는다. ("icon-close.png" 는 "icon-close" 로도 "icon-close.png" 로도 찾는다)
  • 쪽 경로에 빗금(/)이 없으면 JSON 이 있는 폴더를 기준으로, 있으면 적힌 그대로 읽는다.
  • 여러 아틀라스를 addSearchAtlas 로 넣어 두면 findFrame, applyFrame(정적) 이 넣은 차례로 찾는다. (앞의 것이 이긴다)

Sprite atlas. (retrieves by name the images gathered into a few page images)

  • Sprites that use the same page image are batched by the engine and drawn at once. (Graphic.setBatchingEnabled)
  • Two JSON formats are read: 1. Made by tools/atlas.cjs. { images: { "name.png": { x, y, width, height, page } }, meta: { imageFileName, pages: [{ imageFileName }] } } If page is absent it is page 0, and if meta.pages is absent it is the single image meta.imageFileName. 2. Page paths and a [page, x, y, width, height] table. { pagePaths: ["path"], frames: { "name": [page, x, y, width, height] } }
  • Images are found by the name with the extension removed. ("icon-close.png" is found as either "icon-close" or "icon-close.png")
  • If a page path has no slash (/), it is read relative to the folder containing the JSON, and if it has one, it is read as written.
  • If several atlases are added with addSearchAtlas, findFrame and applyFrame (static) search them in the order added. (earlier ones win)
예제Example찾을 아틀라스에 넣고 스프라이트에 조각 걸기Add to the search atlases and set a frame on a sprite
import { Engine, EngineConfiguration, Scene, WorldNode, Sprite, SpriteAtlas, Vector2 } from "./build/vanilla.js";

const spriteAtlas = new SpriteAtlas();
await spriteAtlas.loadData({
	pagePaths: ["examples/assets/spritesheet.png"],
	frames: {
		"walk-0.png": [0, 0, 0, 200, 200],
		"walk-1.png": [0, 200, 0, 200, 200],
	},
});
SpriteAtlas.addSearchAtlas(spriteAtlas);

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		const node = new WorldNode();
		node.setContentSize(Vector2.create(200, 200));
		node.setLocalPosition(Vector2.create(100, 100));
		const root = this.getRoot();
		root.addChild(node);
		const sprite = node.addComponent(Sprite);
		const isFound = SpriteAtlas.applySearchFrame(sprite, "walk-1");
		console.log(isFound);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
생성자constructorconstructor()#0.4.0 부터since 0.4.0소스source

생성.

Constructor.

짝Counterpart: vanilla-engine SpriteAtlas.new

정적staticstatic addSearchAtlas(spriteAtlas)#0.4.0 부터since 0.4.0소스source

찾을 아틀라스 넣기. (정적, 맨 뒤에 넣는다, 이미 들었으면 그대로)

Adds an atlas to search. (static, appended to the end, unchanged if already present)

spriteAtlasSpriteAtlas
넣을 아틀라스.Atlas to add.

짝Counterpart: vanilla-engine SpriteAtlas.add_search_atlas

정적staticstatic removeSearchAtlas(spriteAtlas)#0.4.0 부터since 0.4.0소스source

찾을 아틀라스 빼기. (정적)

Removes an atlas to search. (static)

spriteAtlasSpriteAtlas
뺄 아틀라스. (들어 있지 않으면 아무것도 하지 않는다)Atlas to remove. (does nothing if not in the list)

짝Counterpart: vanilla-engine SpriteAtlas.remove_search_atlas

정적staticstatic clearSearchAtlases()#0.4.0 부터since 0.4.0소스source

찾을 아틀라스 모두 빼기. (정적)

Removes all atlases to search. (static)

짝Counterpart: vanilla-engine SpriteAtlas.clear_search_atlases

정적staticstatic findFrame(frameName)#0.4.0 부터since 0.4.0소스source

찾을 아틀라스들에서 조각 찾기. (정적, 넣은 차례로, 없으면 null)

Finds a frame in the search atlases. (static, in the order added, null if absent)

frameNamestring
그림 이름.Image name.

반환ReturnsFrame | null 처음 찾은 조각. (없으면 null)The first frame found. (null if none)

짝Counterpart: vanilla-engine SpriteAtlas.find_frame

정적staticstatic applySearchFrame(sprite, frameName)#0.4.0 부터since 0.4.0소스source

찾을 아틀라스들에서 찾은 조각을 스프라이트에 걸기. (정적, 없으면 그림을 비우고 false)

Applies the frame found in the search atlases to a sprite. (static, clears the image and returns false if absent)

spriteSprite
조각을 걸 스프라이트.Sprite to set the frame on.
frameNamestring
그림 이름.Image name.

반환Returnsboolean 찾아 걸었으면 참.True if found and set.

짝Counterpart: vanilla-engine SpriteAtlas.apply_search_frame

정적staticstatic findFrameInOrder(atlasList, frameName)#0.4.0 부터since 0.4.0소스source

넘긴 아틀라스들에서 차례로 조각 찾기. (정적, 앞의 것이 이긴다, 없으면 null, 찾을 아틀라스 목록을 쓰지 않는다)

Finds a frame in the given atlases in order. (static, earlier ones win, null if absent, does not use the search atlas list)

atlasListSpriteAtlas[]
찾을 아틀라스 목록. (앞의 것이 이긴다)List of atlases to search. (earlier ones win)
frameNamestring
그림 이름.Image name.

반환ReturnsFrame | null 처음 찾은 조각. (없으면 null)The first frame found. (null if none)

짝Counterpart: vanilla-engine SpriteAtlas.find_frame_in_order

예제Example계절 아틀라스를 기본 아틀라스보다 먼저 찾기Search a seasonal atlas before the base atlas
import { SpriteAtlas } from "./build/vanilla.js";

const baseAtlas = new SpriteAtlas();
await baseAtlas.loadData({
	pagePaths: ["examples/assets/spritesheet.png"],
	frames: {
		"walk-0.png": [0, 0, 0, 200, 200],
		"walk-1.png": [0, 200, 0, 200, 200],
	},
});
const seasonAtlas = new SpriteAtlas();
await seasonAtlas.loadData({
	pagePaths: ["examples/assets/spritesheet.png"],
	frames: {
		"walk-0": [0, 400, 400, 200, 200],
	},
});
const frame = SpriteAtlas.findFrameInOrder([seasonAtlas, baseAtlas], "walk-0");
const imageRect = frame.getImageRect();
const fallbackFrame = SpriteAtlas.findFrameInOrder([seasonAtlas, baseAtlas], "walk-1");
const fallbackRect = fallbackFrame.getImageRect();
console.log(imageRect.x, fallbackRect.x);
정적staticstatic applyFrameInOrder(atlasList, sprite, frameName)#0.4.0 부터since 0.4.0소스source

넘긴 아틀라스들에서 차례로 찾은 조각을 스프라이트에 걸기. (정적, 없으면 그림을 비우고 false)

Applies the frame found in order in the given atlases to a sprite. (static, clears the image and returns false if absent)

atlasListSpriteAtlas[]
찾을 아틀라스 목록. (앞의 것이 이긴다)List of atlases to search. (earlier ones win)
spriteSprite
조각을 걸 스프라이트.Sprite to set the frame on.
frameNamestring
그림 이름.Image name.

반환Returnsboolean 찾아 걸었으면 참.True if found and set.

짝Counterpart: vanilla-engine SpriteAtlas.apply_frame_in_order

메서드methodasync load(jsonPath)#0.4.0 부터since 0.4.0소스source

아틀라스 JSON 과 쪽 그림 불러오기. (쪽 그림은 한꺼번에 받는다)

Loads the atlas JSON and the page images. (the page images are fetched all at once)

jsonPathstring
아틀라스 JSON 경로.Atlas JSON path.

반환ReturnsPromise<void> 다 읽으면 풀리는 프로미스. (JSON 을 읽지 못하면 알리고 풀린다, 쪽 그림을 읽지 못하면 거부된다)A promise resolved when fully loaded. (reports and resolves if the JSON cannot be read, rejects if a page image cannot be loaded)

짝Counterpart: vanilla-engine SpriteAtlas.load

메서드methodasync loadData(atlasData, baseDirectory = "")#0.4.0 부터since 0.4.0소스source

아틀라스 자료로 불러오기. (JSON 을 읽은 객체나 코드에 둔 같은 꼴의 객체, 쪽 경로에 빗금이 없으면 baseDirectory 를 앞에 붙인다)

Loads from atlas data. (an object read from JSON or an object of the same shape kept in code, baseDirectory is prepended if a page path has no slash)

atlasDataobject
아틀라스 자료. (tools/atlas.cjs 꼴이나 pagePaths, frames 꼴, 다른 꼴이면 알리고 아무것도 하지 않는다)Atlas data. (the tools/atlas.cjs form or the pagePaths, frames form, reports and does nothing for any other form)
baseDirectorystring
빗금 없는 쪽 경로 앞에 붙일 폴더. (끝의 빗금까지, 기본 빈 글)Folder prepended to page paths without a slash. (including the trailing slash, default empty string)

반환ReturnsPromise<void> 쪽 그림을 다 읽으면 풀리는 프로미스. (읽지 못하면 거부된다)A promise resolved when all page images are loaded. (rejects if one cannot be loaded)

짝Counterpart: vanilla-engine SpriteAtlas.load_data

예제Example코드에 둔 아틀라스 자료로 읽기Load from atlas data kept in code
import { SpriteAtlas } from "./build/vanilla.js";

const spriteAtlas = new SpriteAtlas();
await spriteAtlas.loadData({
	pagePaths: ["examples/assets/spritesheet.png"],
	frames: {
		"walk-0.png": [0, 0, 0, 200, 200],
		"walk-1.png": [0, 200, 0, 200, 200],
	},
});
const frame = spriteAtlas.readFrame("walk-1");
const imageRect = frame.getImageRect();
console.log(spriteAtlas.getPageCount(), spriteAtlas.getFrameNames(), imageRect.x, spriteAtlas.hasFrame("walk-1.png"));
메서드methodreadFrame(frameName)#0.4.0 부터since 0.4.0소스source

조각 읽기. (없으면 null)

Reads a frame. (null if absent)

frameNamestring
그림 이름. (확장자를 떼어도 된다)Image name. (the extension may be dropped)

반환ReturnsFrame | null 조각. (없으면 null)Frame. (null if none)

짝Counterpart: vanilla-engine SpriteAtlas.read_frame

메서드methodhasFrame(frameName)#0.4.0 부터since 0.4.0소스source

조각이 있는지.

Whether the frame exists.

frameNamestring
그림 이름.Image name.

반환Returnsboolean 있으면 참.True if present.

짝Counterpart: vanilla-engine SpriteAtlas.has_frame

메서드methodapplyFrame(sprite, frameName)#0.4.0 부터since 0.4.0소스source

스프라이트에 조각 걸기. (없는 이름이면 그림을 비우고 false)

Applies a frame to a sprite. (if the name does not exist, clears the image and returns false)

spriteSprite
조각을 걸 스프라이트.Sprite to set the frame on.
frameNamestring
그림 이름.Image name.

반환Returnsboolean 찾아 걸었으면 참.True if found and set.

짝Counterpart: vanilla-engine SpriteAtlas.apply_frame

메서드methodgetPageImages()#0.4.0 부터since 0.4.0소스source

쪽 그림 목록 반환.

Returns the list of page images.

반환ReturnsHTMLImageElement[] 쪽 그림 목록. (쪽 번호 차례)Page image list. (in page index order)

짝Counterpart: vanilla-engine SpriteAtlas.get_page_images

메서드methodgetPageCount()#0.4.0 부터since 0.4.0소스source

쪽 수.

Number of pages.

반환Returnsnumber 쪽 수.Number of pages.

짝Counterpart: vanilla-engine SpriteAtlas.get_page_count

메서드methodgetFrameNames()#0.4.0 부터since 0.4.0소스source

이름 목록 반환. (확장자를 뗀 이름과 파일 이름이 함께 든다)

Returns the list of names. (contains both the names without extensions and the file names)

반환Returnsstring[] 이름 목록.Name list.

짝Counterpart: vanilla-engine SpriteAtlas.get_frame_names

메서드methodisLoaded()#0.4.0 부터since 0.4.0소스source

불러왔는지.

Whether it has loaded.

반환Returnsboolean 불러왔으면 참.True if loaded.

짝Counterpart: vanilla-engine SpriteAtlas.is_loaded