├── README.md ├── index.js ├── package.json └── test └── test.js /README.md: -------------------------------------------------------------------------------- 1 | # Simple Modular Scale 2 | 3 | Modular scale based on an array of ratios 4 | 5 | ## Usage 6 | 7 | ```bash 8 | npm i simple-modular-scale 9 | ``` 10 | 11 | ```js 12 | var ms = require('simple-modular-scale') 13 | 14 | var scale = ms({ 15 | base: 16, 16 | ratios: [3/2, 4/3], 17 | length: 8 18 | }) 19 | // [ 16, 24, 32, 48, 64, 96, 128, 192 ] 20 | ``` 21 | 22 | ## Options 23 | 24 | - `base` (default `16`) is the root number from which all values in the scale are derived 25 | - `ratios` (default `[3/2, 4/3]`) is an array of numbers that are multiplied by the previous number in the sequence. Ratios are alternated to create a sort of multi-stranded modular scale. 26 | - `length` (default `16`) is the total number of values output. 27 | 28 | ## Examples 29 | 30 | ### Multiple Ratios 31 | 32 | ```js 33 | var ms = require('simple-modular-scale') 34 | 35 | var scale = ms({ 36 | base: 16, 37 | ratios: [ 9/8, 4/3, 4/3 ], 38 | length: 8 39 | }) 40 | ``` 41 | 42 | Multiplicand | Multiplier | Product 43 | -------------|------------|-------- 44 | 16 | 1 | 16 45 | 16 | 9/8 | 18 46 | 18 | 4/3 | 24 47 | 24 | 4/3 | 32 48 | 32 | 9/8 | 36 49 | 36 | 4/3 | 48 50 | 48 | 4/3 | 64 51 | 64 | 9/8 | 72 52 | 53 | Starting with a base of 16, the next number added is 16 * 9/8, which is 18. 54 | Next 18 is multiplied by `ratios[1]` or 4/3 to produce 24. 55 | Then 24 is multiplied by `ratios[2]` or 4/3 to produce 32. 56 | This sequence continues until an array of `length` is produced. 57 | 58 | ### Multi-Stranded Scales 59 | 60 | For a more traditional multi-stranded scale, call the function twice. 61 | 62 | ```js 63 | var base = 16 64 | var scaleA = ms({ 65 | base: base, 66 | ratios: [5/4], 67 | length: 5 68 | }) 69 | var scaleB = ms({ 70 | base: base, 71 | ratios: [3/2], 72 | length: 5 73 | }) 74 | ``` 75 | 76 | Multiplicand | Multiplier | Product 77 | -------------|------------|-------- 78 | 16 | 1 | 16 79 | 16 | 5/4 | 20 80 | 20 | 5/4 | 25 81 | 25 | 5/4 | 31.25 82 | 31.25 | 5/4 | 39.0625 83 | 84 | Multiplicand | Multiplier | Product 85 | -------------|------------|-------- 86 | 16 | 1 | 16 87 | 16 | 3/2 | 24 88 | 24 | 3/2 | 36 89 | 36 | 3/2 | 54 90 | 54 | 3/2 | 81 91 | 92 | 93 | ### Using Values for Styles in React 94 | 95 | ```js 96 | // styles.js 97 | import ms from 'simple-modular-scale' 98 | 99 | let scale = ms({ 100 | base: 8, 101 | ratios: [9/8, 4/3, 4/3] 102 | }) 103 | 104 | export default { 105 | // font sizes 106 | h1: scale[6], // 32 107 | h2: scale[5], // 24 108 | h3: scale[4], // 18 109 | h4: scale[3], // 16 110 | h5: scale[2], // 12 111 | h6: scale[1] // 9 112 | } 113 | ``` 114 | 115 | MIT License 116 | 117 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = function (opts) { 3 | 4 | var opts = opts || {} 5 | var base = opts.base || 16 6 | // opts.factors will be deprecated 7 | var ratios = opts.ratios || opts.factors || [3/2, 4/3] 8 | var length = opts.length || 16 9 | 10 | var arr = [base] 11 | 12 | for (var i = 0; i < length - 1; i++) { 13 | var n = i % ratios.length 14 | var f = ratios[n] 15 | var a = arr[arr.length - 1] 16 | arr.push(a * f) 17 | } 18 | 19 | return arr 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple-modular-scale", 3 | "version": "1.0.2", 4 | "description": "Modular scale function based on an array of factors", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha test" 8 | }, 9 | "keywords": [ 10 | "design", 11 | "math", 12 | "modular-scale" 13 | ], 14 | "author": "Brent Jackson", 15 | "license": "MIT", 16 | "devDependencies": { 17 | "mocha": "^2.2.5" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 2 | var ms = require('..') 3 | var assert = require('assert') 4 | var scale 5 | 6 | describe('simple-modular-scale', function() { 7 | 8 | it('should return an array', function() { 9 | scale = ms({ 10 | base: 16, 11 | factors: [3/2, 4/3], 12 | length: 8 13 | }) 14 | assert.equal(Array.isArray(scale), true) 15 | }) 16 | 17 | it('should have a length of 8', function() { 18 | console.log(scale) 19 | assert.equal(scale.length, 8) 20 | }) 21 | 22 | }) 23 | 24 | 25 | --------------------------------------------------------------------------------