modular-scale
Advanced tools
Comparing version 5.0.3 to 5.1.0
47
index.js
@@ -1,2 +0,2 @@ | ||
var defaultBase = 1 | ||
var defaultBase = 16 | ||
var defaultRatio = 1.618 | ||
@@ -46,19 +46,44 @@ var ratios = { | ||
return function ms (value) { | ||
return value > 0 | ||
? up(value) | ||
: down(value * -1) | ||
function ms (v, r) { | ||
return v > 0 | ||
? up(v, r) | ||
: down(v * -1, r) | ||
} | ||
function up (value) { | ||
return round(Math.pow(ratio, value) * base) | ||
function up (v, r) { | ||
var c = Math.pow(ratio, v) * base | ||
return r | ||
? round(relative(c)) | ||
: round(c) | ||
} | ||
function down (value) { | ||
return round(base / Math.pow(ratio, value)) | ||
function down (v, r) { | ||
var c = base / Math.pow(ratio, v) | ||
return r | ||
? round(relative(c)) | ||
: round(c) | ||
} | ||
function round (value) { | ||
return Math.round(value * 1000) / 1000 | ||
function relative (v) { | ||
return v / base | ||
} | ||
function round (v) { | ||
return Math.round(v * 1000) / 1000 | ||
} | ||
function steps (v, r) { | ||
v = v || 8 | ||
var s = [] | ||
var half = Math.floor(v * 0.5) | ||
var i = half * -1 | ||
var l = half + 1 | ||
for (i; i < l; i++) { | ||
s.push(ms(i, r)) | ||
} | ||
return s.reverse() | ||
} | ||
ms.steps = steps | ||
return ms | ||
} |
{ | ||
"name": "modular-scale", | ||
"version": "5.0.3", | ||
"version": "5.1.0", | ||
"description": "Module for generating a modular scale.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,17 +15,24 @@ modular-scale | ||
``` | ||
var modularScale = require('modular-scale'), | ||
ms = modularScale({ | ||
ratio: 'goldenSection', | ||
base: '16px' | ||
}) | ||
var modularScale = require('modular-scale') | ||
var ms = modularScale({ | ||
ratio: 'goldenSection', | ||
base: '16px' | ||
}) | ||
ms(4) // 109.656 | ||
``` | ||
// Get a step up from the base font size in the scale | ||
ms(4) // 109.656 | ||
notes | ||
----- | ||
Default output is in pixels, but you can get relative units by dividing the returned value by the base font-size. | ||
// Get a step down from the base font size in the scale | ||
ms(-1) // 9.889 | ||
> ms(4) = 109.656px / 16px = 6.854em|rem | ||
// Get a rem or em relative value | ||
ms(4, true) // 6.854 | ||
// Get an array of steps for use as pixel measurements | ||
ms.steps(6) // [ 67.773, 41.887, 25.888, 16, 9.889, 6.112, 3.777 ] | ||
// Also can return relative values for use with em, rem etc. | ||
ms.steps(6, true) // [ 4.236, 2.618, 1.618, 1, 0.618, 0.382, 0.236 ] | ||
``` | ||
Inspiration | ||
@@ -32,0 +39,0 @@ ----------- |
56
test.js
@@ -44,7 +44,8 @@ var test = require('tape') | ||
var ms = ModularScale() | ||
t.equals(ms(-2), 0.382, 'ms(-2)') | ||
t.equals(ms(-1), 0.618, 'ms(-1)') | ||
t.equals(ms(1), 1.618, 'ms(1)') | ||
t.equals(ms(2), 2.618, 'ms(2)') | ||
t.equals(ms(3), 4.236, 'ms(3)') | ||
t.equals(ms(-2), 6.112, 'ms(-2)') | ||
t.equals(ms(-1), 9.889, 'ms(-1)') | ||
t.equals(ms(0), 16, 'ms(0)') | ||
t.equals(ms(1), 25.888, 'ms(1)') | ||
t.equals(ms(2), 41.887, 'ms(2)') | ||
t.equals(ms(3), 67.773, 'ms(3)') | ||
t.end() | ||
@@ -68,3 +69,3 @@ }) | ||
}) | ||
t.equals(ms(1), 1.618, 'ms(1)') | ||
t.equals(ms(1), 25.888, 'ms(1)') | ||
t.end() | ||
@@ -78,4 +79,45 @@ }) | ||
}) | ||
t.equals(ms(1), 1.618, 'did not bomb when passed garbage input.') | ||
t.equals(ms(1), 25.888, 'did not bomb when passed garbage input.') | ||
t.end() | ||
}) | ||
test('should return a stepped scale', t => { | ||
var ms = ModularScale({ | ||
ratio: ModularScale.ratios.perfectFourth, | ||
base: 18 | ||
}) | ||
t.deepEqual( | ||
ms.steps(9), | ||
[ | ||
56.832, | ||
42.635, | ||
31.984, | ||
23.994, | ||
18, | ||
13.503, | ||
10.13, | ||
7.599, | ||
5.701 | ||
], | ||
'returns correct scale steps' | ||
) | ||
t.end() | ||
}) | ||
test('should return relative units', t => { | ||
var ms = ModularScale() | ||
t.deepEqual( | ||
ms.steps(6, true), | ||
[ | ||
4.236, | ||
2.618, | ||
1.618, | ||
1, | ||
0.618, | ||
0.382, | ||
0.236 | ||
], | ||
'returns relative scale steps' | ||
) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6739
187
41