How to locate a touch's x and y position in a canvas.

80 views
Skip to first unread message

SORCURIS

unread,
Jun 27, 2021, 11:34:02 PM6/27/21
to DroidScript
Say I have a canvas, which is an image with a source of null.

Upon touching the canvas, is there any way to return the X and Y position? Or its raw position in that plane?

Steve Garman

unread,
Jun 28, 2021, 1:12:09 AM6/28/21
to DroidScript
function OnStart()
{
   lay = app.CreateLayout("linear", "VCenter, FillXY")

   txt = app.AddText(lay, "waiting\n",-1,-1,"multiline")
   img = app.AddImage( lay,null,0.8, 0.8 )
   img.SetBackColor( "#cc22cc" )
   img.SetOnTouchDown(img_OnTouch)

   app.AddLayout(lay)
}

function img_OnTouch( ev )
{
   var msg = ev.X + "\n" + ev.Y
   txt.SetText( msg )

Steve Garman

unread,
Jun 28, 2021, 3:23:41 AM6/28/21
to DroidScript
Or if you are working with an HTML5 canvas you may want to look at Touch_events
Reply all
Reply to author
Forward
0 new messages