canvas_layers.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. .. _doc_canvas_layers:
  2. Canvas layers
  3. =============
  4. Viewport and Canvas items
  5. -------------------------
  6. Regular 2D nodes, such as :ref:`Node2D <class_Node2D>` or
  7. :ref:`Control <class_Control>` both inherit from
  8. :ref:`CanvasItem <class_CanvasItem>`, which is the base for all 2D
  9. nodes. CanvasItems can be arranged in trees. Each item will inherit
  10. its parent's transform. This means that when the parent is moved, the children
  11. will move too.
  12. CanvasItem nodes, and nodes inheriting from them, are direct or indirect children of a
  13. :ref:`Viewport <class_Viewport>`, and will be displayed through it.
  14. A Viewport has the property
  15. :ref:`Viewport.canvas_transform <class_Viewport_property_canvas_transform>`,
  16. which allows applying a custom
  17. :ref:`Transform2D <class_Transform2D>` transform to the CanvasItem hierarchy it contains. Nodes such as
  18. :ref:`Camera2D <class_Camera2D>` work by changing that transform.
  19. Effects like scrolling are best achieved by manipulating the canvas transform property. This approach is more
  20. efficient than moving the root canvas item (and hence the whole scene).
  21. Usually though, we don't want *everything* in the game or app to be subject to the canvas
  22. transform. Examples of this are:
  23. - **Parallax Backgrounds**: Backgrounds that move slower than the rest
  24. of the stage.
  25. - **UI**: Think of a user interface (UI) or Heads-up display (HUD) superimposed on our view of the game world. We want a life counter, score display and other elements to retain their screen positions even when our view of the game world is changing.
  26. - **Transitions**: We may want visual effects used for transitions (fades, blends) to remain at a fixed screen location.
  27. How can these problems be solved in a single scene tree?
  28. CanvasLayers
  29. ------------
  30. The answer is :ref:`CanvasLayer <class_CanvasLayer>`,
  31. which is a node that adds a separate 2D rendering layer for all its
  32. children and grand-children. Viewport children will draw by default at
  33. layer "0", while a CanvasLayer will draw at any numeric layer. Layers
  34. with a greater number will be drawn above those with a smaller number.
  35. CanvasLayers also have their own transform and do not depend on the
  36. transform of other layers. This allows the UI to be fixed in screen-space
  37. while our view on the game world changes.
  38. An example of this is creating a parallax background. This can be done
  39. with a CanvasLayer at layer "-1". The screen with the points, life
  40. counter and pause button can also be created at layer "1".
  41. Here's a diagram of how it looks:
  42. .. image:: img/canvaslayers.png
  43. CanvasLayers are independent of tree order, and they only depend on
  44. their layer number, so they can be instantiated when needed.
  45. .. note:: CanvasLayers aren't necessary to control the drawing order of nodes.
  46. The standard way to ensuring that a node is correctly drawn 'in front' or 'behind' others is to manipulate the
  47. order of the nodes in the scene panel. Perhaps counterintuitively, the topmost nodes in the scene panel are drawn
  48. on *behind* lower ones in the viewport. 2d nodes also have a property for controlling their drawing order
  49. (see :ref:`Node2D.z_index <class_Node2D_property_z_index>`).