Jump to content

Any way to get the image from an Sprite or Texture?


caioketo
 Share

Recommended Posts

So I got my AssetLoader working good, and sprites too, but I need to display one image (from asset) in another canvas.

Is there anyway to get the image from the sprite or the texture??

Like I got a Sprite with the texture id 1.jpg from the asset, I want to display these on other canvas.
Thanks in advanced.

Link to comment
Share on other sites

 

That is the source of the base texture, usually an Image object.

 

 

Wouldn't that point to a spritesheet in most cases?

 

I've used the following code to grab images from the sheet after PIXI has loaded it. Should be easy enough to adapt to return the canvases:

var loader = new PIXI.JsonLoader("img\/sprites.json");var individualImages;loader.addEventListener("loaded", function(e){  individualImages = spritesheetToImages(e.content.json, e.content.baseUrl);});function spritesheetToImages(sheetData, baseUrl){  var sheetImg = new Image();  sheetImg.src = baseUrl + sheetData.meta.image;  var images = {};  for (var sprite in sheetData.frames)  {    var frame = sheetData.frames[sprite].frame;    images[sprite] = new Image(frame.w, frame.h);    var canvas = document.createElement("canvas");    canvas.width = frame.w;    canvas.height = frame.h;    var context = canvas.getContext("2d");    context.drawImage(sheetImg, frame.x, frame.y, frame.w, frame.h,      0, 0, frame.w, frame.h);    images[sprite].src = canvas.toDataURL();  }    return images;}
Link to comment
Share on other sites

Yes, source is the original image (spritesheet in some cases). You need that and the frame of the texture to draw it.

 

Here is how a sprite draws itself:

var frame = this.texture.frame;context.drawImage(this.texture.baseTexture.source,                               frame.x,                               frame.y,                               frame.width,                               frame.height,                               (this.anchor.x) * -frame.width,                               (this.anchor.y) * -frame.height,                               frame.width,                               frame.height)
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...