# Difference between revisions of "Calico Processing Reference"

Click on a command for a definition and an example of usage. (Return to Calico Processing.)

### 2D Primitives

Draw an arc defined as a fragment of an ellipse.

arc(x, y, width, height, start-angle, end-angle)

This example draws an arc that is a part of the ellipse centered at (100, 100), with width 150 and height 75, starting at an angle of 0 radians and ending at angle that is the number of radians equivalent to 45°.

```ellipseMode(CENTER)
arc( 100, 100, 150, 75, 0, radians(45) )
```

Draw an ellipse using the current settings.

ellipse(x, y, width, height)

This example draws an ellipse with upper left corner at (100, 100), and a width of 150 and height of 75.

```ellipseMode(CORNER)
ellipse( 100, 100, 150, 75 )
```

Draw a line from (x1, y1) to (x2, y2).

line(x1, y1, x2, y2)

This example draws from the upper left corner of the sketch window to the lower right corner, assuming no transformations ahve been applied.

```line( 0, 0, width(), height() )
```

Draw a point at coordinate (x, y).

point(x, y)

This example draws a large point at the center of the sketch window.

```strokeWeight(10)
point( 0.5*width(), 0.5*height() )
```

Draw a four-sided polygon with vertexes (x1, y1), (x2, y2), (x3, y3), (x4, y4).

quad(x1, y1, x2, y2, x3, y3, x4, y4)

This example draws a yellow diamond shape with a thick border.

```strokeWeight(5)
fill(255, 255, 0)
quad(100, 100, 75, 150, 100, 200, 125, 150)
```

Draw an ellipse using the current settings.

rect(x, y, width, height)

This example draws a blue rectangle with upper left corner at (100, 100) and a width of 200 and height of 150.

```rectMode(CORNER)
fill(0, 0, 255)
rect(100, 100, 200, 150)
```

Draw a three-sided polygon with vertexes (x1, y1), (x2, y2), (x3, y3).

triangle(x1, y1, x2, y2, x3, y3)

This example draws a red right-triangle with the given vertexes.

```fill(255, 0, 0)
triangle(100, 100, 100, 200, 200, 200)
```

### Curves

Determines level of smoothness at spline points for shapes created with curveVertex(). A value of 0.0 is not smooth. Values > 0.0 increase smoothness.

curveTightness(squishy)

Sets smoothness for all future curves to 0.5.

```curveVertex( 0.5 )
```

### Shape Attributes

Sets the meaning of the arguments passed to ellipse() and arc() commands. CENTER is default.

CORNER : The arguments x and y define the upper left corner, and w and h define the ellipse width and height.

CORNERS : The arguments x and y define the upper left corner, and w and h define the lower right corner of ellipse.

CENTER : The arguments x and y define the center of the ellipse, and w and h the width and height.

RADIUS : The arguments x and y define the center of the ellipse, and w and h the radius

• Note that the mode argument can be the built-in constant or a string.

ellipseMode(mode)

Sets ellipseMode to CORNER using built-in constant.

```ellipseMode( CORNER )
```

Sets the meaning of the arguments passed to rect() command. CORNER is default.

CORNER : The arguments x and y define the upper left corner, and w and h define the rect width and height.

CORNERS : The arguments x and y define the upper left corner, and w and h define the lower right corner of rect.

CENTER : The arguments x and y define the center of the rect and w and h the width and height.

RADIUS : The arguments x and y define the center of the rect and w and h the radius.

• Note that the mode argument can be the built-in constant or a string.

rectMode(mode)

Sets rectMode to CENTER using a string argument.

```rectMode( "CENTER" )
```

Turn on subpixel antialiasing.

smooth()

Turns on automatic smoothing.

```smooth()
```

Turn off subpixel antialiasing.

noSmooth()

Turns off automatic smoothing.

```noSmooth()
```

Set the style for line ends. Default is ROUND.

SQUARE :

ROUND :

PROJECT :

• Note that the style argument can be the built-in constant or a string.

