set viewport to speed up mobile taps, hex and square both handle 'pointy top' versions
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,3 +7,5 @@
|
||||
node_modules/*
|
||||
hexmine
|
||||
|
||||
public/js/main.js
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
||||
<meta name="viewport" content="width=device-width">
|
||||
|
||||
<title>Tessellate</title>
|
||||
<link rel="stylesheet" href="css/main.css">
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -6,7 +6,6 @@ import Point from './point.js';
|
||||
import Sketch from './sketch.js';
|
||||
|
||||
import TileCircle from './tileCircle.js';
|
||||
import TileDiamond from './tileDiamond.js';
|
||||
import TileHex from './tileHex.js';
|
||||
import TileSquare from './tileSquare.js';
|
||||
|
||||
@@ -28,7 +27,7 @@ export default class Tessellate {
|
||||
click: (event) => {
|
||||
let x = event.pageX;
|
||||
let y = event.pageY;
|
||||
let tiles = ['circle', 'diamond', 'hex', 'square'];
|
||||
let tiles = ['circle', 'hex', 'square'];
|
||||
let styles = ['filled', 'outline'];
|
||||
let scale = random(10, 50);
|
||||
|
||||
@@ -45,7 +44,6 @@ export default class Tessellate {
|
||||
});
|
||||
|
||||
this.circle = new TileCircle({context: this.sketch.getContext()});
|
||||
this.diamond = new TileDiamond({context: this.sketch.getContext()});
|
||||
this.hex = new TileHex({context: this.sketch.getContext()});
|
||||
this.square = new TileSquare({context: this.sketch.getContext()});
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
|
||||
import Tile from './tile.js';
|
||||
|
||||
import {hypotenuse} from './utils.js';
|
||||
|
||||
let RGBA_DEFAULT = 'rgba(0, 0, 0, 0.5)';
|
||||
|
||||
export default class TileDiamond extends Tile {
|
||||
constructor(settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
filled(settings) {
|
||||
let x = settings.x;
|
||||
let y = settings.y;
|
||||
let scale = settings.scale;
|
||||
|
||||
this.context.beginPath();
|
||||
this.context.moveTo(x+hypotenuse(scale), y);
|
||||
this.context.lineTo(x, y-hypotenuse(scale));
|
||||
this.context.lineTo(x-hypotenuse(scale), y);
|
||||
this.context.lineTo(x, y+hypotenuse(scale));
|
||||
|
||||
this.context.fillStyle = settings.color || RGBA_DEFAULT;
|
||||
this.context.fill();
|
||||
}
|
||||
|
||||
outline(settings) {
|
||||
let x = settings.x;
|
||||
let y = settings.y;
|
||||
let scale = settings.scale;
|
||||
|
||||
this.context.beginPath();
|
||||
this.context.moveTo(x+hypotenuse(scale), y);
|
||||
this.context.lineTo(x, y-hypotenuse(scale));
|
||||
this.context.lineTo(x-hypotenuse(scale), y);
|
||||
this.context.lineTo(x, y+hypotenuse(scale));
|
||||
this.context.closePath();
|
||||
|
||||
this.context.lineWidth = settings.width ? settings.width : 1;
|
||||
this.context.strokeStyle = settings.color || RGBA_DEFAULT;
|
||||
this.context.stroke();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,50 +7,33 @@ export default class TileHex extends Tile {
|
||||
constructor(settings) {
|
||||
super(settings);
|
||||
|
||||
this.sqrt3 = Math.sqrt(3);
|
||||
|
||||
this.hexSides = 6;
|
||||
this.pointyTopCornerX = [];
|
||||
this.pointyTopCornerY = [];
|
||||
this.flatTopCornerX = [];
|
||||
this.flatTopCornerY = [];
|
||||
this.pointyTopCornerX = [];
|
||||
this.pointyTopCornerY = [];
|
||||
|
||||
// dice dots, 0,0 is center
|
||||
// TODO: if pointy: [0].concat(flatTop), if flat: [0].concat(pointy)
|
||||
// this.hexDotX = [0];
|
||||
// this.hexDotY = [0];
|
||||
|
||||
this.hexSlices = 24;
|
||||
this.hexSliceX = [0];
|
||||
this.hexSliceY = [0];
|
||||
|
||||
for (let hexSlice = 0; hexSlice < this.hexSlices; hexSlice++) {
|
||||
this.hexSliceX[hexSlice] = parseInt(Math.cos(((hexSlice/this.hexSlices)*this.hexSides) * (2 * Math.PI) / this.hexSides) * 1000) / 1000;
|
||||
this.hexSliceY[hexSlice] = parseInt(Math.sin(((hexSlice/this.hexSlices)*this.hexSides) * (2 * Math.PI) / this.hexSides) * 1000) / 1000;
|
||||
|
||||
if (((hexSlice-2) % 4) === 0) {
|
||||
let cur = (hexSlice-2) / 4;
|
||||
this.pointyTopCornerX[cur] = this.hexSliceX[hexSlice];
|
||||
this.pointyTopCornerY[cur] = this.hexSliceY[hexSlice];
|
||||
}
|
||||
let hexSides = 6;
|
||||
let hexSlices = 24;
|
||||
for (let hexSlice = 0; hexSlice < hexSlices; hexSlice++) {
|
||||
let x = parseInt(Math.cos(((hexSlice / hexSlices) * hexSides) * (2 * Math.PI) / hexSides) * 1000) / 1000;
|
||||
let y = parseInt(Math.sin(((hexSlice / hexSlices) * hexSides) * (2 * Math.PI) / hexSides) * 1000) / 1000;
|
||||
|
||||
if ((hexSlice % 4) === 0) {
|
||||
let cur = hexSlice / 4;
|
||||
this.flatTopCornerX[cur] = this.hexSliceX[hexSlice];
|
||||
this.flatTopCornerY[cur] = this.hexSliceY[hexSlice];
|
||||
this.flatTopCornerX[cur] = x;
|
||||
this.flatTopCornerY[cur] = y;
|
||||
}
|
||||
|
||||
// if (((hexSlice-2) % 4) === 0) {
|
||||
// let cur = (hexSlice-2) / 4;
|
||||
// this.hexCornerX[cur] = this.hexSliceX[hexSlice];
|
||||
// this.hexCornerY[cur] = this.hexSliceY[hexSlice];
|
||||
// }
|
||||
//
|
||||
// if ((hexSlice % 4) === 0) {
|
||||
// let cur = hexSlice / 4;
|
||||
// this.hexDotX[cur+1] = this.hexSliceX[hexSlice];
|
||||
// this.hexDotY[cur+1] = this.hexSliceY[hexSlice];
|
||||
// }
|
||||
if (((hexSlice-2) % 4) === 0) {
|
||||
let cur = (hexSlice-2) / 4;
|
||||
this.pointyTopCornerX[cur] = x;
|
||||
this.pointyTopCornerY[cur] = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,32 @@
|
||||
|
||||
import Tile from './tile.js';
|
||||
|
||||
import {sqrt2} from './utils.js';
|
||||
|
||||
let RGBA_DEFAULT = 'rgba(0, 0, 0, 0.5)';
|
||||
|
||||
export default class TileSquare extends Tile {
|
||||
constructor(settings) {
|
||||
super(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];
|
||||
}
|
||||
|
||||
filled(settings) {
|
||||
let x = settings.x;
|
||||
let y = settings.y;
|
||||
let scale = settings.scale;
|
||||
let squareCornerX = settings.pointy ? this.diamondX : this.squareX;
|
||||
let squareCornerY = settings.pointy ? this.diamondY : this.squareY;
|
||||
|
||||
this.context.beginPath();
|
||||
this.context.moveTo(x+scale, y+scale);
|
||||
this.context.lineTo(x+scale, y-scale);
|
||||
this.context.lineTo(x-scale, y-scale);
|
||||
this.context.lineTo(x-scale, y+scale);
|
||||
this.context.moveTo(x + scale * squareCornerX[0], y + scale * squareCornerY[0]);
|
||||
this.context.lineTo(x + scale * squareCornerX[1], y + scale * squareCornerY[1]);
|
||||
this.context.lineTo(x + scale * squareCornerX[2], y + scale * squareCornerY[2]);
|
||||
this.context.lineTo(x + scale * squareCornerX[3], y + scale * squareCornerY[3]);
|
||||
|
||||
this.context.fillStyle = settings.color || RGBA_DEFAULT;
|
||||
this.context.fill();
|
||||
@@ -27,12 +36,14 @@ export default class TileSquare extends Tile {
|
||||
let x = settings.x;
|
||||
let y = settings.y;
|
||||
let scale = settings.scale;
|
||||
let squareCornerX = settings.pointy ? this.diamondX : this.squareX;
|
||||
let squareCornerY = settings.pointy ? this.diamondY : this.squareY;
|
||||
|
||||
this.context.beginPath();
|
||||
this.context.moveTo(x+scale, y+scale);
|
||||
this.context.lineTo(x+scale, y-scale);
|
||||
this.context.lineTo(x-scale, y-scale);
|
||||
this.context.lineTo(x-scale, y+scale);
|
||||
this.context.moveTo(x + scale * squareCornerX[0], y + scale * squareCornerY[0]);
|
||||
this.context.lineTo(x + scale * squareCornerX[1], y + scale * squareCornerY[1]);
|
||||
this.context.lineTo(x + scale * squareCornerX[2], y + scale * squareCornerY[2]);
|
||||
this.context.lineTo(x + scale * squareCornerX[3], y + scale * squareCornerY[3]);
|
||||
this.context.closePath();
|
||||
|
||||
this.context.lineWidth = settings.width ? settings.width : 1;
|
||||
|
||||
@@ -31,6 +31,13 @@ export function extend(obj, src) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
// hypotenuse factor of isoscelese right triangle
|
||||
export let sqrt2 = Math.sqrt(2);
|
||||
|
||||
// short width factor given the lenght of a hexagon's side
|
||||
// (2*S gives long width)
|
||||
export let sqrt3 = Math.sqrt(3);
|
||||
|
||||
export function hypotenuse(a, b) {
|
||||
if (b == null) b = a;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user