3.0.4 • Published 1 year ago

spective v3.0.4

Weekly downloads
5
License
MIT
Repository
github
Last release
1 year ago

Spective

This library provides a simple way to create 2D graphics in your browser. Scenes are made up of a hierarchy of just a few types of nodes, and support physics and collisions.

Scene

A scene will add a canvas to your page to display graphics and also serves as the camera node. It can be created in the following ways.

const scene = spective({ ...props });
const scene = new Scene({ ...props });

Canvas Properties

NameDefaultDescription
density2Higher values will render more pixels to improve the sharpness of your scene.
width640The width of the canvas on your page.
height360The height of the canvas on your page.
canvasAllows you to set your own canvas element instead of having a new one created.
divAllows you to set a div, meant to hold your UI, that will be positioned over the canvas that was created.

Camera Properties

Allows the same orientation props as Node instances, described further down.

Layer

Layers are flat planes that sprites can be placed into and can be set up to support parallax effects. They are added directly to the scene instance.

const layer = scene(new Layer({ ...props }))
const layer = scene({ ...props }) // must have parallax prop

Properties

NameDefaultDescription
parallax1Determines how much sprites will move when camera position is updated. Movement will be reduced as value approaches zero and will reverse for negative numbers. Movement will increase for numbers greater than 1.

Asset

Assets are images that can be reused within your scene. They can be added to a layer instance to have them move in relation to the camera position, or directly to the scene to make them a part of the UI.

// add to layer
const asset = layer(new Asset({ ...props }))
const asset = layer({ ...props }) // must have texture prop

// add to UI
const asset = scene(new Asset({ ...props }))
const asset = scene({ ...props }) // must have texture prop

Properties

NameDefaultDescription
textureThe image used for the sprite instances created from this asset. Can either be a path to an image, or a hexadecimal color value as a string starting with '#'.
cells0:0Allows you to create sepearate cells from the image for nodes to use. Colon separates x and y coordinates and space can be used to create more cells.
sxA custom width for cells of the image.
syA custom height for cells of the image.
oxA custom adjustment to the X position of the pivot point of the image.
oyA custom adjustment to the Y position of the pivot point of the image.

Node

Nodes are instances of sprites. They can also hold other nodes to form a group of sprites that move as one group. They can also add code that will run when collisions occur with other nodes.

// add to asset (harder to manage, but can avoid setting asset prop)
const node = asset(new Asset({ ...props }))
const node = asset({ ...props }) // if no parallax, mesh, or texture prop is found

// add to layer
const node = layer(new Asset({ ...props }))
const node = layer({ ...props }) // if no parallax, mesh, or texture prop is found

// add to UI
const node = scene(new Asset({ ...props }))
const node = scene({ ...props }) // if no parallax, mesh, or texture prop is found

Hierarchy Properties

NameDefaultDescription
groupThe group to tie node to. Can also add node to group node directly instead of setting this prop.
assetThe asset to paint the sprite with. Can also add node to asset directly instead of setting this prop.
cell0The cell to use from the ones the asset has set up.

Orientation Properties

NameDefaultDescription
px0X position.
py0Y position.
az0Z angle (counter-clockwise around axis into screen).
ox0X position of the pivot point.
oy0Y position of the pivot point.
sx1X scale.
sy1Y scale.

Animation

Any of the above properties can be given a speed and/or acceleration to update the values over time. The names of these animation props match the orientation props they will affect, except with an 's' or 'a' appended to them.

Collision Properties

NameDefaultDescription
paramMarks node as collidable and is passed to any oncollide functions that are called as a result.
oncollideA function that will be called if this node intersects with any other nodes with a param set.

oncollide function will recieve an event object with the following props.

NameOptionsDescription
action'collide', 'separate'The type of even that has occured. 'separate' indicates that two previously collided objects no longer intersect.
type'COLLIDE_ACTION', 'SEPARATE_ACTION'Constant form of action value. Should use spective.COLLIDE_ACTION and spective.SEPARATE_ACTION when making comparisions.
side0, 1, 2, 3The side of the node that called oncollide that has collided with the node sending the param. The LEFT, RIGHT, BOTTOM, TOP constants, read from the spective module, map to these values and should be used instead of comparing the number values directly.

Declarative Layouts

Declarative programming is a method of describing what the layout of your scene should be in response to state changes. It is a great way to abstract many of the complex tasks needed to manage your scene. Spective can be used along with another library, named Stew to make this possible. Just be sure to pass it the framework object for spective to switch it from its normal front-end mode. The following example assumes you have included both the stew and spective scripts onto your page. Intructions for how Stew works can be found here, .

stew('', ['scene', {},
	['layer', {},
		['asset', { texture: '/player.png' }
			['node', { pya: -100, oncollide: () => {} }]
		],
		['asset', { texture: '#000', oy: 1 }
			['node', { px: -50, sx: 100, sy: 4, param: {} }]
		],
	],
], spective.framework);

Controls

Controls listen for changes to various inputs you choose. Create an instance by passing info on what to listen for to the Control class, followed by the hold and release callback functions. Controls can listen to multiple types of input in one instance and the state of the control can be found by checking if it currently exists in the 'controls' set read from the spective function.

const spaceControl = new spective.Control({ key: ' ' }, onHold, onRelease) // triggered by space key
const touchControl = new spective.Control({ touch: touchEl }, onHold, onRelease) // triggered by touch events on touchEl
const comboControl = new spective.Control({ key: ' ', touch: touchEl }, onHold, onRelease) // triggered by either
spective.controls.has(spaceControl); // check if spaceControl is currently held

Initialization Properties

NameDefaultDescription
keyListen for keyboard events. The values match the ones used by the 'key' prop in JavaScript's 'keydown' and 'keyup' listeners.
clickListen for left clicks on HTML element.
contextListen for right clicks on HTML element.
touchListen for touchscreen events on an HTML element.

Callback Properties

The hold and release callback functions will receive an event object with the following properties.

NameOptionsDescription
input'key', 'click', 'context', 'touch'The input type that triggered the callback.
type'KEY_INPUT', 'CLICK_INPUT', 'CONTEXT_INPUT', 'TOUCH_INPUT'Constant form of input value. Should use spective.KEY_INPUT, spective.CLICK_INPUT, spective.CONTEXT_INPUT, and spective.TOUCH_INPUT when making comparisions.
controlThe Control instance that triggered the callback.

Geometries (coming soon)

Technically 3D objects are allowed and exist in the hierarchy between layers and assets. Assets simply defualt to using a square. Currently, the ability to set custom UV coordinates is still be worked out. It should also be able to parse OBJ files once finished.

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.2-0

1 year ago

2.5.0

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago