Jump to content

Drawing on a texture pixel by pixel


Recommended Posts

nevermind.. I found a method to do this.. however, it is not immediately obvious how you can bind and update the texture when the canvas changes.. 

at any rate.. the following code sample demonstrates how to use a separate canvas as a PIXI.BaseTexture

// I created a canvas element with the id "drawpanel" on my page.// My renderer is WebGL, function LoadCanvasImage() {   var c = document.getElementById("drawpanel");   pixiCanvasRenderer = new PIXI.CanvasRenderer(c.width,c.height,c,true);		   var grd=pixiCanvasRenderer.context.createRadialGradient(75,50,5,90,60,100);       grd.addColorStop(0,"red");       grd.addColorStop(1,"white");   // Fill with gradient       pixiCanvasRenderer.context.fillStyle=grd;       pixiCanvasRenderer.context.fillRect(10,10,150,100);   var source = new Image();       source.src = pixiCanvasRenderer.view.toDataURL();   // in this instance, 'Canvas'.toDataURL returns "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAYE0lEQ…TWsFPLE2MExosAgTXenPKMGIFhI0BgDTu1PDFGYLwI/A9PJKWFOlTAfAAAAABJRU5ErkJggg=="   // reference http://www.html5canvastutorials.com/advanced/html5-canvas-save-drawing-as-an-image/    var BaseTexture = new PIXI.BaseTexture(source, PIXI.scaleModes.LINEAR);   var Texture = new PIXI.Texture(BaseTexture);   var mySprite = new PIXI.Sprite(Texture);   stage.addChild(mySprite);}

I'm not showing the Stage initialization here, or the animation loop.. but that should be easy enough to reference and implement in the pixi examples on github.


EDIT: Added toDataURL instance data, and an article reference on it.

Edited by mrBRC
Link to comment
Share on other sites


Hey guys easiest way is to use:

// create a regular canvasvar canvas = document.create('canvas')// make your canvas awesome...// then use it as a texture:var canvasTexture = PIXI.Texture.fromCanvas(canvas);var sprite = new PIXI.Sprite(canvasTexture)

Thanks, much cleaner, I have one other question, why doing setTexture  works using canvas, and it doesn't using WebGL?

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.

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.


  • Recently Browsing   0 members

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