Problem Scrolling Canvas

Hello,

I am quite new to Xojo programming. I am trying to scroll a picture in a canvas.

1 - I track the initial position of the mouse (mouse1_x and mouse1_y) in the Canvas (and I finish the code with “Return True”)

2 - In the Mousedrag handler of my Canvas I wrote the following code:

dim delta_x, delta_y As Integer
delta_x = mouse1_x - X
delta_y = mouse1_y - Y

If X <> mouse1_x Or Y <> mouse1_Y Then
me.Scroll(delta_x,delta_y)
end if

The handler is running (I checked with a system.beep) but the canvas doesn’t scroll.

Could it be because I use the Macbook trackpad (click and drag)?

Any idea?

Thank you in anticipation and by the way happy New Year!

Hi Yann, I created an example (Mac) for you where you can scroll the image both vertically and horizontally and also move the image with the mouse. Here is the project, I hope it meets your needs.

Welcome to the Forum !

Please, select your code, then press the icon (image) from the tool bar. This gives:

dim delta_x, delta_y As Integer
delta_x = mouse1_x - X
delta_y = mouse1_y - Y

If X <> mouse1_x Or Y <> mouse1_Y Then
me.Scroll(delta_x,delta_y)
end if

You may also use Var instead of Dim (Changed since 2019)

Use instead:
System.DebugLog "Scroll code works"

Click in the IDE bottom/middle icon ( image ) to display the result string.

Gracie Gianni,

Why don’t you use the Canvas.scroll but MouseDown and MouseUp insted?

I see that it works but is there any limitations or bug with the Canvas.scroll handler?

Have a nice day

Hi Emile,

Thank you for your advice.

I changed my code as follow:


If X <> mouse1_x Or Y <> mouse1_Y Then
 System.DebugLog "Scroll code works"
  me.Scroll(delta_x,delta_y)
end if

And I obtained:


17:07:51 : Scroll code works
: Scroll code works
: Scroll code works
: Scroll code works
: Scroll code works
: Scroll code works
: Scroll code works
: Scroll code works

I’m happy to hear that it works. There are many ways to program it I only tried one and the first one that came to mind. Try testing it again and make changes if necessary.

I added a TextLabel to log the result of my code


If X <> mouse1_x Or Y <> mouse1_Y Then
  System.DebugLog "Scroll code works"
  Label_MouseInfo.Text = Label_MouseInfo.Text + Chr(13) + "=======" + Chr(13) + "Delta X : " + str(delta_x) + chr(13) + "Delta Y : " + str(delta_y)
  
  me.Scroll(delta_x,delta_y) 
end if

Result:

Delta X : -2
Delta Y : 1

Delta X : -8
Delta Y : 6

Delta X : -20
Delta Y : 16

etc…

The " me.Scroll(delta_x,delta_y)" part of the code doesn’t “fire”

The only caveats with your code is:

1 - It “sticks” to the mouse. Once you have clicked on the image it keeps scrolling" even if you release the mouse
2 - It doesn’t refresh smoothly like in this video that I took as an example for my code

https://youtu.be/RrKvI-7FXyI

His code works as I want to, but when I reproduce it it doesn’t work :slight_smile:

Thank you anyway,

1 Like

For a perfect result it takes more time. This example is just a possible starting point. I hope you will solve the problem

Hi Gianni,

Thank you for your help.
I finally found a solution that works great with the scroll handler.

I have 2 properties (dx and dy as integer) to store the delta_x and delta_y to scroll the image.

Oddly enough me.scroll(delta_x,delta_y) isn’t sufficient to move the image but I had to add “g. DrawPicture(MyImage,dx, dy)” in the paint handler.

It works very nicely! :+1: :+1: :+1:

1 Like

I’m happy that you finally found the solution. Good job and happy new year!

Than you, the same to you