My first (but best documented) attempt at a 2D browser game engine
I am yet to write documentation for the new features added, since things are currently changing very fast. To give a general idea, I’ve posted the documentation from v1.0 below. Note that many things mentioned might have changed, and lots of new features are emitted. Read at your own risk!
Run some of the demos here:
demo_physics.html
demo_tankcubes.html
demo_tankshoot.html
Basic setup is as follows
<script src="engine/elemental.js"></script>
<canvas id="myCanvas" width="500" height="500"></canvas>
Check the demo_name.html
files for full demos.
(element ID) -> Class -> Canvas instance
Create canvas instance, passing the ID of the HTML element as the only parameter
var canvas = new Elemental.Canvas("myCanvas");
Properties
width (set, get) -> Num
height (set, get) -> Num
size (get) -> Vector
center (get) -> Vector
mousePos (get) -> Vector
(keycode) -> Function -> Bool
Returns if keycode passed was pushed down this frame
(keycode) -> Function -> Bool
Returns if keycode was released this frame
(keycode) -> Function -> Bool
Returns if keycode is currently being held down
(button) -> Function -> Bool
Returns if mouse button was pushed this frame
(button) -> Function -> Bool
Returns if mouse button was released this frame
(button) -> Function -> Bool
Returns if mouse button is currently held down
(color) -> Function
Fills canvas entirely with color
(start, end) {color, width, caps} -> Function
Draws line between two vectors
(font, text, position) {color} -> Function
Writes text to position on screen
(color, position, width, height) -> Function
Draws rectangle at position on screen
(sprite, position) -> Function
Draws sprite at position on screen
(function) -> Function
Begins running passed function roughly 60 times a second
canvas.start(function(cnv, time){
canvas.drawFill("red");
});
It will pass the canvas instance, and the time since last frame as parameters.
() -> Function
The reverse of Elemental.Canvas.start
Contain bindings between key names, and their representative integers.
function frame(context, time) {
if (context.keyHeld(Elemental.Keycodes.SPACE)) {
context.drawFill("black");
} else {
context.drawFill("white");
}
}
Contains bindings between mouse button names, and their representative integers.
function frame(context, time) {
if (context.mouseHeld(Elemental.Mousecodes.LEFT)) {
context.drawFill("black");
} else {
context.drawFill("white");
}
}
(shapes) -> Class -> Sprite instance
Takes an object or array of shapes, and consolidates them into one class. Then passed to canvas.drawSprite for drawing.
var shape1 = ...;
var shape2 = ...;
var mySprite = new Elemental.Sprite([shape1, shape2]);
canvas.drawSprite(mySprite, posn);
Properties
rotation (get, set) -> Num
scale (get, set) -> Num
A shape is a class representing a set of lines and their properties. The basic types are Polygons (including lines) and Arcs. A shape can make up only one simple geometry. The following properties are shared by all Shape instances.
You can also pass a data
keyword argument to any shape. The shape will inherit any properties contained in the passed object. This means you can configure your shape when it is initiated.
var myShape = new Elemental.Shape.Line(data={
lineWidth: 10,
lineColor: "red"
});
Properties
layer (get, set) -> Num
scale (get, set) -> Num
center (get, set) -> Vector
rotation (get, set) -> Num
lineWidth (get, set) -> Num
lineColor (get, set) -> String
lineCaps (get, set) -> String
lineCorners (get, set) -> String
lineMiterLimit (get, set) -> Num
lineDashWidth (get, set) -> Num
lineDashSpacing (get, set) -> Num
fillColor (get, set) -> String
closePath (get, set) -> Bool
strokeFirst (get, set) -> Bool
type (get, set) -> String
(points) {data} -> Class -> Shape.Polygon instance
Creates closed polygon between the passed array of vectors.
var myPoly = new Elemental.Shape.Polygon([
new Vector(0, 0),
new Vector(10, 0),
new Vector(10, 10),
new Vector(0, 10),
]);
(points) {data} -> Class -> Shape.Line instance
Creates a line between passed array of vectors.
var myLine = new Elemental.Shape.Polygon([
new Vector(0, 0),
new Vector(10, 10),
new Vector(20, 10)
]);
(radius) {start, end, data} -> Class -> Shape.Arc instance
Creates an arc given the radius, and the start and end of the segment (in degrees). To create a full circle, start at 0 and end at 360. These are also the defaults for the keyword arguments.
var myArc = new Elemental.Shape.Arc(50, start=0, end=180);
The above code makes a closed semi-circle, with a radius of 50px.
A vector is a representation of a point in the Elemental engine. They have two properties, an X and a Y. Anywhere where Elemental asks for a point, it is looking for a vector.
A vector can be any property with an x
and y
attribute. You can define an acceptable vector any of the following ways…
var myVector = new Elemental.Vector(0, 0);
var myVector = {x: 0, y: 0};
var myVector = Elemental.Vector.Blank;
() -> Static Get -> Vector instance of value (0, 0)
Gives new vector instance, with an x and y of 0.
(vector) -> Static Function -> Bool
Returns whether passed vector is a valid vector.
(vectors...) -> Static Function -> Vector
Returns sum of all vectors. Can pass a numeric value also.
(vectors...) -> Static Function -> Vector
Subtracts each vector from the next. Can pass a numeric value also.
(vectors...) -> Static Function -> Vector
Returns product of all vectors. Can pass a numeric value also.
(vectors...) -> Static Function -> Vector
Divides each vector by the next. Can pass a numeric value also.
An object used to store a series of helper functions that can be used by the user.
(degrees) -> Static Function -> Num
Converts degrees to radians
(radians) -> Static Function -> Num
Converts radians to degrees
(point1, point2) -> Static Function -> Num
Finds angle between two vectors. Angle follows the same conventions as canvas arcs, and works in all 4 quadrants.
(point1, point2) -> Static Function -> Vector
Returns a vector with a normalized “step” to move from point1 to point2.