Every custom UI made with jsui or JSPainter has access to a default Sketch object bound to the global variable "sketch". Use this object to render to the OpenGL-backed drawing context available to all UI objects. Often this is the only instance of the Sketch object that you will use. If you want to render sprites, have multiple layers of images, or create alpha channels, you can construct new instances of the Sketch object.
Constructors
newSketch(width?: number, height?: number);
Create a new Sketch instance
Parameter
Type
Description
optional width
number
width, leave undefined to use the default
optional height
number
height, leave undefined to use the default
Methods
beginstroke
Begin definition of a stroked path
beginstroke(stroke_style: "basic2d" | "line");
Name
Type
Description
stroke_style
"basic2d" | "line"
the stroke style to use
circle
Draw a circle or an arc
Draws a filled circle with radius specified by the radius argument at the current drawing position. If theta_start and theta_end are specified, then an arc will be drawn instead of a full circle. Affected by shapeorient, shapeslice, and shapeprim values.
Copies pixels from the source object to the location specified by the destination_x and destination_y arguments. The initial x and y offset into the source and size of the rectangle copied can be speified by the source_x, source_y, width and height arguments. If these are not present, an x and y offset of zero and width and height equal to the source image is assumed. No scaling of pixels is supported. If blending is enabled in the destination sketch object, alpha blending will be performed and the current alpha color will also be applied globally. he copypixels method is much faster than obtaining the equivalent result using glbindtexture() to texture a plane, and is the recommended means of drawing images when scaling and rotation are not required.
The cube will have width = 2 * scale_x, height = 2 * scale_y, and depth = 2 * scale_z, and will be centered at the current drawing position. By default, scale_y and scale_z will be equal to scale_x. Affected by shapeorient, shapeslice, and shapeprim values.
Draws a cylinder with top radius specified by the radius1 argument, bottom radius specified by the radius2 argument, length specified by the mag argument, and center point at the current drawing position. If the theta_start and theta_end arguments are specified, then a cylindrical wedge will be drawn instead of a full cylinder. Affected by shapeorient, shapeslice, and shapeprim values.
The default2d method is a simple way to set the graphics state to default properties useful for 2D graphics. It is called everytime your object is resized if default2d() has been called more recently than default3d(). It is essentially equivalent to the following set of calls:
The default3d method is a simple way to set the graphics state to default properties useful for 3D graphics. It is called everytime the jsui object is resized if default3d() has been called more recently than default2d(). It is essentially equivalent to the following set of calls:
Returns the depth value associated with the currently rendered pixel at a given absolute screen coordinate.
depthatpixel(x: number, y: number): number;
Name
Type
Description
x
number
screen x coordinate
y
number
screen y coordinate
Return Value
number
ellipse
Draw an ellipse or elliptical arc
Draws a filled ellipse with radii specified by the radius1 and radius2 arguments. If theta_start and theta_end are specified, then an arc will be drawn instead of a full ellipse. Affected by shapeorient, shapeslice, and shapeprim values.
Draws a framed circle with radius specified by the radius argument at the current drawing position. If theta_start and theta_end are specified, then an arc will be drawn instead of a full circle. Affected by shapeorient, shapeslice, and shapeprim values.
Draws a framed ellipse with radii specified by the radius1 and radius2 arguments. If theta_start and theta_end are specified, then an arc will be drawn instead of a full ellipse. Affected by shapeorient, shapeslice, and shapeprim values.
Frees data from the native C peer (created when making a Sketch object), which is not considered by the JavaScript garbage collector, and may consume lots of memory until the garbage collector decides to run based JS allocated memory. Once called, the Sketch object is not available for any other use. It's not necessary to call this function, as the memory will be freed eventually, but you can call it whenever you're done with your Sketch object.
freepeer();
getpixel
Get the pixel data at a given point
Returns an array containing the pixel value at the specified location. This array is ordered RGBA, i.e. array element 0 is red, 1, green, 2, blue, 3 alpha. Color values are floating point numbers in the range 0.-1.
Returns an array containing the width and height of the given string in absolute screen coordinates, taking into account the current font and fontsize.
gettextinfo(text: string): [number, number];
Name
Type
Description
text
string
text to measure
Return Value
[number, number]
glbegin
Begin drawing using low level OpenGL functions
The low level OpenGL function calls (all beginning with gl) are thin wrappers around direct calls to the graphics engine. Typically, use these function calls between calls to Sketch.glbegin() and Sketch.glend(). For many of these functions, look up the documentation for the OpenGL function with the same (or very similar) name.
Draws a line from the current drawing position to the location specified by adding the delta x, y, and z arguments to the current position. After this method has been called, the drawing position is updated by an offset relative to the original drawing position.
line(dx: number, dy: number, dz: number);
Name
Type
Description
dx
number
x offset
dy
number
y offset
dz
number
z offset
linesegment
Draw a line segment
Draws a line from the location specified by the x1, y1, and z1 arguments to the location specified by the x2, y2, and z2 arguments. After this method has been called, the drawing position is updated to the location specified by the x2, y2, and z2 arguments.
Draws a line from the current drawing position to the location specified by the x, y, and z arguments. After this method has been called, the drawing position is updated to the location specified by the x, y, and z arguments.
lineto(x: number, y: number, z: number);
Name
Type
Description
x
number
x coordinate
y
number
y coordinate
z
number
z coordinate
move
Move the drawing position relatively
Moves the drawing position to the location specified by the sum of the current drawing position and the delta x, y, and z arguments.
move(dx: number, dy: number, dz: number);
Name
Type
Description
dx
number
x offset
dy
number
y offset
dz
number
z offset
moveto
Move the current drawing position
Moves the drawing position to the location specified by the x, y, and z arguments.
moveto(x: number, y: number, z: number);
Name
Type
Description
x
number
x coordinate
y
number
y coordinate
z
number
z coordinate
ortho3d
Set the default graphics state for rendering with orthographic projection
The orth3d method is a simple way to set the graphics state to default properties useful for 3D graphics, using an orthographic projection (i.e. object scale is not affected by distance from the camera). It is called every time the jsui object is resized if ortho3d() has been called more recently than default2d(), or default3d(). It is essentially equivalent to the following set of calls:
Draws a plane with top width = 2 * scale_x1, left height = 2 * scale_y1, bottom width = 2 * scale_x2, right height = 2 * scale_y2, and center point at the current drawing position. If scale_y1 is not specified, it will assume the same value as scale_x1. If scale_x2 and scale_y2 are not specified, they will assume the same values as scale_x1 and scale_y1 respectively. Affected by shapeorient, shapeslice, and shapeprim values.
Draws a point at the location specified by the x, y, and z arguments. After this method has been called, the drawing position is updated to the specified location.
point(x: number, y: number, z: number);
Name
Type
Description
x
number
x coordinate
y
number
y coordinate
z
number
z coordinate
quad
Draw a filled quadrilateral
After this method has been called, the drawing position is updated to the location specified by the x4, y4, and z4 arguments.
Draws a rounded plane with width = 2 * scale_x, height = 2 * scale_y, and center point at the current drawing position. The radius of the rounded portion of the plane is determined by the round_amount argument. If scale_y is not specified, it will assume the same value as scale_x. Affected by shapeorient, shapeslice, and shapeprim values.
Returns an array containing the x, y, and z world coordinates associated with a given screen pixel using the same the depth from the camera as 0, 0, 0. Optionally a third depth arg may be specified, which may be useful for hit detection and other applications. The depth value is typically specified in the range 0.-1. where 0 is the near clipping plane, and 1. is the far clipping plane. The worldtoscreen method can be used to determine the depth value of a given world coordinate, and the Sketch.depthatpixel() method can be used to determine the depth value associated with the currently rendered pixel at a given absolute screen coordinate.
Set the number of slices to use when rendering shapes
Increasing the slice_a and slice_b arguments will increase the quality at which the shape is rendered, while decreasing these values will improve performance.
shapeslice(slice_a: number, slice_b: number);
Name
Type
Description
slice_a
number
number of slices to use
slice_b
number
number of slices to use
sphere
Draw a sphere
Draws a sphere with the given radius, centered at the current drawing position. If the theta1_start, theta1_end, theta2_start, and theta2_end arguments are specified, then a section will be drawn instead of a full sphere. Affected by shapeorient, shapeslice, and shapeprim values.
Some stroke styles such as "basic2d" will ignore the z coordinate.
strokepoint(x: number, y: number, z: number);
Name
Type
Description
x
number
x
y
number
y
z
number
z
text
Draw the given text
Draws the text specified by the string argument at the current drawing position, taking into account the current font, fontsize, and text alignment. Text is strictly 2D, and does not take into account any world transformations. After calling the text method, if the x axis text alignment is set to "left", the current drawing position will be updated to reflect the world position associated with the end of the string. If the x axis text alignment is set to "right", the current drawing position will be updated to reflect the world position associated with the end of the string. If the x axis text alignment is set to "center", the current drawing position will remain unchanged.
text(text: string);
Name
Type
Description
text
string
text to draw
textalign
Set the text alignment in x and y
Sets the alignment of text to be drawn with respect to the current drawing position. Default alignment is "left" and "bottom".
Draw a torus centered at the current drawing position. If theta1_start, theta1_end, theta2_start, and theta2_end are specified, then a section will be drawn instead of a full torus. Affected by shapeorient, shapeslice, and shapeprim values.
Returns the screen coordinate for a given world coordinate
Returns an array containing the x, y, and depth screen coordinates associated with a given world coordinate. The depth value is typically specified in the range 0.-1. where 0 is the near clipping plane, and 1. is the far clipping plane.