Ball follows mouse with delay canvas












0















So I am kinda beginner here and decided to try and learn working with canvas a little bit. I followed some tutorials and managed to make a 'ball' to follow my mouse. And it works. However there is a delay. When i move my mouse the 'balls' follows my mouse but with delay. It is always late a little bit. So my question is... Is this is how it is supposed to be (that how canvas work and there is no way around it)m or I am doing something wrong.



Here is my javascript code:



let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function (event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;
function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();


My HTML:



<body>
<canvas></canvas>
<script src="javascript.js"></script>
</body>





let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

<canvas></canvas>





Any help would be greatly appreciated.



P.S. Updated as requested. This is my entire code.










share|improve this question

























  • Please post a complete, working example that demonstrates the problem.

    – ggorlen
    Nov 23 '18 at 4:19






  • 1





    Have you see this answer?

    – Starfight
    Nov 23 '18 at 10:44






  • 1





    Displaying the mouse position will always be one frame behind, there is nothing you can do to change that. You can however hide the mouse element.style.cursor = "none" when you drag an object and that removes the perceptual appearance of the delay.

    – Blindman67
    Nov 23 '18 at 11:05











  • Ok my man, thanks for the response.

    – Happy Coconut
    Nov 23 '18 at 11:11
















0















So I am kinda beginner here and decided to try and learn working with canvas a little bit. I followed some tutorials and managed to make a 'ball' to follow my mouse. And it works. However there is a delay. When i move my mouse the 'balls' follows my mouse but with delay. It is always late a little bit. So my question is... Is this is how it is supposed to be (that how canvas work and there is no way around it)m or I am doing something wrong.



Here is my javascript code:



let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function (event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;
function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();


My HTML:



<body>
<canvas></canvas>
<script src="javascript.js"></script>
</body>





let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

<canvas></canvas>





Any help would be greatly appreciated.



P.S. Updated as requested. This is my entire code.










share|improve this question

























  • Please post a complete, working example that demonstrates the problem.

    – ggorlen
    Nov 23 '18 at 4:19






  • 1





    Have you see this answer?

    – Starfight
    Nov 23 '18 at 10:44






  • 1





    Displaying the mouse position will always be one frame behind, there is nothing you can do to change that. You can however hide the mouse element.style.cursor = "none" when you drag an object and that removes the perceptual appearance of the delay.

    – Blindman67
    Nov 23 '18 at 11:05











  • Ok my man, thanks for the response.

    – Happy Coconut
    Nov 23 '18 at 11:11














0












0








0








So I am kinda beginner here and decided to try and learn working with canvas a little bit. I followed some tutorials and managed to make a 'ball' to follow my mouse. And it works. However there is a delay. When i move my mouse the 'balls' follows my mouse but with delay. It is always late a little bit. So my question is... Is this is how it is supposed to be (that how canvas work and there is no way around it)m or I am doing something wrong.



Here is my javascript code:



let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function (event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;
function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();


My HTML:



<body>
<canvas></canvas>
<script src="javascript.js"></script>
</body>





let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

<canvas></canvas>





Any help would be greatly appreciated.



P.S. Updated as requested. This is my entire code.










share|improve this question
















So I am kinda beginner here and decided to try and learn working with canvas a little bit. I followed some tutorials and managed to make a 'ball' to follow my mouse. And it works. However there is a delay. When i move my mouse the 'balls' follows my mouse but with delay. It is always late a little bit. So my question is... Is this is how it is supposed to be (that how canvas work and there is no way around it)m or I am doing something wrong.



Here is my javascript code:



let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function (event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;
function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();


My HTML:



<body>
<canvas></canvas>
<script src="javascript.js"></script>
</body>





let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

<canvas></canvas>





Any help would be greatly appreciated.



P.S. Updated as requested. This is my entire code.






let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

<canvas></canvas>





let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

<canvas></canvas>






javascript html5-canvas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 4:47









Tamil Selvan C

14.5k93659




14.5k93659










asked Nov 23 '18 at 4:15









Happy CoconutHappy Coconut

2391416




2391416













  • Please post a complete, working example that demonstrates the problem.

    – ggorlen
    Nov 23 '18 at 4:19






  • 1





    Have you see this answer?

    – Starfight
    Nov 23 '18 at 10:44






  • 1





    Displaying the mouse position will always be one frame behind, there is nothing you can do to change that. You can however hide the mouse element.style.cursor = "none" when you drag an object and that removes the perceptual appearance of the delay.

    – Blindman67
    Nov 23 '18 at 11:05











  • Ok my man, thanks for the response.

    – Happy Coconut
    Nov 23 '18 at 11:11



















  • Please post a complete, working example that demonstrates the problem.

    – ggorlen
    Nov 23 '18 at 4:19






  • 1





    Have you see this answer?

    – Starfight
    Nov 23 '18 at 10:44






  • 1





    Displaying the mouse position will always be one frame behind, there is nothing you can do to change that. You can however hide the mouse element.style.cursor = "none" when you drag an object and that removes the perceptual appearance of the delay.

    – Blindman67
    Nov 23 '18 at 11:05











  • Ok my man, thanks for the response.

    – Happy Coconut
    Nov 23 '18 at 11:11

















Please post a complete, working example that demonstrates the problem.

– ggorlen
Nov 23 '18 at 4:19





Please post a complete, working example that demonstrates the problem.

– ggorlen
Nov 23 '18 at 4:19




1




1





Have you see this answer?

– Starfight
Nov 23 '18 at 10:44





Have you see this answer?

– Starfight
Nov 23 '18 at 10:44




1




1





Displaying the mouse position will always be one frame behind, there is nothing you can do to change that. You can however hide the mouse element.style.cursor = "none" when you drag an object and that removes the perceptual appearance of the delay.

– Blindman67
Nov 23 '18 at 11:05





Displaying the mouse position will always be one frame behind, there is nothing you can do to change that. You can however hide the mouse element.style.cursor = "none" when you drag an object and that removes the perceptual appearance of the delay.

– Blindman67
Nov 23 '18 at 11:05













Ok my man, thanks for the response.

– Happy Coconut
Nov 23 '18 at 11:11





Ok my man, thanks for the response.

– Happy Coconut
Nov 23 '18 at 11:11












1 Answer
1






active

oldest

votes


















1














The best you can do is remove the perceptual delay, which is only apparent because you have the mouse and the moving object visible at the same time. The delay is very small and without the cursor people just don't notice.



The example just hides the mouse when it is over the canvas.



Note that you should only hide the cursor is you have something else to represent the mouse position.






let canvas = document.querySelector('canvas');
let c = canvas.getContext('2d');

canvas.style.cursor = "none";

canvas.width = innerWidth-40;
canvas.height = innerHeight-40;

let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

addEventListener('mousemove', function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
})

function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;

this.update = function() {
// eventualy some other code here...
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
};
};

let ball;

function init() {
ball = new Ball(mouse.x, mouse.y, 30, 'red');
};

function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
ball.x = mouse.x;
ball.y = mouse.y;
ball.update();
};

init();
animate();

<canvas></canvas>








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440579%2fball-follows-mouse-with-delay-canvas%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The best you can do is remove the perceptual delay, which is only apparent because you have the mouse and the moving object visible at the same time. The delay is very small and without the cursor people just don't notice.



    The example just hides the mouse when it is over the canvas.



    Note that you should only hide the cursor is you have something else to represent the mouse position.






    let canvas = document.querySelector('canvas');
    let c = canvas.getContext('2d');

    canvas.style.cursor = "none";

    canvas.width = innerWidth-40;
    canvas.height = innerHeight-40;

    let mouse = {
    x: innerWidth / 2,
    y: innerHeight / 2
    };

    addEventListener('mousemove', function(event) {
    mouse.x = event.clientX;
    mouse.y = event.clientY;
    })

    function Ball(x, y, radius, color) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color;

    this.update = function() {
    // eventualy some other code here...
    this.draw();
    };

    this.draw = function() {
    c.beginPath();
    c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
    c.fillStyle = this.color;
    c.fill();
    c.closePath();
    };
    };

    let ball;

    function init() {
    ball = new Ball(mouse.x, mouse.y, 30, 'red');
    };

    function animate() {
    requestAnimationFrame(animate);
    c.clearRect(0, 0, canvas.width, canvas.height);
    ball.x = mouse.x;
    ball.y = mouse.y;
    ball.update();
    };

    init();
    animate();

    <canvas></canvas>








    share|improve this answer




























      1














      The best you can do is remove the perceptual delay, which is only apparent because you have the mouse and the moving object visible at the same time. The delay is very small and without the cursor people just don't notice.



      The example just hides the mouse when it is over the canvas.



      Note that you should only hide the cursor is you have something else to represent the mouse position.






      let canvas = document.querySelector('canvas');
      let c = canvas.getContext('2d');

      canvas.style.cursor = "none";

      canvas.width = innerWidth-40;
      canvas.height = innerHeight-40;

      let mouse = {
      x: innerWidth / 2,
      y: innerHeight / 2
      };

      addEventListener('mousemove', function(event) {
      mouse.x = event.clientX;
      mouse.y = event.clientY;
      })

      function Ball(x, y, radius, color) {
      this.x = x;
      this.y = y;
      this.radius = radius;
      this.color = color;

      this.update = function() {
      // eventualy some other code here...
      this.draw();
      };

      this.draw = function() {
      c.beginPath();
      c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
      c.fillStyle = this.color;
      c.fill();
      c.closePath();
      };
      };

      let ball;

      function init() {
      ball = new Ball(mouse.x, mouse.y, 30, 'red');
      };

      function animate() {
      requestAnimationFrame(animate);
      c.clearRect(0, 0, canvas.width, canvas.height);
      ball.x = mouse.x;
      ball.y = mouse.y;
      ball.update();
      };

      init();
      animate();

      <canvas></canvas>








      share|improve this answer


























        1












        1








        1







        The best you can do is remove the perceptual delay, which is only apparent because you have the mouse and the moving object visible at the same time. The delay is very small and without the cursor people just don't notice.



        The example just hides the mouse when it is over the canvas.



        Note that you should only hide the cursor is you have something else to represent the mouse position.






        let canvas = document.querySelector('canvas');
        let c = canvas.getContext('2d');

        canvas.style.cursor = "none";

        canvas.width = innerWidth-40;
        canvas.height = innerHeight-40;

        let mouse = {
        x: innerWidth / 2,
        y: innerHeight / 2
        };

        addEventListener('mousemove', function(event) {
        mouse.x = event.clientX;
        mouse.y = event.clientY;
        })

        function Ball(x, y, radius, color) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.color = color;

        this.update = function() {
        // eventualy some other code here...
        this.draw();
        };

        this.draw = function() {
        c.beginPath();
        c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
        c.fillStyle = this.color;
        c.fill();
        c.closePath();
        };
        };

        let ball;

        function init() {
        ball = new Ball(mouse.x, mouse.y, 30, 'red');
        };

        function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);
        ball.x = mouse.x;
        ball.y = mouse.y;
        ball.update();
        };

        init();
        animate();

        <canvas></canvas>








        share|improve this answer













        The best you can do is remove the perceptual delay, which is only apparent because you have the mouse and the moving object visible at the same time. The delay is very small and without the cursor people just don't notice.



        The example just hides the mouse when it is over the canvas.



        Note that you should only hide the cursor is you have something else to represent the mouse position.






        let canvas = document.querySelector('canvas');
        let c = canvas.getContext('2d');

        canvas.style.cursor = "none";

        canvas.width = innerWidth-40;
        canvas.height = innerHeight-40;

        let mouse = {
        x: innerWidth / 2,
        y: innerHeight / 2
        };

        addEventListener('mousemove', function(event) {
        mouse.x = event.clientX;
        mouse.y = event.clientY;
        })

        function Ball(x, y, radius, color) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.color = color;

        this.update = function() {
        // eventualy some other code here...
        this.draw();
        };

        this.draw = function() {
        c.beginPath();
        c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
        c.fillStyle = this.color;
        c.fill();
        c.closePath();
        };
        };

        let ball;

        function init() {
        ball = new Ball(mouse.x, mouse.y, 30, 'red');
        };

        function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);
        ball.x = mouse.x;
        ball.y = mouse.y;
        ball.update();
        };

        init();
        animate();

        <canvas></canvas>








        let canvas = document.querySelector('canvas');
        let c = canvas.getContext('2d');

        canvas.style.cursor = "none";

        canvas.width = innerWidth-40;
        canvas.height = innerHeight-40;

        let mouse = {
        x: innerWidth / 2,
        y: innerHeight / 2
        };

        addEventListener('mousemove', function(event) {
        mouse.x = event.clientX;
        mouse.y = event.clientY;
        })

        function Ball(x, y, radius, color) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.color = color;

        this.update = function() {
        // eventualy some other code here...
        this.draw();
        };

        this.draw = function() {
        c.beginPath();
        c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
        c.fillStyle = this.color;
        c.fill();
        c.closePath();
        };
        };

        let ball;

        function init() {
        ball = new Ball(mouse.x, mouse.y, 30, 'red');
        };

        function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);
        ball.x = mouse.x;
        ball.y = mouse.y;
        ball.update();
        };

        init();
        animate();

        <canvas></canvas>





        let canvas = document.querySelector('canvas');
        let c = canvas.getContext('2d');

        canvas.style.cursor = "none";

        canvas.width = innerWidth-40;
        canvas.height = innerHeight-40;

        let mouse = {
        x: innerWidth / 2,
        y: innerHeight / 2
        };

        addEventListener('mousemove', function(event) {
        mouse.x = event.clientX;
        mouse.y = event.clientY;
        })

        function Ball(x, y, radius, color) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.color = color;

        this.update = function() {
        // eventualy some other code here...
        this.draw();
        };

        this.draw = function() {
        c.beginPath();
        c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
        c.fillStyle = this.color;
        c.fill();
        c.closePath();
        };
        };

        let ball;

        function init() {
        ball = new Ball(mouse.x, mouse.y, 30, 'red');
        };

        function animate() {
        requestAnimationFrame(animate);
        c.clearRect(0, 0, canvas.width, canvas.height);
        ball.x = mouse.x;
        ball.y = mouse.y;
        ball.update();
        };

        init();
        animate();

        <canvas></canvas>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 11:11









        Blindman67Blindman67

        27.1k52764




        27.1k52764
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440579%2fball-follows-mouse-with-delay-canvas%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]