Below is the source file powering the Randomize function of Pixievolt No. 1 Logo Creator as of version 2, provided as a companion to this blog post. All rights reserved (not open source).
import { fonts, colors, icons, name1stParts, name2ndParts } from "./data.mjs"; | |
export function randomSettings() { | |
let textEffects, noOfTextEffects = weightedIndexSelection([5, 40, 40, 15]); | |
if (noOfTextEffects == 3) { | |
textEffects = [true, true, true]; | |
} else if (noOfTextEffects == 2) { | |
textEffects = [true, true, true]; | |
textEffects[Math.floor( Math.random() * 3 )] = false; | |
} else if (noOfTextEffects == 1) { | |
textEffects = [false, false, false]; | |
textEffects[Math.floor( Math.random() * 3 )] = true; | |
} else { | |
textEffects = [false, false, false]; | |
} | |
let [italic, smallCaps, textGlow] = textEffects; | |
let fontWeights = fonts.map( | |
smallCaps | |
? (font) => { return font.name == "DejaVu Sans" ? 4 : 3; } | |
: (font) => { return font.name == "DejaVu Sans" ? 1 : 2; } | |
); | |
let fontId = weightedIndexSelection(fontWeights); | |
let colorId = Math.floor( Math.random() * colors.length ); | |
let iconId = weightedIndexSelection( icons.map( (icon) => { return icon.cc ? 4 : 3; } ) ); | |
let name1stPartId = Math.floor( Math.random() * name1stParts.length ); | |
let name2ndPartId = Math.floor( Math.random() * name2ndParts.length ); | |
let no = Math.floor( Math.random() * 101 ); | |
return {fontId, italic, smallCaps, textGlow, colorId, | |
iconId, name1stPartId, name2ndPartId, no}; | |
} | |
function weightedIndexSelection(array) { | |
let weightsTotal = array.reduce( (total, weight) => { return total + weight; } ); | |
let key = Math.floor( Math.random() * weightsTotal ); | |
for (let i = 0; i < array.length; ++i) { | |
if ( key < array[i] ) { | |
return i; | |
} else { | |
key -= array[i]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment