Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8249821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:40:23+00:00 2026-06-07T23:40:23+00:00

I made an little test to check out HTML 5. In my test I

  • 0

I made an little test to check out HTML 5.

In my test I have a <canvas> and want to drag this one.
But I have a little problem, my canvas is kinda shaking.

Could someone help me plz?

Fiddle:
http://jsfiddle.net/wnxFK/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:40:25+00:00Added an answer on June 7, 2026 at 11:40 pm

    Calculating layerX and layerY once at the start of the drag helps create a smoother effect:

    http://jsfiddle.net/jB5YN/

    So at the start you have:

    var mousedown = false;
    
    var layerX = 0;
    var layerY = 0;
    
    function onMouseDown(event)
    {
        var canvas = document.getElementById("myCanvas");
        mousedown = true;
    
        layerX  = event.layerX;
        layerY  = event.layerY;
    
        // Code
        drag(event);
    }
    

    And when you come to drag you have:

    function drag(event)
    {
         var clientX = ('clientX' in event) ? (event.clientX) : (event.offsetX);
         var clientY = ('clientY' in event) ? (event.clientY) : (event.offsetY);
    
         var canvas = document.getElementById("myCanvas");
         var context = canvas.getContext('2d');
    
         var x = (event.clientX- layerX);
                var y = (event.clientY- layerY);
    
         var log = document.getElementById("log");
         log.innerHTML = "x: " + x + " y: " + y;
    
         canvas.style.left = x + 'px';
         canvas.style.top = y + 'px';
    }
    

    Also, I’ve moved the events to hook up only once at the start, just feels a bit neater to me!

    Update

    Just for a complete answer, if you want to avoid layerX and layerY which seem to be in the process of being deprecated in webkit, you can calculate them yourself:

    http://jsfiddle.net/B3TYe/

    The change being:

    var offsetX= 0;
    var offsetY= 0;
    
    function onMouseDown(event)
    {
        var canvas = document.getElementById("myCanvas");
        mousedown = true;
    
        var left = (canvas.style.left=="")? 0:parseInt(canvas.style.left);
        var top = (canvas.style.top=="")? 0:parseInt(canvas.style.top);
    
        offsetX = event.pageX - left;
        offsetY = event.pageY - top;
    
        // Code
        drag(event);
    }
    

    Andy

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little test-case which is supposed to show two widgets, with one
I made a little example of my problem here: http://peterbriers.be/test/float_html5.html As you can see,
Please excuse the simplicity and length of this, but I have a little test
I have made little test (with Jsoup 1.6.1): String s = +Jsoup.parse(<td></td>).select(td).size(); System.out.println(Selected elements
Can I get a little help with this? In a test project, I have
I made some little programs with Swing-components in JRuby. Now I want to convert
I made this little function from code snippets around the net. It does what
I've made a little forum and I want parse the date on newest posts
Made this nice little loop for hiding and showing div's, works as a charm
I have made a YUI module a little like the code shown below, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.