Skip to content

Instantly share code, notes, and snippets.

Created June 8, 2015 09:54
Show Gist options
  • Save anonymous/b910bbb0cfea82bcf880 to your computer and use it in GitHub Desktop.
Save anonymous/b910bbb0cfea82bcf880 to your computer and use it in GitHub Desktop.
How to correctly cleanup PIXI.JS to avoid memory leaks
<!doctype html>
<html>
<head>
<script src="lib/pixi.js"></script>
</head>
<body>
</body>
<script>
var app = (function() {
console.log("Hi there!");
var view, stage, renderer;
return {
start: function() {
console.log("start");
renderer = new PIXI.WebGLRenderer(800, 600);
view = renderer.view;
// The renderer will create a canvas element for you that you can then insert into the DOM.
document.body.appendChild(view);
// You need to create a root container that will hold the scene you want to draw.
stage = new PIXI.Container();
var bunnyTexture = PIXI.Texture.fromImage('assets/images/grumpy_cat.png');
var bunny = new PIXI.Sprite(bunnyTexture);
var text = new PIXI.Text("Grumpy cat says MEHHHW", {font: "bold 25px verdana", fill: "#FFFFFF"});
text.position.x = 100;
text.position.y = 400;
stage.addChild(text);
// Setup the position and scale of the bunny
bunny.position.x = 100;
bunny.position.y = 100;
// Add the bunny to the scene we are building.
stage.addChild(bunny);
// kick off the animation loop (defined below)
animate();
function animate() {
if(!view) {
console.info("no view - stopping animate");
return;
}
// start the timer for the next animation loop
requestAnimationFrame(animate);
// this is the main render call that makes pixi draw your container and its children.
renderer.render(stage);
}
},
stop: function() {
console.log("stopping");
renderer.plugins.interaction.destroy();
renderer = null;
stage.destroy(true);
stage = null;
document.body.removeChild(view);
view = null;
for (var textureId in PIXI.utils.TextureCache) {
PIXI.utils.BaseTextureCache[textureId].destroy(true);
}
}
}
})();
app.start();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment