<canvas id='draw'/>
<script>
const canvas = document.querySelector('#draw')
const ctx = canvas.getContext('2d')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
ctx.strokeStyle = 'green'
ctx.lineJoin = 'round'
ctx.lineCap = 'round'
let isDrawing = false
let lastX = 0, lastY =0
let hue = 0, direction = true
function draw(e) {
if(!isDrawing) return
ctx.strokeStyle = `hsl(${hue},100%,50%)`
ctx.beginPath()
ctx.moveTo(lastX, lastY)
ctx.lineTo(e.offsetX, e.offsetY)
ctx.stroke()
[lastX,lastY] = [e.offsetX, e.offsetY]
hue++
if(hue>= 360){hue = 0}
if(ctx.lineWidth >= 100|| ctx.lineWidth <=1){
direction = !direction
}
if direction? ctx.linewidth++ ; ctx.linewidth--
}
canvas.addEventListener('mousemove', ()=>{
isDrawing = true;
[lastX,lastY] = [e.offsetX, e.offsetY]
})
canvas.addEventListener('mousedown', ()=> isDrawing = true)
canvas.addEventListener('mouseup', ()=> isDrawing = false)
canvas.addEventListener('mouseout', ()=> isDrawing = false)
</script>