Skip to main content

Building Your First App

Learn how to build a simple color generator app using LiveCodes. This app will let users click a button to generate random background colors.

Try the completed project below:

.

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"html": "<div class=\"container\">\n <h1>Color Generator</h1>\n <div class=\"color-display\" id=\"colorDisplay\">\n <span id=\"colorCode\">#3498db</span>\n </div>\n <button id=\"generateBtn\">Generate Color</button>\n <p class=\"hint\">Click the button to generate a random color!</p>\n</div>\n",
"css": "body {\n margin: 0;\n font-family: Arial, sans-serif;\n display: flex;\n justify-content: center;\n align-items: center;\n min-height: 100vh;\n background: #3498db;\n transition: background 0.5s ease;\n}\n\n.container {\n text-align: center;\n background: white;\n padding: 40px;\n border-radius: 20px;\n box-shadow: 0 10px 30px rgba(0,0,0,0.2);\n}\n\nh1 {\n margin: 0 0 30px 0;\n color: #333;\n}\n\n.color-display {\n background: #f0f0f0;\n padding: 30px;\n border-radius: 10px;\n margin-bottom: 30px;\n}\n\n#colorCode {\n font-size: 2rem;\n font-weight: bold;\n font-family: 'Courier New', monospace;\n color: #333;\n}\n\n#generateBtn {\n background: #333;\n color: white;\n border: none;\n padding: 15px 40px;\n font-size: 1.1rem;\n border-radius: 10px;\n cursor: pointer;\n transition: transform 0.2s;\n}\n\n#generateBtn:hover {\n transform: scale(1.05);\n}\n\n#generateBtn:active {\n transform: scale(0.95);\n}\n\n.hint {\n margin-top: 20px;\n color: #666;\n font-size: 0.9rem;\n}\n",
"js": "const colorDisplay = document.getElementById('colorDisplay');\nconst colorCode = document.getElementById('colorCode');\nconst generateBtn = document.getElementById('generateBtn');\n\nfunction getRandomColor() {\n const letters = '0123456789ABCDEF';\n let color = '#';\n for (let i = 0; i < 6; i++) {\n color += letters[Math.floor(Math.random() * 16)];\n }\n return color;\n}\n\nfunction updateColor() {\n const newColor = getRandomColor();\n document.body.style.background = newColor;\n colorCode.textContent = newColor;\n}\n\ngenerateBtn.addEventListener('click', updateColor);\n\n// Generate a color on page load\nupdateColor();\n"
}
};
createPlayground('#container', options);

What We'll Build

A fun color generator with:

  • Random color generation
  • Display the color code
  • Simple, clean interface

Project Setup

  1. Open LiveCodes
  2. We'll use HTML, CSS, and JavaScript

Implementation

HTML Structure

<div class="container">
<h1>Color Generator</h1>
<div class="color-display" id="colorDisplay">
<span id="colorCode">#3498db</span>
</div>
<button id="generateBtn">Generate Color</button>
<p class="hint">Click the button to generate a random color!</p>
</div>

Styling

body {
margin: 0;
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #3498db;
transition: background 0.5s ease;
}

.container {
text-align: center;
background: white;
padding: 40px;
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

h1 {
margin: 0 0 30px 0;
color: #333;
}

.color-display {
background: #f0f0f0;
padding: 30px;
border-radius: 10px;
margin-bottom: 30px;
}

#colorCode {
font-size: 2rem;
font-weight: bold;
font-family: 'Courier New', monospace;
color: #333;
}

#generateBtn {
background: #333;
color: white;
border: none;
padding: 15px 40px;
font-size: 1.1rem;
border-radius: 10px;
cursor: pointer;
transition: transform 0.2s;
}

#generateBtn:hover {
transform: scale(1.05);
}

#generateBtn:active {
transform: scale(0.95);
}

.hint {
margin-top: 20px;
color: #666;
font-size: 0.9rem;
}

JavaScript Logic

const colorDisplay = document.getElementById('colorDisplay');
const colorCode = document.getElementById('colorCode');
const generateBtn = document.getElementById('generateBtn');

function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}

function updateColor() {
const newColor = getRandomColor();
document.body.style.background = newColor;
colorCode.textContent = newColor;
}

generateBtn.addEventListener('click', updateColor);

// Generate a color on page load
updateColor();

How It Works

  1. HTML: Creates a simple layout with a color display and button
  2. CSS: Styles the interface and adds smooth transitions
  3. JavaScript:
    • getRandomColor(): Generates a random hex color
    • updateColor(): Changes the background and displays the color code
    • Event listener: Triggers color change on button click

Testing Your App

Try these features:

  1. Click "Generate Color" multiple times
  2. Watch the smooth color transitions
  3. See the color code update
  4. Notice the button hover and click effects

Challenge: Enhance Your App

Try adding these features:

  • Copy color code to clipboard when clicked
  • Add a history of recent colors
  • Let users save their favorite colors
  • Add different color format options (RGB, HSL)

Congratulations!

You've just built your color generator app with LiveCodes!

Compare your version with the completed project above. Did you add any personal touches?

Next Steps

Complete Code Summary

Concepts Covered: DOM manipulation, events, random generation, CSS transitions

Time to Build: 10-15 minutes