API

From CodeStuff
Jump to: navigation, search

Simple Functions

Where functions use parameters of x and y as a pair they refer to screen coordinates.

clear();

Clears the screen

setColour(colourValue);

Sets the drawing colour to be colourValue. colourValue is a text string that can take several forms.

These are all valid values for setColour. "black" "green" "orange" "rgb(50,20,10)" "rgba(60,20,40,0.5)" "#ff00ff"

You can read more about what colour names are available and how the different forms work on wikipedia http://en.wikipedia.org/wiki/Web_colors

drawCircle(x, y, radius);

Draws an unfilled circle with the center at x, y

drawLine(x1, y1, x2, y2);

Draws a line starting at x1, y1 and ending at x2, y2

drawRectangle (x, y, width, height);

Draws an unfilled rectangle with the top left corner at x, y.

fillCircle (x, y, radius);

Draws a filled circle with the center at x, y


fillRectangle (x, y, width, height);

Draws a filled rectangle with the top left corner at x, y.

print (text, x, y);

Draws text at x,y If x and y are not given, the text will appear below the last printed line.

makeBackground();

Turns the current contents of the screen into a background image that remains after a clear() call.

loadImage(imageURL,framesWide,framesHigh);

Loads an Image from a url and returns an object that can be used for drawImage. The image download will begin immediately, but drawing the image will have no effect until the download has completed. This means if you try to draw an image immediately after a loadImage call it will most likely not appear since it has not had time to download.

drawImage(image, x, y)

Draws an image with the top left corner at x,y on the screen.

keyIsDown(keyCode)

Returns true if the key indicated by keyCode is held down.

run(move,draw,updateRate)

This function expects move and draw to be functions. It will set up a timer and repeatedly call move() and draw() updateRate times a second.

keyWentDown(keyCode)

Returns true if the key indicated by keyCode was pressed since the last move() cycle of a program using run().


canvasSave()

saves the canvas state. Remembers position, scale and angle of the canvas.

canvasRestore()

sets the canvas state back to what it was when you last called canvasSave.

canvasTranslate(x,y)

canvasRotate(angle)

canvasScale(scaleX,scaleY)

drawPolygon(...a list of x,y values...)

fillPolygon(...a list of x,y values...)

getMouseInfo()

Returns the position of the mouse pointer and the state of the mouse buttons.

distance(a,b)

Returns the distance between the point (a.x,a.y) and (b.x,b.y)

rgb(r,g,b)

Returns the text representation of the colour using r,g,b as numbers for the the red green and blue parts.

grey(shade)

Returns the text representation of a shade of grey using shade as the value for all of the red green and blue parts.

setRgb(r,g,b)

sets the colour using three numbers for the red green and blue parts. this is equivalent to setColour(rgb(r,g,b));


setGrey(shade)

sets the colour to a shade of grey this is equivalent to setColour(grey(sade));

noise2d(x,y)

returns a value from a 2d field of noise.

The returned value will be in the range -1...1.

noise3d(x,y,z)

returns a value from a 3d field of noise

Objects

eventManager

The eventManager receives events from the hosting webpage and passes them out to attached listeners.

Programs can receive events by installing listeners with eventManager.addEventListener(eventName,eventFunction)

The events that can be received are

  • mousedown
  • mouseup
  • mousemove
  • keydown
  • keyup
  • batchnotify

The object passed to the event listener will not have the full details that may exist in the browser. It is a simple object with no methods and just a few of the standard fields transferred from the browser event.

inputManager

The input Manager tracks input events to provide a poll-able state. The functions getMouseInfo(), keyIsDown(), and keyWentDown() access methods of inputManager with the same name.

Key down events are accumulated in a buffer and transferred to the keyWentDown buffer when inputManager.cycle() is called. The run(move,draw) function calls inputManager.cycle() before each move() call.