77 lines
1.6 KiB
HTML
77 lines
1.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>HTML5 Canvas</title>
|
|
<link rel="icon" href="https://fav.farm/🔥" />
|
|
</head>
|
|
<body>
|
|
<ul>
|
|
<li id="0" class="color">Red</li>
|
|
<li id="122" class="color">Green</li>
|
|
<li id="255" class="color">Blue</li>
|
|
</ul>
|
|
<canvas id="draw" width="800" height="800"></canvas>
|
|
<script>
|
|
|
|
let isDrawing = false;
|
|
let lastX = 0;
|
|
let lastY = 0;
|
|
|
|
const canvas = document.querySelector('#draw');
|
|
canvas.width = window.innerWidth;
|
|
canvas.height = window.innerHeight;
|
|
canvas.addEventListener('mousemove', draw);
|
|
canvas.addEventListener('mouseup', () => isDrawing = false);
|
|
canvas.addEventListener('mouseout', () => isDrawing = false);
|
|
|
|
canvas.addEventListener('mousedown', (e) => {
|
|
isDrawing = true;
|
|
[lastX, lastY] = [e.offsetX, e.offsetY];
|
|
});
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
ctx.strokeStyle = '#BADA55';
|
|
ctx.lineJoin = 'round';
|
|
ctx.lineCap = 'round';
|
|
|
|
const buttons = document.querySelectorAll('.color');
|
|
buttons.forEach(button => button.addEventListener('click', updateColor));
|
|
|
|
function updateColor() {
|
|
ctx.strokeStyle = `hsl(${this.id}, 100%, 50%)`;
|
|
}
|
|
|
|
function draw(e) {
|
|
if(!isDrawing) return;
|
|
ctx.beginPath();
|
|
ctx.moveTo(lastX, lastY);
|
|
[lastX, lastY] = [e.offsetX, e.offsetY];
|
|
ctx.lineTo(lastX, lastY);
|
|
ctx.stroke();
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
html, body {
|
|
margin: 0;
|
|
}
|
|
|
|
ul {
|
|
display: inline-block;
|
|
list-style-type: none;
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
li {
|
|
float: left;
|
|
display: block;
|
|
padding: 5px;
|
|
border: 2px solid black;
|
|
}
|
|
</style>
|
|
|
|
</body>
|
|
</html>
|