# Repainting a Canvas

**URL:** <https://forum.xojo.com/t/repainting-a-canvas/25923>\
**Category:** General\
**Created:** [August 7, 2015, 6:47am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923 "2015-08-07T06:47:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_van\_der\_Linden](https://forum.xojo.com/user_avatar/forum.xojo.com/alexander_van_der_linden/32/323_2.png) [@Alexander\_van\_der\_Linden](https://forum.xojo.com/u/Alexander_van_der_Linden)\
**Post date:** [August 7, 2015, 6:47am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/1 "2015-08-07T06:47:17Z")

</div>

I have a canvas on which I paint several items with a timer. Some move, some not.  
I noticed that the Paint event wipes the Canvas - I mean I have to repaint the items that do not move? How do I keep these items on the canvas - they don’t need a repaint…

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [August 7, 2015, 6:49am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/2 "2015-08-07T06:49:43Z")

</div>

You can set a certain part of the canvas to be refreshed.

For example to only refresh the top left 100x100 rectangle:

canvas1.invalidate(0,0,100,100,false)

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 7, 2015, 6:52am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/3 "2015-08-07T06:52:14Z")

</div>

Draw the non-moving parts into a buffer image. In the Paint event, draw that first, then the moving parts on top of it.

---

<div class="post-metadata">

**Author:** ![Alexander\_van\_der\_Linden](https://forum.xojo.com/user_avatar/forum.xojo.com/alexander_van_der_linden/32/323_2.png) [@Alexander\_van\_der\_Linden](https://forum.xojo.com/u/Alexander_van_der_Linden)\
**Post date:** [August 7, 2015, 7:05am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/4 "2015-08-07T07:05:04Z")

</div>

Christoph: thanks. Didn’t know that, I look into it.

Tim: the Paint event fires about 10 times a second… your solution will not deal with the flicker I think (Windows, I noticed that already)…

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [August 7, 2015, 7:49am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/5 "2015-08-07T07:49:34Z")

</div>

I think Tim’s solution should work just fine, and is worth trying. Just make sure to set the DoubleBuffer property of the main canvas to True to prevent any flicker.

I’ve used this approach in many projects before without problems.

---

<div class="post-metadata">

**Author:** ![Alexander\_van\_der\_Linden](https://forum.xojo.com/user_avatar/forum.xojo.com/alexander_van_der_linden/32/323_2.png) [@Alexander\_van\_der\_Linden](https://forum.xojo.com/u/Alexander_van_der_Linden)\
**Post date:** [August 7, 2015, 8:30am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/6 "2015-08-07T08:30:59Z")

</div>

OK, I’m busy implementing that. Thanks.

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [August 7, 2015, 8:53am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/7 "2015-08-07T08:53:36Z")

</div>

Alexander, if you’re interested, Alain Bailleul is a guru with graphics, and here is an example project of his, where he does an amazing job of using buffers to create some awesome animated effects.

[Download Project](http://www.zoclee.com/screenshots/tmx.zip)

![](http://www.zoclee.com/screenshots/tmx.png)

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [August 7, 2015, 8:54am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/8 "2015-08-07T08:54:15Z")

</div>

I’ve learned a lot about 2D graphics animation by working through this code… geniet dit 😉

---

<div class="post-metadata">

**Author:** ![Alexander\_van\_der\_Linden](https://forum.xojo.com/user_avatar/forum.xojo.com/alexander_van_der_linden/32/323_2.png) [@Alexander\_van\_der\_Linden](https://forum.xojo.com/u/Alexander_van_der_Linden)\
**Post date:** [August 7, 2015, 9:11am UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/9 "2015-08-07T09:11:44Z")

</div>

Daar geniet ik zeker van!

Wow… I will look into his code…

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:52pm UTC](https://forum.xojo.com/t/repainting-a-canvas/25923/10 "2020-10-29T17:52:51Z")

</div>


