How to load multiple HTML5 canvas on the same page (the proper method)
Hi,
I've been struggling to load multiple canvas animations on the same page. At the beggining I thought that exporting the movies with different namespaces and reloading the libraries in a sequential flow might work, but it doesn't. It always loads just the last animation loaded. More info here: Coding challenge: what am I doing wrong?
Here is a sample of what I'm doing:
1st: Publish two flash movies with custom namespaces for "lib" set in the "publish settings": "libFirst" and "libSecond".
2nd: Edit the canvas tags in the HTML page. One called "firstCanvas" and the other one called "secondCanvas"
3rd: Edit the javascript like this:
<script>
// change the default namespace for the CreateJS libraries:
var createjsFirst = createjsFirst||{};
var createjs = createjsFirst;
</script>
<script src="//code.createjs.com/easeljs-0.7.1.min.js"></script>
<script src="//code.createjs.com/tweenjs-0.5.1.min.js"></script>
<script src="//code.createjs.com/movieclip-0.7.1.min.js"></script>
<script src="{{assets}}/js/first.js"></script>
<script>
function initFirstAnimation() {
var canvas, stage, exportRoot;
canvas = document.getElementById("firstCanvas");
exportRoot = new libFirst.first();
stage = new createjsFirst.Stage(canvas);
stage.addChild(exportRoot);
stage.update();
createjsFirst.Ticker.setFPS(libFirst.properties.fps);
createjsFirst.Ticker.addEventListener("tick", stage);
}
</script>
<script>
// change the default namespace for the CreateJS libraries:
var createjsSecond = createjsSecond||{};
var createjs = createjsSecond;
</script>
<script src="//code.createjs.com/easeljs-0.7.1.min.js"></script>
<script src="//code.createjs.com/tweenjs-0.5.1.min.js"></script>
<script src="//code.createjs.com/movieclip-0.7.1.min.js"></script>
<script src="{{assets}}/js/second.js"></script>
<script>
function initSecondAnimation() {
var canvas, stage, exportRoot;
canvas = document.getElementById("secondCanvas");
exportRoot = new libSecond.second();
stage = new createjsSecond.Stage(canvas);
stage.addChild(exportRoot);
stage.update();
createjsSecond.Ticker.setFPS(libSecond.properties.fps);
createjsSecond.Ticker.addEventListener("tick", stage);
}
</script>
<body onload="initFirstAnimation(); initSecondAnimation();">
Could someone please reply with the best practice on how to do this? If possible, without the need to reload all the libraries...
If I only need to show one flash movie at a time, would it be more efficient to cut & paste the canvas tag using jQuery in the DOM and reloading a different lib on it?
Many thanks!
#flash #reborn