// Here all the people will be placed
aPeople = [];
$("#btnCreate").click(function () {
$("h2").text('THE WORLD IS POPULATED');
createPerson();
});
$("#btnSave").click(function () {
saveWorld();
});
$("#btnClear").click(function () {
$(".person").remove();
$("h2").text('THE WORLD IS EMPTY');
clearWorld();
});
function regenerateWorld() {
// Check if you have something to regenerate
if (localStorage.sWorld == undefined) {
$("h2").text('THE WORLD IS EMPTY');
} else {
// Get the world from the local storage
sWorld = localStorage.sWorld;
// convert the world into a json object
aWorld = JSON.parse(sWorld);
console.log(aWorld);
// loop through the object and rebuild the divs (people)
for (i = 0; i < aWorld.length; i++) {
sColor = aWorld[i].sColor;
sPositionTop = aWorld[i].positionTop;
sPositionLeft = aWorld[i].positionLeft;
sTheDivPerson = '<div class="person" style="background-color:' + sColor + '; left:' + sPositionLeft + 'px; top:' + sPositionTop + 'px"></div>';
$("body").append(sTheDivPerson);
}
}
console.log("regenerating the world ...");
}
;
function saveWorld() {
// Take the array of people
// make it into an string
sPeople = JSON.stringify(aPeople);
// save it in the localstorage
localStorage.sWorld = sPeople;
console.log("Saving the world state ...");
}
;
function createPerson() {
console.log("Creating a person ...");
aBackgroundColors = ["blue", "red", "black"];
iRandomNumber = getRandomNumber(0, 2);
sRandomColor = aBackgroundColors[iRandomNumber];
iPositionTop = getRandomNumber(0, 1000);
iPositionLeft =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.