strokeCap(style)

Sets line ends to be drawn square.

```strokeCap( SQUARE )
```

Set the style for the way line segments are joined. Default is MITER.

BEVEL :

ROUND :

MITER :

• Note that the style argument can be the built-in constant or a string.

strokeJoin(style)

Sets line joints to be rounded.

```strokeJoin( "ROUND" )
```

Sets the weight of stroked lines. Can be any integer >=1. Default is 1.

strokeWeight(weight)

Sets line width to 3 pixels.

```strokeWeight( 3 )
```

### Vertex

Starts accumulating points to be included in a new shape.

beginShape()

Begin a new shape.

```beginShape()
```

Adds a point to a shape, connected from the previous point by a straight line.

vertex(x, y)

Add a vertex to a shape at coordinates 100, 100.

```vertex( 100, 100 )
```

Adds a point to a shape, connected from the previous point by a smooth line, the degree of smoothness determined by the parameter passed to the curveTightness() function.

curveVertex(x, y)

Create a smooth closed shape that is made up of several points from a list.

```pts = [(100, 250), (50, 80), (200, 100), (300, 80), (250, 240)]
beginShape()
for p in pts:
curveVertex(p, p)
endShape(True)
```

Adds a point to a shape, connected from the previous point by a smooth line, with line end slopes determined by the lines that the curve segment endpoints makes with control points (cx1, cy1) and (cx2, cy2).

bezierVertex(cx1, cy1,cx2, cy2, x, y)

Create an interesting sigmoidal curve starting at a high plateau and ending at a low plateau.

```noFill()
beginShape()
vertex(100, 100)
bezierVertex( 200, 100, 200, 100, 200, 200 )
bezierVertex( 200, 300, 200, 300, 300, 300 )
endShape()
```

Stops accumulating shape points and renders a shape. If TorF is True, the shape is closed.

endShape([TorF])

End a shape and close it by connecting the first and last points.

```endShape( True )
```

## Structure

Delay the program for the specified number of milliseconds.

delay(millis)

In addition to a brief pause in execution, the delay() function will allow the sketch to update. It is useful when choosing to execute a main loop that runs an animation or some other periodic check of state. This Python example draws randomly placed ellipses until the mouse is pressed.

```from Processing import *

window(500, 500)

while True:
ellipse( random(width()), random(height()), 30, 30 )
if isMousePressed() == True: break
delay(100)

exit()
```

This second example continuously draws randomly located lines, but also simultaneously processes mousePressed events.

```from Processing import *
window(600, 400)

# A function that prints the current mouse coordinates
def printMouse():
print( mouseX(), mouseY() )

# Print mouse coordinates when the mouse is pressed
onMousePressed += printMouse

# Continuously draw random lines
while True:
x1, y1 = random(0, 600), random(0, 400)
x2, y2 = random(0, 600), random(0, 400)
line(x1, y1, x2, y2)
delay(200)
```

Start the internal looping engine that periodically fires the onLoop event handler.

loop()

This Python example fires the printSecond() function every 1000 milliseconds (1 second).

```from Processing import *

window(500, 500)

def printSecond(o, e):
print( second() )

frameRate(1000)
onLoop += printSecond
loop()
```

Stop the internal looping engine, so that the onLoop event handler will not be called.

noLoop()

```noLoop()
```

A synonym for loop()

startLoop()

```startLoop()
```

A synonym for noLoop()

stopLoop()

```stopLoop()
```

Redraw the sketch on demand. Useful when immediateMode(False) is used.

redraw()

```redraw()
```

This is the event raised when loop() or startLoop() is called. Handle the onLoop event by adding an event handler function to onLoop using the += operator.

onLoop()

See loop() for an example. Note that all event handler functions must take two arguments - the first argument is the internal PTimer object that raised the event, and the second is the PElapsedEventArgs object containing arguments associated with the event.

```onLoop += myLoopHandler
```

Sets the frames per second to display when using the onLoop event.

frameRate(fps)

Calls the onLoop event hander 10 times per second.

```frameRate( 10 )
```

Process any pending events.

doEvents()

Continues to handle events while in a while-loop.

```while True:
doEvents()
```

## Environment

Creates a new sketch window.

window( width, height )

Draws a magic 8-Ball.

```from Processing import *

# Open an window and fill background with black
window( 600, 400 )
background( 0 )

# Draw a white circle in the center
fill(255)
ellipse( 300, 200, 200, 200 )

# Draw a large "8"
fill(0)
textSize(100)
text("8", 265, 245 )
```

Returns True if window currently has focus. False otherwise.

focused()

Prints 'click me' if the window does not have focus.

```if focused() == False:
print( 'click me' )
```

Returns the number of frames that have been displayed thus far.

frameCount()

Prints frame count.

```print( str( frameCount() ) + ' frames have been displayed' )
```

Puts the sketch window in fullscreen mode using the entire display.

fullscreen()

Use an entire laptop screen for this sketch.

```window(1024,768)
fullscreen()
```

Leave fullscreen mode.

unfullscreen()

Returns the window height.

height()

Set the window height to the variable h.

```h = height()
```

Returns the window width.

width()

Set the window width to the variable w.

```w = width()
```

Returns the width of the screen.

displayWidth()

Store the width of the screen using a variable w.

```w = displayWidth()
```

Returns the height of the screen.

displayHeight()

Store the height of the screen using a variable h.

```h = displayHeight()
```

Resize the sketch window.

size(width, height)

Dynamically resize the sketch window to a width of 600 and a height of 400.

```size( 600, 400 )
```

If TorF is True, the sketch is updated after every draw. If False, call redraw() to update the sketch.

immediateMode(TorF)

Turns off immediate mode.

```immediateMode( False )
```

If TorF is True, the window remains above others, even if it loses focus.

keepAbove(TorF)

Sets window to remain above others.

```keepAbove( True )
```

### Mouse

Returns the current mouse x-coordinate.

mouseX()

Sets x to current mouse x-coordinate.

```x = mouseX()
```

Returns the current mouse y-coordinate.

mouseY()

Sets y to current mouse y-coordinate.

```y = mouseY()
```

Returns the previous mouse x-coordinate.

pmouseX()

Draws a line from previous to current mouse position.

```line( pmouseX(), pmouseY(), mouseX(), mouseY() )
```

Returns the previous mouse y-coordinate.

pmouseY()

Draws a line from previous to current mouse position.

```line( pmouseX(), pmouseY(), mouseX(), mouseY() )
```

Returns a number representing the mouse button currently pressed. 1 for left mouse button, 2 for center mouse button, 3 for right mouse button, and 0 for no mouse buttons pressed.

mouseButton()

Prints string when left mouse is pressed.

```if mouseButton() == 1:
print( 'Left mouse pressed' )
```

Returns True if mouse is pressed. False otherwise.

isMousePressed()

Prints string when any mouse button is pressed.

```if isMousePressed() == True:
print( 'mouse is pressed' )
```

Event raised when mouse is moved but not pressed.

onMouseMoved

Prints 'mouse moved' whenever the mouse is moved with no buttons pressed.

```def doMouseMoved(o, e):
print( 'mouse moved')

onMouseMoved += doMouseMoved
```

Event raised when mouse is pressed and moved.

onMouseDragged

Implements a simple scribble program by drawing a line from the previous mouse position to the current mouse position each time the mouse is dragged.

```def doMouseDragged(o, e):
line(pmouseX(), pmouseY(), mouseX(), mouseY())

onMouseDragged += doMouseDragged
```

Event raised when mouse is pressed.

onMousePressed

Prints the mouse location when pressed.

```def doMousePressed(o, e):
print( 'mouse pressed at ' + str(mouseX()) + ', ' + str(mouseY()) )

onMousePressed += doMousePressed
```

Event raised when mouse is released.

onMouseReleased

