Skip to content

Instantly share code, notes, and snippets.

@roovo
Created August 31, 2016 17:04
Show Gist options
  • Save roovo/edff73230cb454a206e796f6e3fa71b0 to your computer and use it in GitHub Desktop.
Save roovo/edff73230cb454a206e796f6e3fa71b0 to your computer and use it in GitHub Desktop.
var _user$project$Native_CanvasTexture = function() {
function higherPow2(aSize){
return Math.pow(2, Math.ceil(Math.log(aSize) / Math.log(2)));
}
function makeTextCanvas(context, text, fontSize, width, height) {
context.canvas.width = width;
context.canvas.height = height;
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.fillStyle = "rgba(0, 0, 0, 0.0)";
context.fill();
context.font = fontSize + "px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "white";
context.fillText(text, width / 2, height / 2);
return context.canvas;
}
function loadCanvasTexture(text) {
var fontSize = 22;
var context = document.createElement("canvas").getContext("2d");
context.font = fontSize + "px monospace";
var width = context.measureText(text).width + 16;
var height = fontSize + 10;
var canvas = makeTextCanvas(context, text, fontSize, higherPow2(width), higherPow2(height));
return { ctor: 'Texture', img: canvas };
}
return {
canvasTexture: loadCanvasTexture
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment