handle changes of board size
This commit is contained in:
@@ -9,7 +9,7 @@ const DEFAULTS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default class Cartographer {
|
export default class Cartographer {
|
||||||
constructor(settings) {
|
constructor (settings) {
|
||||||
[
|
[
|
||||||
'getOriginX',
|
'getOriginX',
|
||||||
'getOriginY',
|
'getOriginY',
|
||||||
@@ -22,6 +22,8 @@ export default class Cartographer {
|
|||||||
'setOriginY',
|
'setOriginY',
|
||||||
|
|
||||||
'zoom',
|
'zoom',
|
||||||
|
|
||||||
|
'remap',
|
||||||
].map(method => this[method] = this[method].bind(this));
|
].map(method => this[method] = this[method].bind(this));
|
||||||
|
|
||||||
this.settings = Object.assign({}, DEFAULTS, settings);
|
this.settings = Object.assign({}, DEFAULTS, settings);
|
||||||
@@ -34,12 +36,12 @@ export default class Cartographer {
|
|||||||
this.setOriginY(this.settings.canvasHeight, this.settings.centerY);
|
this.setOriginY(this.settings.canvasHeight, this.settings.centerY);
|
||||||
}
|
}
|
||||||
|
|
||||||
getOriginX() {return this.originX;}
|
getOriginX () {return this.originX;}
|
||||||
getOriginY() {return this.originY;}
|
getOriginY () {return this.originY;}
|
||||||
|
|
||||||
getScale() {return this.scale;}
|
getScale () {return this.scale;}
|
||||||
|
|
||||||
checkScale(canvasHeight, canvasWidth) {
|
checkScale (canvasHeight, canvasWidth) {
|
||||||
const heightMin = this.height ? this.calculateVerticalScale(canvasHeight, this.height) : 0;
|
const heightMin = this.height ? this.calculateVerticalScale(canvasHeight, this.height) : 0;
|
||||||
const widthMin = this.width ? this.calculateHorizontalScale(canvasWidth, this.width) : 0;
|
const widthMin = this.width ? this.calculateHorizontalScale(canvasWidth, this.width) : 0;
|
||||||
|
|
||||||
@@ -81,7 +83,7 @@ export default class Cartographer {
|
|||||||
return this.originX === newX && this.originY === newY;
|
return this.originX === newX && this.originY === newY;
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkMove(event) {
|
_checkMove (event) {
|
||||||
if (this.negativeTiles) {
|
if (this.negativeTiles) {
|
||||||
this._checkMoveNegativeTiles(event);
|
this._checkMoveNegativeTiles(event);
|
||||||
}
|
}
|
||||||
@@ -90,7 +92,7 @@ export default class Cartographer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkMovePositiveTiles(event) {
|
_checkMovePositiveTiles (event) {
|
||||||
const canvasWidth = event.width;
|
const canvasWidth = event.width;
|
||||||
const canvasHeight = event.height;
|
const canvasHeight = event.height;
|
||||||
|
|
||||||
@@ -118,7 +120,7 @@ export default class Cartographer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_checkMoveNegativeTiles(event) {
|
_checkMoveNegativeTiles (event) {
|
||||||
const colWidth = this.horizontalDistance();
|
const colWidth = this.horizontalDistance();
|
||||||
const rowHeight = this.verticalDistance();
|
const rowHeight = this.verticalDistance();
|
||||||
|
|
||||||
@@ -141,7 +143,7 @@ export default class Cartographer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zoom(event) {
|
zoom (event) {
|
||||||
const scaleOrig = this.scale;
|
const scaleOrig = this.scale;
|
||||||
|
|
||||||
let scaleTemp = scaleOrig * event.scaleStep;
|
let scaleTemp = scaleOrig * event.scaleStep;
|
||||||
@@ -161,4 +163,13 @@ export default class Cartographer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remap (config) {
|
||||||
|
Object.assign(this, pick(config, ['height', 'width', 'negativeTiles']));
|
||||||
|
|
||||||
|
this.checkScale(config.canvasHeight, config.canvasWidth);
|
||||||
|
|
||||||
|
this.setOriginX(config.canvasWidth, config.centerX);
|
||||||
|
this.setOriginY(config.canvasHeight, config.centerY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export default class Sketch {
|
|||||||
|
|
||||||
[
|
[
|
||||||
'getContext',
|
'getContext',
|
||||||
|
'getSize',
|
||||||
'onResize',
|
'onResize',
|
||||||
'render',
|
'render',
|
||||||
].map(method => this[method] = this[method].bind(this));
|
].map(method => this[method] = this[method].bind(this));
|
||||||
@@ -32,6 +33,13 @@ export default class Sketch {
|
|||||||
return this.context;
|
return this.context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSize () {
|
||||||
|
return {
|
||||||
|
canvasWidth: this.container.offsetWidth,
|
||||||
|
canvasHeight: this.container.offsetHeight,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onResize (event) {
|
onResize (event) {
|
||||||
const width = this.container.offsetWidth;
|
const width = this.container.offsetWidth;
|
||||||
const height = this.container.offsetHeight;
|
const height = this.container.offsetHeight;
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ export class Tessellate {
|
|||||||
'getTilePoints',
|
'getTilePoints',
|
||||||
'draw',
|
'draw',
|
||||||
'resize',
|
'resize',
|
||||||
|
'remap',
|
||||||
].map(method => {this[method] = this[method].bind(this)});
|
].map(method => {this[method] = this[method].bind(this)});
|
||||||
|
|
||||||
this.checkSettings(settings);
|
this.checkSettings(settings);
|
||||||
@@ -111,10 +112,14 @@ export class Tessellate {
|
|||||||
}, funky.pick(this.settings, ['desktopPress', 'moveThreshold', 'doubletapThreshold', 'pressThreshold', 'wheelFactor'])));
|
}, funky.pick(this.settings, ['desktopPress', 'moveThreshold', 'doubletapThreshold', 'pressThreshold', 'wheelFactor'])));
|
||||||
|
|
||||||
const cartographer = selectCartographer(this.settings.board, this.settings.orientation);
|
const cartographer = selectCartographer(this.settings.board, this.settings.orientation);
|
||||||
this.cartographer = new cartographer(Object.assign({
|
this.cartographer = new cartographer(Object.assign(this.sketch.getSize(), funky.pick(this.settings, [
|
||||||
canvasWidth: this.sketch.getContext().canvas.width,
|
'centerX',
|
||||||
canvasHeight: this.sketch.getContext().canvas.height,
|
'centerY',
|
||||||
}, funky.pick(this.settings, ['centerX', 'centerY', 'height', 'width', 'scale', 'negativeTiles'])));
|
'height',
|
||||||
|
'width',
|
||||||
|
'scale',
|
||||||
|
'negativeTiles',
|
||||||
|
])));
|
||||||
}
|
}
|
||||||
|
|
||||||
checkSettings (settings) {
|
checkSettings (settings) {
|
||||||
@@ -262,4 +267,11 @@ export class Tessellate {
|
|||||||
|
|
||||||
this.move(moveForScale);
|
this.move(moveForScale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remap ({height, width}) {
|
||||||
|
this.cartographer.remap(Object.assign({
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
}, this.sketch.getSize()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user