Prints the mouse location when released.

```def doMouseReleased(o, e):
print( 'mouse released at ' + str(mouseX()) + ', ' + str(mouseY()) )

onMouseReleased += doMouseReleased
```

Event raised when mouse is pressed and immediately released at the exact same sketch location.

onMouseClicked

Prints the mouse location when the mouse is clicked.

```def doMouseClicked(o, e):
print( 'mouse clicked at ' + str(mouseX()) + ', ' + str(mouseY()) )

onMouseClicked += doMouseClicked
```

### Keyboard

Returns the Gdk.Key value of the key currently pressed or 0 if none pressed.

key()

Prints the current key pressed.

```print( key() )
```

Returns the numeric code of the key currently pressed or 0 if none pressed.

keyCode()

Prints the code of the current key pressed.

```print( keyCode() )
```

Returns True if a key is pressed. False otherwise.

isKeyPressed()

Checks if a key is pressed every hald second, and prints the key and its code if pressed.

```while True:
if isKeyPressed() == True:
print( 'key pressed is ' + str(key()) + ' and has code ' + str( keyCode() ) )
delay(500)
```

Event raised when a key is pressed.

onKeyPressed

Prints the key and keyCode when a key is pressed.

```def doKeyPressed(o, e):
print( key(), keyCode() )

onKeyPressed += doKeyPressed
```

Event raised when a key is released.

onKeyReleased

Prints 'key up' when the key is released.

```def doKeyReleased(o, e):
print( 'key up' )

onKeyReleased += doKeyReleased
```

### Conversion

Attempts to convert a value to a binary representation in the form of a string of 1s and 0s

toBinary(val)

Prints the binary representation of 255, which is 11111111

```print( toBinary(255) )
```

Attempts to convert val to True or False

toBoolean(val)

Converts the string "True" to the boolean True and prints it.

```print( toBoolean( "True" ) )
```

Attempts to convert val to a byte in the range [0, 255]

toByte(val)

Converts the integer 128 to a byte and prints it.

```print( toByte( 128 ) )
```

Attempts to convert val to a character

toChar(val)

Converts the code 65 to 'A' and prints it

```print( toChar( 65 )
```

Attempts to convert val to a floating point number

toFloat(val)

Converst the integer -12345 to a floating point number and prints it.

```print( toFloat( -12345 ) )
```

Attempts to convert val to a double precision number

toDouble(val)

Converts the integer -1234567890 to a double precision number and prints it.

```print( toDouble( -1234567890 ) )
```

Attempts to convert val to a 32 bit integer

toInt(val)

Converts the number -12345.678 to the integer -12345 and prints it.

```print( toInt( -12345.678 ) )
```

Attempts to convert val to a 64 bit integer

toLong(val)

Converts the number -1234567890.098 to the long -1234567890 and prints it.

```print( toLong( -1234567890.098 ) )
```

Attempts to convert val to a string

toString(val)

Converts the boolean False to the string "False" and prints it.

```print( toString( False ) )
```

Attempts to convert val to a hexadecimal representation in the form of a string

toHex(val)

Converts the number 255 to its hex representation "FF" and prints it.

```print( toHex( 255 ) )
```

Attempts to convert a hex string to an integer

fromHex(str)

Converts the hex string "FF" to its numeric representation of 255 and prints it.

```print( fromHex( "FF" ) )
```

Attempts to convert a string of 1s and 0s to an integer

fromBinary(str)

Converts the bit string "1111" to the integer 15 and prints it.

```print( fromBinary( "1111" ) )
```

### Time & Date

Return the number of milliseconds the sketch window has existed.

millis()

Set the variable m to the number of milliseconds since the sketch window was created, if one exists.

```m = millis()
```

Returns the number of seconds that have passed on the system clock in the current minute. This function will only return numbers from 0 to 59.

second()

Sets the variable s to the current number of seconds on the system clock.

```s = second()
```

Returns the number of minutes that have passed on the system clock in the current hour. This function will only return numbers from 0 to 59.

minute()

Sets the variable m to the current number of minutes on the system clock.

```m = minute()
```

Returns the number of hours that have passed on the system clock in the current day. This function will only return numbers from 0 to 23.

hour()

Sets the variable h to the current number of hours on the system clock.

```h = hour()
```

Returns the number of the current day in the current month. This function will only return numbers from 1 to 31.

day()

Sets the variable d to a number that represents the current day of the month.

```d = day()
```

Returns the number of the current month in the current year. This function will only return numbers from 1 to 12.

month()

Sets the variable d to a number that represents the current day of the month.

```m = month()
```

Returns the current year.

year()

Sets the variable y to the current year.

```y = year()
```

## Transform

Saves the current transformation.

pushMatrix()

```pushMatrix()
```

Restores the last saved tranformation.

popMatrix()

```popMatrix()
```

Clears all transformations previously applied.

resetMatrix()

```resetmatrix()
```

Rotates the current coordinates system by angle radians about the point 0, 0.

rotate(angle)

Rotate the coordinate system by 45 degrees, draw a rotated ellipse, and then restore the coordinate system to the settings before the rotation.

```pushMatrix()
ellipse(100, 100, 75, 50)
popMatrix()
```

Scales the current coordinate system by a factor of s about the point 0, 0.

scale(s)

Scale the coordiante system by a factor of 2 in both directions.

```scale( 2 )
```

Scales the current coordinate system by a factor of sx in the x-direction and sy in the y-direction, all about the point 0, 0.

scale(sx, sy)

Draw a circle that looks like an ellipse due to a non-uniform scaling in the x- and y-directions.

```scale( 2, 1 )
ellipse( 100, 100, 50, 50 )
```

Translates the current coordinate system by tx in the x-direction and ty in the y-direction.

translate(tx, ty)

Draw a rectangle at 100, 100 by translating the coordinate system.

```translate( 100, 100 )
rect( 0, 0, 50, 75 )
```

### Color Setting

Fill the sketch background with a color defined by red, green, blue and alpha. All values are in the range [0, 255].

background(red, green, blue, alpha)

Fills the background with a semitransparent red.

```background( 255, 0, 0, 128)
```

Fill the sketch background with a color defined by red, green and blue. All values are in the range [0, 255].

background(red, green, blue)

Fills the background with a pure yellow.

```background( 255, 255, 0)
```

Fill the sketch background with a color based on gray and alpha, where gray used for red, green and blue values. All values are in the range [0, 255].

background(gray, alpha)

Fills the background with a semitransparent black.

```background( 0, 128 )
```

Fill the sketch background with a color in which gray is used for red, green and blue values. All values are in the range [0, 255].

background(gray)

Fills the background with gray.

```background( 128 )
```

Fill the sketch background with a color value created with the color() command.

background(color)

Fills the background with blue created with the color() command.

```blue = color( 0, 0, 255 )
background( blue )
```

Fill future shapes with a color defined by red, green, blue and alpha. All values are in the range [0, 255].

fill(red, green, blue, alpha)

Fills new shapes with a semitransparent red.

```fill( 255, 0, 0, 128)
```

Fill future shapes with a color defined by red, green and blue. All values are in the range [0, 255].

fill(red, green, blue)

Fills new shapes with a pure yellow.

```fill( 255, 255, 0)
```

Fill future shapes with a color based on gray and alpha, where gray used for red, green and blue values. All values are in the range [0, 255].

fill(gray, alpha)

Fills new shapes with a semitransparent black.

```fill( 0, 128 )
```

Fill future shapes with a color in which gray is used for red, green and blue values. All values are in the range [0, 255].

fill(gray)

Fills new shapes with gray.

```fill( 128 )
```

Fill future shapes with a color value created with the color() command.

fill(color)

Draw a blue rectangle.

```blue = color( 0, 0, 255 )
fill( blue )
rect(0, 0, 50, 50)
```

Turn off shape filling.

noFill()

