canvas_layers.rst 3.1 KB

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