pixi.js
    Preparing search index...

    Class RoundedRectangle

    The RoundedRectangle object represents a rectangle with rounded corners. Defined by position, dimensions and corner radius.

    // Basic rectangle creation
    const rect = new RoundedRectangle(100, 100, 200, 150, 20);
    // Use as container hit area
    container.hitArea = new RoundedRectangle(0, 0, 100, 100, 10);
    // Check point containment
    const isInside = rect.contains(mouseX, mouseY);
    // Get bounds
    const bounds = rect.getBounds();
    • Position defined by top-left corner
    • Radius clamped to half smallest dimension
    • Common in UI elements

    Rectangle For non-rounded rectangles

    Implements

    Index

    Constructors

    • Parameters

      • x: number = 0

        The X coordinate of the upper-left corner of the rounded rectangle

      • y: number = 0

        The Y coordinate of the upper-left corner of the rounded rectangle

      • width: number = 0

        The overall width of this rounded rectangle

      • height: number = 0

        The overall height of this rounded rectangle

      • radius: number = 20

        Controls the radius of the rounded corners

      Returns RoundedRectangle

    Properties

    height: number

    The overall height of this rounded rectangle

    // Basic height setting
    const rect = new RoundedRectangle();
    rect.height = 150; // Total height will be 150
    0
    
    radius: number

    Controls the radius of the rounded corners

    // Basic radius setting
    const rect = new RoundedRectangle(0, 0, 200, 150);
    rect.radius = 20;

    // Clamp to maximum safe radius
    rect.radius = Math.min(rect.width, rect.height) / 2;

    // Create pill shape
    rect.radius = rect.height / 2;
    • Automatically clamped to half of smallest dimension
    • Common values: 0-20 for UI elements
    • Higher values create more rounded corners
    20
    
    type: SHAPE_PRIMITIVE = 'roundedRectangle'

    The type of the object, mainly used to avoid instanceof checks

    // Check shape type
    const shape = new RoundedRectangle(0, 0, 100, 100, 20);
    console.log(shape.type); // 'roundedRectangle'

    // Use in type guards
    if (shape.type === 'roundedRectangle') {
    console.log(shape.radius);
    }
    'roundedRectangle'
    

    SHAPE_PRIMITIVE For all shape types

    width: number

    The overall width of this rounded rectangle

    // Basic width setting
    const rect = new RoundedRectangle();
    rect.width = 200; // Total width will be 200
    0
    
    x: number

    The X coordinate of the upper-left corner of the rounded rectangle

    // Basic x position
    const rect = new RoundedRectangle();
    rect.x = 100;
    0
    
    y: number

    The Y coordinate of the upper-left corner of the rounded rectangle

    // Basic y position
    const rect = new RoundedRectangle();
    rect.y = 100;
    0
    

    Methods

    • Creates a clone of this Rounded Rectangle.

      Returns RoundedRectangle

      A copy of the rounded rectangle

      // Basic cloning
      const original = new RoundedRectangle(100, 100, 200, 150, 20);
      const copy = original.clone();

      // Clone and modify
      const modified = original.clone();
      modified.radius = 30;
      modified.width *= 2;

      // Verify independence
      console.log(original.radius); // 20
      console.log(modified.radius); // 30
    • Checks whether the x and y coordinates given are contained within this Rounded Rectangle

      Parameters

      • x: number

        The X coordinate of the point to test

      • y: number

        The Y coordinate of the point to test

      Returns boolean

      Whether the x/y coordinates are within this Rounded Rectangle

      // Basic containment check
      const rect = new RoundedRectangle(100, 100, 200, 150, 20);
      const isInside = rect.contains(150, 125); // true
      // Check corner radius
      const corner = rect.contains(100, 100); // false if within corner curve
      • Returns false if width/height is 0 or negative
      • Handles rounded corners with radius check
    • Copies another rectangle to this one.

      Parameters

      Returns this

      Returns itself

      // Basic copying
      const source = new RoundedRectangle(100, 100, 200, 150, 20);
      const target = new RoundedRectangle();
      target.copyFrom(source);

      // Chain with other operations
      const rect = new RoundedRectangle()
      .copyFrom(source)
      .getBounds(rect);
    • Returns the framing rectangle of the rounded rectangle as a Rectangle object

      Parameters

      • Optionalout: Rectangle

        Optional rectangle to store the result

      Returns Rectangle

      The framing rectangle

      // Basic bounds calculation
      const rect = new RoundedRectangle(100, 100, 200, 150, 20);
      const bounds = rect.getBounds();
      // bounds: x=100, y=100, width=200, height=150

      // Reuse existing rectangle
      const out = new Rectangle();
      rect.getBounds(out);
      • Rectangle matches outer dimensions
      • Ignores corner radius
    • Checks whether the x and y coordinates given are contained within this rectangle including the stroke.

      Parameters

      • pX: number

        The X coordinate of the point to test

      • pY: number

        The Y coordinate of the point to test

      • strokeWidth: number

        The width of the line to check

      • alignment: number = 0.5

        The alignment of the stroke (1 = inner, 0.5 = centered, 0 = outer)

      Returns boolean

      Whether the x/y coordinates are within this rectangle's stroke

      // Basic stroke check
      const rect = new RoundedRectangle(100, 100, 200, 150, 20);
      const isOnStroke = rect.strokeContains(150, 100, 4); // 4px line width

      // Check with different alignments
      const innerStroke = rect.strokeContains(150, 100, 4, 1); // Inside
      const centerStroke = rect.strokeContains(150, 100, 4, 0.5); // Centered
      const outerStroke = rect.strokeContains(150, 100, 4, 0); // Outside
    • Returns a string representation of an object.

      Returns string