10-02-2017, contact: markus.vaananen+d3random (at) gmail.com http://chazu.arkku.net/misc/diablo/d3_random/d3_random.html This is how my d3_random.html works. It is continuation to my earlier test project, d3_flavor.html (http://chazu.arkku.net/misc/diablo/d3_flavor/d3_flavor.html). d3_random.html "generates" new flavor texts based on flavor texts ripped from Diablo 3. The "base texts" are Diablo 3 flavor texts with some words omitted from them. In place of the omitted words I have put elements which are filled with randomly selected new words with Javascript. The new words are in arrays inside the Javascript file. The titles of the texts are also generated. Here I'll go into detail on the functionality of the page. ______________ Selecting which "base text" to show (updated 16-02-2017): All my "base texts" are in an array inside the js file: var baseTexts = [ 'To be one with the is to take what you need and what you must.', 'We are all connected in and in the . Partake of the spirits of the dead and flourish like the jungle that wild after the .', ' in the fire of , warrior.', ... ] Then this code randomly selects one of the texts: function newBaseText() { var randomNumero = Math.floor(Math.random() * (baseTexts.length)); return baseTexts[randomNumero]; } $(function() { var elementit = $("div.flavor"); elementit.each(function(id, object) { $(this).html(newBaseText()); $(".capital").css("text-transform", "capitalize"); }) }); In the first function, named newBaseText, we generate a random number based on the length of the baseTexts array. The second function is set to run the newBaseText script for the div with class "flavor". The function inserts the selected texts into the element and it is displayed on the page. The "capitalize" part is set to capitalize the first letter of elements with the class "capital". One of the texts above contains a randomised words as it's first letter in the sentence. I have put the class "capital" on it. This is because the words are all lower case in the arrays. If I didn't include this, some sentences would start with a lower case letter, and this is unacceptable. In my html I have only this:
" + noun[randomNumber] + "
").html()); }); With the above code we get sentences which make sense (well, at least a bit more sense): A warrior out of place is no warrior at all. ______________ Generating titles: The titles consist of two parts: Both title classes have their own code to pick words. The title arrays look like this, I shortened these here: var title1 = [ 'Astral', 'Elemental', 'Red', '', 'The', "Marauder's", 'Dawn of', 'Lord of the', ] var title2 = [ 'Wisdom', 'Hearts', 'Tale', 'Conquest', 'Dominion', 'Vengeance', ] With the title1 array containing more than just simple words we can get a variety of combinations, such as: Astral Hearts The Conquest Red Conquest Marauder's Tale Elemental Tale Lord of the Wisdom Dominion Dawn of Vengeance The generated titles don't have anything to do with the actual texts which are displayed, but that's not a problem as I think it makes the whole thing a bit more "cryptic". ______________ A button to show new content (updated 16-02-2017): Next, I wanted a button to create and show a new text. A page load generates a new text, but a button is nice to have, and I think it's a bit faster than refreshing. I put a button in the html: I put the word list arrays and the other scripts inside a function: function showContent() { } Then I put this code just outside the showContent function at the end of the js file: $(function() { $(document).ready(showContent); $('.reload-button').on("click", function() { $('.adjectiveSame, .nounSame').empty(); showContent(); }); }); So, on page load the above function executes all the scripts and shows the content. We empty the adjectiveSame and nounSame elements. If I didn't inlcude this, those elements would show multiple randomised words, with one new for every button click. We don't want this so we empty the elements. Then we run the "showContent" function which generates and shows the new content, just as if this was a new page load. ______________ So this is all the functionality there is to this page. You can check out the word lists, texts list and the code from the javascript file: http://chazu.arkku.net/misc/diablo/d3_random/wordlist.js For the gradient background I used this online gradient tool, it's very neat: http://www.colorzilla.com/gradient-editor/ Credits: https://medium.freecodecamp.com/creating-a-bare-bones-quote-generator-with-javascript-and-html-for-absolute-beginners-5264e1725f08#.rplx824je ______________ UPDATES: 16-02-2017 Completely updated the "base text" selection system, the