Skip to content

Commit

Permalink
fix: various bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
GivenBY committed Feb 21, 2024
1 parent e77f8c5 commit 82053f0
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 151 deletions.
Binary file added assets/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 74 additions & 31 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,102 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DrawBoard</title>
<script src="main.js" defer></script>
<meta name="title" content="DrawBoard" />
<meta
name="description"
content="DrawBoard is a versatile whiteboard tool with features for easy drawing, including undo/redo functionality and a variety of shapes and eraser options."
/>

<!-- Open Graph (Facebook) -->
<meta property="og:type" content="website" />
<meta property="og:url" content="https://givenby.github.io/DrawBoard/" />
<meta property="og:title" content="DrawBoard - Draw anything You Like" />
<meta
property="og:description"
content="DrawBoard is a versatile whiteboard tool with features for easy drawing, including undo/redo functionality and a variety of shapes and eraser options."
/>
<meta property="og:image" content="image.jpg" />

<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image" />
<meta
property="twitter:url"
content="https://givenby.github.io/DrawBoard/"
/>
<meta
property="twitter:title"
content="DrawBoard - Draw anything You Like"
/>
<meta
property="twitter:description"
content="DrawBoard is a versatile whiteboard tool with features for easy drawing, including undo/redo functionality and a variety of shapes and eraser options."
/>
<meta property="twitter:image" content="image.jpg" />

<!-- Stylesheets -->
<link rel="stylesheet" href="style.css" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css"
/>

<!-- Fonts -->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@200;400;600&display=swap"
rel="stylesheet"
/>

<!-- Favicons -->
<link rel="icon" type="image/x-icon" href="/assets/images/logo.png" />

<title>DrawBoard</title>
</head>

<body>
<div class="enclosed">
<div class="container">
<div class="sidebar">
<div class="size">
<button id="decrease">&minus;</button
><input
class="input-field"
type="number"
id="size"
value="1"
min="1"
max="100"
onchange="updateValue(event)"
/>

<button id="increase">&plus;</button>
</div>
<input class="color-picker" value="#000000" type="color" id="color" />

<div class="toolbox">
<div class="text-center">
<input
style="width: 20px"
type="text"
id="size"
value="1"
inputmode="numeric"
pattern="\d*"
onchange="updateValue(event)"
/>

<div>
<button id="decrease">-</button>
<button id="increase">+</button>
</div>
</div>
<label for="color"></label>
<input class="color-picker" value="#000000" type="color" id="color" />
<button id="clear">
<div class="tooltip">
<i class="fa-solid fa-eraser fa-1x"></i>
<i class="fa-solid fa-eraser fa-lg"></i>
<span class="tooltiptext">Clear All</span>
</div>
</button>
</div>
<div class="toolbox">
<button id="redo">
<button id="undo">
<div class="tooltip">
<i class="fa-solid fa-rotate-left fa-2x"></i>
<span class="tooltiptext">undo</span>
<i class="fa-solid fa-rotate-left fa-lg"></i>
<span class="tooltiptext">Undo</span>
</div>
</button>
<button id="undo">
<button id="redo">
<div class="tooltip">
<i class="fa-solid fa-rotate-right fa-2x"></i>
<span class="tooltiptext">redo</span>
<i class="fa-solid fa-rotate-right fa-lg"></i>
<span class="tooltiptext">Redo</span>
</div>
</button>
</div>
</div>
<canvas id="canvas" width="1600" height="1000"></canvas>
<div class="enclosed">
<canvas id="canvas" style="background: #fff"></canvas>
</div>
</div>
<!-- Scripts -->
<script src="main.js" defer></script>
</body>
</html>
173 changes: 104 additions & 69 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,143 @@
const canvas = document.getElementById("canvas");
const sizeElement = document.getElementById("size");
const colorElement = document.getElementById("color");
const clearElement = document.getElementById("clear");
const increaseButton = document.getElementById("increase");
const decreaseButton = document.getElementById("decrease");
const redoButton = document.getElementById("redo");
const undoButton = document.getElementById("undo");
const ctx = canvas.getContext("2d");
function setUpCanvas() {
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.translate(0.5, 0.5);
var sizeWidth = (100 * window.innerWidth) / 100 - 60,
sizeHeight = (100 * window.innerHeight) / 100 || 766;
canvas.width = sizeWidth;
canvas.height = sizeHeight;
canvas.style.width = sizeWidth;
canvas.style.height = sizeHeight;
}

