Draw a line code not working

I used my code on both fast and slow movement and I always get a continual line.  I even built for device and got the same result so I cannot explain your results… as even a slow machine would simply draw a line from the last known point to the latest point (skipping intermediary points) with my code.  i.e. you would lose detail but still would have an continuous line.

I would copy Photoshop and draw an actual brush (i.e. an png) but your project just got way more complicated.

I will need to write code that stores the point data in a table as the line is drawn

This is not done for you so you will need to store the point data and write it to a file.

why you’ve used a “return false”

I should have done this before:

-- always local to avoid global references local group, pt, line = display.newGroup(), {}, nil -- you can get other math functions here: https://gist.github.com/HoraceBury/9431861 local function length( ax, ay, bx, by ) local width, height = bx-ax, by-ay return (width\*width + height\*height)^0.5 end function group:touch(event) if (event.phase == "began") then -- keep track of what has the touch input group.hasFocus = true display.currentStage:setFocus( group ) -- we're not storing every point, but that would be easy (see my first listing) pt = { x=event.x, y=event.y } -- we always report that we capture the began phase return true -- only deal with moved, ended or cancelled phases if this object is capturing the touch input elseif (group.hasFocus) then -- almost every touch will be a moved phase if (event.phase == "moved") then if (length( pt.x, pt.y, event.x, event.y ) \> 1) then -- again, could store every input point pt = { x=event.x, y=event.y } if (line == nil) then line = display.newLine( event.xStart, event.yStart, pt.x, pt.y ) line:setStrokeColor (1,1,1) line.strokeWidth = 20 else line:append( pt.x, pt.y ) end end else -- effectively ended and cancelled phases... -- we no longer want to track which object is taking the input and the stage should allow other objects to get the touch events group.hasFocus = false display.currentStage:setFocus( nil ) end -- moved and cancelled events were handled by this object, so return true return true end -- here we have not handled any phase because hasFocus was false, so this touch event was not ours, so return false return false end Runtime:addEventListener( "touch", group )

I should point out that if this were a multitouch environment, you would want to be identifying the Touch ID at the same time as checking the hasFocus (or instead of) upon the object receiving the event, so as to discard any events the object is not interested in.

TL;DR: The ‘return true’ and ‘return false’ tell the system that we handled or did not handle the event (respectively) and so to stop or continue (respectively) passing the event down the list of objects handling touches.

Redrawing the line completely is necessary when the whole line changes. You might be smoothing the line and so the input points would not even be the points generated from touches, but calculated using a bezier curve (as mine are). Drawing the initial line could be directly rendered, but then remove the line and smooth it when drawing is finished, for example.

I’m still confused as to why SGS is not seeing the breaking lines.

In my simplified code all “moved” is doing is appending points to a line. Logically the only thing that would break this code is a break in the touch event chain causing an “ended” and a new “began”.  This would be simple to test with a print in non “moved” phases.

I was using a 480x800 canvas on an HD device but screen resolution shouldn’t really be the issue.

Hey SGS - I added some print commands to your code and played around with it a bit more on my machine:

[lua]local line = nil

touchListener = function(event)

  if event.phase == “moved” then

    print(“moved”)

    if line then

  print(“append”, event.x, event.y)

      line:append(event.x,event.y)

    else

      print(“began”, event.xStart, event.yStart, event.x, event.y)

      line = display.newLine(event.xStart, event.yStart, event.x, event.y)

      line:setStrokeColor (1,1,1)

      line.strokeWidth = 20

    end

  elseif event.phase == “ended” then

    print(“ended”)

    line:append(event.x,event.y)

  end

  return true

end

Runtime:addEventListener(“touch”, touchListener)[/lua]

I can’t say I’ve figured out what the issue is but I have two observations when I look at what’s been drawn on the simulator screen and what’s been printed to the console. Here is one example where I pressed down the mouse, moved the mouse (pretty quickly) and then released the mouse:

