2014-08-17 03:23:48 +00:00
|
|
|
// Creates an HD canvas element on page and
|
|
|
|
// returns a reference to the element
|
2014-09-27 21:46:24 +00:00
|
|
|
var createCanvasElement = function (width, height, cellSize, id, insertAfter, background) {
|
2014-08-25 18:15:56 +00:00
|
|
|
width *= cellSize;
|
|
|
|
height *= cellSize;
|
|
|
|
|
2014-08-17 03:23:48 +00:00
|
|
|
// Creates a scaled-up canvas based on the device's
|
|
|
|
// resolution, then displays it properly using styles
|
|
|
|
function createHDCanvas (ratio) {
|
|
|
|
var canvas = document.createElement('canvas');
|
2014-08-25 17:49:50 +00:00
|
|
|
var ctx = canvas.getContext('2d');
|
2014-08-17 03:23:48 +00:00
|
|
|
|
|
|
|
// Creates a dummy canvas to test device's pixel ratio
|
|
|
|
ratio = (function () {
|
2014-08-25 17:49:50 +00:00
|
|
|
var ctx = document.createElement('canvas').getContext('2d');
|
2014-08-17 03:23:48 +00:00
|
|
|
var dpr = window.devicePixelRatio || 1;
|
2014-08-25 17:49:50 +00:00
|
|
|
var bsr = ctx.webkitBackingStorePixelRatio ||
|
|
|
|
ctx.mozBackingStorePixelRatio ||
|
|
|
|
ctx.msBackingStorePixelRatio ||
|
|
|
|
ctx.oBackingStorePixelRatio ||
|
|
|
|
ctx.backingStorePixelRatio || 1;
|
2014-08-17 03:23:48 +00:00
|
|
|
return dpr / bsr;
|
|
|
|
})();
|
|
|
|
|
|
|
|
canvas.width = width * ratio;
|
|
|
|
canvas.height = height * ratio;
|
|
|
|
canvas.style.width = width + 'px';
|
|
|
|
canvas.style.height = height + 'px';
|
2014-08-25 17:49:50 +00:00
|
|
|
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
|
|
ctx.font = 'bold ' + cellSize + 'px Arial';
|
|
|
|
|
2014-08-22 23:17:02 +00:00
|
|
|
if (id) canvas.id = id;
|
2014-09-27 21:46:24 +00:00
|
|
|
if (background) canvas.style.background = 'rgb(' + background + ')';
|
2014-08-17 03:23:48 +00:00
|
|
|
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
|
|
|
var canvas = createHDCanvas();
|
|
|
|
|
|
|
|
if (insertAfter) insertAfter.parentNode.insertBefore(canvas, insertAfter.nextSibling);
|
|
|
|
else document.body.appendChild(canvas);
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createCanvasElement: createCanvasElement
|
|
|
|
};
|