Skip to content

Add drawRoundedImage for rounded-corner image drawing - #64

Merged
treeform merged 1 commit into
masterfrom
rounded-image
Sep 1, 2026
Merged

treeform merged 1 commit into
masterfrom
rounded-image

Conversation

@treeform

@treeform treeform commented Sep 1, 2026 •

Copy link
Copy Markdown
Owner

What

Adds sk.drawRoundedImage(name, pos, size, radius, color) to draw an atlas image filling any size with rounded corners, plus a manual test. The image keeps its aspect ratio: it is scaled uniformly to cover the destination rect, centered, and the overflow is cropped (CSS object-fit: cover behavior).

How it works

The geometry is built from nine pieces:

  1. An inner rect from (r, r) to (size - r, size - r).
  2. Four edge rects (top, bottom, left, right strips between the corners).
  3. Four quarter-circle triangle fans pivoting on each corner's arc center, drawn with the existing drawTriangle.

Every vertex goes through one shared linear map from the destination rect into a centered, aspect-matched sub-rect of the atlas entry, so UVs are continuous across all nine pieces — no seams or texture misalignment where the fans meet the rects, and no stretching.

Fan segment count is arc-length based: one segment per ~2px of on-screen arc (scaled by uiScale for retina), minimum 4, so large corners get more triangles automatically and never stair-step. Radius clamps to min(w, h) / 2 (a full clamp renders a clean circle) and radii under half a pixel fall back to a single plain quad.

Also adds the matching no-op stub in semantics.nim so the silkyTesting build keeps compiling.

Test

tests/manual_rounded_image.nim, modeled on the 9-patch manual test: width/height/radius scrubbers, the original 48x48 debug.9patch for reference, and the rounded version drawn below. It also accepts --screenshot to render one frame to tmp/rounded_screenshot.png and exit.

Verified with framebuffer captures: wide rect (300x200, crops top/bottom), tall rect (150x400, crops left/right), and full-circle radius clamp (300x300); nim check tests/test_semantic.nim passes for the testing build.

🤖 Generated with Claude Code

Builds the shape from an inner rect, four edge rects and four
triangle fans, with UVs linearly mapped from the destination rect
into the atlas entry so the texture is continuous across pieces.
Fan segment count is arc-length based (~2px per segment, uiScale
aware) so corners stay smooth at any radius. Radius clamps to
min(w, h) / 2 and falls back to a plain quad when near zero.

Adds tests/manual_rounded_image.nim with width/height/radius
scrubbers and a --screenshot mode that dumps the framebuffer to
tmp/rounded_screenshot.png, plus the silkyTesting stub.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@treeform
treeform merged commit b077c1e into master Sep 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant