CodeJo Help
Full source code and additional examples available at Sourceforge.
Having trouble? Report it in the CodeJo Ticket Tracker!
Mode
Project
Editor
Terminal
Colors
Grid Canvas
Fabric Canvas
Web Canvas
Document Canvas
Time
Sound
Keyboard Input
Mouse Input
Gamepad Input
Canvas Mode
- grd()
- Switch into Grid Canvas mode. Shortcut for load('Scratchpad-grd') This is the default when loading the CodeJo page.
- fab()
- Switch into Fabric Canvas mode. Shortcut for load('Scratchpad-fab')
- web()
- Switch into Web Canvas mode. Shortcut for load('Scratchpad-web')
- doc()
- Switch into Document Canvas mode. Shortcut for load('Scratchpad-doc')
- NOTE:
- When in grd mode, grd will refer to the codejo.grd module, fab to the codejo.fab module when in fab mode, etc.
Project
- catalog()
- List projects, with their associated render mode.
- catalog(True)
- Include abandoned projects in the catalog listing (projects which were not saved before leaving the page).
- load('<projname>')
- Load the project named 'projname' and switch to the correct render mode.
- reload()
- Load the most recently abandoned project.
- run('<projname>')
- Load 'projname' and start it by calling the main() function.
- run()
- Start the currently loaded project by calling the main() function.
- save('<newname>')
- Save a copy of the currently loaded project as 'newname'.
- new('<newname>', render=None)
- Start a new project called 'newname'. Renderer as specified or using the current renderer if not.
- export()
- Download the currently loaded project.
- export('<projname>')
- Download the project named 'projname'.
- proj.setting('<setting_name>', '<value>')
- Save value in this project's setting called setting_name. [Note, only integer values are currently implemented.]
- proj.setting('<setting_name>')
- Return the previously stored value in this project's setting called setting_name, or 0 if this setting has never been set before.
- proj._import('<projname>')
- Load the project named 'projname' from the static/examples/ folder.
- proj._import('<projname>', force=True)
- Load the named project. Overwrite if a project with that name already exists.
- proj._import('<projname>', force=True, all=True)
- Load and overwrite all projects in the included examples.
Editor
- Ctrl-.
- Increase font size.
- Ctrl-,
- Decrease font size.
- Ctrl-;
- Reset to original font size.
- Ctrl-'
- New editor page.
Terminal
- Ctrl-.
- Increase font size.
- Ctrl-,
- Decrease font size.
- Ctrl-;
- Reset to original font size.
Colors
- Color('<Name>')
- [Note: Captital C] Create color named 'Name' [Note: Does not set the currently active color!] (See colornames() for a complete list of named colors).
- Color('<#ABCDEF>')
- Create color with hex value '#ABCDEF'.
- Color('<#FFFFFFAA>')
- Like Color('<#FFFFFF>') but including alpha (transparency) channel.
- Color(r, g, b)
- Create color using red, green, and blue components in the range (0 <= R, G, B <= 255).
- Color((r, g, b))
- Like Color(r, g, b) but sending color data as a 3-tuple instead of as separate values.
- Color('rgb(r, g, b)')
- Like Color(r, g, b) but sending color data as a string.
- Color(r, g, b, a)
- Like Color(r, g, b) but with alpha (transparency) channel (0 <= A <= 255).
- Color((r, g, b, a))
- Like Color(r, g, b, a) but sending color data as a 4-tuple instead of separate values.
- Color('rgba(r, g, b, a)')
- Like Color(r, g, b, a) but sending color data as a string.
- Color.from_hsl(h, s, l)
- Create a color using the given hue (0-360) saturation (0-100) and lightness (0-100) values.
- Color.avg(<*colors>)
- Create a color by averaging the given colors.
- Color('random')
- Create a randomly selected color (May not be the same as any specific named color).
- Color(random=True)
- Same as Color('random').
- Color(random='rlight')
- Create a random "light" color. (200 <= R, G, and B <= 255).
- Color(random='rmedium')
- Create a random "medium" color. (100 <= R, G, and B < 200).
- Color(random='rdark')
- Create a random "dark" color. (0 <= R, G, and B < 100).
- Color(random='<family>')
- Return a random color from the color family named 'family'. (See colorfams() for a complete list).
- Color(family='<family>')
- Same as color(random='<family>').
- <color>.nudge(r=150)
- Starting from the given Color, create a new Color by setting the red component to 150. [Note that the parameter value must be numeric!]
- <color>.nudge(r='+15')
- Starting from the given Color, create a new Color by adding 15 to the red component. [Note that the parameter value must be a string!]
- <color>.nudge(g='-11')
- Starting from the given Color, create a new Color by subtracting 11 from the green component. [Note that the parameter value must be a string!]
- <color>.nudge(b='92%')
- Starting from the given Color, create a new Color with the blue component set to 92% of the current value. [Note that the parameter value must be a string!]
- <color>.nudge(r=55, g='105%')
- Starting from the given Color, create a new Color with the red component set to 55 and the green component set to 105% of its current value. [Note the mix of numeric and string values!]
- <color>.nudge(h='+1', s='95%')
- Color values can be nudged using hue, saturation, and lightness values also. [Note that nudge can either use rgba nudgers or hsl nudgers, but not both simultaneously.]
- <color>.hexval()
- Color instance method that returns the color value as a hex color string.
- <color>.rgb()
- Color instance method that returns the 'rgb(r, g, b)' form of the Color as a string. [NOTE: To get the actual values as a 3-tuple, use <color>.rgb(True)]
- <color>.rgb01()
- Color instance method that returns the 'rgb(r, g, b)' form of the Color as a string. This method uses rgb values 0-1 instead of 0-255. [NOTE: To get the actual values, use <color>.rgb01(True)]
- <color>.hsl()
- Color instance method that returns the 'hsl(h, s, l)' form of the Color as a string. [NOTE: To get the actual values, use <color>.hsl(True)]
- color()
- [Note: Lower-case c] return the currently active color.
- color(...)
- Create a new Color object using the same parameter formats used in the Color constructor, but ALSO set the currently active color.
- nudge(...)
- Like the <Color>.nudge(...) methods, but start from the currently active color, and set the currently active color to the newly created and returned Color object.
- colornames()
- Return a list of all valid color names. (See the swatcher example for color swatches.)
- colorfams()
- Return a list of all valid color family names. (See the colfams example for color family swatches.)
- colorfams('<family>')
- Return a list of all colors in color family named 'family'.
Grid Canvas
- gr()
- Low-resolution. Pixels are 11x11 screen-resolution pixels.
- hgr()
- High-resolution. Pixels are 3x3 screen-resolution pixels.
- xgr()
- Full resolution.
- NOTE:
- To change resolution without clearing the screen, use setgridsize or setpixelsize
- canvas.setgridsize(w, h)
- Set custom grid size to w pixels wide and h pixels high.
- canvas.setpixelsize(w, h)
- Set custom grid size, using w and h as the width and height of the grid pixels in terms of full-resolution pixels.
- WIDTH
- Number of grid pixels across the canvas.
- HEIGHT
- Number of grid pixels vertically.
- clear(bgc=None)
- Clear the canvas to the given color, or to the current background color if not specified.
- canvas.random(margin=0)
- Return a randomly selected point on the canvas as a 2-tuple. Specifying margin will leave a border of margin pixels around the edge that will not be chosen from.
- plot(x, y)
- Light up the pixel at (x, y) using the current color.
- plot((x, y))
- Same as plot(x, y) but sending point as a 2-tuple instead of as separate values.
- plot('random')
- Plot a randomly selected point using the current color.
- line(x1, y1, x2, y2)
- Light up the pixels along the line from (x1, y1) to (x2, y2) using the current color.
- line((x1, y1), (x2,y2))
- Same as line(x1, y1, x2, y2) but sending points as 2-tuples instead of as separate values.
- lineto(x2, y2)
- Same as line(x1, y1, x2, y2) where (x1, y1) was the most recently plotted point.
- lineto((x2, y2))
- Same as lineto(x2, y2) but sending point as a 2-tuple instead of as separate values.
- ray(x1, y1, x2, y2)
- Light up pixels from (x1, y1) extending through (x2, y2) and continuing to the edge of the canvas.
- ray(x1, y1, a=<degrees>)
- Light up pixels from (x1, y1) extending along the line at angle a degrees until hitting the edge of the screen. (a = 0 to the right, positive angles counter-clockwise).
- rect(x, y, w, h, color=None)
- Draw a rectangle in grid coordinates from (x, y) with width w and height h using specified color, or the currently-active color if not specified.
- write(txt)
- Draw some pixellated text on the grid.
- write(txt, x, y)
- Start writing text txt at location (x, y).
- write(txt, (x, y))
- Same as write(txt, x, y) but sending point as a 2-tuple instead of as separate values.
- write(txt, wrap=True)
- Same as write(txt) but if the text would run off the screen, go back to the left edge and move down a line before continuing. wrap= is also available as a keyword in (..., x, y) and (..., (x, y)) forms also.
- colorat(x, y)
- Return the canvas color at location (x, y).
- canvas.copy(x, y, w, h, x2, y2)
- Copy the pixels in the rect (x, y, w, h) to the new location (x2, y2).
- canvas.move(x, y, w, h, x2, y2)
- Move the pixels in the rect (x, y, w, h) to (x2, y2) leaving background color where the original rect was.
Fabric Canvas
- clear(bgc=None)
- Clear the canvas to the given color, or to the current background color if not specified. [NOTE: also resets canvas.auto to False].
- canvas.random(margin=0)
- Return a randomly selected point on the canvas as a 2-tuple. Specifying margin will leave a border of margin pixels around the edge that will not be chosen.
- canvas.lift(<fabobj>)
- Raise the Fabric object fabobj to the front, so that it is not obscured by any other object. Objects lifted later will be most on top.
- canvas.setauto([True|False])
- Set whether object updates and canvas updates will be automatic (slower, but simpler) or need to be handled manually by your program (faster, but more involved).
- canvas.renderall()
- When canvas is not in auto mode, show all recent object updates.
- WIDTH
- Number of pixels across the canvas.
- HEIGHT
- Number of pixels vertically.
- Rect(x, y, w, h, *, c=None)
- Create a rectangle object at location (x, y) with width w and height h. Color can also be specified, or it will be taken from the currently active color.
- Circle(x, y, r, *, c=None)
- Create a circle object at location (x, y) and with radius r.
- Triangle(x, y, b, h, *, c=None)
- Create an isosceles triangle object at location (x, y) and with base b and height h.
- Polygon(x, y, pts, *, c=None)
- Create a shape at location (x, y) outlined by the points specified in list pts. [Note: Points are relative to the starting point.]
- Polyline(x, y, pts, w=3, *, c=None)
- Create a polyline object at location (x, y) and with points specified in list pts. [Note: Points are relative to the starting point.]
- Text(x, y, txt, size=12, *, c=None)
- Create an text object at location (x, y) and with text txt and size and color specified or defaults.
- <fabobj>.remove()
- Remove the Fabric object <fabobj> from the scene.
- <fabobj>.send_update()
- When canvas is not in auto mode, manually send updates about this object's position and other attributes.
- <fabobj>.outline(w=1, c='Black')
- Set the outline width and color for this object. Use .outline(None) to remove the outline.
- colorat(x, y)
- Return the canvas color at location (x, y).
Web Canvas
- clear(bgc=None)
- Clear the canvas to the given color, or to the current background color if not specified.
- canvas.random(margin=0)
- Return a randomly selected point on the canvas as a 2-tuple. Specifying margin will leave a border of margin pixels around the edge that will not be chosen.
- WIDTH
- Number of pixels across the canvas.
- HEIGHT
- Number of pixels vertically.
- text(x, y, txt, size=12, c=None)
- Draw filled text txt to canvas at location (x, y) using font size and color if provided, or defaults.
- line(x1, y1, x2, y2, /, w=2, c=None)
- Draw a line from (x1, y1) to (x2, y2) with specified width and color, or defaults.
- lineto(x2, y2)
- Same as line(x1, y1, x2, y2) where (x1, y1) was the most recently plotted point.
- rect(x, y, w, h, color=None)
- Draw a rectangle starting at (x1, y1) with width w and height h using specified color, or the default.
Document Canvas
- clear(bgc=None)
- Clear the canvas to the given color, or to the current background color if not specified.
- canvas.random(margin=0)
- Return a randomly selected point on the canvas as a 2-tuple. Specifying margin will leave a border of margin pixels around the edge that will not be chosen.
- canvas.lift(<elem>)
- Raise the Document Element elem to the front, so that it is not obscured by any other element. Elements lifted later will be most on top.
[NOTE: Only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items.]
- WIDTH
- Number of pixels across the canvas.
- HEIGHT
- Number of pixels vertically.
- Styles(**kw)
- Create a Styles object, using keyword arguments to specify JavaScript-formatted CSS styles and values.
- H1(text, styles=None)
- Create an HTML Heading element. Defaults to large, centered text. Additional style parameters can be passed in as a Styles object
- Div(children=None, styles=None)
- Create an HTML Div element. If given, children can be a list of child elements, or a string of text which will be used as the div's entire inner HTML.
- AbsDiv(x, y, w, h, *, children=None, c=None, bgc=None, styles=None)
- Create an absolutely-positioned HTML Div element. Position and size must be specified during element creation, but can also be changed later.
- <elem>.change_styles(*args, **kw)
- Alter the styles for the given element by sending in a Styles objects or by using keyword arguments.
Time
- sleep(seconds)
- Delay execution for a given number of seconds. Use a floating-point number for subsecond precision.
- tick(fps)
- sleep() long enough to maintain fps frames per second.
Sound
- init()
- Required before any other sound calls.
- beep()
- A simple sound.
- beep(frequency=440, duration=200, waveform='sine', delay=None)
- A specific frequency can be requested. Give duration for a longer or shorter tone as required. For different effects, choose waveform from 'sine', 'square', 'triangle', or 'sawtooth'. To play a tone in the future, add a delay in milliseconds.
- chord()
- Play several tones simultaneously. Can sound better than a single-tone beep. Or much worse.
- chord(frequencies=[220, 440, 880], duration=200, waveform='sine', delay=None)
- Pass the desired frequencies as a list. As in beep() parameters can also be passed for duration, waveform, and delay.
- Tone( waveform='sine',
attack=0.1,
attack_ramp='linear',
peak=1,
decay=0.2,
decay_ramp='linear',
sustain=0.7,
release=0.5,
release_tc=0.07,
detune=0,
delay=0,
cotones=[])
- Create a type of sound with the given sound shape parameters.
- <tone>.play(freq)
- Start playing the Tone using the given frequency. Returns the soundid (needed to stop the tone later.)
- <tone>.stop(sid)
- Stop playing the Tone that returned sid when starting.
- <tone>.add_cotone(<tone>)
- Add a tone that will always play alongside this one.
- <tone>.add_cotone(detune)
- Add a cotone, copying this one, but adding a detune factor of detune.
Keyboard Input
- kb_grab()
- Take keyboard focus for the canvas area.
- kb_release()
- Return keyboard focus to the interactive terminal area.
- getkey()
- Return the next key that has been pressed, or None if no key has been pressed since last checked.
- keystate(<keys>|'keys'|None)
- Return a list of all keys currently pressed, or if a list of keys is specified (or a string, if no special keys are required) return a list of those keys that are currently pressed.
- Special Key Values
-
"ControlLeft"
"ShiftLeft"
"AltLeft"
"ControlRight"
"ShiftRight"
"AltRight"
"ArrowUp"
"ArrowDown"
"ArrowLeft"
"ArrowRight"
"Enter"
"Backspace"
"Escape"
Mouse Input
- getclick()
- Return info about the most recent mouse click, or None if no click has occured since the last check. Info returned is the point of the click (x, y) and the mouse button pressed [0 - left button, 1 - center button, 2 - right button].
- getdraganddrop()
- Return info about the most recent mouse drag and drop, or None if no recent drag and drop has occured. Info is the point the drag started (x, y) the mouse button pressed [0 - left button, 1 - center button, 2 - right button] and the point the drop occured (x, y)
Gamepad Input
- gp_grab()
- Take focus for the canvas area and begin polling for gamepad events.
- gp_release()
- Return focus to the interactive console area.
- getpadraw(<pad>)
- Return the raw gamepad data for gamepad pad (0 or 1 are currently supported). Your returned data will likely be different, but on my system, with my old Nintendo-style gamepads, it looks like this:
- Raw gamepad return (example)
-
0:D-pad X-value
1:D-pad Y-value
6:X-button
7:A-button
8:Left Bumper
9:Y-button
12:Select-button
13:Start-button
23:B-button
24:Right Bumper