new CodeHSGraphics(options)
Set up an instance of the graphics library.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
options |
dictionary
|
Options, primarily .canvas, the selector string for the canvas. If multiple are returned, we'll take the first one. If none is passed, we'll look for any canvas tag on the page. |
Methods
(static) getConstructorModificationString() → {string}
Generate strings for the public constructors to bring them to the
public namespace without having to call them with the graphics instance.
- Source:
Returns:
- Type:
-
string
Line broken constructor declarations.
(static) getNamespaceModifcationString() → {string}
Generate strings for the public methods to bring them to the
public namespace without having to call them with the graphics instance.
- Source:
Returns:
- Type:
-
string
Line broken function definitions.
(static) registerConstructorMethod(name)
Adds a constructor to the public constructors constant.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
Name of the object to be constructed. |
(static) registerPublicMethod(name)
Adds a method to the public methods constant.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
Name of the method. |
add(elem)
Add an element to the graphics instance.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
elem |
Thing
|
A subclass of Thing to be added to the graphics instance. |
audioChangeMethod(tag, fn)
Assign a function as a callback for when audio data changes for audio
being played in a graphics program.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
tag |
object
|
Audio element playing sound to analyze |
fn |
function
|
A callback to be triggered on audio data change. |
canvasExists() → {boolean}
Return if the graphics canvas exists.
- Source:
Returns:
- Type:
-
boolean
Whether or not the canvas exists.
canvasHasInstance()
Whether the selected canvas already has an instance associated.
- Source:
deviceMotionMethod(fn)
Assign a function as a callback for device motion events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered device motion events. |
deviceOrientationMethod(fn)
Assign a function as a callback for device orientation events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on device orientation events. |
drawBackground()
Draw the background color for the current object.
- Source:
elementExistsWithParameters(params) → {boolean}
Check if an element exists with the given paramenters.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
params |
object
|
Dictionary of parameters for the object. Includes x, y, heigh, width, color, radius, label and type. |
Returns:
- Type:
-
boolean
fullReset()
Reset all timers to 0 and clear timers and canvas.
- Source:
getCanvas() → {object}
Return the current canvas we are using. If there is no
canvas on the page this will return null.
- Source:
Returns:
- Type:
-
object
The current canvas.
getContext() → {context}
Return the 2D graphics context for this graphics
object, or null if none exists.
- Source:
Returns:
- Type:
-
context
The 2D graphics context.
getDistance(x1, y1, x2, y2) → {number}
Get the distance between two points, (x1, y1) and (x2, y2)
- Source:
Parameters:
Name | Type | Description |
---|---|---|
x1 |
number
|
|
y1 |
number
|
|
x2 |
number
|
|
y2 |
number
|
Returns:
- Type:
-
number
Distance between the two points.
getElementAt(x, y) → {Thing|null}
Get an element at a specific point.
If several elements are present at the position, return the one put there first.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
x |
number
|
The x coordinate of a point to get element at. |
y |
number
|
The y coordinate of a point to get element at. |
getHeight() → {float}
Get the height of the entire graphics canvas.
- Source:
Returns:
- Type:
-
float
The height of the canvas.
getWidth() → {float}
Get the width of the entire graphics canvas.
- Source:
Returns:
- Type:
-
float
The width of the canvas.
isKeyPressed(keyCode) → {boolean}
Check if a key is currently pressed
- Source:
Parameters:
Name | Type | Description |
---|---|---|
keyCode |
integer
|
Key code of key being checked. |
Returns:
- Type:
-
boolean
Whether or not that key is being pressed.
keyDownMethod(fn)
Assign a function as a callback for keydown events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on keydown events. |
keyUpMethod(fn)
Assign a function as a callback for key up events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on key up events. |
mouseClickMethod(fn)
Assign a function as a callback for click (mouse down, mouse up) events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on click events. |
mouseDownMethod(fn)
Assign a function as a callback for mouse down events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on mouse down. |
mouseDragMethod(fn)
Assign a function as a callback for drag events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on drag events. |
mouseMoveMethod(fn)
Assign a function as a callback for mouse move events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on mouse move events. |
mouseUpMethod(fn)
Assign a function as a callback for mouse up events.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
A callback to be triggered on mouse up events. |
remove(elem)
Remove a specific element from the canvas.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
elem |
Thing
|
The element to be removed from the canvas. |
resetAllState()
Resets the graphics instance to a clean slate.
- Source:
runCode(code)
This is how you run the code, but get access to the
state of the graphics library. The current instance
becomes accessible in the code.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
code |
string
|
The code from the editor. |
setBackgroundColor(color)
Set the background color of the canvas.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
color |
Color
|
The desired color of the canvas. |
setCurrentCanvas(canvasSelector)
Set the current canvas we are working with. If no canvas
tag matches the selectorv then we will just have the current
canvas set to null.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
canvasSelector |
string
|
String representing canvas class or ID. Selected with jQuery. |
setGraphicsTimer(fn, time, data, name)
Set a graphics timer.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
The function to be executed on the timer. |
time |
number
|
The time interval for the function. |
data |
object
|
Any arguments to be passed into `fn`. |
name |
string
|
The name of the timer. |
setSize(w, h)
Set the size of the canvas.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
w |
number
|
Desired width of the canvas. |
h |
number
|
Desired height of the canvas. |
setTimer(fn, time, data, name)
Create a new timer
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
Function to be called at intervals. |
time |
integer
|
Time interval to call function `fn` |
data |
dictionary
|
Any data associated with the timer. |
name |
string
|
Name of this timer. |
setup()
Set up the graphics instance to prepare for interaction
- Source:
stopTimer(fn)
Remove a timer associated with a function.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
Function whose timer is removed. note 'fn' may also be the name of the function. |
updateAudio()
This function is called on a timer. Calls the student's audioChangeCallback
function and passes it the most recent audio data.
- Source:
waitingForClick() → {boolean}
Whether the graphics instance is waiting for a click.
- Source:
Returns:
- Type:
-
boolean
Whether or not the instance is waiting for a click.