2

Writing a simple script to have two buttons. One button animates a "painting" feature where rectangles follow the cursor around the canvas. The other button would display a rectangle that follows the canvas but doesn't leave a trail like the other. I have the buttons linked to do different functions. Right now the follow button doesn't work, it does clear the canvas but it still allows you to paint. It seems that the paint function is still running after I hit the follow button. Any help is appreciated.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas</title>
</head>
<body>
<input type="button" value="Paint" onclick="isPaint()">
<input type="button" value="Follow" onclick="isFollow()">
<canvas id="myCanvas" width="500" height="500"></canvas>

<script>

function isPaint(){

var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');

//change draw color
ctx.fillStyle = "#FF0000";


ctx.clearRect(0,0,500,500);

canvas.addEventListener("mousemove", function(event) {
    ctx.fillRect(event.x,event.y,10,10);    
})
}


function isFollow(){

var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');

//change draw color
ctx.fillStyle = "#FF0000";

ctx.clearRect(0,0,500,500);

    }

</script>
</body>
</html>
2
  • You clearly know how to use addEventListener, not read up on removeEventlistener, and remove the inline javascript and you'll figure it out. Commented Apr 10, 2014 at 15:34
  • Hard to read up when I didn't know what was causing it. Everyone starts somewhere. Thanks for the help though Commented Apr 10, 2014 at 15:41

3 Answers 3

1

Work for me : http://jsfiddle.net/XF7m4/1/

Using document.getElementById("paint").onclick= function (){

PS :And think to remove the mousemouse listener ;)

Sign up to request clarification or add additional context in comments.

2 Comments

When I use that the follow still paints, when all its currently set to do is clear the canvas
Thank you for your help as well. Its starting to make sense now :)
1

To achieve what you are trying to do you will need to enhance your code a little more.

The main idea is to bind the mousemove event only once and clear the canvas if it is to behave like a follow.

Try using the following code:

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Canvas</title>
    </head>
    <body>
        <input type="button" value="Paint" onclick="CanvasPainter.paint()" />
        <input type="button" value="Follow" onclick="CanvasPainter.follow()" />
        <canvas id="myCanvas" width="500" height="500"></canvas>

        <script type="text/javascript">

        (function () {

            var canvas = document.getElementById('myCanvas');
            var ctx = canvas.getContext('2d');

            //change draw color
            ctx.fillStyle = "#FF0000";


            var CanvasPainter = {
                isPaint: false,

                isFollow: false,

                paint: function () {
                    this.isPaint = true;
                    this.isFollow = false;
                    return;
                },

                follow: function () {
                    this.isPaint = false;
                    this.isFollow = true;
                    return;
                }
            };

            window.CanvasPainter = CanvasPainter;

            canvas.addEventListener("mousemove", function (event) {
                if (CanvasPainter.isPaint || CanvasPainter.isFollow) {

                    if (CanvasPainter.isFollow) {
                        // Clear canvas on every mousemove if it is a follow
                        ctx.clearRect(0, 0, 500, 500);
                    }

                    ctx.fillRect(event.x, event.y, 10, 10);
                }
                return;
            });

            return;
        })();
        </script>
    </body>
</html>

Hope this helps!

3 Comments

Thank you very much for this! This way makes more sense. I wanted an if statement but wasn't sure how to go about it. Thanks again
@Arthur it was made by modifying the OP's code even if it is only a little.
@user2980869 You're most welcome! However I wonder why you didn't mark this as the answer. :)
0

Your isPaint() function is adding an event listener for "mousemove" to the canvas, but it isn't getting removed in the isFollow() function. If you remove your listener in the isFollow() function this should fix your problem.

1 Comment

Looking this up now. Thank you for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.