# Problem Scrolling Canvas

**URL:** <https://forum.xojo.com/t/problem-scrolling-canvas/83754>\
**Category:** Getting Started\
**Created:** [January 1, 2025, 1:55pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754 "2025-01-01T13:55:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yann\_DUPONT](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/3be4f8/32.png) [@Yann\_DUPONT](https://forum.xojo.com/u/Yann_DUPONT)\
**Post date:** [January 1, 2025, 1:55pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/1 "2025-01-01T13:55:03Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![anon69598124](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@anon69598124](https://forum.xojo.com/u/anon69598124)\
**Post date:** [January 1, 2025, 2:34pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/2 "2025-01-01T14:34:16Z")

</div>

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.

> **[102.3 KB file on MEGA](https://mega.nz/file/UCMRkLbJ#MsVJqB5wG-N05NKP9m9tcS1zvrW-ZMzifhsbTq2iSqM)**

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [January 1, 2025, 2:56pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/3 "2025-01-01T14:56:04Z")

</div>

> [@Yann\_DUPONT](#):
>

Welcome to the Forum !

Please, select your code, then press the icon ( ![image](https://forum.xojo.com/uploads/default/original/3X/5/5/55abdb6f3e253c1de588d806502ba6beca167d9c.png)) from the tool bar. This gives:

```auto
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)

> [@Yann\_DUPONT](#):
>
> The handler is running (I checked with a system.beep) but the canvas doesn’t scroll.

Use instead:  
`System.DebugLog "Scroll code works"`

Click in the IDE bottom/middle icon ( ![image](https://forum.xojo.com/uploads/default/original/3X/d/d/dd09be07164944cb2967f0fc76a1fbc87b33d8e3.png) ) to display the result string.

---

<div class="post-metadata">

**Author:** ![Yann\_DUPONT](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/3be4f8/32.png) [@Yann\_DUPONT](https://forum.xojo.com/u/Yann_DUPONT)\
**Post date:** [January 1, 2025, 4:06pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/4 "2025-01-01T16:06:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yann\_DUPONT](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/3be4f8/32.png) [@Yann\_DUPONT](https://forum.xojo.com/u/Yann_DUPONT)\
**Post date:** [January 1, 2025, 4:13pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/5 "2025-01-01T16:13:33Z")

</div>

Hi Emile,

Thank you for your advice.

I changed my code as follow:

```auto

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  
…

---

<div class="post-metadata">

**Author:** ![anon69598124](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@anon69598124](https://forum.xojo.com/u/anon69598124)\
**Post date:** [January 1, 2025, 4:32pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/6 "2025-01-01T16:32:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Yann\_DUPONT](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/3be4f8/32.png) [@Yann\_DUPONT](https://forum.xojo.com/u/Yann_DUPONT)\
**Post date:** [January 1, 2025, 4:35pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/7 "2025-01-01T16:35:35Z")

</div>

I added a TextLabel to log the result of my code

```auto

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”

---

<div class="post-metadata">

**Author:** ![Yann\_DUPONT](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/3be4f8/32.png) [@Yann\_DUPONT](https://forum.xojo.com/u/Yann_DUPONT)\
**Post date:** [January 1, 2025, 4:39pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/8 "2025-01-01T16:39:06Z")

</div>

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](https://youtu.be/RrKvI-7FXyI)

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

Thank you anyway,

---

<div class="post-metadata">

**Author:** ![anon69598124](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@anon69598124](https://forum.xojo.com/u/anon69598124)\
**Post date:** [January 1, 2025, 4:44pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/9 "2025-01-01T16:44:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yann\_DUPONT](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/3be4f8/32.png) [@Yann\_DUPONT](https://forum.xojo.com/u/Yann_DUPONT)\
**Post date:** [January 1, 2025, 6:49pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/10 "2025-01-01T18:49:56Z")

</div>

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! 👍 👍 👍

---

<div class="post-metadata">

**Author:** ![anon69598124](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@anon69598124](https://forum.xojo.com/u/anon69598124)\
**Post date:** [January 1, 2025, 6:53pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/11 "2025-01-01T18:53:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yann\_DUPONT](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/3be4f8/32.png) [@Yann\_DUPONT](https://forum.xojo.com/u/Yann_DUPONT)\
**Post date:** [January 1, 2025, 8:13pm UTC](https://forum.xojo.com/t/problem-scrolling-canvas/83754/12 "2025-01-01T20:13:48Z")

</div>

Than you, the same to you
