Skip to content

Instantly share code, notes, and snippets.

@markknol
Last active November 3, 2015 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markknol/cc31b146938a1508c02c to your computer and use it in GitHub Desktop.
Save markknol/cc31b146938a1508c02c to your computer and use it in GitHub Desktop.
Custom flambe.js embed-script for JavaScript target only
/**
* Custom flambe embed for JavaScript-target only.
* Author: Mark Knol - http://blog.stroep.nl
*/
var flambe = {};
/**
* Embed a Flambe game into the page.
*
* @return True if the game was successfully embedded.
* False if the browser doesn't have a recent enough browser.
*/
flambe.embed = function (urls, elementId) {
var container = document.getElementById(elementId);
if (container == null) {
throw new Error("Could not find element [id=" + elementId + "]");
}
var canvas = document.createElement("canvas");
if ("getContext" in canvas) {
canvas.id = elementId + "-canvas";
container.appendChild(canvas);
// Expose the canvas so Haxe can use it
flambe.canvas = canvas;
var script = document.createElement("script");
script.onload = function () {
flambe.canvas = null;
};
script.src = urls[0];
container.appendChild(script);
return true;
}
// Nothing was embedded!
return false;
};
<!-- embed script looks like this: -->
<script src="flambe.js"></script>
<script>
flambe.embed(["targets/main-html.js"], "div_main");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment