var d = document.getElementById('draw');
d.addEventListener('mousedown', function(e) {
ctx = d.getContext("2d");
var flag = true;
ctx.beginPath();
ctx.lineWidth = "10"
ctx.strokeStyle = "green";
ctx.moveTo(e.clientX, e.clientY);
d.addEventListener('mousemove', function(e) {
if (flag == true) {
ctx.lineTo(e.clientX, e.clientY);
ctx.stroke();
}
d.addEventListener('mouseup', function(e) {
flag = false;
});
});
});
I'm doing 30 days javascript challenge and have a problem with respect to the above code. The addEventListener function('mousemove') can recognize ctx object. I thought the function cannot recognize ctx since no arguments have been passed to the function.
If I take the function out of the outer function(mousedown) and then an error occurs.
Any help would be appreciated.
Thanks