caioketo Posted June 24, 2014 Share Posted June 24, 2014 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. Quote Link to comment Share on other sites More sharing options...
xerver Posted June 24, 2014 Share Posted June 24, 2014 sprite.texture.baseTexture.sourceThat is the source of the base texture, usually an Image object. Quote Link to comment Share on other sites More sharing options...
giraluna Posted June 26, 2014 Share Posted June 26, 2014 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;} Quote Link to comment Share on other sites More sharing options...
xerver Posted June 27, 2014 Share Posted June 27, 2014 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) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.