-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path07-fit.py
37 lines (29 loc) · 1.15 KB
/
07-fit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from nodeboxgl.graphics import *
# This example demonstrated how to fit text to a path using the directed() command
# (thanks to Karsten Wolf).
# Create a Text object for each character.
txt = "NodeBox for OpenGL"
glyphs = [Text(ch, fontname="Droid Sans Mono") for ch in txt]
def draw(canvas):
background(1)
# Create a path where the mouse position controls the curve.
dx = canvas.mouse.x
dy = canvas.mouse.y
path = BezierPath()
path.moveto(100, 250)
path.curveto(200, 250, dx, dy, 400, 250)
# Calculate points on the path and draw a character at each points.
# The directed() command yields (angle, point)-tuples.
# The angle can be used to rotate each character along the path curvature.
points = path.points(amount=len(glyphs), start=0.05, end=0.95)
for i, (angle, pt) in enumerate(directed(points)):
push()
translate(pt.x, pt.y)
rotate(angle)
text(glyphs[i], x=-textwidth(glyphs[i])/2)
pop()
drawpath(path, fill=None, stroke=(0,0,0,0.5))
line(path[-1].x, path[-1].y, dx, dy, stroke=(0,0,0,0.1))
canvas.fps = 30
canvas.size = 500, 500
canvas.run(draw)