From a2479f2d2f3988e411b2bacb360d7156ede58ac1 Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Fri, 19 Feb 2016 14:28:15 -0500 Subject: [PATCH] improving module structure and naming --- src/main.js | 29 ++++++++++++++--------------- src/tessellate.js | 15 ++++++++------- src/utils.js | 24 +++++++++++++----------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/main.js b/src/main.js index 9f4ec19..0878c4c 100644 --- a/src/main.js +++ b/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 })); } diff --git a/src/tessellate.js b/src/tessellate.js index f98c895..f0bfe52 100644 --- a/src/tessellate.js +++ b/src/tessellate.js @@ -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({ diff --git a/src/utils.js b/src/utils.js index 3cfc085..219919a 100644 --- a/src/utils.js +++ b/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;