Pixi.js: Sprite์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋Š” ์–ธ์ œ ์œ ํšจํ•ฉ๋‹ˆ๊นŒ?

์— ๋งŒ๋“  2013๋…„ 03์›” 30์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: pixijs/pixi.js

ํ…์Šค์ฒ˜๋ฅผ ์Šคํ”„๋ผ์ดํŠธ์— ๋กœ๋“œํ•œ ๋‹ค์Œ ์Šคํ”„๋ผ์ดํŠธ์˜ ๋„ˆ๋น„๋ฅผ ์š”์ฒญํ•˜๋ฉด ๊ฐ’์ด 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);
}

๋„์›€์ด ๋˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค :)

๋ชจ๋“  3 ๋Œ“๊ธ€

์•„ ์˜ˆ! ์ด๋Š” ํ…์Šค์ฒ˜์˜ ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋˜์ง€ ์•Š์•„ ๋„ˆ๋น„์™€ ๋†’์ด๊ฐ€ ์–ผ๋งˆ์ธ์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค./
๊ทธ๋Ÿฐ ์˜๋ฏธ์—์„œ ์ผ๋ฐ˜ 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 ํ•จ์ˆ˜ ๋ณ€๊ฒฝ

์ด ์Šค๋ ˆ๋“œ๋Š” ๋‹ซํžŒ ํ›„ ์ตœ๊ทผ ํ™œ๋™์ด ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋™์œผ๋กœ ์ž ๊ฒผ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋ฒ„๊ทธ์— ๋Œ€ํ•œ ์ƒˆ ๋ฌธ์ œ๋ฅผ ์—ฌ์‹ญ์‹œ์˜ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