# lv_canvas.h (/api/widgets/canvas/lv_canvas_h)



<RelatedHeaders name="lv_canvas_private.h" isPrivate="false" />

<ApiSummary functions="14" macros="1" variables="1" />

Functions [#functions]

<ApiTabs items="[&#x22;Setters (4)&#x22;,&#x22;Getters (4)&#x22;,&#x22;Other (6)&#x22;]">
  <ApiTab value="Setters (4)">
    <ApiMember kind="function" name="lv_canvas_set_buffer" file="widgets/canvas/lv_canvas.h" line="62" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L62">
      lv_canvas_set_buffer [#lv_canvas_set_buffer]

      Set a buffer for the canvas.

      Use <ApiLink name="lv_canvas_set_draw_buf" display="lv_canvas_set_draw_buf()" /> instead if you need to set a buffer with alignment requirement.

      ```c title=" " lineNumbers=1
      void lv_canvas_set_buffer(lv_obj_t *obj, void *buf, int32_t w, int32_t h, lv_color_format_t cf)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name  | Type                                             | Description                                                                                                                                                                                                                                                                                                                                             |
      | ----- | ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
      | `obj` | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to a canvas object                                                                                                                                                                                                                                                                                                                              |
      | `buf` | `void *`                                         | buffer where content of canvas will be. The required size is (lv\_image\_color\_format\_get\_px\_size(cf) \* w) / 8 \* h) It can be allocated with <ApiLink name="lv_malloc" display="lv_malloc()" /> or it can be statically allocated array (e.g. static <ApiLink name="lv_color_t" /> buf\[100\*50]) or it can be an address in RAM or external SRAM |
      | `w`   | <ApiLink name="int32_t" />                       | width of canvas                                                                                                                                                                                                                                                                                                                                         |
      | `h`   | <ApiLink name="int32_t" />                       | height of canvas                                                                                                                                                                                                                                                                                                                                        |
      | `cf`  | <ApiLink name="lv_color_format_t" />             | color format. `LV_COLOR_FORMAT...`                                                                                                                                                                                                                                                                                                                      |
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_set_draw_buf" file="widgets/canvas/lv_canvas.h" line="71" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L71">
      lv_canvas_set_draw_buf [#lv_canvas_set_draw_buf]

      Set a draw buffer for the canvas. A draw buffer either can be allocated by <ApiLink name="lv_draw_buf_create" display="lv_draw_buf_create()" /> or defined statically by `LV_DRAW_BUF_DEFINE_STATIC`. When buffer start address and stride has alignment requirement, it's recommended to use `lv_draw_buf_create`.

      ```c title=" " lineNumbers=1
      void lv_canvas_set_draw_buf(lv_obj_t *obj, lv_draw_buf_t *draw_buf)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name       | Type                                                       | Description                |
      | ---------- | ---------------------------------------------------------- | -------------------------- |
      | `obj`      | <ApiLink name="lv_obj_t" display="lv_obj_t *" />           | pointer to a canvas object |
      | `draw_buf` | <ApiLink name="lv_draw_buf_t" display="lv_draw_buf_t *" /> | pointer to a draw buffer   |
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_set_px" file="widgets/canvas/lv_canvas.h" line="89" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L89">
      lv_canvas_set_px [#lv_canvas_set_px]

      Set a pixel's color and opacity

      ```c title=" " lineNumbers=1
      void lv_canvas_set_px(lv_obj_t *obj, int32_t x, int32_t y, lv_color_t color, lv_opa_t opa)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name    | Type                                             | Description               |
      | ------- | ------------------------------------------------ | ------------------------- |
      | `obj`   | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to a canvas       |
      | `x`     | <ApiLink name="int32_t" />                       | X coordinate of the pixel |
      | `y`     | <ApiLink name="int32_t" />                       | Y coordinate of the pixel |
      | `color` | <ApiLink name="lv_color_t" />                    | the color                 |
      | `opa`   | <ApiLink name="lv_opa_t" />                      | the opacity               |

      <Callout type="info">
        The following color formats are supported LV\_COLOR\_FORMAT\_I1/2/4/8, LV\_COLOR\_FORMAT\_A8, LV\_COLOR\_FORMAT\_RGB565, LV\_COLOR\_FORMAT\_RGB888, LV\_COLOR\_FORMAT\_XRGB8888, LV\_COLOR\_FORMAT\_ARGB8888
      </Callout>

      <Callout type="info">
        this function invalidates the canvas object every time, for best performance, if you're changing a lot of pixels in a loop, call `lv_display_enable_invalidation` before the loop starts so the invalidation isn't done on every call
      </Callout>
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_set_palette" file="widgets/canvas/lv_canvas.h" line="101" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L101">
      lv_canvas_set_palette [#lv_canvas_set_palette]

      Set the palette color of a canvas for index format. Valid only for `LV_COLOR_FORMAT_I1/2/4/8`

      ```c title=" " lineNumbers=1
      void lv_canvas_set_palette(lv_obj_t *obj, uint8_t index, lv_color32_t color)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name    | Type                                             | Description                                                                                                                                                                          |
      | ------- | ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
      | `obj`   | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to canvas object                                                                                                                                                             |
      | `index` | <ApiLink name="uint8_t" />                       | the palette color to set:<br />- for `LV_COLOR_FORMAT_I1`: 0..1<br />- for `LV_COLOR_FORMAT_I2`: 0..3<br />- for `LV_COLOR_FORMAT_I4`: 0..15<br />- for `LV_COLOR_FORMAT_I8`: 0..255 |
      | `color` | <ApiLink name="lv_color32_t" />                  | the color to set                                                                                                                                                                     |
    </ApiMember>
  </ApiTab>

  <ApiTab value="Getters (4)">
    <ApiMember kind="function" name="lv_canvas_get_draw_buf" file="widgets/canvas/lv_canvas.h" line="107" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L107">
      lv_canvas_get_draw_buf [#lv_canvas_get_draw_buf]

      ```c title=" " lineNumbers=1
      lv_draw_buf_t * lv_canvas_get_draw_buf(lv_obj_t *obj)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name  | Type                                             |
      | ----- | ------------------------------------------------ |
      | `obj` | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> |
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_get_px" file="widgets/canvas/lv_canvas.h" line="116" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L116">
      lv_canvas_get_px [#lv_canvas_get_px]

      Get a pixel's color and opacity

      ```c title=" " lineNumbers=1
      lv_color32_t lv_canvas_get_px(lv_obj_t *obj, int32_t x, int32_t y)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name  | Type                                             | Description               |
      | ----- | ------------------------------------------------ | ------------------------- |
      | `obj` | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to a canvas       |
      | `x`   | <ApiLink name="int32_t" />                       | X coordinate of the pixel |
      | `y`   | <ApiLink name="int32_t" />                       | Y coordinate of the pixel |

      **Returns:** <ApiLink name="lv_color32_t" /> — ARGB8888 color of the pixel
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_get_image" file="widgets/canvas/lv_canvas.h" line="123" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L123">
      lv_canvas_get_image [#lv_canvas_get_image]

      Get the image of the canvas as a pointer to an <ApiLink name="lv_image_dsc_t" /> variable.

      ```c title=" " lineNumbers=1
      lv_image_dsc_t * lv_canvas_get_image(lv_obj_t *canvas)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name     | Type                                             | Description                |
      | -------- | ------------------------------------------------ | -------------------------- |
      | `canvas` | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to a canvas object |

      **Returns:** <ApiLink name="lv_image_dsc_t" display="lv_image_dsc_t *" /> — pointer to the image descriptor.
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_get_buf" file="widgets/canvas/lv_canvas.h" line="132" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L132">
      lv_canvas_get_buf [#lv_canvas_get_buf]

      Return the pointer for the buffer. It's recommended to use this function instead of the buffer form the return value of <ApiLink name="lv_canvas_get_image" display="lv_canvas_get_image()" /> as is can be aligned

      ```c title=" " lineNumbers=1
      const void * lv_canvas_get_buf(lv_obj_t *canvas)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name     | Type                                             | Description                |
      | -------- | ------------------------------------------------ | -------------------------- |
      | `canvas` | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to a canvas object |

      **Returns:** `const void *` — pointer to the buffer
    </ApiMember>
  </ApiTab>

  <ApiTab value="Other (6)">
    <ApiMember kind="function" name="lv_canvas_create" file="widgets/canvas/lv_canvas.h" line="41" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L41">
      lv_canvas_create [#lv_canvas_create]

      Create a canvas object

      ```c title=" " lineNumbers=1
      lv_obj_t * lv_canvas_create(lv_obj_t *parent)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name     | Type                                             | Description                                                   |
      | -------- | ------------------------------------------------ | ------------------------------------------------------------- |
      | `parent` | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to an object, it will be the parent of the new canvas |

      **Returns:** <ApiLink name="lv_obj_t" display="lv_obj_t *" /> — pointer to the created canvas
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_copy_buf" file="widgets/canvas/lv_canvas.h" line="147" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L147">
      lv_canvas_copy_buf [#lv_canvas_copy_buf]

      Copy a buffer to the canvas

      ```c title=" " lineNumbers=1
      void lv_canvas_copy_buf(lv_obj_t *obj, const lv_area_t *canvas_area, lv_draw_buf_t *src_buf, const lv_area_t *src_area)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name          | Type                                                       | Description                                                                 |
      | ------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------- |
      | `obj`         | <ApiLink name="lv_obj_t" display="lv_obj_t *" />           | pointer to a canvas object                                                  |
      | `canvas_area` | <ApiLink name="lv_area_t" display="const lv_area_t *" />   | the area of the canvas to copy the new data to                              |
      | `src_buf`     | <ApiLink name="lv_draw_buf_t" display="lv_draw_buf_t *" /> | pointer to a buffer holding the source data                                 |
      | `src_area`    | <ApiLink name="lv_area_t" display="const lv_area_t *" />   | the area of the source buffer to copy from. If NULL, copy the whole buffer. |

      <Callout type="info">
        canvas\_area and src\_area should be the same size. If canvas\_area and the size of src\_buf are the same, src\_area can be left NULL.
      </Callout>
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_fill_bg" file="widgets/canvas/lv_canvas.h" line="156" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L156">
      lv_canvas_fill_bg [#lv_canvas_fill_bg]

      Fill the canvas with color

      ```c title=" " lineNumbers=1
      void lv_canvas_fill_bg(lv_obj_t *obj, lv_color_t color, lv_opa_t opa)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name    | Type                                             | Description          |
      | ------- | ------------------------------------------------ | -------------------- |
      | `obj`   | <ApiLink name="lv_obj_t" display="lv_obj_t *" /> | pointer to a canvas  |
      | `color` | <ApiLink name="lv_color_t" />                    | the background color |
      | `opa`   | <ApiLink name="lv_opa_t" />                      | the desired opacity  |
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_init_layer" file="widgets/canvas/lv_canvas.h" line="164" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L164">
      lv_canvas_init_layer [#lv_canvas_init_layer]

      Initialize a layer to use LVGL's generic draw functions (lv\_draw\_rect/label/...) on the canvas. Needs to be usd in pair with `lv_canvas_finish_layer`.

      ```c title=" " lineNumbers=1
      void lv_canvas_init_layer(lv_obj_t *canvas, lv_layer_t *layer)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name     | Type                                                 | Description                               |
      | -------- | ---------------------------------------------------- | ----------------------------------------- |
      | `canvas` | <ApiLink name="lv_obj_t" display="lv_obj_t *" />     | pointer to a canvas                       |
      | `layer`  | <ApiLink name="lv_layer_t" display="lv_layer_t *" /> | pointer to a layer variable to initialize |
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_finish_layer" file="widgets/canvas/lv_canvas.h" line="172" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L172">
      lv_canvas_finish_layer [#lv_canvas_finish_layer]

      Wait until all the drawings are finished on layer. Needs to be usd in pair with `lv_canvas_init_layer`.

      ```c title=" " lineNumbers=1
      void lv_canvas_finish_layer(lv_obj_t *canvas, lv_layer_t *layer)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name     | Type                                                 | Description                    |
      | -------- | ---------------------------------------------------- | ------------------------------ |
      | `canvas` | <ApiLink name="lv_obj_t" display="lv_obj_t *" />     | pointer to a canvas            |
      | `layer`  | <ApiLink name="lv_layer_t" display="lv_layer_t *" /> | pointer to a layer to finalize |
    </ApiMember>

    <ApiMember kind="function" name="lv_canvas_buf_size" file="widgets/canvas/lv_canvas.h" line="183" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L183">
      lv_canvas_buf_size [#lv_canvas_buf_size]

      Just a wrapper to `LV_CANVAS_BUF_SIZE` for bindings.

      ```c title=" " lineNumbers=1
      uint32_t lv_canvas_buf_size(int32_t w, int32_t h, uint8_t bpp, uint8_t stride)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name     | Type                       |
      | -------- | -------------------------- |
      | `w`      | <ApiLink name="int32_t" /> |
      | `h`      | <ApiLink name="int32_t" /> |
      | `bpp`    | <ApiLink name="uint8_t" /> |
      | `stride` | <ApiLink name="uint8_t" /> |
    </ApiMember>
  </ApiTab>
</ApiTabs>

Macros [#macros]

<ApiMember kind="macro" name="LV_CANVAS_BUF_SIZE" file="widgets/canvas/lv_canvas.h" line="178" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L178">
  LV_CANVAS_BUF_SIZE [#lv_canvas_buf_size-1]

  ```c title=" " lineNumbers=1
  #define LV_CANVAS_BUF_SIZE(w, h, bpp, stride) \
      (((((w * bpp + 7) >> 3) + stride - 1) & ~(stride - 1)) * h + LV_DRAW_BUF_ALIGN)
  ```
</ApiMember>

Variables [#variables]

<ApiMember kind="variable" name="lv_canvas_class" file="widgets/canvas/lv_canvas.h" line="30" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/widgets/canvas/lv_canvas.h#L30">
  lv_canvas_class [#lv_canvas_class]

  ```c title=" " lineNumbers=1
  const lv_obj_class_t lv_canvas_class
  ```
</ApiMember>

Dependencies [#dependencies]

<FileIncludes includes="[&#x22;lv_conf_internal.h&#x22;, &#x22;lv_image.h&#x22;, &#x22;lv_draw_image.h&#x22;]" includedBy="[&#x22;lv_barcode.h&#x22;, &#x22;lv_qrcode.h&#x22;, &#x22;lv_canvas_private.h&#x22;]" transitiveIncludes="[&#x22;lv_anim.h&#x22;, &#x22;lv_area.h&#x22;, &#x22;lv_array.h&#x22;, &#x22;lv_assert.h&#x22;, &#x22;lv_bidi.h&#x22;, &#x22;lv_color.h&#x22;, &#x22;lv_color_op.h&#x22;, &#x22;lv_conf_kconfig.h&#x22;, &#x22;lv_display.h&#x22;, &#x22;lv_draw.h&#x22;, &#x22;lv_draw_arc.h&#x22;, &#x22;lv_draw_blur.h&#x22;, &#x22;lv_draw_buf.h&#x22;, &#x22;lv_draw_label.h&#x22;, &#x22;lv_draw_line.h&#x22;, &#x22;lv_draw_rect.h&#x22;, &#x22;lv_draw_triangle.h&#x22;, &#x22;lv_event.h&#x22;, &#x22;lv_ext_data.h&#x22;, &#x22;lv_flex.h&#x22;, &#x22;lv_font.h&#x22;, &#x22;lv_fs.h&#x22;, &#x22;lv_grad.h&#x22;, &#x22;lv_grid.h&#x22;, &#x22;lv_group.h&#x22;, &#x22;lv_image_decoder.h&#x22;, &#x22;lv_image_dsc.h&#x22;, &#x22;lv_indev.h&#x22;, &#x22;lv_layout.h&#x22;, &#x22;lv_ll.h&#x22;, &#x22;lv_log.h&#x22;, &#x22;lv_math.h&#x22;, &#x22;lv_matrix.h&#x22;, &#x22;lv_mem.h&#x22;, &#x22;lv_obj.h&#x22;, &#x22;lv_obj_class.h&#x22;, &#x22;lv_obj_draw.h&#x22;, &#x22;lv_obj_event.h&#x22;, &#x22;lv_obj_pos.h&#x22;, &#x22;lv_obj_property.h&#x22;, &#x22;lv_obj_property_names.h&#x22;, &#x22;lv_obj_scroll.h&#x22;, &#x22;lv_obj_style.h&#x22;, &#x22;lv_obj_style_gen.h&#x22;, &#x22;lv_obj_tree.h&#x22;, &#x22;lv_observer.h&#x22;, &#x22;lv_palette.h&#x22;, &#x22;lv_profiler.h&#x22;, &#x22;lv_profiler_builtin.h&#x22;, &#x22;lv_sprintf.h&#x22;, &#x22;lv_string.h&#x22;, &#x22;lv_style.h&#x22;, &#x22;lv_style_gen.h&#x22;, &#x22;lv_style_properties.h&#x22;, &#x22;lv_symbol_def.h&#x22;, &#x22;lv_text.h&#x22;, &#x22;lv_tick.h&#x22;, &#x22;lv_timer.h&#x22;, &#x22;lv_types.h&#x22;]" />
