NodeBox 变量
预置的变量
NodeBox 配置了一系列的预置变量,这些变量只能读,而不能写。
WIDTH: 绘画区域的宽度,可以通过size(300,300)来定义
HEIGHT: 绘画区域的高度,可以通过size(300,300)来定义
PAGENUM: 在多页的导出中可以显示当前页的页码
FRAME: 在动画中显示当前帧数
下面一些能够用在交互中:
MOUSEX: 当前鼠标的水平坐标
MOUSEY: 当前鼠标的垂直坐标
mousedown: 当鼠标按下时,值为True
keydown: 当相关的key按下时,值为True
key: 最后一个按下的Key值
keycode: 上一个按键的值,整数的形式。
KEY_UP, KEY_DOWN_ KEY_LEFT, KEY_RIGHT, KEY_BACKSPACE contain the keycodes for the arrow keys and the backspace key.
Node 输入参数
var("label",TEXT)
var("x",NUMBER,50,1,100)
var("y",NUMBER,50,1,100)
text(label,x,y)
var变量的参数
var(name, NUMBER, default, min, max)
var(name, TEXT, default="hello")
var(name, BOOLEAN)
var(command, BUTTON)
NodeBox 列表
列表
NodeBox中的列表和代码中的列表是类似的,同样是以0为起始索引。实际上就是使用了python中的列表类型。
列表的一个很有趣的demo:
size(200,200)
words = ["Lists", "are", "fun"]
colors = [color(1,0,0), color(1,1,1), color(0,0,0)]
for i in range(40):
x = random(WIDTH)
y = random(HEIGHT)
rotate(random(360))
fontsize(random(10,100))
fill(choice(colors))
text(choice(words), x, y)
这个例子从world和colors中随机选择颜色的字体,在页面上随机放置。
形成的图片如下:
多次运行可以形成不同的图片样式,这种很多海报会死用类似的方式,用杂乱的排版来形成比较好看的广告。
字典值:
字典值dictionaries ,直接使用python的数据类型。
String 字符串变量
String 是一个列表
其实这也是python的基本语法的例子,不过给出了一个demo还是挺好的,作为相应的例子,把几个方法也给我们展示了一下。
size(400,400)
fill(0.2)
rect(0,0,WIDTH,HEIGHT)
fill(1)
stroke(0.2)
strokewidth(1)
font("Zapfino")
for i in range(80):
fontsize(random(400))
rotate(random(360))
chars = "abcdefghijklmnopqrstuvwxyz"
p = textpath(choice(chars), random(WIDTH), random(HEIGHT))
drawpath(p)
这里用到几个比较重要的概念,使用choice(chars)说明chars是一个列表结构。
textpath代表了文本的描边,使用drawpath 可以对描边进行绘画。
运行结果如下:
String的一些相关方法
下面是String的一些相关方法
- string.upper(): returns the string in uppercase.
- string.lower(): returns the string in lowercase.
- string.capitalize(): returns the string with the first character capitalized.
- string.find(text, start=0): returns the index position of text in the string.
- string.replace(old, new): replaces all old text in the string with new
- string.split(): returns the string as a list of words.
- string.join(list): concatenates a list of words.
这里也基本上都是python的一些基本方法
文件读入
size(500,500)
str = open("Applications/NodeBox/mo.txt").read()
str = str.decode("utf-8")
fill(0.2)
rect(0,0,WIDTH,HEIGHT)
fill(1)
font(u"黑体",13)
text(u"中国体育代表团", 20,30)
font(u"兰亭黑-简",11)
lineheight(1.4)
text(str,20,50,width=450)
使用文件读入方式来来读入相应的字符串。 如果为中文的话,需要使用decode一次utf-8。
最后的执行结果