JSprite is a JavaScript library that gives display functionalities in JavaScript, and allows you to work with Sprite objects that have methods to manipulate them.
To start programming with JSprite, you first need to import fabric.js
(what JSprite uses), and the actual JSprite
library.
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js'></script>
<script type='text/javascript' src='https://rawgit.com/cyoce/JSprite/master/Sprite.js'></script>
Alternatively, you can just copy the template.html
file, found here.
JSprite will automatically add defined sprites to the selected canvas. To select a canvas, use JSprite.frame = canvas
, where canvas
is the id
of the canvas you want to add sprites to.
To create a sprite constructor, use the following:
var Sprite = JSprite(function (name){
this.name = name;
console.log(name);
});
This will create a Sprite constructor. The sprite constructor is what you will use to create sprites of that type.
Once you have your sprite constructor, you probably want to then make sprites as well. This is done with the new
keyword, used on the value you stored as the sprite constructor.
var player = new Sprite('username');
Note: when a sprite is created, it will be automatically added to the screen.
A sprite can be manipulated with any of its built-in methods. In the sprite constructor, or any of the sprite's event callbacks, it can be referred to using the this
keyword, e.g. this.goto(100,100)
. In the global context or from another sprite, you can refer to it by a variable it is stored in, e.g. sprite1.goto(100,100)
.
sprite.goto(x,y)
-- set the sprite's coordinates to(x,y)
, with(0,0)
at the center of the canvas.sprite.move(steps)
-- move the sprite forwardsteps
pixels, in the direction of its anglesprite.point(a[,b])
-- ifa
is an object, point towards the object's X and Y coordinates. Ifa
is a number andb
is a number, point towards(a,b)
. Ifa
is a number andb
is not specified, point in directiona
sprite.turn(steps)
-- increase the sprite's angle bysteps
degrees.
sprite.x
-- the sprite's x-coordinatesprite.y
-- the sprite's y-coordinatesprite.image
-- the image the sprite uses.sprite.image = string
-- if the string starts with#
, sets the image to an<img>
element on the page with id ofstring
. Otherwise, it finds an image at the URL ofstring
.sprite.angle
-- the direction the sprite is facing.sprite.angle = number
-- points the sprite in the specified direction, modulo 360 (negative numbers turn backwards).sprite1.touching(sprite2)
-- checks if the two sprites are touching. If they are, returnssprite2
, if not, returnsnull
. It can be given an infinite number of arguments, each representing a new sprite. If given a sprite constructor (class), it will return the first sprite made by that class touching it. Note: it checks the image's outline, not the actual image. It doesn't matter if the touching areas have alpha.
JSprite supports a few events, mostly related to the mouse
sprite.onmousedown
-- function that is fired when the mouse is clicked anywheresprite.onmouseup
-- function that is called when mouse is releasedsprite.onmousemove
-- function that is called when the mouse is movedsprite.onkeydown
-- called initially when a key is pressed, passed the key as a stringsprite.onkeydup
-- called when key is released, passed the key as a stringsprite.update
-- function that is called everyJSprite.timer.int
ms when program is inrunning
state
To call a function when key
is pressed/released, use sprite.keydown[key] = function(){}
or sprite.keyup[key] = function(){}
.
The timer
object can be accessed by using JSprite.timer
. It has the following properties:
JSprite.timer.int
-- the interval every sprite'supdate
function is called. It is given one argument,t
, the time (in ms) since the last call.JSprite.timer.scale
-- the multiplier given to thet
argument inupdate
functions. Defaults to0.01
(converts ms to seconds). The program can start regularly calling theupdate
method by usingJSprite.start()
, and it can be ended withJSprite.stop()
. This can be called at the start of the file, or can be bound to a button.
The mouse
object can be accessed via JSprite.mouse
. It has the following properties:
JSprite.mouse.x
-- x-coordinate of the mouse (bound inside the canvas)JSprite.mouse.y
-- y-coordinate of the mouse (bound inside the canvas)JSprite.mouse.last
-- stores last x and y coordinates of the mouseJSprite.mouse.down
-- boolean,true
if mouse1 is currently held down,false
if not.
The key
object can be accessed via JSprite.key
. It has the following properties:
JSprite.key.pressed(key)
-- returnstrue
ifkey
is currently held down,false
if not.