Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
<title>Drawing App</title>
</head>
<body class="flex flex-col items-center justify-center h-full py-8 px-8 w-full lg:w-4/5 xl:w-3/4 2xl:w-2/3 mx-auto">
<canvas id="canvas" class="w-full max-h-full aspect-video border-2 border-black border-solid mb-8"></canvas>
<div class="relative w-full max-h-full aspect-video border-2 border-black border-solid mb-8">
<canvas id="canvas" class="absolute w-full h-full"></canvas>
<canvas id="preview" class="absolute w-full h-full"></canvas>
</div>
<div class="w-full flex flex-row justify-between">
<div id="colors" class="grid grid-rows-2 grid-cols-4 gap-1 justify-self-start">
<button id="black" class="bg-black setting"></button>
Expand Down
57 changes: 49 additions & 8 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ import { colors } from "./colors";

const container = document.getElementById("canvas-container");
const canvas = document.getElementById("canvas");
const preview = document.getElementById("preview");
const width = 1920;
const height = 1080;

// context of the canvas
const context = canvas.getContext("2d");
context.imageSmoothingEnabled = true;
function setupCanvas(canvasToSetup) {
// context of the canvas
const context = canvasToSetup.getContext("2d");
context.imageSmoothingEnabled = true;

// resize canvas (CSS does scale it up or down)
canvas.height = height;
canvas.width = width;
// resize canvas (CSS does scale it up or down)
canvasToSetup.height = height;
canvasToSetup.width = width;
return context;
}

const context = setupCanvas(canvas);
const previewCtx = setupCanvas(preview);

function setColor(e, color) {
context.strokeStyle = colors[color];
context.fillStyle = colors[color];

previewCtx.strokeStyle = colors[color];
previewCtx.fillStyle = colors[color];

selectColor(e);
}

Expand Down Expand Up @@ -76,9 +87,11 @@ function setMode(e, mode) {
case 'path':
window.addEventListener("mousedown", startPath);
window.addEventListener("mouseup", endPath);
window.addEventListener("mousemove", updatePath);

activeEvents['mousedown'] = startPath;
activeEvents['mouseup'] = endPath;
activeEvents['mousemove'] = updatePath;
break;
case 'polygon':
window.addEventListener("mousedown", startPolygon);
Expand Down Expand Up @@ -110,6 +123,9 @@ const sizes = {

function setSize(e, size) {
context.lineWidth = size;

previewCtx.lineWidth = size;

selectSize(e);
}

Expand Down Expand Up @@ -170,12 +186,36 @@ function draw(e) {

// --- Path ---

const startPos = {
x: 0,
y: 0
}

function startPath(e) {
drawing = true;
context.beginPath();

let { x, y } = getMousePos(canvas, e);
startPos.x = x;
startPos.y = y;

draw(e)
}

function updatePath(e) {
if (!drawing) return;
clearCanvas(previewCtx);

let { x: startX, y: startY } = startPos;
let { x, y } = getMousePos(canvas, e);
console.log(startX, startY, x, y);

previewCtx.beginPath();
previewCtx.moveTo(startX, startY);
previewCtx.lineTo(x, y);
previewCtx.stroke();
}

function endPath(e) {
drawing = false;
let { x, y } = getMousePos(canvas, e);
Expand Down Expand Up @@ -242,7 +282,8 @@ function endRect(e) {

// --- Clear ---

function clearCanvas() {
function clearCanvas(context) {
console.log("Clearing canvas");
context.clearRect(0, 0, canvas.width, canvas.height);
}

Expand All @@ -262,7 +303,7 @@ function initialize() {
sizeButton.addEventListener('click', (e) => { setSize(e, sizes[sizeButton.id])} );
}

document.getElementById('clear').addEventListener('click', clearCanvas);
document.getElementById('clear').addEventListener('click', (e) => clearCanvas(context));

// set default settings
context.lineCap = 'round';
Expand Down
Loading