JSFiddle - React, Tailwind, and code Playground
by velo_ninja
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Professional Canvas with Theme Switch</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
height: 100%;
background-color: var(--bg-color);
transition: background-color 0.3s;
}
:root {
--bg-color: #ffffff;
--grid-light: rgba(0, 0, 0, 0.05);
--grid-dark: rgba(0, 0, 0, 0.15);
--text-color: #333;
--shape-fill: #f5f5f5;
--shape-border: #333;
}
.dark-mode {
--bg-color: #121212;
--grid-light: rgba(255, 255, 255, 0.05);
--grid-dark: rgba(255, 255, 255, 0.15);
--text-color: #e0e0e0;
--shape-fill: #1e1e1e;
--shape-border: #e0e0e0;
}
#stage {
width: 100vw;
height: 100vh;
position: relative;
}
canvas {
display: block;
background-color: var(--bg-color);
transition: background-color 0.3s;
}
.theme-toggle {
position: absolute;
top: 16px;
right: 16px;
background: #eee;
border: none;
padding: 10px 16px;
border-radius: 20px;
cursor: pointer;
font-family: sans-serif;
font-size: 14px;
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
z-index: 10;
}
.dark-mode .theme-toggle {
background: #333;
color: #fff;
}
</style>
</head>
<body>
<div id="stage">
<canvas id="myCanvas"></canvas>
<button class="theme-toggle" id="themeToggle">🌙 Dark</button>
</div>
<script>
class Canvas {
constructor(canvasId) {
this.canvas = document.getElementById(canvasId);
this.ctx = this.canvas.getContext('2d', { alpha: true });
this.gridSize = 40;
this.scale = 1;
this.offsetX = 0;
this.offsetY = 0;
...