Methods
- getPromiseResults
Utility function that returns a promise that resolves after the given list of promises finishes its work. - getPromiseResultsAll
Utility function that returns a promise that resolves or is rejected after the given list of promises finishes its work. - getTimePosition
Gets the position of a time value within an interval. - newFrames
Returns the number of frame-length intervals that occurred since the last known time, where a frame's length is 1/60 of a second. - toGLColor
Creates a 4-element array representing a color.
getPromiseResults(promises, [progressResolve], [progressReject])
Utility function that returns a promise that resolves after the given list of promises finishes its work.
Parameters
promises
(Type: Array.<Promise>)
an array containing promise objectsprogressResolve
(Type: function) (optional)
A function called as each individual promise is resolved.progressReject
(Type: function) (optional)
A function called as each individual promise is rejected.
Return Value
A promise that is never rejected and resolves when all of the promises are each resolved or rejected. The result of the promise will be an object with three keys:
- "successes" - contains a list of results from the promises that succeeded, in the order in which those promises were listed.
- "failures" - contains a list of results from the promises that failed, in the order in which those promises were listed.
- "results" - contains a list of boolean values for each promise, in the order in which the promises were listed. True means success, and false means failure.
getPromiseResultsAll(promises, [progressResolve], [progressReject])
Utility function that returns a promise that resolves or is rejected after the given list of promises finishes its work.
Parameters
promises
(Type: Array.<Promise>)
an array containing promise objectsprogressResolve
(Type: function) (optional)
a function called as each individual promise is resolved; optionalprogressReject
(Type: function) (optional)
a function called as each individual promise is rejected; optional
Return Value
A promise that is resolved when all of the promises are each resolved; the result will be an array of results from those promises, in the order in which those promises were listed. Will be rejected if any of the promises is rejected; the result will be an object as specified in getPromiseResults. (Type: Promise)
getTimePosition(timer, timeInMs, intervalInMs)
Gets the position of a time value within an interval. This is useful for doing animation cycles lasting a certain number of seconds, such as rotating a shape in a 5-second cycle. This method may be called any number of times each frame.
Parameters
timer
(Type: Object)
An object that will hold two properties:- "time" - initial time value, in milliseconds.
- "lastTime" - last known time value, in milliseconds. Will be set to the value given in "timeInMs" before returning.
{}
ornew Object()
.timeInMs
(Type: number)
A time value, in milliseconds. This could be the parameter received in arequestAnimationFrame()
callback method.intervalInMs
(Type: number)
The length of the interval (animation cycle), in milliseconds.
Return Value
A value in the range [0, 1), where closer to 0 means "timeInMs" lies closer to the start, and closer to 1 means closer to the end of the interval. If an initial time wasn't set, returns 0. (Type: number)
Examples
The following code sets an angle of
rotation, in degrees, such that an object rotated with the
angle does a 360-degree turn in 5 seconds (5000 milliseconds).
The variable time
is assumed to be a time
value in milliseconds, such as the parameter of a
requestAnimationFrame()
callback method.
var angle = 360 * getTimePosition(timer, time, 5000);
newFrames(timer, timeInMs)
Returns the number of frame-length intervals that occurred since the last known time, where a frame's length is 1/60 of a second. This method should be called only once each frame.
Parameters
timer
(Type: Object)
An object described in getTimePosition.timeInMs
(Type: number)
A time value, in milliseconds. This could be the parameter received in arequestAnimationFrame()
callback method. .
Return Value
The number of frame-length intervals relative to the last known time held in the parameter "timer". The number can include fractional frames. If an initial time or last known time wasn't set, returns 0. (Type: number)
toGLColor(r, [g], [b], [a])
Creates a 4-element array representing a color. Each element can range from 0 to 1 and specifies the red, green, blue or alpha component, respectively. This method also converts HTML and CSS colors to 4-element RGB colors. The following lists the kinds of colors accepted:
- HTML colors with the syntax
#RRGGBB
or#RRGGBBAA
, where RR is the hexadecimal form of the red component (00-FF), GG is the hexadecimal green component, BB is the hexadecimal blue component, and AA is the hexadecimal alpha component. Example: #88DFE0. - HTML colors with the syntax
#RGB
or#RGBA
, where R is the hexadecimal form of the red component (0-F), G is the hexadecimal green component, B is the hexadecimal blue component, and A is the hexadecimal alpha component. Example: #8DE. - CSS colors with the syntax
rgb(red, green, blue)
orrgba(red, green, blue, alpha)
wherered
,green
, andblue
are the red, green, and blue components, respectively, either as a number (0-255) or as a percent, andalpha
is a number from 0-1 specifying the alpha component. Examples:rgb(255,0,0)
,rgb(100%,50%,0%)
,rgba(20,255,255,0.5)
. - CSS colors with the syntax
hsl(hue, sat, light)
orhsla(hue, sat, light, alpha)
wherehue
is the hue component in degrees (0-360),sat
andlight
are the saturation and lightness components, respectively, as percents, andalpha
is a number from 0-1 specifying the alpha component. Examples:rgb(255,0,0)
,hsl(200,50%,50%)
,hsla(20,80%,90%,0.5)
. - CSS colors such as
red
,green
,white
,lemonchiffon
,chocolate
, and so on, including the newly addedrebeccapurple
. - The value
transparent
, meaning transparent black.
For more information, see the "Color Strings" tutorial.
Parameters
r
(Type: Array.<number> | number | string)
One of the following:- A color vector or string, which can be one of these:
- An array of three color components, each of which ranges from 0 to 1. The three components are red, green, and blue in that order.
- An array of four color components, each of which ranges from 0 to 1. The three components are red, green, blue, and alpha in that order.
- A string specifying an HTML or CSS color, in one of the formats mentioned above in the method description.
- A number specifying the red component. Must range from 0 to 1.
- A color vector or string, which can be one of these:
g
(Type: number) (optional)
Green color component (0-1). May be null or omitted if a string or array is given as the "r" parameter.b
(Type: number) (optional)
Blue color component (0-1). May be null or omitted if a string or array is given as the "r" parameter.a
(Type: number) (optional)
Alpha color component (0-1). If the "r" parameter is given and this parameter is null, undefined, or omitted, this value is treated as 1.0.
Return Value
The color as a 4-element array; if the color is invalid, returns [0,0,0,0], or transparent black. Numbers less than 0 are clamped to 0, and numbers greater than 1 are clamped to 1. (Type: Array.<number>)