improving module structure and naming
This commit is contained in:
29
src/main.js
29
src/main.js
@@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
import {random} from './utils.js';
|
|
||||||
|
|
||||||
import Tessellate from './tessellate.js';
|
import Tessellate from './tessellate.js';
|
||||||
import {TileCircle, TileHexagon, TileSquare} from './tessellate.js';
|
|
||||||
|
import {utils} from './tessellate.js';
|
||||||
|
import {DrawCircle, DrawHexagon, DrawSquare} from './tessellate.js';
|
||||||
import {Cell} from './tessellate.js';
|
import {Cell} from './tessellate.js';
|
||||||
|
|
||||||
class Demo {
|
class Demo {
|
||||||
@@ -17,9 +17,9 @@ class Demo {
|
|||||||
draw: this.draw
|
draw: this.draw
|
||||||
});
|
});
|
||||||
|
|
||||||
this.circle = new TileCircle();
|
this.circle = new DrawCircle();
|
||||||
this.square = new TileSquare();
|
this.square = new DrawSquare();
|
||||||
this.hexagon = new TileHexagon();
|
this.hexagon = new DrawHexagon();
|
||||||
|
|
||||||
this.tiles = ['circle', 'hexagon', 'square'];
|
this.tiles = ['circle', 'hexagon', 'square'];
|
||||||
this.styles = ['filled', 'outline'];
|
this.styles = ['filled', 'outline'];
|
||||||
@@ -28,20 +28,19 @@ class Demo {
|
|||||||
click(event) {
|
click(event) {
|
||||||
let x = event.pageX;
|
let x = event.pageX;
|
||||||
let y = event.pageY;
|
let y = event.pageY;
|
||||||
let scale = random(10, 50);
|
let scale = utils.random(10, 50);
|
||||||
|
|
||||||
this.map.push(new Cell({
|
this.map.push(new Cell({
|
||||||
tile: this.tiles[random(this.tiles.length-1)],
|
tile: this.tiles[utils.random(this.tiles.length-1)],
|
||||||
style: this.styles[random(this.styles.length-1)],
|
style: this.styles[utils.random(this.styles.length-1)],
|
||||||
name: 'gavin',
|
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
scale,
|
scale,
|
||||||
pointyTop: random(1) ? true : false,
|
pointyTop: utils.random(1) ? true : false,
|
||||||
red: random(255),
|
red: utils.random(255),
|
||||||
green: random(255),
|
green: utils.random(255),
|
||||||
blue: random(255),
|
blue: utils.random(255),
|
||||||
alpha: random(25,75)/100
|
alpha: utils.random(25,75)/100
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
|
|
||||||
import {noop, random} from './utils.js';
|
import * as utils from './utils';
|
||||||
|
export {utils};
|
||||||
|
|
||||||
import OnTap from './onTap.js';
|
import OnTap from './onTap.js';
|
||||||
import Point from './point.js';
|
import Point from './point.js';
|
||||||
import Sketch from './sketch.js';
|
import Sketch from './sketch.js';
|
||||||
|
|
||||||
import TileCircle from './tileCircle.js';
|
import DrawCircle from './drawCircle.js';
|
||||||
import TileSquare from './tileSquare.js';
|
import DrawSquare from './drawSquare.js';
|
||||||
import TileHexagon from './tileHexagon.js';
|
import DrawHexagon from './drawHexagon.js';
|
||||||
export {TileCircle, TileHexagon, TileSquare};
|
export {DrawCircle, DrawHexagon, DrawSquare};
|
||||||
|
|
||||||
import Cell from './cell.js';
|
import Cell from './cell.js';
|
||||||
export {Cell};
|
export {Cell};
|
||||||
@@ -17,8 +18,8 @@ export default class Tessellate {
|
|||||||
constructor(settings) {
|
constructor(settings) {
|
||||||
['click', 'draw'].map(method => {this[method] = this[method].bind(this)});
|
['click', 'draw'].map(method => {this[method] = this[method].bind(this)});
|
||||||
|
|
||||||
this.drawCB = settings.draw || noop;
|
this.drawCB = settings.draw || utils.noop;
|
||||||
this.clickCB = settings.click || noop;
|
this.clickCB = settings.click || utils.noop;
|
||||||
this.element = document.querySelector(settings.element);
|
this.element = document.querySelector(settings.element);
|
||||||
|
|
||||||
this.sketch = new Sketch({
|
this.sketch = new Sketch({
|
||||||
|
|||||||
18
src/utils.js
18
src/utils.js
@@ -1,6 +1,13 @@
|
|||||||
|
|
||||||
export function noop() {};
|
export function noop() {};
|
||||||
|
|
||||||
|
// hypotenuse factor of isoscelese right triangle
|
||||||
|
export const sqrt2 = Math.sqrt(2);
|
||||||
|
|
||||||
|
// short width factor given the lenght of a hexagon's side
|
||||||
|
// (2*S gives long width)
|
||||||
|
export const sqrt3 = Math.sqrt(3);
|
||||||
|
|
||||||
export function throttleEvent(type, name, obj) {
|
export function throttleEvent(type, name, obj) {
|
||||||
obj = obj || window;
|
obj = obj || window;
|
||||||
let running = false;
|
let running = false;
|
||||||
@@ -25,21 +32,16 @@ export function clone(obj) {
|
|||||||
return JSON.parse(JSON.stringify(obj));
|
return JSON.parse(JSON.stringify(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extend(obj, src) {
|
export function extend(obj, ...sources) {
|
||||||
|
sources.forEach(src => {
|
||||||
for (let key in src) {
|
for (let key in src) {
|
||||||
if (src.hasOwnProperty(key)) obj[key] = src[key];
|
if (src.hasOwnProperty(key)) obj[key] = src[key];
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return obj;
|
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) {
|
export function hypotenuse(a, b) {
|
||||||
if (b == null) b = a;
|
if (b == null) b = a;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user