Draw a rectangle with black outline and no fill color.

```noFill()
stroke(0)
rect(0, 0, 50, 50)
```

Stroke future shapes with a color defined by red, green, blue and alpha. All values are in the range [0, 255].

stroke(red, green, blue, alpha)

Strokes new shapes with a semitransparent red.

```stroke( 255, 0, 0, 128)
```

Stroke future shapes with a color defined by red, green and blue. All values are in the range [0, 255].

stroke(red, green, blue)

Strokes new shapes with a pure yellow.

```stroke( 255, 255, 0)
```

Stroke future shapes with a color based on gray and alpha, where gray used for red, green and blue values. All values are in the range [0, 255].

stroke(gray, alpha)

Strokes new shapes with a semitransparent black.

```stroke( 0, 128 )
```

Stroke future shapes with a color in which gray is used for red, green and blue values. All values are in the range [0, 255].

stroke(gray)

Strokes new shapes with gray.

```stroke( 128 )
```

Stroke future shapes with a color value created with the color() command.

stroke(color)

Draw a rectangle with blue outline.

```blue = color( 0, 0, 255 )
stroke( blue )
rect(0, 0, 50, 50)
```

Turn off shape stroke.

noStroke()

Draw a blue filled rectangle no outline.

```noStroke()
fill(0)
rect(0, 0, 50, 50)
```

Extract the red component from the color clr.

red(clr)

Print the red component of the color clr.

```print( red( clr ) )
```

Extract the green component from the color clr.

green(clr)

Print the green component of the color clr.

```print( green( clr ) )
```

Extract the blue component from the color clr.

blue(clr)

Swap the red and blue components of the color clr.

```r = red( clr )
b = blue( clr )
g = green( clr )
clr = color( b, r, g )
```

Extract the transparency component from the color clr.

alpha(clr)

Print the transparencey of the color clr.

```print( alpha( clr ) )
```

Extract the hue from the color clr.

hue(clr)

Print the hue of the color clr.

```print( hue( clr ) )
```

Extract the saturation from the color clr.

saturation(clr)

Print the saturation of the color clr.

```print( saturation( clr ) )
```

Extract the brightness from the color clr.

brightness(clr)

Print the brightness of the color clr.

```print( brightness( clr ) )
```

Build and return a new color from the red, blue, green and alpha byte values.

color(red, green, blue, alpha)

Create a semitransparent red color.

```semi = color (255, 0, 0, 128)
```

Build and return a new color from the red, blue and green byte values.

color(red, green, blue)

Create a yellow color.

```yellow = color( 255, 255, 0)
```

Build and return a new color using the gray byte as the values for red, green and blue, and alpha for transparency.

color(gray, alpha)

Create a semitransparent gray.

```semi = color( 255, 128 )
```

Build and return a new color using the gray byte as the values for red, green and blue.

color(gray)

Create a mid-scale gray color.

```gray = color( 128  )
```

## Image

An object that represents an image.

PImage()

Load an image from a file and draw it on the window at coordinates 10, 10.

```img = loadImage("butterfly.png")
image( img, 10, 10 )
```

There is support for moving an Image from Calico Processing to Calico Graphics, and back:

```from Myro import makePicture
from Processing import createImage

p = makePicture(100, 100)
i = createImage(p.toBitmap())   # turn Graphics.Picture into a Bitmap and load in Processing
p2 = makePicture(i.toBitmap())  # turn Processing.Image into a Bitmap and load in Graphics
```

Creates and returns a new Pimage of size width by height in the format. Valid formats include RGB, ARGB and ALPHA. Format arguments can be provided as built-in constants or strings.

createImage(width, height, format)

Create a new Pimage, fill all pixels with the color red, and draw at coordinates 20, 20

```img = createImage(100, 100, ARGB)
for r in range( 100 ):
for c in range( 100 ):
img.setPixel(c, r, color(255, 0, 0) )
img.updatePixels()
image( img, 20, 20 )
```

Draw the Pimage img at coordinates x, y

image(img, x, y)

Load an image from a file and draw it on the window at coordinates 10, 10.

```img = loadImage("butterfly.png")
image( img, 10, 10 )
```

Draw the Pimage img at coordinates x, y, with the specified width and height

image(img, x, y, width, height)

Load an image from a file and draw it on the window at coordinates 10, 10 with a width of 100 pixels and a height of 75 pixels.

```img = loadImage("butterfly.png")
image( img, 10, 10, 100, 75 )
```

Sets the mode for drawing images. Can be CORNER, CENTER or CORNERS, or strings of the same name.

imageMode(mode)

Draw an image named img centered at coordinates 250, 250

```imageMode(CENTER)
image( img, 250, 250 )
```

Loads the file at filepath and returns a new PImage.

Load an image from a file and draw it on the window at coordinates 10, 10.

```img = loadImage("butterfly.png")
image( img, 10, 10 )
```

Copies the current sketch window and returns as a new PImage.

get()

Get the current sketch as a new PImage and save in a variable named mySketch.

```mySketch = get()
```

Copies a portion of the sketch window sketch and returns as a new PImage. The portion of the window copied has upper left corner of (x, y) and dimensions width by height.

get(x, y, width, height)

Get part of the current sketch with an upper left corner of (100, 100) and a width of 50, and height of 75. Assign the new PImage returned to the variable imagePart.

```imagePart = get(100, 100, 50, 75)
```

### Pixels

Copies all color data out of the image or sketch window into an internal memory buffer, making it available to the getPixel() and setPixel() functions.

Load all pixels from the sketch window making them available for modification.

```loadPixels()
```

Gets and returns the pixel color at column i, row j.

getPixel(i, j)

Get the pixel color at column 100, row 75 and set to the variable clr.

```clr = getPixel( 100, 75 )
```

Set the pixel color at column i, row j.

setPixel(i, j, color)

Set the pixel color at column 100, row 75 to blue.

```setPixel( 100, 75, color(0, 0, 255) )
```

Copies all color data out of the internal memory buffer back to the image or sketch window, making it available to be drawn with image() .

updatePixels()

Copies pixels from the memory buffer back to the sketch window.

```updatePixels()
```

Draw the text txt such that the lower left corner of the box surrounding the text is at the coordinate x, y.

text(txt, x, y)

Draw the text "Blah" above and to the right of the coodinate 100, 100.

```text( "Blah", 100, 100)
```

Draw the text txt within the box with upper left corner at x,y and width w, height h.

text(txt, x, y, w, h)

Draw "Blah" in the rectangle with the corner at 100, 100 and width 200, height 50.

```text( "Blah", 100, 100, 200, 50 )
```

Draw <a href = "#http://www.pygtk.org/docs/pygtk/pango-markup-language.html >markup text</a> txt such that the lower left corner of the box surrounding the text is at the coordinate x, y.

markup(txt, x, y)

Draw the text "Blah" boldly above and to the right of the coodinate 100, 100.

```markup( "<b>Blah</b>", 100, 100)
```

Draw <a href = "#http://www.pygtk.org/docs/pygtk/pango-markup-language.html >markup text</a> txt within the box with upper left corner at x,y and width w, height h.

markup(txt, x, y, w, h)

Draw bold "Blah" in the rectangle with the corner at 100, 100 and width 200, height 50.

```text( "<b>Blah</b>", 100, 100, 200, 50 )
```

### Text Attributes

Set the size of text to be drawn with a factor that roughly equals the text point size.

textSize(size)

Set text size to 12 pts.

```textSize( 12 )
```

Set the font.

textFont(fontName)

Set font family to Courier.

```textFont( "Courier" )
```

List the available fonts.

listFonts()

Use the first font.

```fonts = listFonts()
textFont(fonts)
```

Returns the width of the string text in pixels.

textWidth(text)

Estimate the width of the text "Blah" and save to twidth.

```twidth = textWidth( "Blah" )
```

