npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

box-sprite-svg

v1.2.0

Published

2d physics container for svg elements

Downloads

10

Readme

box-sprite-svg

2d physics container for svg elements

example

To drive an svg circle around, starting from this html:

<!doctype html5>
<html>
  <body>
    <svg xmlns="http://www.w3.org/2000/svg">
      <circle id="player" cx=250 cy=250 r=10 fill="blue"></circle>
    </svg>
    <script src="bundle.js"></script>
  </body>
</html>

we can wrap the player element in a sprite and change its velocity when a key is pressed:

var Sprite = require('box-sprite-svg');
var Loop = require('frame-loop');
var keyname = require('keynames');

var elem = document.querySelector('svg #player');
var sp = Sprite(elem);

var engine = Loop(function (dt) {
    sp.tick(dt);
});
engine.run();

window.addEventListener('keydown', function (ev) {
    var name = keyname[ev.which];
    if (name === 'left') sp.velocity.x = -300;
    if (name === 'right') sp.velocity.x = 300;
    if (name === 'up') sp.velocity.y = -300;
    if (name === 'down') sp.velocity.y = 300;
});
window.addEventListener('keyup', function (ev) {
    var name = keyname[ev.which];
    if (name === 'left' || name === 'right') sp.velocity.x = 0;
    if (name === 'up' || name === 'down') sp.velocity.y = 0;
});

methods

var Sprite = require('box-sprite-svg');

var sp = Sprite(elem, fn)

Create a new sprite instance sp given an element elem.

Optionally register an fn handler for the 'tick' event.

You can also create a new Sprite using inheritance with a module like inherits:

var Sprite = require('box-sprite-svg');
var inherits = require('inherits');

inherits(Player, Sprite);

function Player (elem) {
    if (!(this instanceof Player)) return new Player(elem);
    Sprite.call(this, elem);
}

Player.prototype.jump = function () {
    if (this.position.y !== 0) return;
    this.velocity.y = -1450;
    this.acceleration.y = 120;
};

sp.bbox()

Return the bounding rectangle of the underlying svg element. This is very handy for computing collisions among sprites.

sp.appendTo(target)

Append the current sprite element to a dom node or query selector string target.

sp.tick(dt)

Call this method when you want the sprite to advance by dt, a time delta in milliseconds.

sp.reset()

Set the motion properties all back to (0,0), remove the element from its parent node, and unregister all listeners.

This method is very handy for reusing sprites, which you'll probably want to do for performance reasons.

properties

sp.acceleration = { x: 0, y: 0 }

Change in velocity as pixels per second per second

sp.velocity = { x: 0, y: 0 }

Change in position as pixels per second

sp.position = { x: 0, y: 0 }

Coordinates as pixels

events

sp.on('tick', function (dt) {})

Every time .tick(dt) is called with the time delta dt, this event fires.

sp.on('reset', function () {})

When the .reset() method is called, this event fires.

install

With npm do:

npm install box-sprite-svg

license

MIT