close_path

1fn close_path(inout self)

The close_path function completes the current path by drawing a line from the last point back to the starting point. This effectively closes the shape, ensuring that it is a continuous loop. It's commonly used when you need to create closed shapes like polygons.

Parameters

  • self (inout): A reference to the Sdf2d instance. The function updates the internal state of self by connecting the current position back to the starting position of the path.

Returns

  • void: This function does not return a value but modifies the Sdf2d context to close the current path.

Example

1fn pixel(self) -> vec4 {
2    // Create an Sdf2d drawing context for the current viewport.
3    let sdf = Sdf2d::viewport(self.pos * self.rect_size);
4
5    // Start a new path at point (50.0, 10.0).
6    sdf.move_to(50.0, 10.0);
7
8    // Draw lines to create a triangle.
9    sdf.line_to(90.0, 80.0);   // Line to point (90.0, 80.0).
10    sdf.line_to(10.0, 80.0);   // Line to point (10.0, 80.0);
11    
12    // Close the path, connecting back to the starting point.
13    sdf.close_path();
14
15    // Fill the shape with a solid red color.
16    sdf.fill(#f00);
17
18    // Return the final color result.
19    return sdf.result;
20}

Explanation

In this example:

  • Create Drawing Context: We initialize an Sdf2d drawing context using the current position (self.pos) and size (self.rect_size) of the viewport.
  • Start Path: We set the starting point of our path to (50.0, 10.0) using sdf.move_to.
  • Draw Lines: We draw two lines to create the sides of a triangle:
    • From the starting point to (90.0, 80.0) using sdf.line_to(90.0, 80.0).
    • Then to (10.0, 80.0) using sdf.line_to(10.0, 80.0).
  • Close Path: We use sdf.close_path() to draw a line from the current point back to the starting point (50.0, 10.0), completing the triangle.
  • Apply Fill: We fill the closed path with red color using sdf.fill(#f00).
  • Return Result: We return sdf.result, which contains the final rendered color after all drawing operations.

Notes

  • Path Creation: The combination of move_to, line_to, and close_path allows you to construct complex shapes by defining their outlines point by point.
  • State Management: The close_path function uses the start_pos and last_pos internally to determine where to draw the closing line.
  • Drawing Order: Ensure that you define your entire path and call close_path before applying fills or strokes to render the shape correctly.