Clipping And Scrolling (frames)
OSL provides a powerful frame system for clipping content and creating scrollable areas in your applications.
Basic Clipping
The frame command lets you define a rectangular area that clips (masks) any content drawn inside it. Content outside the frame's boundaries will not be visible.
Syntax
frame left top right bottom (
// Content to be clipped goes here
)The frame boundaries are defined by four coordinates:
left: Left edge position (x-coordinate)top: Top edge position (y-coordinate)right: Right edge position (x-coordinate)bottom: Bottom edge position (y-coordinate)
Example: Basic Clipping
// Define frame boundaries
left = -100
right = 100 // Creates a 200px wide frame
top = 100
bottom = -100 // Creates a 200px tall frame
mainloop:
frame left top right bottom (
// Draw a large square that will be clipped
square 10000 10000 20
// Only the portion within the 200x200 frame will be visible
)Scrollable Frames
You can create scrollable areas in two ways:
Two-dimensional scrolling with content width and height
Vertical-only scrolling with just content height
Two-Dimensional Scrolling Syntax
Additional parameters:
[content_width, content_height]: Dimensions of the scrollable content area"frame_id": Unique identifier for the scrollable frame
Vertical-Only Scrolling Syntax
Additional parameters:
content_height: Total height of the scrollable content (integer)"frame_id": Unique identifier for the scrollable frame
Example: Two-Dimensional Scrolling
Example: Vertical-Only Scrolling
Important Notes
Frame Coordinates
Coordinates are relative to the center of the screen
Positive Y goes up, negative Y goes down
Frame size = (right - left) × (top - bottom)
Scrolling Behavior
Scrollbars appear automatically when content exceeds frame size
Scroll position is tracked via
scroll_xandscroll_yvariables orframe.scrollframe.scroll_hrespectivelyContent coordinates are relative to scroll position
Best Practices
Use meaningful frame IDs for easier management
Consider padding inside frames for better appearance
Update content layout based on scroll position
Clean up or reset scroll position when needed
Common Use Cases
Long text documents
Image galleries
Lists and menus
Content that exceeds screen size
Last updated
Was this helpful?