Skip to content

Instantly share code, notes, and snippets.

@patrickfuller
Last active October 10, 2018 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickfuller/4e238344f823fcfced68e42cbf663e00 to your computer and use it in GitHub Desktop.
Save patrickfuller/4e238344f823fcfced68e42cbf663e00 to your computer and use it in GitHub Desktop.
Grid Maker
"""
Create a custom grid-paper design as a minified vector image.
This uses a unique adaptation of "stroke-dasharray" to create the grid
pattern in a very small svg.
Usage:
python grid_maker.py > grid.svg
python grid_mater.py --help
Functionalized to adjust minor and major grid size, color, and grid line
thickness.
"""
import argparse
base = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}">
<g stroke-width="{height}" fill="none" stroke="{color}">
<path stroke-dasharray="{i}" d="M0,{hh:.1f}h{width}"/>
<path stroke-dasharray="{a}" d="M0,{hh:.1f}h{width}"/>
</g>
<g stroke-width="{width}" fill="none" stroke="{color}">
<path stroke-dasharray="{i}" d="M{hw:.1f},0v{height}"/>
<path stroke-dasharray="{a}" d="M{hw:.1f},0v{height}"/>
</g>
</svg>
'''.strip('\n')
def get_dasharray(spacing, stroke):
"""Get stroke-dasharray string from input arguments."""
on = stroke / 100
off = spacing - 2 * on
return '{:.2f},{:.2f},{:.2f},0'.format(on, off, on)
parser = argparse.ArgumentParser(description="Create a custom grid-paper "
"design as a minified vector image.")
parser.add_argument('--width', type=int, default=100, help="Width of "
"the vector image.")
parser.add_argument('--height', type=int, default=100, help="Height of "
"the vector image.")
parser.add_argument('--color', type=str, default="#444", help="Grid "
"color, as RGB hex.")
parser.add_argument('--minor', type=int, default=1, help="Minor grid size, "
"relative to vector image dimensions.")
parser.add_argument('--major', type=int, default=10, help="Major grid size, "
"relative to vector image dimensions.")
parser.add_argument('--minor-stroke', type=int, default=2, help="Minor stroke "
"width.")
parser.add_argument('--major-stroke', type=int, default=5, help="Major stroke "
"width.")
args = parser.parse_args()
if not 1 <= args.width <= 1000:
raise ValueError("Only accepting widths 1-1000")
if not 1 <= args.height <= 1000:
raise ValueError("Only accepting heights 1-1000")
svg = base.format(
width=args.width,
height=args.height,
color=args.color,
hw=args.width / 2,
hh=args.height / 2,
i=get_dasharray(args.minor, args.minor_stroke),
a=get_dasharray(args.major, args.major_stroke),
)
print(svg)
@patrickfuller
Copy link
Author

Example usage:

python grid_maker.py

grid

python grid_maker.py --width=41 --height=70 --color=blue

grid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment