How can I draw a line?

How can I make it so the user can draw a line. A line like in snapchat.

Do you mean you want the user to be able to draw a line via touch and drag ?

Yeah

Corona isn’t really designed for this. It’s display model is about having a pile of things on the screen that you manipulate - so you declare a sprite or whatever, then move it or scale it or whatever. Circles, Lines, Rectangles, Polygons are all the same model. 

I don’t think it’s possible under the current version to set a line end point, though it is technically not difficult - the docs are confusing because it doesn’t “draw a line”, it creates a display object that represents a line which isn’t the same thing at all.

You have two options - either remove and replace the line object itself whenever you want to repaint it is one, but I would do it by setting the line size to a fixed value - say (1,1) and then using xScale and yScale to change the actual line.

So, if you have a  line from (0,0) - (1,1) and scale it by -32,48 it will go from (0,0) to (-32,48) - a little bit of maths is required for this (because you have to take into account the origin if it isn’t (0,0) and that will scale as well and turn anchoring off) so I’d be inclined to encapsulate this in a function and write a separate little test app to make sure it is working correctly.

I needed to do this myself, so I figured it out.

How to have a line in Corona that you can move arbitrarily, from anywhere to anywhere :slight_smile:

First, the line must be created with this irrespective of what you want to do with it.

local someLine = display.newLine(0,0,1,0)

Then, use this function to position it. The basic idea is that it moves the origin to where it starts, scales the horizontal line to the right length (calculated using pythagoras) and rotates it to the right angle. (using arctangent). The only catch is the math.max() which is required because a line where x1=x2 and y1=y2, e.g. zero length produces a scale of 0 which Corona cares for not …

local function positionLine(lineObject,x1,y1,x2,y2) lineObject.x,lineObject.y = x1,y1 x2 = x2 - x1 y2 = y2 - y1 local lineSize = math.max(0.001,math.sqrt(x2\*x2+y2\*y2)) lineObject.xScale = lineSize lineObject.rotation = math.deg(math.atan2(y2,x2)) end

You can set the stroke colour and size as per normal. 

How would I make that so I can touch and drag to create that line?

Do you mean you want the user to be able to draw a line via touch and drag ?

Yeah

Corona isn’t really designed for this. It’s display model is about having a pile of things on the screen that you manipulate - so you declare a sprite or whatever, then move it or scale it or whatever. Circles, Lines, Rectangles, Polygons are all the same model. 

I don’t think it’s possible under the current version to set a line end point, though it is technically not difficult - the docs are confusing because it doesn’t “draw a line”, it creates a display object that represents a line which isn’t the same thing at all.

You have two options - either remove and replace the line object itself whenever you want to repaint it is one, but I would do it by setting the line size to a fixed value - say (1,1) and then using xScale and yScale to change the actual line.

So, if you have a  line from (0,0) - (1,1) and scale it by -32,48 it will go from (0,0) to (-32,48) - a little bit of maths is required for this (because you have to take into account the origin if it isn’t (0,0) and that will scale as well and turn anchoring off) so I’d be inclined to encapsulate this in a function and write a separate little test app to make sure it is working correctly.

I needed to do this myself, so I figured it out.

How to have a line in Corona that you can move arbitrarily, from anywhere to anywhere :slight_smile:

First, the line must be created with this irrespective of what you want to do with it.

local someLine = display.newLine(0,0,1,0)

Then, use this function to position it. The basic idea is that it moves the origin to where it starts, scales the horizontal line to the right length (calculated using pythagoras) and rotates it to the right angle. (using arctangent). The only catch is the math.max() which is required because a line where x1=x2 and y1=y2, e.g. zero length produces a scale of 0 which Corona cares for not …

local function positionLine(lineObject,x1,y1,x2,y2) lineObject.x,lineObject.y = x1,y1 x2 = x2 - x1 y2 = y2 - y1 local lineSize = math.max(0.001,math.sqrt(x2\*x2+y2\*y2)) lineObject.xScale = lineSize lineObject.rotation = math.deg(math.atan2(y2,x2)) end

You can set the stroke colour and size as per normal. 

How would I make that so I can touch and drag to create that line?