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).
Last active
March 26, 2020 03:46
-
-
Save PixievoltNo1/94e0435108b42abdd58ee95faa0d2fc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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