This canvas have been creating an error since I created it and I haven't found a solution after all my research. so I need a hand. I need the canvas to create a straight line but it end up creating multiple straight lines from the starting point when the mouse is moving instead of one straight line.
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]> <html class="no-js"> <!--<![endif]-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
</head>
<style>
*{
box-sizing: border-box;
}
body,html{
padding: 0;
margin: 0;
height: 100vh;
width: 100vw;
}
</style>
<body>
<canvas>
</canvas>
<script>
var colorLine = 'black'
var fillLine = 'none'
var prevX = ''
var prevY = ''
var isDrawing = false
var snapshot;
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
let backgroundSetUp = () => {
canvas.height = window.innerHeight - 50;
canvas.width = window.innerWidth - 50;
ctx.fillStyle = colorLine;
ctx.fillStyle = fillLine
};
let startDrawing = e => {
isDrawing = true
prevX = e.offsetX
prevY = e.offsetY
ctx.beginPath();
ctx.strokeStyle = 'black'
ctx.fillStyle = '#fff'
}
let Drawing = e => {
if(isDrawing === false)
return
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke()
}
let stopDrawing = e => {
isDrawing = false
}
canvas.onmousedown = ( e => {
startDrawing(e)
})
canvas.onmousemove = ( e => {
Drawing(e)
})
canvas.onmouseup = ( e => {
stopDrawing(e)
})
backgroundSetUp()
</script>
</body>
</html>