From 226cc82ea1f543ed5d1243570af0891bee8a000c Mon Sep 17 00:00:00 2001 From: Lewis Hemens Date: Sun, 12 Jan 2014 11:09:55 +0000 Subject: [PATCH] Added dirty bits for efficient drawing --- js-lib/controller.js | 9 +++++---- js-lib/state-controller.js | 4 ++-- js-lib/state.js | 21 ++++++++++++--------- js-lib/view.js | 30 +++++++++++++++++++++++++++--- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/js-lib/controller.js b/js-lib/controller.js index 2488811..4626e84 100644 --- a/js-lib/controller.js +++ b/js-lib/controller.js @@ -68,10 +68,10 @@ ascii.Controller.prototype.handleMove = function(position) { // Drag in progress, update the view origin. if (this.dragOrigin != null) { - this.view.offset = this.dragOrigin.add( + this.view.setOffset(this.dragOrigin.add( this.pressVector .subtract(position) - .scale(1 / this.view.zoom)); + .scale(1 / this.view.zoom))); } }; @@ -93,8 +93,9 @@ ascii.Controller.prototype.handleRelease = function(position) { * @param {number} delta */ ascii.Controller.prototype.handleZoom = function(delta) { - this.view.zoom *= delta > 0 ? 1.1 : 0.9; - this.view.zoom = Math.max(Math.min(this.view.zoom, 5), 0.2); + var newzoom = this.view.zoom * (delta > 0 ? 1.1 : 0.9); + newzoom = Math.max(Math.min(newzoom, 5), 0.2); + this.view.setZoom(newzoom); }; /** diff --git a/js-lib/state-controller.js b/js-lib/state-controller.js index 822e00a..988f81e 100644 --- a/js-lib/state-controller.js +++ b/js-lib/state-controller.js @@ -18,7 +18,7 @@ ascii.StateController = function(state) { * @param {ascii.Vector} position */ ascii.StateController.prototype.handleDrawingPress = function(position) { - this.state.getCell(position).value = 'O'; + this.state.setValue(position, 'O'); }; /** @@ -33,5 +33,5 @@ ascii.StateController.prototype.handleDrawingRelease = function(position) { * @param {ascii.Vector} position */ ascii.StateController.prototype.handleDrawingMove = function(position) { - this.state.getCell(position).value = 'O'; + this.state.setValue(position, 'O'); }; diff --git a/js-lib/state.js b/js-lib/state.js index 24c816b..4f3173d 100644 --- a/js-lib/state.js +++ b/js-lib/state.js @@ -11,15 +11,6 @@ ascii.Cell = function() { /** @type {?string} */ this.value = null; }; -/** - * Sets the cells value. - * - * @param {string} value - */ -ascii.Cell.prototype.setValue = function(value) { - this.value = value; -}; - /** * Holds the entire state of the diagram as a 2D array of cells. * @@ -28,6 +19,7 @@ ascii.Cell.prototype.setValue = function(value) { ascii.State = function() { /** @type {Array.>} */ this.cells = new Array(MAX_GRID_SIZE); + /** @type {boolean} */ this.dirty = true; for (var i = 0; i < this.cells.length; i++) { this.cells[i] = new Array(MAX_GRID_SIZE); @@ -46,3 +38,14 @@ ascii.State = function() { ascii.State.prototype.getCell = function(vector) { return this.cells[vector.x][vector.y]; }; + +/** + * Sets the cells value at the given position. + * + * @param {ascii.Vector} position + * @param {string} value + */ +ascii.State.prototype.setValue = function(position, value) { + this.getCell(position).value = value; + this.dirty = true; +}; diff --git a/js-lib/view.js b/js-lib/view.js index 473fdae..5f6b495 100644 --- a/js-lib/view.js +++ b/js-lib/view.js @@ -12,11 +12,15 @@ goog.require('ascii.Vector'); * @param {ascii.State} state */ ascii.View = function(state) { + /** @type {ascii.State} */ this.state = state; + /** @type {Element} */ this.canvas = document.getElementById('ascii-canvas'); /** @type {Object} */ this.context = this.canvas.getContext('2d'); + /** @type {number} */ this.zoom = 1; /** @type {ascii.Vector} */ this.offset = new ascii.Vector(7500, 7500); - /** @type {ascii.State} */ this.state = state; + /** @type {boolean} */ this.dirty = true; + this.resizeCanvas(); }; @@ -26,14 +30,18 @@ ascii.View = function(state) { ascii.View.prototype.resizeCanvas = function() { this.canvas.width = document.documentElement.clientWidth; this.canvas.height = document.documentElement.clientHeight; + this.dirty = true; }; /** * Starts the animation loop for the canvas. Should only be called once. */ ascii.View.prototype.animate = function() { - // TODO: Only render incrementally. - this.render(); + if (this.dirty || this.state.dirty) { + this.dirty = false; + this.state.dirty = false; + this.render(); + } var view = this; window.requestAnimationFrame(function() { view.animate(); }); }; @@ -96,6 +104,22 @@ ascii.View.prototype.render = function() { } }; +/** + * @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; +}; + /** * Given a screen coordinate, find the frame coordinates. * @param {ascii.Vector} vector