Jun 06 08:57:39.339 moved began 150 86 150 86 Jun 06 08:57:39.363 moved append 150 86 Jun 06 08:57:39.393 moved append 150 86 Jun 06 08:57:39.394 moved append 150 86 moved append 150 86 Jun 06 08:57:39.397 moved append 150 86 Jun 06 08:57:39.425 moved append 150 86 Jun 06 08:57:39.425 moved append 150 86 moved append 150 86 Jun 06 08:57:39.429 moved append 150 86 Jun 06 08:57:39.456 moved append 150 86 Jun 06 08:57:39.456 moved append 150 86 moved append 150 86 Jun 06 08:57:39.462 moved append 150 86 Jun 06 08:57:39.490 moved append 150 86 Jun 06 08:57:39.491 moved append 150 86 moved append 150 86 Jun 06 08:57:39.494 moved append 150 86 Jun 06 08:57:39.515 moved Jun 06 08:57:39.515 append 150 86 moved append 150 86 Jun 06 08:57:39.549 moved append 150 86 Jun 06 08:57:39.549 moved append 150 86 moved append 150 86 Jun 06 08:57:39.553 moved Jun 06 08:57:39.553 append 150 86 Jun 06 08:57:39.560 moved append 150 86 Jun 06 08:57:39.591 moved append 150 86 Jun 06 08:57:39.592 moved append 150 86 moved append 152 88 moved append 152 88 moved append 154 92 Jun 06 08:57:39.596 moved append 154 92 Jun 06 08:57:39.596 moved append 156 96 Jun 06 08:57:39.623 moved append 156 96 Jun 06 08:57:39.624 moved append 158 100 moved append 158 100 moved append 160 102 moved append 160 102 moved append 162 106 Jun 06 08:57:39.628 moved Jun 06 08:57:39.628 append 162 106 moved append 162 110 Jun 06 08:57:39.646 moved append 162 110 Jun 06 08:57:39.646 moved append 164 114 moved append 164 114 moved append 166 118 Jun 06 08:57:39.686 moved append 168 128 Jun 06 08:57:39.686 moved append 168 128 moved append 170 132 moved append 170 132 moved append 170 142 moved append 170 142 moved append 172 156 Jun 06 08:57:39.691 moved append 172 156 Jun 06 08:57:39.691 moved append 172 168 Jun 06 08:57:39.711 moved append 172 168 Jun 06 08:57:39.711 moved append 172 182 moved append 172 182 moved append 174 188 Jun 06 08:57:39.741 moved append 174 188 Jun 06 08:57:39.741 moved append 174 202 moved append 174 202 moved append 174 214 moved append 174 214 moved append 176 228 moved append 176 228 moved append 178 238 Jun 06 08:57:39.747 moved append 178 238 Jun 06 08:57:39.770 moved append 178 244 Jun 06 08:57:39.771 moved append 178 244 moved append 180 250 moved append 180 250 moved append 180 258 Jun 06 08:57:39.775 moved append 180 258 Jun 06 08:57:39.775 moved append 180 262 Jun 06 08:57:39.779 moved append 180 262 Jun 06 08:57:39.798 moved append 180 264 Jun 06 08:57:39.799 moved append 180 264 moved append 180 266 moved append 180 266 moved append 180 266 Jun 06 08:57:39.804 ended

Here is what was drawn on the simulator screen: 

<Hmmm … when I try to insert my screenshot in this post I keep getting the error ‘You are not allowed to use that image extension on this community’ no matter whether I use png or jpeg. Not sure why that is or what to do about it … >

So what was drawn on the screen is just a small triangle, and when I look at the console output I see lots of repetitions, i.e. identical x-y-pairs being appended, starting with the x-y-pair “156 86”.

What this makes me wonder is whether the Corona append function is buggy and doesn’t work well when you keep appending the same point to a line. I realise though that this doesn’t explain why the code seems to be working fine on your machine.

