This commit is contained in:
gavin
2018-07-29 01:40:45 +00:00
committed by Gitea
parent 18f978cc8a
commit 5cb4308251
5 changed files with 173 additions and 39 deletions

View File

@@ -1,12 +1,49 @@
import {sqrt2} from './utils.js';
import {POINTY} from './consts.js';
import {FLAT, POINTY} from './consts.js';
const DEFAULTS = {
pipScale: 0.05,
pipDistance: 0.4,
};
export default class DrawSquare {
constructor(settings) {
this.settings = Object.assign({}, DEFAULTS, settings);
this.squareX = [1, 1, -1, -1];
this.squareY = [1, -1, -1, 1];
this.diamondX = [sqrt2, 0, -sqrt2, 0];
this.diamondY = [0, -sqrt2, 0, sqrt2];
this.diamondX = [sqrt2, 0, -sqrt2, 0, sqrt2 / 2, -sqrt2 / 2];
this.diamondY = [0, -sqrt2, 0, sqrt2, -sqrt2 / 2, sqrt2 / 2];
this.squarePips = {};
this.squarePips[FLAT] = [];
this.squarePips[POINTY] = [];
const getFlatVertex = ([x, y]) => [x ? this.squareX[x - 1] : 0, y ? this.squareY[y - 1] : 0];
// start with 0,0 as center
this.squarePips[FLAT][1] = [[0, 0]].map(getFlatVertex);
this.squarePips[FLAT][2] = [[1, 1], [3, 3]].map(getFlatVertex);
this.squarePips[FLAT][3] = [[0, 0], [1, 1], [3, 3]].map(getFlatVertex);
this.squarePips[FLAT][4] = [[1, 1], [2, 2], [3, 3], [4, 4]].map(getFlatVertex);
this.squarePips[FLAT][5] = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]].map(getFlatVertex);
this.squarePips[FLAT][6] = [[1, 0], [1, 1], [2, 2], [3, 0], [3, 3], [4, 4]].map(getFlatVertex);
this.squarePips[FLAT][7] = [[0, 0], [1, 0], [1, 1], [2, 2], [3, 0], [3, 3], [4, 4]].map(getFlatVertex);
this.squarePips[FLAT][8] = [[0, 1], [0, 3], [1, 0], [1, 1], [2, 2], [3, 0], [3, 3], [4, 4]].map(getFlatVertex);
this.squarePips[FLAT][9] = [[0, 0], [0, 1], [0, 3], [1, 0], [1, 1], [2, 2], [3, 0], [3, 3], [4, 4]].map(getFlatVertex);
const getPointyVertex = ([x, y]) => [x ? this.diamondX[x - 1] : 0, y ? this.diamondY[y - 1] : 0];
// start with 0,0 as center
this.squarePips[POINTY][1] = [[0, 0]].map(getPointyVertex);
this.squarePips[POINTY][2] = [[1, 1], [3, 3]].map(getPointyVertex);
this.squarePips[POINTY][3] = [[0, 0], [1, 1], [3, 3]].map(getPointyVertex);
this.squarePips[POINTY][4] = [[1, 1], [2, 2], [3, 3], [4, 4]].map(getPointyVertex);
this.squarePips[POINTY][5] = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]].map(getPointyVertex);
this.squarePips[POINTY][6] = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]].map(getPointyVertex);
this.squarePips[POINTY][7] = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6]].map(getPointyVertex);
this.squarePips[POINTY][8] = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [5, 6], [6, 5], [6, 6]].map(getPointyVertex);
this.squarePips[POINTY][9] = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [5, 6], [6, 5], [6, 6]].map(getPointyVertex);
}
fill(context, scale, x, y, cell) {
@@ -40,5 +77,31 @@ export default class DrawSquare {
context.strokeStyle = cell.getColor();
context.stroke();
}
pip(context, scale, cellX, cellY, vertexX, vertexY, pipRadius, pipDistance) {
const x = cellX + (pipDistance * vertexX);
const y = cellY + (pipDistance * vertexY);
context.moveTo(x, y);
context.arc(x, y, pipRadius*3, 0, Math.PI*2, true);
}
pips(context, scale, x, y, cell) {
const {orientation, pips} = cell;
if (this.squarePips[orientation] && this.squarePips[orientation][pips]) {
const pipRadius = scale * this.settings.pipScale;
const pipDistance = scale * this.settings.pipDistance;
context.beginPath();
this.squarePips[orientation][pips]
.forEach(([pipX, pipY]) => this.pip(context, scale, x, y, pipX, pipY, pipRadius, pipDistance));
context.closePath();
context.fillStyle = 'rgb(0,0,0)';
context.fill();
}
}
}