From 90cf1723a8766ff05f2547265825a0585bf0b78d Mon Sep 17 00:00:00 2001 From: gavin Date: Thu, 23 Aug 2018 02:03:06 +0000 Subject: [PATCH] Center non-negative, finite, boards (#14) --- src/cartographer.js | 27 ++++++++++++++++++++------- src/consts.js | 2 +- src/main.js | 4 ++-- src/tessellate.js | 14 +++++++------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/cartographer.js b/src/cartographer.js index ced8a73..528d4f3 100644 --- a/src/cartographer.js +++ b/src/cartographer.js @@ -17,6 +17,8 @@ export default class Cartographer { 'move', '_checkMove', + '_setOriginX', + '_setOriginY', 'zoom', ].map(method => this[method] = this[method].bind(this)); @@ -25,13 +27,8 @@ export default class Cartographer { this._checkScale(this.canvasHeight, this.canvasWidth); - this.originX = has(this, 'originX') ? this.originX : - this.negativeTiles ? parseInt(this.canvasWidth / 2) : - this.tileWidth() / 2; - - this.originY = has(this, 'originY') ? this.originY : - this.negativeTiles ? parseInt(this.canvasHeight / 2) : - this.canvasHeight - (this.tileHeight() / 2); + this._setOriginX(); + this._setOriginY(); } getOriginX() {return this.originX;} @@ -46,6 +43,22 @@ export default class Cartographer { this.scale = this.scaleMin > this.scale ? this.scaleMin : this.scale; } + _setOriginX () { + this.originX = has(this, 'originX') ? this.originX : + this.negativeTiles ? parseInt(this.canvasWidth / 2) : + this.width ? ((this.width * this.tileWidth()) / -2) + (this.canvasWidth / 2) + (this.tileWidth() / 2) : + this.tileWidth() / 2; + } + + _setOriginY () { + const boardHeight = this.height * this.tileHeight(); + + this.originY = has(this, 'originY') ? this.originY : + this.negativeTiles ? parseInt(this.canvasHeight / 2) : + this.height ? (boardHeight) - ((boardHeight - this.canvasHeight) / 2) - (this.tileHeight() / 2) : + this.canvasHeight - (this.tileHeight() / 2); + } + move(event) { if (event.deltaX) { diff --git a/src/consts.js b/src/consts.js index a8b2c2e..3b182bc 100644 --- a/src/consts.js +++ b/src/consts.js @@ -12,7 +12,7 @@ export const BOARD_STYLES = { export const FLAT = 'flat'; export const POINTY = 'pointy'; -export const TILE_ORIENTATIONS = { +export const ORIENTATION_STYLES = { FLAT, POINTY, }; diff --git a/src/main.js b/src/main.js index 2bb5afe..cd11136 100644 --- a/src/main.js +++ b/src/main.js @@ -4,7 +4,7 @@ const PRESS_RIPPLE = ONE_SECOND / 3; const DEFAULTS = { board: Tessellate.BOARD_STYLES.HEX, style: Tessellate.DRAW_STYLES.FILL, - orientation: Tessellate.TILE_ORIENTATIONS.FLAT, + orientation: Tessellate.ORIENTATION_STYLES.FLAT, tile: Tessellate.TILE_STYLES.HEX, }; @@ -107,7 +107,7 @@ class Demo { drawStyle: Tessellate.DRAW_STYLES.FILL, tileStyle: Tessellate.TILE_STYLES.CIRCLE, - orientation: Tessellate.TILE_ORIENTATIONS.FLAT, + orientation: Tessellate.ORIENTATION_STYLES.FLAT, red: 255, green: 127, diff --git a/src/tessellate.js b/src/tessellate.js index a01d287..2b0d7e2 100644 --- a/src/tessellate.js +++ b/src/tessellate.js @@ -22,7 +22,7 @@ import { TILE_STYLES, BOARD_STYLES, FLAT, POINTY, - TILE_ORIENTATIONS, + ORIENTATION_STYLES, FILL, OUTLINE, DRAW_STYLES, } from './consts.js'; @@ -64,13 +64,13 @@ function selectCartographer(board, orientation) { } export class Tessellate { - static get TILE_STYLES() {return TILE_STYLES} - static get BOARD_STYLES() {return BOARD_STYLES} - static get TILE_ORIENTATIONS() {return TILE_ORIENTATIONS} // TODO: rename to ORIENTATION_STYLES - static get DRAW_STYLES() {return DRAW_STYLES} + static get TILE_STYLES() {return TILE_STYLES} + static get BOARD_STYLES() {return BOARD_STYLES} + static get ORIENTATION_STYLES() {return ORIENTATION_STYLES} + static get DRAW_STYLES() {return DRAW_STYLES} - static get TILES() {return TILES} - static get Cell() {return Cell} + static get TILES() {return TILES} + static get Cell() {return Cell} static get Shapes() {return Shapes} static get utils() {return utils}