|
| 1 | +"""Demonstrates antialiasing for Lines, Points, and Text. |
| 2 | +
|
| 3 | +Three nodes are shown — a diagonal line, a disc point, and a text label — |
| 4 | +each supporting the ``antialias`` property. |
| 5 | +
|
| 6 | +**Interaction** |
| 7 | +- Left-click anywhere in the view to toggle antialiasing on all nodes. |
| 8 | + The status text updates to reflect the current state. |
| 9 | +
|
| 10 | +Antialiasing is most noticeable on the diagonal line (jagged stairstepping |
| 11 | +appears on its edges without AA) and on the curved edge of the disc. |
| 12 | +""" |
| 13 | + |
| 14 | +import cmap |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +import scenex as snx |
| 18 | +from scenex.app.events import Event, MousePressEvent |
| 19 | +from scenex.utils import projections |
| 20 | + |
| 21 | +# --- Diagonal line on the left side --- |
| 22 | +# A steep diagonal makes aliasing (stairstepping) clearly visible. |
| 23 | +line = snx.Line( |
| 24 | + vertices=np.array([[-0.7, -0.5, 0], [0.1, 0.6, 0]], dtype=np.float32), |
| 25 | + color=snx.UniformColor(color=cmap.Color("white")), |
| 26 | + width=2.0, |
| 27 | + antialias=True, |
| 28 | +) |
| 29 | + |
| 30 | +# --- Single disc point on the right side --- |
| 31 | +point = snx.Points( |
| 32 | + vertices=np.array([[0.65, 0.0, 0]], dtype=np.float32), |
| 33 | + size=30, |
| 34 | + scaling="fixed", |
| 35 | + face_color=snx.UniformColor(color=cmap.Color("orange")), |
| 36 | + edge_color=snx.UniformColor(color=cmap.Color("white")), |
| 37 | + edge_width=2.0, |
| 38 | + antialias=True, |
| 39 | +) |
| 40 | + |
| 41 | +# --- Status label at the bottom --- |
| 42 | +status_text = snx.Text( |
| 43 | + text="Antialiasing: ON (click to toggle)", |
| 44 | + color=cmap.Color("yellow"), |
| 45 | + size=14, |
| 46 | + antialias=True, |
| 47 | + transform=snx.Transform().translated((0, -0.8, 0)), |
| 48 | +) |
| 49 | + |
| 50 | +view = snx.View( |
| 51 | + scene=snx.Scene(children=[line, point, status_text]), |
| 52 | + camera=snx.Camera(), |
| 53 | +) |
| 54 | + |
| 55 | + |
| 56 | +def _toggle_antialias(event: Event) -> bool: |
| 57 | + if isinstance(event, MousePressEvent): |
| 58 | + # Toggle the antialiasing state for all nodes and update the status text. |
| 59 | + new_aa = not line.antialias |
| 60 | + line.antialias = new_aa |
| 61 | + point.antialias = new_aa |
| 62 | + status_text.antialias = new_aa |
| 63 | + state = "ON" if new_aa else "OFF" |
| 64 | + status_text.text = f"Antialiasing: {state} (click to toggle)" |
| 65 | + return True |
| 66 | + return False |
| 67 | + |
| 68 | + |
| 69 | +view.set_event_filter(_toggle_antialias) |
| 70 | + |
| 71 | +snx.show(view) |
| 72 | +view.camera.projection = projections.orthographic(2, 2, 1e5) |
| 73 | +snx.run() |
0 commit comments