window.onload = setUpCanvas();
const sizeEl = document.getElementById("size");
const colorEl = document.getElementById("color");
const clearEl = document.getElementById("clear");
const increaseBtn = document.getElementById("increase");
const decreaseBtn = document.getElementById("decrease");
// Resize the canvas
function resizeCanvas() {
canvas.width = window.innerWidth - 170;
canvas.height = window.innerHeight - 40;
}
window.addEventListener("resize", resizeCanvas);
resizeCanvas();

let size = 1;
let x = undefined;
let y = undefined;
let color = "black";
let isPressed = false;

canvas.addEventListener("mousedown", (e) => {
isPressed = true;

x = e.offsetX;
y = e.offsetY;
// Update the Color of the brush
colorElement.addEventListener("change", (e) => {
color = e.target.value;
});

canvas.addEventListener("mouseup", () => {
isPressed = false;

x = undefined;
y = undefined;
});
let size = 1;

// Update the size of the brush on the screen using input Field
function updateValue(e) {
const newSize = parseInt(e.target.value, 10);

// Check if parsing was successful and newSize is a valid integer
if (!isNaN(newSize)) {
size = newSize;
sizeEl.value = size;
sizeElement.value = size;
} else {
console.log("Invalid input. Please enter a numeric value.");
}
}

canvas.addEventListener("mousemove", (e) => {
if (isPressed) {
const x2 = e.offsetX;
const y2 = e.offsetY;

drawCircle(x2, y2);
drawLine(x, y, x2, y2);
x = x2;
y = y2;
}
});

function drawCircle(x, y) {
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
}

function drawLine(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.strokeStyle = color;
ctx.lineWidth = size * 2;
ctx.stroke();
}

increaseBtn.addEventListener("click", () => {
// Increase and decrease the size of the brush using buttons
increaseButton.addEventListener("click", () => {
size += 1;

if (size > 50) {
size = 50;
}
sizeEl.value = size;
sizeElement.value = size;
updateSizeOnScreen();
});

decreaseBtn.addEventListener("click", () => {
decreaseButton.addEventListener("click", () => {
size -= 1;

if (size < 1) {
size = 1;
}
sizeEl.value = size;
sizeElement.value = size;
updateSizeOnScreen();
});

colorEl.addEventListener("change", (e) => {
color = e.target.value;
// Drawing Constraints
let isDrawing = false;
let lines = [];
let undoStack = [];
let points = [];
var mouse = { x: 0, y: 0 };
var previous = { x: 0, y: 0 };

// Drawing Functionality
canvas.addEventListener("mousedown", (e) => {
isDrawing = true;
previous = { x: mouse.x, y: mouse.y };
mouse = mousePos(canvas, e);
points = [];
points.push({ x: mouse.x, y: mouse.y });
});

canvas.addEventListener("mousemove", (e) => {
if (isDrawing) {
previous = { x: mouse.x, y: mouse.y };
mouse = mousePos(canvas, e);
points.push({ x: mouse.x, y: mouse.y });
ctx.beginPath();
ctx.moveTo(previous.x, previous.y);
ctx.lineTo(mouse.x, mouse.y);
ctx.strokeStyle = color;
ctx.lineWidth = size;
ctx.lineCap = "round";
ctx.stroke();
}
});

canvas.addEventListener("mouseup", (e) => {
isDrawing = false;
lines.push(points);
console.log(lines);
});

clearEl.addEventListener("click", () => {
// Helps to clear everything on the canvas
clearElement.addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});

// Undo Redo Event Listeners
undoButton.addEventListener("click", Undo);
redoButton.addEventListener("click", redo);

// Function to find the mouse position
function mousePos(canvas, e) {
var ClientRect = canvas.getBoundingClientRect();
return {
x: Math.round(e.clientX - ClientRect.left),
y: Math.round(e.clientY - ClientRect.top),
};
}

// Function to draw the paths again on the canvas
function drawPaths() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
lines.forEach((path) => {
ctx.beginPath();
ctx.moveTo(path[0].x, path[0].y);
for (let i = 1; i < path.length; i++) {
ctx.lineTo(path[i].x, path[i].y);
}
ctx.strokeStyle = color;
ctx.lineWidth = size;
ctx.lineCap = "round";
ctx.stroke();
});
}

// Undo Redo Functionality
function Undo() {
if (lines.length === 0) return;
undoStack.push(lines.pop());
drawPaths();
}
function redo() {
if (undoStack.length === 0) return;
lines.push(undoStack.pop());
drawPaths();
}
Loading

0 comments on commit 82053f0

Please sign in to comment.