OK there is definitely something with your setup

If I run the same thing I get

22:20:17.032 began 198.33332824707 342.5 198.33332824707 340.83331298828 22:20:17.079 append 199.16665649414 339.16665649414 22:20:17.095 append 199.16665649414 338.33331298828 22:20:17.111 append 199.16665649414 337.5 22:20:17.111 append 199.16665649414 336.66665649414 22:20:17.142 append 200 335.83331298828 22:20:17.142 append 200 334.16665649414 22:20:17.157 append 200.83332824707 333.33331298828

You should never get a moved event if you haven’t moved.  As you can see mine fires an event if either x or y has moved by 1px (which is why IMHO that checking the move is more than 1px is not needed).  Also I am showing fractional whereas you are only showing ints.

What does your config.lua look like?

Also if you just tap the screen at various points my code should just draw a straight line from point to point.  DO you see that behaviour?

To attach screen shots, you have to go to the “More Reply Options” button and upload your image from there.

Rob

what’s your build number?  the fractional touch event x/y was only recently fixed

3079 so recent(ish)

@dave do you get a continuous line with this code?

local line = nil touchListener = function(event) if event.phase == "moved" then if line then line:append(event.x,event.y) else line = display.newLine(event.xStart, event.yStart, event.x, event.y) line:setStrokeColor (1,1,1) line.strokeWidth = 20 end elseif event.phase == "ended" then line:append(event.x,event.y) end return true end Runtime:addEventListener("touch", touchListener)&nbsp;

*update*   latest daily build still has fractional event.x, event.y values.  So I guess not fixed…

23:53:40.804 Copyright (C) 2009-2017 C o r o n a L a b s I n c . 23:53:40.804 Version: 3.0.0 23:53:40.804 Build: 2017.3085 23:53:40.804 Platform: GenericAndroidDevice / x64 / 10.0 / Intel(R) HD Graphics 520 / 4.4.0 - Build 21.20.16.4590 / 2017.3085 / en\_GB | GB | en\_GB | en 23:53:42.360 187.5 408.33331298828 23:53:42.391 183.33332824707 404.16665649414 23:53:42.391 183.33332824707 403.33331298828 23:53:42.406 182.5 400.83331298828 23:53:42.406 181.66665649414 399.16665649414 23:53:42.406 180 398.33331298828 23:53:42.422 180 395 23:53:42.422 179.16665649414 395 23:53:42.437 178.33332824707 390.83331298828

sorry for confusion, my question was for the OP:  what’s THEIR build#?

OP’s simulator output looks like int’s (but could result from just low-res simulator device that matches low-res content)

BUT, if using an old build, then event values can be quantized to content resolution (throwing away some detail, resulting in duplicate coords, etc).  might just be a red herring, but symptoms match.

Your output, with the fractionals, looks like what I’d expect from “random” content size on “random” device.

And yes, your code draws a smooth line for me. :slight_smile:

I will rest my case

Hi davebollinger - my build number is 2017.3068.

I’ll update to the latest now and try the code again.

I only realised now there is no auto-update for Corona??

Have updated to latest build 2017.3086 but still get the same behaviour:

