How do I specify mouse position over DrawingCanvas?

0 favourites
  • 6 posts
From the Asset Store
Vector illustration of a UFO with an alien in three different positions.
  • I'm making a rgb color eyedropper tool.

    The workflow is as follows:

    1. Have a picture in the form of a sprite.
    2. Set DrawingCanvas size and position to that of the sprite.
    3. Paste picture into the DrawingCanvas.
    4. Save snapshot of the DrawingCanvas.
    5. When ready, on mouse click I do "snapshotRedAt(mouse.x,mouse.y)

    That returns nothing when I click on the canvas, yet gives some readings when clicking outside the canvas which probably means that the coordinates for snapshotRedAt need to be relative?

    How do I then provide mouse coordinates for snapshotRedAt correctly in order to read pixel at that precise spot (independently of where the canvas is located in the layout?)

    thanks!

  • Try this:

    snapshotRedAt(mouse.x-DrawingCanvas.BBoxLeft,mouse.y-DrawingCanvas.BBoxTop)

    Also, check out this demo I made some time ago:

    howtoconstructdemos.com/hsv-color-picker-tool-c3p

  • Thanks dop2000, that seems to be working.

    It still however only reads pixels in a fragment of the canvas. Upon logging things to the console it seems that for some reason the snapshot has different dimensions than the DrawingCanvas?

    //dimensions of sprite and canvas after pasting sprite (x and y)
    Dimensions of picture:768 511
    dimensions of DrawingCanvas: 768 511
    //dimensions of snapshot (inside "on snapshot")
    snapshot width: 339 snapshot height:226
    
    

    Any idea why would that happen?

  • Yeah, snapshot dimensions may be different, you need to take this into account:

    construct.net/en/make-games/manuals/construct-3/plugin-reference/drawing-canvas

    So I'm guessing you need (mouse.x-DrawingCanvas.BBoxLeft)/DrawingCanvas.PixelScale

    or possibly multiply :)

  • Yeah I ended up dividing by relation between DrawingCanvas width and Snapshot width which gets the proportions in order (got that from your capx)... but that's such a random problem to have o_O.

    Thanks!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Be aware, that when the window size changes (in "On resolution change" event), you might need to redraw the canvas and regenerate the snapshot.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)