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 {TileCircle, TileHexagon, TileSquare} from './tessellate.js';
|
||||
|
||||
import {utils} from './tessellate.js';
|
||||
import {DrawCircle, DrawHexagon, DrawSquare} from './tessellate.js';
|
||||
import {Cell} from './tessellate.js';
|
||||
|
||||
class Demo {
|
||||
@@ -17,9 +17,9 @@ class Demo {
|
||||
draw: this.draw
|
||||
});
|
||||
|
||||
this.circle = new TileCircle();
|
||||
this.square = new TileSquare();
|
||||
this.hexagon = new TileHexagon();
|
||||
this.circle = new DrawCircle();
|
||||
this.square = new DrawSquare();
|
||||
this.hexagon = new DrawHexagon();
|
||||
|
||||
this.tiles = ['circle', 'hexagon', 'square'];
|
||||
this.styles = ['filled', 'outline'];
|
||||
@@ -28,20 +28,19 @@ class Demo {
|
||||
click(event) {
|
||||
let x = event.pageX;
|
||||
let y = event.pageY;
|
||||
let scale = random(10, 50);
|
||||
let scale = utils.random(10, 50);
|
||||
|
||||
this.map.push(new Cell({
|
||||
tile: this.tiles[random(this.tiles.length-1)],
|
||||
style: this.styles[random(this.styles.length-1)],
|
||||
name: 'gavin',
|
||||
tile: this.tiles[utils.random(this.tiles.length-1)],
|
||||
style: this.styles[utils.random(this.styles.length-1)],
|
||||
x,
|
||||
y,
|
||||
scale,
|
||||
pointyTop: random(1) ? true : false,
|
||||
red: random(255),
|
||||
green: random(255),
|
||||
blue: random(255),
|
||||
alpha: random(25,75)/100
|
||||
pointyTop: utils.random(1) ? true : false,
|
||||
red: utils.random(255),
|
||||
green: utils.random(255),
|
||||
blue: utils.random(255),
|
||||
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 Point from './point.js';
|
||||
import Sketch from './sketch.js';
|
||||
|
||||
import TileCircle from './tileCircle.js';
|
||||
import TileSquare from './tileSquare.js';
|
||||
import TileHexagon from './tileHexagon.js';
|
||||
export {TileCircle, TileHexagon, TileSquare};
|
||||
import DrawCircle from './drawCircle.js';
|
||||
import DrawSquare from './drawSquare.js';
|
||||
import DrawHexagon from './drawHexagon.js';
|
||||
export {DrawCircle, DrawHexagon, DrawSquare};
|
||||
|
||||
import Cell from './cell.js';
|
||||
export {Cell};
|
||||
@@ -17,8 +18,8 @@ export default class Tessellate {
|
||||
constructor(settings) {
|
||||
['click', 'draw'].map(method => {this[method] = this[method].bind(this)});
|
||||
|
||||
this.drawCB = settings.draw || noop;
|
||||
this.clickCB = settings.click || noop;
|
||||
this.drawCB = settings.draw || utils.noop;
|
||||
this.clickCB = settings.click || utils.noop;
|
||||
this.element = document.querySelector(settings.element);
|
||||
|
||||
this.sketch = new Sketch({
|
||||
|
||||
24
src/utils.js
24
src/utils.js
@@ -1,6 +1,13 @@
|
||||
|
||||
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) {
|
||||
obj = obj || window;
|
||||
let running = false;
|
||||
@@ -25,21 +32,16 @@ export function clone(obj) {
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
|
||||
export function extend(obj, src) {
|
||||
for (let key in src) {
|
||||
if (src.hasOwnProperty(key)) obj[key] = src[key];
|
||||
}
|
||||
export function extend(obj, ...sources) {
|
||||
sources.forEach(src => {
|
||||
for (let key in src) {
|
||||
if (src.hasOwnProperty(key)) obj[key] = src[key];
|
||||
}
|
||||
});
|
||||
|
||||
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