Jun 07 05:32:46.256 Copyright (C) 2009-2017 C o r o n a L a b s I n c . Version: 3.0.0 Build: 2017.3086 Jun 07 05:32:46.271 Loading project from: ~/Dropbox/Archive Rene/Corona/Bouncing balls Jun 07 05:32:46.271 Project sandbox folder: ~/Library/Application Support/Corona Simulator/Bouncing balls-595A9E7EAC84D232BD3AEB40A40A4AA9 Jun 07 05:32:46.274 Platform: iPhone / x86\_64 / 10.12.5 / AMD Radeon Pro 455 OpenGL Engine / 2.1 ATI-1.51.8 / 2017.3086 / en-GB | DE | en\_DE | en Jun 07 05:33:05.042 moved began 324 408 324 408 Jun 07 05:33:05.066 moved append 324 408 Jun 07 05:33:05.074 moved append 324 408 Jun 07 05:33:05.104 moved append 324 408 Jun 07 05:33:05.104 moved append 324 408 moved append 324 408 Jun 07 05:33:05.107 moved append 324 408 Jun 07 05:33:05.141 moved append 324 408 Jun 07 05:33:05.141 moved append 324 408 moved append 324 408 moved append 324 408 Jun 07 05:33:05.172 moved append 324 408 Jun 07 05:33:05.173 moved append 324 408 moved append 324 408 moved append 324 408 Jun 07 05:33:05.201 moved append 324 408 Jun 07 05:33:05.201 moved append 324 408 moved append 324 408 Jun 07 05:33:05.206 moved append 324 408 Jun 07 05:33:05.237 moved append 324 408 Jun 07 05:33:05.238 moved append 324 408 moved append 324 408 Jun 07 05:33:05.238 moved append 324 408 Jun 07 05:33:05.241 moved append 320 408 Jun 07 05:33:05.260 moved append 320 408 Jun 07 05:33:05.261 moved append 312 408 moved append 312 408 moved append 304 408 Jun 07 05:33:05.278 moved append 304 408 Jun 07 05:33:05.278 moved append 296 408 moved append 296 408 moved append 288 408

This is my config file:

-- -- For more information on config.lua see the Project Configuration Guide at: -- https://docs.coronalabs.com/guide/basics/configSettings -- application = { content = { width = 320, height = 480, scale = "letterbox", fps = 60, --[[imageSuffix = { ["@2x"] = 2, ["@4x"] = 4, }, --]] }, }

In the Corona Simulator, iPhone 5 is selected.

Not sure why I’m not getting the same behaviour on my machine as SGS does on his, using the same code.

So when your device is continually reporting “324 408” are you moving the mouse around?  The fact that the event.y value stays at 408 would imply you managed to draw a completely vertical line.

Have you tried different simulator skins and also do a device build and test it on your phone/tablet.

Different simulator skins didn’t fix the interrupted-line-behaviour although with other skins I did get fractional touch event x/y.

A DEVICE BUILD FINALLY FIXED THE INTERRUPTED-LINE BEHAVIOUR AND CREATED THE EXPECTED SMOOTH-LINES!!

I created builds for my iPhone 7 Plus and for my iPad Pro. The behaviour on the iPhone was still a bit odd, for example tapping didn’t always properly append the existing line but sometimes started a new line. The behaviour on the iPad Pro was flawless.

So, in summary: SGS’s code works fine when I do an iOS device build and test it on my iPhone 7 Plus and iPad Pro. For some reason, when I run the code in the Simulator, however, I get interrupted lines.

Thanks again SGS for your code and your discussion points!

Perhaps it would be worth for me to submit the odd Simulator behaviour as an issue / bug fix candidate somewhere?

I’m guessing you are using a Mac?  I use a PC and this might explain why it works for me in sim but not you.  I would submit it as a Mac sim bug.

Yes that’s right, I’m using a Mac. I wonder if @horacebury does, too, who had the same issue when he ran your code in his sim.

Just to be clear… We used to not pass fractional values. A bug was reported and we now pass fractional values… if the operating system pass them to us.  Fractional values are expected.

Rob

Yes that’s right, I’m using a Mac. I wonder if @horacebury does, too, who had the same issue when he ran your code in his sim.

I was going to ask this (but had to be at work) and yes, I’m using a 2016 Mac Pro.

I have noticed that touch events are fired  continuously when pressing on the track pad but only when moving if a double-tap (to cause a an OS-touch-and-drag). However, all touch events will pass fractional values, as Rob says.

I’ve found that building for Corona Live and testing on device resolved a lot of confusion.