• Stars
    star
    821
  • Rank 55,549 (Top 2 %)
  • Language
    HTML
  • License
    Other
  • Created about 13 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

โžฟ Create loopy loading animations

Sonic.js

Sonic is a tool that you can use to create spinny-loady-thingies on the fly. It's best for shapes that loop.

E.g. a square:

var square = new Sonic({
    width: 100,
    height: 100,
    fillColor: '#000',
    path: [
        ['line', 10, 10, 90, 10],
        ['line', 90, 10, 90, 90],
        ['line', 90, 90, 10, 90],
        ['line', 10, 90, 10, 10]
    ]
});

square.play();

document.body.appendChild(square.canvas);

Square demo: http://padolsey.net/p/Sonic/repo/demo/square.html

How does it work?

Sonic works by drawing a shape (by default a square, fillRect) at tiny intervals along a pre-defined path. You define the path via the path option:

Drawing methods are specified in the path array like so:

[methodName, arguments...]

Methods and their arguments:

  • ['line', startX, startY, endX, endY]
  • ['bezier', startX, startY, endX, endY, cp1x, cp1y, cp2x, cp2y]
  • ['arc', cx, cy, radius, startDegree, endDegree] (degrees, not radians!)

Options

Options that you can pass to new Sonic({...}) include:

  • path: An array which defines the path taken by sonic. Look at the square example above. Each array item is an array with the format [methodName, arguments...], with the available methods specified above (line, bezier and arc).
  • width: The pixel width of the canvas (note: not including padding)
  • height: The pixel height of the canvas (note: not including padding)
  • padding (default: 0): The pixel padding.
  • fillColor: The canvas' context's fill color (e.g. red, rgb(255,0,0), #F00)
  • strokeColor: The canvas' context's stroke color (e.g. green, rgb(0,255,0), #0F0)
  • fps (default: 25): How many frames per second. More fps means smoother animation and sonic will progress through the specified path quicker. Less frames means the opposite. There shouldn't be much need to change this.
  • stepsPerFrame (default: 1): This integer specifies how many steps of an animation should occur on each frame.
  • pointDistance (default: .05): The distance between points (0..1) in each path (that is, each sub-path under the main path). If you draw a line, and set the pointDistance to 0.1, then there will be ten steps in the animation (ten points).
  • trailLength (default: 1): The length of sonic's trail (0..1). A length of one means that it's like a snake trying to eat its own tail.
  • step (default: "square"): A function OR a name of a function found in Sonic.stepMethods. This function will be called on every step of the animation. See below (under "more control") for more info on this function.
  • domClass (default: "sonic"): A class to be applied to the <canvas> element.

More control:

Custom shapes can be drawn with the help of step:

new Sonic({
	//...
	step: function(point, index, frame) {

		// point is an object { x: n, y: n, progress: n}
		// point.progress is progress of point (0..1)
		// relative to other points in that single draw

		// index is the progress relative to entire shape

		// frame is the current frame (0..1) 

		// E.g. let's draw a tiny circle:

		this._.beginPath();
		this._.moveTo(point.x, point.y);
		this._.arc(point.x, point.y, 5, 0, Math.PI*2, false);
		this._.closePath();
		this._.fill();

		// this == Sonic instance
		// this._ == canvas context
		// this.alpha = alpha opacity

	}
});

For more demos, see: https://github.com/padolsey/sonic.js/blob/master/demo/demo.html

FAQ:

Isn't it slow/inefficient?

No, not really. It only has to run the calculations (i.e. functions for line, arc, and bezier) for one loop, then Sonic caches the produced image and simply calls putImageData on every subsequent frame. It's pretty quick.

Older browsers!?

Cadell Christo (cadc) has made sonicGIF, which generares GIFs for you to use in older browsers.

There's no anticlockwise arg for arc, why?

It's not needed. You can make an arc progress in an anti-clockwise direction by going from, 360 to 0, instead of 0 to 360.

More Repositories

1

findAndReplaceDOMText

๐Ÿ” Find and replace DOM text
JavaScript
776
star
2

operative

๐Ÿ• Seamlessly create Web Workers
JavaScript
655
star
3

relevancy.js

๐Ÿ“Š Sort/Weigh strings by relevance
JavaScript
286
star
4

satisfy

๐Ÿ”ฎ Generate HTML from CSS selectors
JavaScript
204
star
5

cron.js

Cron for JavaScript. Still in development.
JavaScript
122
star
6

SIML

Simplified Markup
JavaScript
97
star
7

vic.js

Validation & Input Correction abstraction
JavaScript
91
star
8

mini

A tiny selector engine; only the essentials
JavaScript
57
star
9

at.hn

JavaScript
44
star
10

parseScripts

A simple abstraction to simplify the process of adding your own JavaScript enhancements. Read README.txt
32
star
11

ZergRush

JavaScript
27
star
12

parsers-built-with-js

Trying to build a list of parsers/interpreters/converters built in JavaScript.
27
star
13

jsapi-info

Publically configurable, centralised store of JS libs' API source code
JavaScript
25
star
14

jQuery.Interface

Sub classing jQuery methods to creating your own classes with DOM interfaces
JavaScript
21
star
15

string.prototype

Cool String.prototype additions
JavaScript
20
star
16

redoku

๐Ÿ”ก Little Regex "Sudoku" game generator
HTML
18
star
17

debug

debug.js is an in-browser JavaScript error-reporter
JavaScript
14
star
18

sonic-creator

JavaScript
13
star
19

javascript-dom-general

Random JavaScript
JavaScript
13
star
20

commentData

Keep per-element metadata in HTML comments.
JavaScript
13
star
21

rGen

Generate strings to fulfill basic regular expressions using rGen.
JavaScript
12
star
22

SiteTraverser

A JavaScript "toolkit" you can use to create bookmarklets specifically designed to crawl websites looking for the presence or lack of certain features
JavaScript
12
star
23

embedHelper

"embedHelper" is a tiny helper-library used to aid in the unobtrusive inclusion and execution of 3rd party scripts - it's used within the 3rd party script.
JavaScript
10
star
24

PhotoWall

JavaScript
8
star
25

cssDataRequest

Cross-domain Ajax via CSS
JavaScript
8
star
26

jQuery-Commandments

I am not a prophet of the revered John Resig, but I have done my best to compile a list of commandments. One applicable to jQuery programming and the other applicable to jQuery plugin authoring.
JavaScript
7
star
27

twtl

twtl is a tiny in-browser testing library
JavaScript
6
star
28

jquery-costume-party

JavaScript
6
star
29

lewis

JavaScript
6
star
30

jQuery.macroSelect

Macro selectors incl jQuery method names -- one step beyond what CSS3 offers.
JavaScript
5
star
31

CanvasThing

JavaScript
5
star
32

receptive

JavaScript
3
star
33

lamb

JavaScript
2
star
34

tik

MVC server for node
JavaScript
1
star
35

proxyfoo

ESHarmony Proxies stuff
JavaScript
1
star