026
LVL 01 — WAVE DEFENDER SESSION 026 DAY 26

SETTINGS PERSISTENCE

🎫 PIXELCRAFT-014
Feature | 🔵 Easy | Priority: 🟡 Medium

Dark mode, last-used filter settings, and recently opened files all reset on refresh. Persist user preferences locally.
CONCEPTS.UNLOCKED
{ }
Objects
{ key: value } — structured data with named properties. The fundamental way to group related data: { theme: 'dark', brightness: 0 }.
🪆
Nested Objects & Arrays
Complex data structures: objects inside objects, arrays inside objects. filterValues: { brightness: 0, contrast: 1 } and recentFiles: [] inside a settings object.
📋
JSON
JSON.stringify() converts objects to storable strings. JSON.parse() converts strings back to objects. The universal data exchange format of the web.
💾
localStorage
Browser storage that persists between sessions. Survives refresh, tab close, even browser restart. Up to ~5MB per origin. Key-value pairs, strings only (hence JSON).
📥📤
localStorage API
localStorage.setItem(key, value) — save. localStorage.getItem(key) — load. localStorage.removeItem(key) — delete. Simple, synchronous, string-based.
🛡
Safe Deserialization
JSON.parse() can throw if the stored string is corrupted. Always wrap in try/catch with a fallback to defaults. Never trust stored data blindly.
HANDS-ON.TASKS
01
Create a Settings Object
const defaultSettings = { theme: 'dark', lastFilter: null, filterValues: { brightness: 0, contrast: 1, saturation: 0, }, recentFiles: [], };
This is a nested object: filterValues is an object inside defaultSettings, and recentFiles is an array. Objects can contain any data type, including other objects and arrays.
02
Save and Load Functions
function saveSettings(settings) { localStorage.setItem( 'pixelcraft-settings', JSON.stringify(settings) ); } function loadSettings() { const stored = localStorage.getItem('pixelcraft-settings'); if (stored) { try { return JSON.parse(stored); } catch (e) { console.warn('Corrupted settings, using defaults'); return { ...defaultSettings }; } } return { ...defaultSettings }; }
The spread operator { ...defaultSettings } creates a shallow copy so each call gets its own object — modifying one doesn't affect the original defaults.
03
Persist on Every Change
brightnessSlider.addEventListener('input', (e) => { const value = parseInt(e.target.value); settings.filterValues.brightness = value; saveSettings(settings); });

Every slider move, every toggle, every preference change → save immediately. No "Save" button needed — the app just remembers.

04
Restore on Page Load
const settings = loadSettings(); brightnessSlider.value = settings.filterValues.brightness; brightnessValue.textContent = settings.filterValues.brightness;

On page load, read stored settings and apply to every UI control. The app picks up exactly where the user left off.

05
"Clear All Data" & Recent Files

Add a "Clear All Data" button in the settings panel that calls localStorage.removeItem('pixelcraft-settings').

Track recently opened files — metadata only: name, date, thumbnail hash. Never store actual image data in localStorage (5MB limit!).

06
Close the Ticket
git switch -c feature/PIXELCRAFT-014-persistence git add src/scripts/ git commit -m "Persist settings with localStorage (PIXELCRAFT-014)" git push origin feature/PIXELCRAFT-014-persistence # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Serialization and persistence.

RAM is volatile — data dies when the process ends. Storage is persistent — data survives. The bridge is serialization: converting live objects into a storable format. Deserialization is the reverse.

// This universal pattern powers:

Database storage  → rows serialized to disk
Network comms     → objects serialized for
                     transmission
Game save files   → game state → file
Browser cookies   → small data persisted
API responses     → JSON over HTTP

// JSON is just one serialization format
// among many (XML, Protocol Buffers,
// MessagePack, BSON...).
"Persistent App"
[A] Add theme persistence: save dark/light preference, restore on load, and apply the correct CSS variables immediately (no flash of wrong theme).
[B] Build a "Recent Files" panel showing the last 5 opened files with name, date, and a small thumbnail. Store metadata in localStorage.
[C] Open DevTools → Application → Local Storage. Inspect the stored JSON. Manually edit a value and refresh — watch the app load your edit.
REF.MATERIAL
ARTICLE
Mozilla Developer Network
Complete reference for JavaScript objects: properties, methods, Object.keys(), Object.entries(), spread, destructuring.
OBJECTSMDNREFERENCE
VIDEO
Web Dev Simplified
JSON.stringify, JSON.parse, nested data, and common pitfalls. Short and practical.
JSONPRACTICAL
ARTICLE
Mozilla Developer Network
Official localStorage API reference: setItem, getItem, removeItem, clear, storage events, and limitations.
LOCALSTORAGEMDN
VIDEO
Web Dev Simplified
When to use each storage mechanism: size limits, persistence, scope, security, and practical differences.
STORAGECOMPARISON
ARTICLE
javascript.info
Interactive deep dive into objects: creation, access, computed properties, references vs copies, and the spread operator.
OBJECTSINTERACTIVE
// LEAVE EXCITED BECAUSE
PixelCraft remembers! Settings survive refresh. Your app has memory. This is the first step toward applications that feel alive across sessions.