Skip to content
draw_shadowed

draw_shadowed

Mojo function 🡭

draw_shadowed

fn def draw_shadowed(mut dst: Canvas, layer: Canvas, x: Int, y: Int, shadow_color: Color, blur_radius: Float64, offset_x: Int, offset_y: Int)

Composite layer onto dst at (x, y) with a blurred, tinted, offset copy of it underneath – a drop shadow, or (with offset_x = offset_y = 0) a soft glow.

Since a Mojo closure cannot capture a generic “draw my shape” call, the caller renders their shape into layer themselves, onto a transparent background, before calling this. draw_shadowed then:

  1. builds a shadow layer the same size as layer plus padding (see _shadow_pad), layer’s alpha tinted to shadow_color (color replaced, alpha multiplied by shadow_color’s own alpha);
  2. blurs the shadow layer by blur_radius;
  3. composites it onto dst at (x + offset_x, y + offset_y), shifted back by the padding;
  4. composites layer itself onto dst at (x, y), unchanged, on top.

Both composites go through _composite_onto, so dst’s active clip and blend mode apply to the shadow and the shape alike.

Args:

  • dst (Canvas): Canvas composited onto.
  • layer (Canvas): The shape, already rendered onto a transparent background. Unchanged.
  • x (Int): Destination column for layer’s left edge.
  • y (Int): Destination row for layer’s top edge.
  • shadow_color (Color): Color of the shadow/glow.
  • blur_radius (Float64): blur() radius applied to the shadow layer.
  • offset_x (Int): Horizontal shift of the shadow from the shape.
  • offset_y (Int): Vertical shift of the shadow from the shape.

Raises:

Error: layer’s dimensions are negative once padded (does not happen for a layer that was itself validly constructed).