diff --git a/Drawing App/index.html b/Drawing App/index.html new file mode 100644 index 0000000..55a27b8 --- /dev/null +++ b/Drawing App/index.html @@ -0,0 +1,22 @@ + + + + + + + Drawing App + + + +
+ + 10 + + + + +
+ + + + \ No newline at end of file diff --git a/Drawing App/script.js b/Drawing App/script.js new file mode 100644 index 0000000..e39a8c0 --- /dev/null +++ b/Drawing App/script.js @@ -0,0 +1,94 @@ +const canvas = document.getElementById('canvas'); +const increaseBtn = document.getElementById('increase'); +const decreaseBtn = document.getElementById('decrease'); +const sizeEL = document.getElementById('size'); +const colorEl = document.getElementById('color'); +const clearEl = document.getElementById('clear'); + + +const ctx = canvas.getContext('2d'); +canvas.width = 800; +canvas.height = 700; + + +let size = 10; +let isPressed = false; +colorEl.value = 'black'; +let color = colorEl.value; +let x=0; +let y=0; + +canvas.addEventListener('mousedown', (e) => { + isPressed = true + x = e.offsetX + y = e.offsetY +}) + +document.addEventListener('mouseup', (e) => { + isPressed = false + x = undefined + y = undefined +}) + +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() +} + +function updateSizeOnScreen() { + sizeEL.innerText = size +} + +increaseBtn.addEventListener('click', () => { + size += 5 + if(size > 50) { + size = 50 + } + updateSizeOnScreen() +}) + +decreaseBtn.addEventListener('click', () => { + size -= 5 + if(size < 5) { + size = 5 + } + updateSizeOnScreen() +}) + +// Saving drawing as image +let saveBtn = document.querySelector(".save") +saveBtn.addEventListener("click", () => { + let data = canvas.toDataURL("imag/png") + let a = document.createElement("a") + a.href = data + // what ever name you specify here + // the image will be saved as that name + a.download = "sketch.png" + a.click() +}) + +colorEl.addEventListener('change', (e) => color = e.target.value) + +clearEl.addEventListener('click', () => ctx.clearRect(0,0, canvas.width, canvas.height)) diff --git a/Drawing App/style.css b/Drawing App/style.css new file mode 100644 index 0000000..81b35ef --- /dev/null +++ b/Drawing App/style.css @@ -0,0 +1,52 @@ +@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap'); + +* { + box-sizing: border-box; +} + +body { + background-color: #f5f5f5; + font-family: 'Roboto', sans-serif; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + height: 100vh; + margin: 0; +} + +canvas { + border: 2px solid steelblue; + cursor : crosshair; +} + +.toolbox { + background-color: steelblue; + border: 1px solid slateblue; + display: flex; + width: 804px; + padding: 1rem; +} + +.toolbox > * { + background-color: #fff; + border: none; + display: inline-flex; + align-items: center; + justify-content: center; + font-size: 2rem; + height: 50px; + width: 120px; + margin: 0.25rem; + padding: 0.25rem; + cursor: pointer; +} + +.save{ + font-weight: bold; + background-color: #0234ed; +} + +.toolbox > *:last-child { + margin-left: auto; +} \ No newline at end of file