code Copy code
<header>
<div class="title">Interactive Drawing Coach</div>
<div class="controls-row">
<div class="chalk-palette">
<button class="chalk-btn active" style="background: #ffffff; color: #ffffff;" data-color="#ffffff" title="White Chalk"></button>
<button class="chalk-btn" style="background: #fef08a; color: #fef08a;" data-color="#fef08a" title="Yellow Chalk"></button>
<button class="chalk-btn" style="background: #22d3ee; color: #22d3ee;" data-color="#22d3ee" title="Teal Chalk"></button>
<button class="chalk-btn" style="background: #fbcfe8; color: #fbcfe8;" data-color="#fbcfe8" title="Pink Chalk"></button>
</div>
<button class="btn-icon" id="clearBtn" title="Erase Board">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="3 6 5 6 21 6"></polyline>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
<line x1="10" y1="11" x2="10" y2="17"></line>
<line x1="14" y1="11" x2="14" y2="17"></line>
</svg>
</button>
</div>
</header>
<div class="studio-container">
<div class="board-wrapper" id="boardWrapper">
<canvas id="sparksCanvas"></canvas>
<canvas id="paintCanvas"></canvas>
<div class="coach-container" id="coachContainer">
<div class="speech-bubble" id="speechBubble">Nice lines! Keep going!</div>
<div class="coach-character" id="coachCharacter">
<!-- Integrated Head Structure Layered Properly -->
<div class="teacher-head" id="teacherHead">
<div class="eyes-container">
<div class="eye"><div class="pupil" id="pupil-left"></div></div>
<div class="eye"><div class="pupil" id="pupil-right"></div></div>
</div>
<div class="glasses-frame">
<div class="lens"></div>
<div class="bridge"></div>
<div class="lens"></div>
</div>
<div class="mouth-smile"></div>
<div class="goatee-beard"></div>
<div class="hair-container">
<div class="hair-swoop-tuft"></div>
<div class="hair-left-sideburn"></div>
<div class="hair-main"></div>
</div>
</div>
<!-- Side Mounted Twiddling Fidget Arm -->
<div class="coach-arm-wrapper" id="coachArm">
<div class="cartoon-sleeve"></div>
<div class="cartoon-hand">
<div class="twiddle-chalk"></div>
</div>
</div>
<div class="coach-body" id="teacherBody"></div>
</div>
</div>
</div>
</div>
<footer>
<div class="legend-box">
<div class="legend-item">
<div class="dot"></div>
<span>Chalkboard Sandbox</span>
</div>
</div>
</footer>
<script>
const canvas = document.getElementById('paintCanvas');
const ctx = canvas.getContext('2d');
const sparksCanvas = document.getElementById('sparksCanvas');
const sparksCtx = sparksCanvas.getContext('2d');
const boardWrapper = document.getElementById('boardWrapper');
const clearBtn = document.getElementById('clearBtn');
const coachContainer = document.getElementById('coachContainer');
const coachArm = document.getElementById('coachArm');
const pupilLeft = document.getElementById('pupil-left');
const pupilRight = document.getElementById('pupil-right');
const speechBubble = document.getElementById('speechBubble');
let isDrawing = false;
let lastX = 0;
let lastY = 0;
let particles = [];
let chalkColor = '#ffffff';
const maxPupilOffset = 6;
const quotes = [
"Excellent sketch!",
"Brilliant geometry right there.",
"Smooth stroke lines!",
"Keep that canvas flowing!",
"Fascinating technique!",
"Looking wonderful!"
];
const penPosition = {
clientX: window.innerWidth * 0.5,
clientY: window.innerHeight * 0.4
};
function resizeCanvases() {
canvas.width = boardWrapper.clientWidth;
canvas.height = boardWrapper.clientHeight;
sparksCanvas.width = boardWrapper.clientWidth;
sparksCanvas.height = boardWrapper.clientHeight;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.lineWidth = 6;
ctx.shadowBlur = 2;
}
window.addEventListener('resize', resizeCanvases);
resizeCanvases();
document.querySelectorAll('.chalk-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelector('.chalk-btn.active').classList.remove('active');
btn.classList.add('active');
chalkColor = btn.getAttribute('data-color');
});
});
canvas.addEventListener('pointerdown', (e) => {
isDrawing = true;
const rect = canvas.getBoundingClientRect();
lastX = e.clientX - rect.left;
lastY = e.clientY - rect.top;
ctx.beginPath();
ctx.moveTo(lastX, lastY);
canvas.setPointerCapture(e.pointerId);
// Add the dynamic twiddling modifier class
coachArm.classList.add('drawing-active');
speechBubble.classList.remove('active');
updateTargetCoordinates(e);
});
function updateTargetCoordinates(e) {
penPosition.clientX = e.clientX;
penPosition.clientY = e.clientY;
}
canvas.addEventListener('pointermove', (e) => {
const rect = canvas.getBoundingClientRect();
const currentX = e.clientX - rect.left;
const currentY = e.clientY - rect.top;
updateTargetCoordinates(e);
if (isDrawing) {
const distMoved = Math.sqrt((currentX - lastX)**2 + (currentY - lastY)**2);
const pressureOpacity = Math.max(0.6, Math.min(0.9, 1 - (distMoved * 0.015)));
ctx.strokeStyle = hexToRGBA(chalkColor, pressureOpacity);
ctx.shadowColor = hexToRGBA(chalkColor, 0.5);
ctx.lineTo(currentX, currentY);
ctx.stroke();
spawnChalkSparks(currentX, currentY, chalkColor);
lastX = currentX;
lastY = currentY;
}
});
function stopDrawing(e) {
if (!isDrawing) return;
isDrawing = false;
ctx.closePath();
// Stop twiddling action class
coachArm.classList.remove('drawing-active');
// Trigger talking cloud
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
speechBubble.textContent = randomQuote;
speechBubble.classList.add('active');
}
canvas.addEventListener('pointerup', stopDrawing);
canvas.addEventListener('pointercancel', stopDrawing);
canvas.addEventListener('pointerleave', stopDrawing);
clearBtn.addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles = [];
sparksCtx.clearRect(0, 0, sparksCanvas.width, sparksCanvas.height);
speechBubble.classList.remove('active');
});
function hexToRGBA(hex, alpha) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
function spawnChalkSparks(x, y, colorHex) {
const r = parseInt(colorHex.slice(1, 3), 16);
const g = parseInt(colorHex.slice(3, 5), 16);
const b = parseInt(colorHex.slice(5, 7), 16);
for (let i = 0; i < 2; i++) {
particles.push({
x: x,
y: y,
vx: (Math.random() - 0.5) * 4,
vy: (Math.random() - 0.5) * 4 - 0.5,
alpha: 1.0,
size: Math.random() * 2 + 1,
color: `rgba(${r}, ${g}, ${b}, `
});
}
}
function animationFrameTick() {
// Interactive Gaze Tracking Loop
[pupilLeft, pupilRight].forEach(pupil => {
const parentEye = pupil.parentElement;
const eyeRect = parentEye.getBoundingClientRect();
const eyeCenterX = eyeRect.left + eyeRect.width / 2;
const eyeCenterY = eyeRect.top + eyeRect.height / 2;
const gazeDeltaX = penPosition.clientX - eyeCenterX;
const gazeDeltaY = penPosition.clientY - eyeCenterY;
const gazeAngle = Math.atan2(gazeDeltaY, gazeDeltaX);
const gazeDistance = Math.sqrt(gazeDeltaX * gazeDeltaX + gazeDeltaY * gazeDeltaY);
const pupilDistance = Math.min(gazeDistance * 0.06, maxPupilOffset);
pupil.style.transform = `translate(${Math.cos(gazeAngle) * pupilDistance}px, ${Math.sin(gazeAngle) * pupilDistance}px)`;
});
// Spark particles renderer
sparksCtx.clearRect(0, 0, sparksCanvas.width, sparksCanvas.height);
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.x += p.vx;
p.y += p.vy;
p.vy += 0.06;
p.alpha -= 0.03;
if (p.alpha <= 0) {
particles.splice(i, 1);
continue;
}
sparksCtx.fillStyle = p.color + p.alpha + ')';
sparksCtx.beginPath();
sparksCtx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
sparksCtx.fill();
}
requestAnimationFrame(animationFrameTick);
}
animationFrameTick();
</script>