Trying to get the drag speed

Hello everyone.

New to app inventor and I've run into an issue that I could use some help with. I'm trying to draw lines on a canvas, but I want the line size to be dependent on the drag speed. The problem is that the canvas dragged event doesn't have a speed parameter. I tried grabbing it from the canvas flung event but it doesn't change the line size in real time, only when you let up and drag again. If I reset the size in the canvas touchedup event it gets even more random and weird. Does anyone have a way to adjust the line size in real time based on the speed of a drag?

Welcome James.

True so do your own calculation.

Have you tried to calculate 'speed' using the Math Blocks knowing

Dragged(startX,startY,prevX,prevY,currentX,currentY,draggedAnySprite) so that when the user does a drag from one point (prevX, prevY) to another (x, y). The pair (startX, startY) indicates where the user first touched the screen, and “draggedAnySprite” indicates whether a sprite is being dragged.
Possibly use a Clock to calculate the speed in pixels per millisecond based on prevX and prevY compared to currentX and currentY.

Sorry, I don't have an equation. You might experiment.

Keeping the previous drag event's Clock1.SystemTime in a global variable would help reduce the math load, since it's in milliseconds.

1 Like

Yeah, I was afraid that I was going to have to write my own drag speed function, as I'm not the greatest mathematician, but I'm sure I can come up with something that will work. If I do I'll post the solution, sure it will help someone else in the future. Thanks

Perhaps something else worth looking at:

If you draw small circles or points with the Dragged event, the gap between circles gets bigger the faster you draw. You could either use this phenomenom directly (?) or measure the distance between circles and set line widths/circle radii accordingly ?

This seems to do it, needs more work...

Thanks, with some modification this solution worked well, I may tinker some more to see if I can get something slightly smoother, but for the most part this works as I was hoping.

As you can see it gives the pen strokes a more natural look, I'm also planning to use a similar solution with sprites and the dynamic components extension to hopefully create a brush system, but one step at a time. Here's the block of code I settled on.

Thanks again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.