Results 1 to 2 of 2

Thread: Easiest way to move canvas in JS?

  1. #1
    Join Date
    Feb 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Easiest way to move canvas in JS?

    I've made a tool (realruler.com) mostly for fun as my first attempt to code something relatively useful in JS.
    I've also use w3css to make things responsive on various screen sizes.

    The ruler in the upper part of the screen is basically coded in java script as a bunch of lines drawn on canvas. There's a special function which draws these lines along with ticks and everything else.
    Now, let's just say I'd like to make this canvas "draggable" with mouse? Nothing fancy, just to be able to move it a bit up or down.

    How to achieve that the easiest way?

    Thanks a lot in advance.
    Last edited by Beverleyh; 02-12-2016 at 06:16 AM. Reason: Formatting

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This question implies that this is done all the time and it is, just not with canvas as far as I know. However, your canvas is in a div, so that can be moved about easily enough in response to mouse or finger gestures. But, unless there already is a drag and drop api for canvas, which I doubt, I would think one would need to determine whether or not on touchstart/mousedown the coordinates were over the ruler and then, if so, until touchend/mouseup or for some reason (going offscreen somehow, as is possible, though that might want to be dealt with depending upon what ensues after that) tracking the movement of the pointing device and translating that into repositioning of this parent div, which would need to be positioned either absolutely or fixed for this to work. A placeholder div of the same or similar dimensions to that occupied by the ruler would also be needed to preserve the current layout, as once an element moves, unless it is fixed or absolute, everything else around it will also move, and unless there is a placeholder, it will initially be on top of something else in the layout.

    That's sort of how i would go about it from scratch. Before we go down that road, see if this might work for you:

    http://www.dynamicdrive.com/dynamici...drag/index.htm

    You can even try applying it to the canvas element itself, though I think it may have to be used on the canvas element's parent division.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    mlegg (02-19-2016)

Similar Threads

  1. Replies: 1
    Last Post: 09-17-2008, 11:31 PM
  2. Might be the easiest thing ever...
    By ampersand12 in forum Looking for such a script or service
    Replies: 1
    Last Post: 03-25-2008, 10:00 PM
  3. Replies: 6
    Last Post: 02-28-2008, 04:34 AM
  4. Replies: 1
    Last Post: 05-25-2007, 03:39 PM
  5. Which Server Script is the Easiest?
    By Rockonmetal in forum Looking for such a script or service
    Replies: 13
    Last Post: 02-26-2007, 04:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •