Created
June 10, 2026 02:29
-
-
Save GaryLee/d9b69967ac7bb60979255f498bf5a39d to your computer and use it in GitHub Desktop.
A example to generate SVG with 10x10 text grid with clipping setting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # The script uses svg.py to generate SVG files. | |
| from svg import SVG, Circle, Line, Path, Text, Rect, ClipPath, Path, Defs, G | |
| import numpy as np | |
| def main(): | |
| canvas_width = 600 | |
| canvas_height = 500 | |
| padding_x = 10 | |
| padding_y = 10 | |
| x_points = np.linspace(0, canvas_width, 10+1) | |
| y_points = np.linspace(0, canvas_height, 10+1) | |
| width = x_points[1] - x_points[0] | |
| height = y_points[1] - y_points[0] | |
| clip_padding = 2 | |
| elements = [] | |
| elements.append( | |
| Defs(elements=[ | |
| ClipPath(id="clip", elements=[ | |
| Rect(x=clip_padding, y=clip_padding, width=width-2*clip_padding, height=height-2*clip_padding, fill="none", stroke="none") | |
| ]) | |
| ]) | |
| ) | |
| for x in x_points[:-1]: | |
| x = x + padding_x | |
| for y in y_points[:-1]: | |
| y = y + padding_y | |
| elements.append( | |
| G(transform=f"translate({x}, {y})", elements=[ | |
| Rect(x=0, y=0, width=width, height=height, fill="white", stroke="black", stroke_width=2), | |
| Text(text=f"({x:.0f}, {y:.0f})", x=width/2, y=height/2, font_size=20, fill="black", text_anchor="middle", dominant_baseline="middle", clip_path="url(#clip)"), | |
| ]) | |
| ) | |
| canvas = SVG( | |
| width=canvas_width + 2 * padding_x, | |
| height=canvas_height + 2 * padding_y, | |
| elements=elements, | |
| ) | |
| with open("canvas.svg", "w") as f: | |
| f.write(str(canvas)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment