Difference between revisions of "API"

From CodeStuff
Jump to: navigation, search
(Created page with "==Simple Functions== ===clear();=== Clears the screen ===drawCircle(x, y, radius);=== Draws an unfilled circle with the center at x, y ===drawLine(x1, y1, x2, y2);=== Draws...")
 
Line 1: Line 1:
 
==Simple Functions==
 
==Simple Functions==
 +
 +
Where functions use parameters of x and y as a pair they refer to screen [[coordinates]].
  
 
===clear();===
 
===clear();===
 
Clears the screen
 
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);===
 
===drawCircle(x, y, radius);===
Line 26: Line 41:
 
Turns the current contents of the screen into a background image that remains after a clear() call.
 
Turns the current contents of the screen into a background image that remains after a clear() call.
  
===loadImage(imageURL);===
+
===loadImage(imageURL,framesWide,framesHigh);===
 
Loads an Image from a url and returns an object that can be used for drawImage.
 
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.  
 
The image download will begin immediately, but drawing the image will have no effect until the download has completed.  
Line 47: Line 62:
 
Returns true if the key indicated by keyCode was pressed since the last move() cycle of a program using run().
 
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...)===
  
 
==Objects==
 
==Objects==

Revision as of 00:19, 26 April 2012

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.

getMousePostion()

Returns an object with x and y fields indicating the current mouse pointer position;

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...)

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 getMousePostion(), 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.