add: base files

This commit is contained in:
sadorowo 2023-10-20 09:32:10 +02:00 committed by GitHub
commit 4d70b47308
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 0 deletions

29
index.html Normal file
View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Strona z ciekawym wykorzystaniem JS</title>
<link rel="stylesheet" href="style.css"/>
<script defer src="script.js"></script>
</head>
<body>
<div id="menu">
<select id="color">
<option value="green">Zielony</option>
<option value="red">Czerwony</option>
<option value="yellow">Żółty</option>
<option value="orange">Pomarańczowy</option>
<option value="gray">Szary</option>
<option value="black">Czarny</option>
<option value="lime">Limonkowy</option>
</select>
<input type="range" min="5" max="40" value="5" id="weight"/>
<button id="clear">Wyczyść rysunek!</button>
</div>
<canvas id="canvas"></canvas>
</body>
</html>

56
script.js Normal file
View File

@ -0,0 +1,56 @@
document.addEventListener('contextmenu', event => event.preventDefault());
document.addEventListener('mousedown', startPainting);
document.addEventListener('mouseup', stopPainting);
document.addEventListener('mousemove', draw);
window.addEventListener('resize', resize);
const colorSelector = document.getElementById('color');
const weightSlider = document.getElementById('weight');
const clearButton = document.getElementById('clear');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let color = 'green';
let weight = 5;
clearButton.addEventListener('click', () => ctx.clearRect(0, 0, canvas.width, canvas.height))
weightSlider.addEventListener('change', e => weight = e.target.value)
colorSelector.addEventListener('change', e => color = e.target.value)
resize();
function resize() {
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
}
let lastPos = { x: 0, y: 0 };
let paint = false;
function getPosition(event) {
lastPos.x = event.clientX - canvas.offsetLeft;
lastPos.y = event.clientY - canvas.offsetTop;
}
function startPainting(event) {
paint = true;
getPosition(event);
}
function stopPainting() {
paint = false;
}
function draw(event) {
if (!paint) return;
ctx.beginPath();
ctx.lineWidth = weight;
ctx.lineCap = 'round';
ctx.strokeStyle = color;
ctx.moveTo(lastPos.x, lastPos.y);
getPosition(event);
ctx.lineTo(lastPos.x, lastPos.y);
ctx.stroke();
}

34
style.css Normal file
View File

@ -0,0 +1,34 @@
body {
margin: 0;
overflow: hidden;
}
#canvas {
width: 100%;
height: 100%;
}
#menu {
background-color: #fff;
position: absolute;
margin: 10px;
}
button {
border: none;
color: #fff;
background-color: #2f3136;
padding: 5px;
border-radius: 5px;
transition: ease-in-out .2s;
}
button:hover {
background-color: #646466;
}
select {
border: 2px solid #000;
padding: 5px;
border-radius: 5px;
}