Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bramstein/466793 to your computer and use it in GitHub Desktop.
Save bramstein/466793 to your computer and use it in GitHub Desktop.
var wordSpacing = [2.333, -0.333, 1, 0.7, -0.25, 0.083].map(function (v) {
return [v, 15 + Math.round(Math.random() * 4)];
});
function truncate(x) {
if (x > 0 && x < 0.1) {
return 0;
}
if (x < 0 && x > -0.1) {
return 0;
}
return x;
}
wordSpacing.forEach(function (v) {
var wordSpace = truncate(v[0]),
spaces = v[1],
integerWordSpace = Math.round(wordSpace),
adjustment = wordSpace - integerWordSpace,
integerAdjustment = adjustment < 0 ? Math.floor(adjustment) : Math.ceil(adjustment),
totalAdjustment = Math.round(adjustment * spaces),
i = 0, result = [];
for (; i < spaces; i += 1) {
result[i] = integerWordSpace;
if (i * Math.abs(integerAdjustment) < Math.abs(totalAdjustment)) {
result[i] += integerAdjustment;
}
}
var actual = result.reduce(function (p, c) {
return p + c
}, 0) / result.length;
console.log('spaces: ' + spaces + ', wordSpace: ' + wordSpace + ', actual: ' + actual);
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment