diff options
Diffstat (limited to 'index.js')
| -rw-r--r-- | index.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100644 index 0000000..43dc537 --- /dev/null +++ b/index.js @@ -0,0 +1,40 @@ +const canvas = document.getElementById('drawing-board'); +const ctx = canvas.getContext('2d'); + +const canvasOffsetX = canvas.offsetLeft; +const canvasOffsetY = canvas.offsetTop; + +canvas.width = window.innerWidth - canvasOffsetX; +canvas.height = window.innerHeight - canvasOffsetY; + +let isPainting = false; +let lineWidth = 5; +let startX; +let StartY; +ctx.strokeStyle = "blue"; + +const draw = (e) => { + if (!isPainting) { + return; + } + + ctx.lineWidth = lineWidth; + ctx.lineCap = 'round'; + + ctx.lineTo(e.clientX - canvasOffsetX, e.clientY - canvasOffsetY); + ctx.stroke(); + +} +canvas.addEventListener('mousedown', (e) => { + isPainting = true; + startX = e.clientX; + StartY = e.clientY; +}); + +canvas.addEventListener('mouseup', (e) => { + isPainting = false; + ctx.stroke(); + ctx.beginPath(); +}); + +canvas.addEventListener('mousemove', draw);
\ No newline at end of file |
