Spaces:
Running
Running
/** | |
* @class Rectangular backend | |
* @private | |
*/ | |
ROT.Display.Rect = function(context) { | |
ROT.Display.Backend.call(this, context); | |
this._spacingX = 0; | |
this._spacingY = 0; | |
this._canvasCache = {}; | |
this._options = {}; | |
} | |
ROT.Display.Rect.extend(ROT.Display.Backend); | |
ROT.Display.Rect.cache = false; | |
ROT.Display.Rect.prototype.compute = function(options) { | |
this._canvasCache = {}; | |
this._options = options; | |
var charWidth = Math.ceil(this._context.measureText("W").width); | |
this._spacingX = Math.ceil(options.spacing * charWidth); | |
this._spacingY = Math.ceil(options.spacing * options.fontSize); | |
this._context.canvas.width = options.width * this._spacingX; | |
this._context.canvas.height = options.height * this._spacingY; | |
} | |
ROT.Display.Rect.prototype.draw = function(data, clearBefore) { | |
if (this.constructor.cache) { | |
this._drawWithCache(data, clearBefore); | |
} else { | |
this._drawNoCache(data, clearBefore); | |
} | |
} | |
ROT.Display.Rect.prototype._drawWithCache = function(data, clearBefore) { | |
var x = data[0]; | |
var y = data[1]; | |
var ch = data[2]; | |
var fg = data[3]; | |
var bg = data[4]; | |
var hash = ""+ch+fg+bg; | |
if (hash in this._canvasCache) { | |
var canvas = this._canvasCache[hash]; | |
} else { | |
var b = this._options.border; | |
var canvas = document.createElement("canvas"); | |
var ctx = canvas.getContext("2d"); | |
canvas.width = this._spacingX; | |
canvas.height = this._spacingY; | |
ctx.fillStyle = bg; | |
ctx.fillRect(b, b, canvas.width-b, canvas.height-b); | |
if (ch) { | |
ctx.fillStyle = fg; | |
ctx.font = this._context.font; | |
ctx.textAlign = "center"; | |
ctx.textBaseline = "middle"; | |
ctx.fillText(ch, this._spacingX/2, this._spacingY/2); | |
} | |
this._canvasCache[hash] = canvas; | |
} | |
this._context.drawImage(canvas, x*this._spacingX, y*this._spacingY); | |
} | |
ROT.Display.Rect.prototype._drawNoCache = function(data, clearBefore) { | |
var x = data[0]; | |
var y = data[1]; | |
var ch = data[2]; | |
var fg = data[3]; | |
var bg = data[4]; | |
if (clearBefore) { | |
var b = this._options.border; | |
this._context.fillStyle = bg; | |
this._context.fillRect(x*this._spacingX + b, y*this._spacingY + b, this._spacingX - b, this._spacingY - b); | |
} | |
if (!ch) { return; } | |
this._context.fillStyle = fg; | |
this._context.fillText(ch, (x+0.5) * this._spacingX, (y+0.5) * this._spacingY); | |
} | |
ROT.Display.Rect.prototype.computeSize = function(availWidth, availHeight) { | |
var width = Math.floor(availWidth / this._spacingX); | |
var height = Math.floor(availHeight / this._spacingY); | |
return [width, height]; | |
} | |
ROT.Display.Rect.prototype.computeFontSize = function(availWidth, availHeight) { | |
var boxWidth = Math.floor(availWidth / this._options.width); | |
var boxHeight = Math.floor(availHeight / this._options.height); | |
/* compute char ratio */ | |
var oldFont = this._context.font; | |
this._context.font = "100px " + this._options.fontFamily; | |
var width = Math.ceil(this._context.measureText("W").width); | |
this._context.font = oldFont; | |
var ratio = width / 100; | |
var widthFraction = ratio * boxHeight / boxWidth; | |
if (widthFraction > 1) { /* too wide with current aspect ratio */ | |
boxHeight = Math.floor(boxHeight / widthFraction); | |
} | |
return Math.floor(boxHeight / this._options.spacing); | |
} | |