asciiflow2/js-lib/view.js

171 lines
5.0 KiB
JavaScript
Raw Normal View History

2014-01-05 00:24:48 +00:00
/**
2014-01-12 10:37:38 +00:00
* Handles view operations, state and management of the screen.
*
2014-01-05 00:24:48 +00:00
* @constructor
2014-01-12 10:37:38 +00:00
* @param {ascii.State} state
2014-01-05 00:24:48 +00:00
*/
2014-01-09 20:18:46 +00:00
ascii.View = function(state) {
2014-01-12 11:09:55 +00:00
/** @type {ascii.State} */ this.state = state;
/** @type {Element} */ this.canvas = document.getElementById('ascii-canvas');
/** @type {Object} */ this.context = this.canvas.getContext('2d');
2014-01-12 11:09:55 +00:00
/** @type {number} */ this.zoom = 1;
/** @type {ascii.Vector} */ this.offset = new ascii.Vector(7500, 7500);
2014-01-12 11:09:55 +00:00
/** @type {boolean} */ this.dirty = true;
this.resizeCanvas();
2014-01-05 00:24:48 +00:00
};
2014-01-12 10:37:38 +00:00
/**
* Resizes the canvas, should be called if the viewport size changes.
*/
2014-01-09 20:18:46 +00:00
ascii.View.prototype.resizeCanvas = function() {
this.canvas.width = document.documentElement.clientWidth;
this.canvas.height = document.documentElement.clientHeight;
2014-01-12 11:09:55 +00:00
this.dirty = true;
2014-01-05 00:24:48 +00:00
};
/**
* Starts the animation loop for the canvas. Should only be called once.
*/
2014-01-09 20:18:46 +00:00
ascii.View.prototype.animate = function() {
if (this.dirty) {
2014-01-12 11:09:55 +00:00
this.dirty = false;
this.render();
}
2014-01-08 22:24:16 +00:00
var view = this;
window.requestAnimationFrame(function() { view.animate(); });
};
/**
* Renders the given state to the canvas.
* TODO: Room for efficiency here still. Drawing should be incremental,
* however performance is currently very acceptable on test devices.
*/
2014-01-09 20:18:46 +00:00
ascii.View.prototype.render = function() {
2014-01-12 09:27:10 +00:00
var context = this.context;
context.setTransform(1, 0, 0, 1, 0, 0);
// Clear the visible area.
2014-01-12 09:27:10 +00:00
context.clearRect(0, 0, this.canvas.width, this.canvas.height);
2014-01-12 09:27:10 +00:00
context.scale(this.zoom, this.zoom);
2014-01-12 10:37:38 +00:00
context.translate(
this.canvas.width / 2 / this.zoom,
this.canvas.height / 2 / this.zoom);
2014-01-12 09:27:10 +00:00
// Only render grid lines and cells that are visible.
var startOffset = this.screenToCell(new ascii.Vector(
-RENDER_PADDING,
-RENDER_PADDING));
var endOffset = this.screenToCell(new ascii.Vector(
this.canvas.width + RENDER_PADDING,
this.canvas.height + RENDER_PADDING));
// Render the grid.
2014-01-12 10:37:38 +00:00
context.lineWidth = '1';
context.strokeStyle = '#EEEEEE';
2014-01-12 09:27:10 +00:00
context.beginPath();
for (var i = startOffset.x; i < endOffset.x; i++) {
context.moveTo(
2014-01-12 10:37:38 +00:00
i * CHARACTER_PIXELS - this.offset.x,
0 - this.offset.y);
2014-01-12 09:27:10 +00:00
context.lineTo(
2014-01-12 10:37:38 +00:00
i * CHARACTER_PIXELS - this.offset.x,
this.state.cells.length * CHARACTER_PIXELS - this.offset.y);
}
2014-01-12 09:27:10 +00:00
for (var j = startOffset.y; j < endOffset.y; j++) {
context.moveTo(
0 - this.offset.x,
2014-01-12 10:37:38 +00:00
j * CHARACTER_PIXELS - this.offset.y);
2014-01-12 09:27:10 +00:00
context.lineTo(
2014-01-12 10:37:38 +00:00
this.state.cells.length * CHARACTER_PIXELS - this.offset.x,
j * CHARACTER_PIXELS - this.offset.y);
}
this.context.stroke();
// Render cells.
this.context.font = '15px Courier New';
2014-01-12 09:27:10 +00:00
for (var i = startOffset.x; i < endOffset.x; i++) {
for (var j = startOffset.y; j < endOffset.y; j++) {
var cell = this.state.getCell(new ascii.Vector(i, j));
if (cell.hasScratch() || cell.isSpecial()) {
this.context.fillStyle = cell.hasScratch() ? '#DEF' : '#F5F5F5';
context.fillRect(
i * CHARACTER_PIXELS - this.offset.x,
(j - 1) * CHARACTER_PIXELS - this.offset.y,
CHARACTER_PIXELS, CHARACTER_PIXELS);
}
var cellValue = this.state.getDrawValue(new ascii.Vector(i, j));
if (cellValue != null) {
this.context.fillStyle = '#000000';
context.fillText(cellValue,
2014-01-12 10:37:38 +00:00
i * CHARACTER_PIXELS - this.offset.x + 3,
j * CHARACTER_PIXELS - this.offset.y - 2);
}
}
}
};
2014-01-08 23:06:08 +00:00
2014-01-12 11:09:55 +00:00
/**
* @param {number} zoom
*/
ascii.View.prototype.setZoom = function(zoom) {
this.zoom = zoom;
this.dirty = true;
};
/**
* @param {ascii.Vector} offset
*/
ascii.View.prototype.setOffset = function(offset) {
this.offset = offset;
this.dirty = true;
};
2014-01-08 23:06:08 +00:00
/**
* Given a screen coordinate, find the frame coordinates.
* @param {ascii.Vector} vector
* @return {ascii.Vector}
*/
ascii.View.prototype.screenToFrame = function(vector) {
return new ascii.Vector(
2014-01-12 10:37:38 +00:00
(vector.x - this.canvas.width / 2) / this.zoom + this.offset.x,
(vector.y - this.canvas.height / 2) / this.zoom + this.offset.y);
};
/**
* Given a frame coordinate, find the screen coordinates.
* @param {ascii.Vector} vector
* @return {ascii.Vector}
2014-01-08 23:06:08 +00:00
*/
ascii.View.prototype.frameToScreen = function(vector) {
return new ascii.Vector(
2014-01-12 10:37:38 +00:00
(vector.x - this.offset.x) * this.zoom + this.canvas.width / 2,
(vector.y - this.offset.y) * this.zoom + this.canvas.height / 2);
2014-01-08 23:06:08 +00:00
};
/**
* Given a frame coordinate, return the indices for the nearest cell.
* @param {ascii.Vector} vector
* @return {ascii.Vector}
*/
ascii.View.prototype.frameToCell = function(vector) {
return new ascii.Vector(
2014-01-12 10:37:38 +00:00
Math.round((vector.x - CHARACTER_PIXELS / 2) / CHARACTER_PIXELS),
Math.round((vector.y + CHARACTER_PIXELS / 2) / CHARACTER_PIXELS));
};
/**
* Given a screen coordinate, return the indices for the nearest cell.
* @param {ascii.Vector} vector
* @return {ascii.Vector}
*/
ascii.View.prototype.screenToCell = function(vector) {
return this.frameToCell(this.screenToFrame(vector));
};
2014-01-08 23:06:08 +00:00