ํ ์ค์ฒ๋ฅผ ์คํ๋ผ์ดํธ์ ๋ก๋ํ ๋ค์ ์คํ๋ผ์ดํธ์ ๋๋น๋ฅผ ์์ฒญํ๋ฉด ๊ฐ์ด 1์ด ๋ฉ๋๋ค. ์ ๋๋ฉ์ด์ ๋ฃจํ๋ฅผ ๋ช ๋ฒ ๊ฑฐ์น ํ์์ผ ๋๋น๊ฐ ์ ํจํด์ง๋๋ค. ๋ก๋๋ ํ ์ค์ฒ์ ํฌ๊ธฐ๋ฅผ ํ์ธํ๋ ์ ๋ขฐํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?
var stage = new PIXI.Stage(0x66FF99);
var renderer = new PIXI.CanvasRenderer(800, 600);
document.body.appendChild(renderer.view);
var texture = PIXI.Texture.fromImage("stone.png");
var stone = new PIXI.Sprite(texture);
console.log(stone.width + ', ' + stone.height);
stage.addChild(stone);
console.log(stone.width + ', ' + stone.height);
requestAnimFrame( animate );
console.log(stone.width + ', ' + stone.height);
renderer.render(stage);
console.log(stone.width + ', ' + stone.height);
console.log()
๋ ๋ชจ๋ 128, 128์ด์ด์ผ ํ์ง๋ง "1, 1"์ ๊ธฐ๋กํฉ๋๋ค. ํด๋น console.log()
๋ฅผ ์ ๋๋ฉ์ด์
๋ฃจํ์ ๋ฃ์ผ๋ฉด 128, 128์ด ์ถ๋ ฅ๋๊ธฐ ์์ํฉ๋๋ค.
์ ์! ์ด๋ ํ
์ค์ฒ์ ์ด๋ฏธ์ง๊ฐ ๋ก๋๋์ง ์์ ๋๋น์ ๋์ด๊ฐ ์ผ๋ง์ธ์ง ์ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค./
๊ทธ๋ฐ ์๋ฏธ์์ ์ผ๋ฐ HTML ์ด๋ฏธ์ง์ ๊ฑฐ์ ๋น์ทํ๊ฒ ๋์ํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ assetLoader๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ๋ฏธ๋ฆฌ ๋ก๋ํ๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ ๊ฒํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํ ์ค์ฒ๋ฅผ ๋ง๋ค ๋ ๋๋น์ ๋์ด๊ฐ ํญ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ๋ฉ๋๋ค.
var assetsToLoad = [ "stone.png"];
// create a new loader
loader = new PIXI.AssetLoader(assetsToLoad);
// use callback
loader.onComplete = onAssetsLoaded
//begin load
loader.load();
function onAssetsLoaded()
{
var texture = PIXI.Texture.fromImage("stone.png");
var sprite = new PIXI.Sprite(texture);
// this will log the correct width and height as the image was preloaded into the pixi.js cache
console.log(stone.width + ', ' + stone.height);
}
๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ํ ์ค์ฒ์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ๋๋ค. ์ด๋ฏธ์ง๊ฐ ๋ก๋๋๋ฉด '์ ๋ฐ์ดํธ' ์ด๋ฒคํธ๊ฐ ์ ๋ฌ๋ฉ๋๋ค.
var texture = PIXI.Texture.fromImage("stone.png");
var sprite = new PIXI.Sprite(texture);
if(texture.baseTexture.hasLoaded)
{
// this will log the correct width and height as the image has already loaded
console.log(stone.width + ', ' + stone.height);
}
else
{
texture.addEventListener("update", onTextureUpdate)
}
function onTextureUpdate()
{
// this will log the correct width and height as the image has loaded
console.log(stone.width + ', ' + stone.height);
}
๋์์ด ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค :)
๊ทธ๋ฐ ๊ฒ๋ ๊ฐ๋ฅํฉ๋๋ค : https://github.com/GoodBoyDigital/pixi.js/issues/29
๋๋น ๋ฐ ๋์ด๊ฐ ์ค์ ๋์๋์ง ํ์ธํ๊ธฐ ์ํด setTexture ํจ์ ๋ณ๊ฒฝ
์ด ์ค๋ ๋๋ ๋ซํ ํ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ ๊ฒผ์ต๋๋ค. ๊ด๋ จ ๋ฒ๊ทธ์ ๋ํ ์ ๋ฌธ์ ๋ฅผ ์ฌ์ญ์์ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ ์! ์ด๋ ํ ์ค์ฒ์ ์ด๋ฏธ์ง๊ฐ ๋ก๋๋์ง ์์ ๋๋น์ ๋์ด๊ฐ ์ผ๋ง์ธ์ง ์ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค./
๊ทธ๋ฐ ์๋ฏธ์์ ์ผ๋ฐ HTML ์ด๋ฏธ์ง์ ๊ฑฐ์ ๋น์ทํ๊ฒ ๋์ํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ๋ฐฉ๋ฒ์ assetLoader๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ๋ฏธ๋ฆฌ ๋ก๋ํ๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ ๊ฒํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํ ์ค์ฒ๋ฅผ ๋ง๋ค ๋ ๋๋น์ ๋์ด๊ฐ ํญ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ๋ฉ๋๋ค.
๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ํ ์ค์ฒ์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ๋๋ค. ์ด๋ฏธ์ง๊ฐ ๋ก๋๋๋ฉด '์ ๋ฐ์ดํธ' ์ด๋ฒคํธ๊ฐ ์ ๋ฌ๋ฉ๋๋ค.
๋์์ด ๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค :)