<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Improved Canvas Drawing Pad</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
:root{
--btn-size:34px;
--gap:8px;
}
*{box-sizing:border-box}
body{
margin:0;
padding:1.2rem;
font-family:system-ui,Arial,sans-serif;
background:#fafafa;
display:flex;
flex-direction:column;
align-items:center;
}
h2{margin:0 0 1rem}
#toolbar{
display:flex;
flex-wrap:wrap;
align-items:center;
gap:var(--gap);
margin-bottom:var(--gap);
}
.color{
width:var(--btn-size);
height:var(--btn-size);
border:2px solid #fff;
border-radius:4px;
cursor:pointer;
transition:transform .15s;
}
.color:hover{transform:scale(1.1)}
.color.active{outline:2px solid #333}
#clear{
padding:.5rem 1rem;
font-weight:600;
border:none;
border-radius:4px;
cursor:pointer;
background:#e0e0e0;
}
#clear:hover{background:#d0d0d0}
canvas{
/* Visual size in CSS pixels */
width:800px;
height:500px;
max-width:100%;
border:1px solid #ccc;
background:#fff;
box-shadow:0 0 6px rgba(0,0,0,.15);
touch-action:none; /* stop touch scrolling */
user-select:none; /* prevent highlight on double‑tap */
}
@media (max-width:850px){
canvas{height:60vh}
}
</style>
</head>
<body>
<h2>Drawing Pad 🎨 (rev 2)</h2>
<div id="toolbar">
<div class="color active" data-c="#000" style="background:#000"></div>
<div class="color" data-c="#e74c3c" style="background:#e74c3c"></div>
<div class="color" data-c="#3498db" style="background:#3498db"></div>
<div class="color" data-c="#27ae60" style="background:#27ae60"></div>
<button id="clear">Clear</button>
</div>
<canvas id="c"></canvas>
<script>
/* ----------------------------------------------------
Canvas Hi‑DPI setup
---------------------------------------------------- */
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d', {willReadFrequently:false});
let DPR = window.devicePixelRatio || 1;
/* make the backing store (canvas.width/height) big enough
to hold crisp pixels, but keep the CSS size unchanged */
function resizeCanvas(){
const rect = canvas.getBoundingClientRect();
const needW = Math.round(rect.width * DPR);
const needH = Math.round(rect.height * DPR);
// only resize if needed – avoids clearing drawings on “fake” resizes
if (canvas.width !== needW || canvas.height !== needH){
const img = ctx.getImageData(0,0,canvas.width,canvas.height); // store drawing
canvas.width = needW;
canvas.height = needH;
ctx.setTransform(DPR,0,0,DPR,0,0); // reset the transform
ctx.putImageData(img,0,0); // restore drawing
}
}
resizeCanvas();
window.addEventListener('resize', ()=>{
DPR = window.devicePixelRatio || 1;
resizeCanvas();
});
/* ----------------------------------------------------
Drawing state
---------------------------------------------------- */
let isDown = false;
let lastX = 0, lastY = 0;
let strokeColor = '#000';
const LINE_W = 2;
ctx.lineWidth = LINE_W;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
/* util to get *canvas* coordinates from a pointer event */
function pointerPos(evt){
const rect = canvas.getBoundingClientRect();
return {
x: (evt.clientX - rect.left),
y: (evt.clientY - rect.top)
};
}
/* pointer handlers */
canvas.addEventListener('pointerdown', e=>{
if(e.button!==0) return; // only left / primary button
isDown = true;
const p = pointerPos(e);
lastX = p.x;
lastY = p.y;
});
canvas.addEventListener('pointermove', e=>{
if(!isDown) return;
const p = pointerPos(e);
ctx.strokeStyle = strokeColor;
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(p.x, p.y);
ctx.stroke();
lastX = p.x;
lastY = p.y;
});
const stop = ()=> isDown=false;
canvas.addEventListener('pointerup', stop);
canvas.addEventListener('pointerleave',stop);
canvas.addEventListener('pointercancel',stop);
/* ----------------------------------------------------
Color palette
---------------------------------------------------- */
document.querySelectorAll('.color').forEach(btn=>{
btn.addEventListener('click', ()=>{
document.querySelector('.color.active')?.classList.remove('active');
btn.classList.add('active');
strokeColor = btn.dataset.c;
});
});
/* ----------------------------------------------------
Clear button
---------------------------------------------------- */
document.getElementById('clear').addEventListener('click', ()=>{
ctx.clearRect(0,0,canvas.width,canvas.height);
});
</script>
</body>
</html>