Returns the height of the string text in pixels.

textHeight(text)

Estimate the height of the text "Blah" and save to theight.

```theight = textHeight( "Blah" )
```

Determines how text will be aligned horizontally within its rectangle. The parameter xalign can be one of the constants LEFT, CENTER or RIGHT, or the string equivalents.

textAlign(xalign)

Draw "Blah" aligned to the right side of the specified rectangle.

```textAlign( RIGHT )
text( "Blah", 100, 100, 200, 50 )
```

The parameter xalign determines how text will be aligned horizontally within its rectangle. It can be one of the constants LEFT, CENTER or RIGHT, or the string equivalents. The parameter yalign determines how text will be aligned vertically within its rectangle. It can be one of the constants TOP, CENTER, BOTTOM or BASELINE, or the string equivalents.

textAlign(xalign, yalign)

Draw "Blah" in the lower right corner of the specified rectangle.

```textAlign( RIGHT, BASELINE )
text( "Blah", 100, 100, 200, 50 )
```

### Calculation

Calculates and returns the closest integer greater than x.

ceil(x)

Returns 3

```ceil(2.3)
```

Calculates and returns the closest integer smaller than x.

floor(x)

Returns 2

```floor(2.3)
```

Returns the value of x constrained to the range [min, max]

constrain(x, min, max)

Returns 2.0

```constrain(2.3, 0.0, 2.0)
```

Computes the distance between the points (x1, y1) and (x2, y2)

dist(x1, y1, x2, y2)

Returns 5.0

```dist(0.0, 0.0, 3.0, 4.0)
```

Computes the length of the line from (0, 0) to (x, y)

mag(x, y)

Returns 5.0

```mag( 3.0, 4.0 )
```

Maps x from the range [min1, max1] to the range [min2, max2]

map(x, min1, max1, min2, max2)

Returns 0.5

```map( 250.0, 0.0, 500.0, 0.0, 1.0 )
```

Returns the largest of several numbers.

max(x1, x2, x3 …)

Returns 3

```max(3, 1, -1)
```

Returns the smallest of several numbers.

min(x1, x2, x3 …)

Returns -1

```min(3, 1, -1)
```

Calculates and return the integer closest to x.

round(x)

Returns 2.0

```round( 2.3 )
```

Translate degrees to radians and return the result.

Returns 3.141592653589793 (π)

```radians( 180 )
```

Translate radians to degrees and return the result.

Returns 180.0. Uses the built-in constant PI.

```degrees(PI)
```

### Random

Returns a pseudo-random number in the range [0.0, max]

random(max)

Compute a pseudo-random number between 0.0 and 100.0.

```random( 100.0 )
```

Returns a pseudo-random number in the range [min, max]

random(min, max)

Compute a pseudo-random number between 50.0 and 100.0.

```random( 50.0, 100.0 )
```

Reseed the pseudo-random number generator with seed.

randomSeed(seed)

Reseed the pseudo-random number generator with 10.0.

```randomSeed( 10.0 )
```

Get a Perlin random number in 1 dimension.

noise(x)

Get a Perlin random number in 1 dimension using x as parameter.

```noise(0)
```

Get a Perlin random number in 2 dimensions.

noise(x, y)

Get a Perlin random number in 2 dimensions using x and y as parameters.

```noise(0, 0)
```

Get a Perlin random number in 3 dimensions.

noise(x, y, z)

Get a Perlin random number in 3 dimensions using x, y, and z as parameters.

```noise(0, 0, 0)
```

Reseed the Perlin random number generator with seed.

noiseSeed(seed)

Reseed the Perlin random number generator with 0 (which means randomize the seed).

```noiseSeed( 0 )
```

## Constants

The constant π/2

HALF_PI

Rotate by 90°

```rotate( HALF_PI )
```

The constant π

PI

Rotate by 180°

```rotate( PI )
```

The constant π/4

QUARTER_PI

Rotate by 45°

```rotate( QUARTER_PI )
```