:loop: Create loopy loading animations

Overview

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.

Comments
  • Please use an OSI approved license

    Please use an OSI approved license

    Hi, I just had to delete your code from our source trees after I found it with a scan for bad licenses.

    sonic.js is licensed WTFPL, which is not a proper license https://en.wikipedia.org/w/index.php?title=WTFPL&oldid=712745928#Characteristics https://en.wikipedia.org/w/index.php?title=WTFPL&oldid=712745928#Effectiveness_as_license_or_waiver and then you have a top level license that is unlicense, contradicting the file header license. Sadly, unlicense is not a proper license either. http://programmers.stackexchange.com/questions/147111/what-is-wrong-with-the-unlicense

    If you care to share you software without causing issues or worries for others, please use MIT/BSD, or some license like Apache2. Thanks.

    opened by marcmerlin 3
  • HTML5 shims question

    HTML5 shims question

    I am curious to know if we use HTML5 shims, would these work with IE9 and below? If yes, please reply as soon as you can.

    Thank you

    (currently my laptop is under construction and cannot check it myself)

    opened by thednp 3
  • [enhancement] Add missing bower.json.

    [enhancement] Add missing bower.json.

    Hey, maintainer(s) of padolsey/sonic.js!

    We at VersionEye are working hard to keep up the quality of the bower's registry.

    We just finished our initial analysis of the quality of the Bower.io registry:

    7530 - registered packages, 224 of them doesnt exists anymore;

    We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

    Sadly, your library padolsey/sonic.js is one of them.

    Can you spare 15 minutes to help us to make Bower better?

    Just add a new file bower.json and change attributes.

    {
      "name": "padolsey/sonic.js",
      "version": "1.0.0",
      "main": "path/to/main.css",
      "description": "please add it",
      "license": "Eclipse",
      "ignore": [
        ".jshintrc",
        "**/*.txt"
      ],
      "dependencies": {
        "<dependency_name>": "<semantic_version>",
        "<dependency_name>": "<Local_folder>",
        "<dependency_name>": "<package>"
      },
      "devDependencies": {
        "<test-framework-name>": "<version>"
      }
    }
    
    

    Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

    NB! Please validate your bower.json with jsonlint before commiting your updates.

    Thank you!

    Timo, twitter: @versioneye email: [email protected] VersionEye - no more legacy software!

    opened by timgluz 1
  • Added support for crisp rendering on hi-density screens

    Added support for crisp rendering on hi-density screens

    Currently, rendering on hi pixel density screens (e.g retina) looks pixelated and low-res. I've added a little code to support variable pixel density out of the box and ensure crisp rendering on any screen. The pixel density ratio is calculated automatically, but can also be set explicitly via a 'pixelRatio' property in the Options hash passed to the ctor.

    opened by asiFarran 1
  • Bower support

    Bower support

    Hello,

    are there any intentions to make this repo visible on the bower package manager=?

    It should be an easy job and I can drop you a pull request if you want, but I firstly need you to tag this repo with the right version (look at jquery repo and see that has a number of tags that points to specific commits)

    it would be nice to install this library with just a bower install sonic :)

    opened by vshjxyz 1
  • Support High DPI screens

    Support High DPI screens

    All modern devices comes with HDPI screens, so it is very important to support this devices. More info at http://www.html5rocks.com/en/tutorials/canvas/hidpi/

    opened by acelot 1
Owner
James Padolsey
perpetual amateur & coder & humanist & human
James Padolsey
Official Repository of Finite Loop Club website.

Finite Loop Club - Official Repo Built with: Running the Project Clone the repo git clone https://github.com/FiniteLoop-NMAMIT/flc-website Install the

FiniteLoop Club NMAMIT 4 Dec 26, 2022
Fast and lightweight dependency-free vanilla JavaScript polyfill for native lazy loading / the awesome loading='lazy'-attribute.

loading="lazy" attribute polyfill Fast and lightweight vanilla JavaScript polyfill for native lazy loading, meaning the behaviour to load elements rig

Maximilian Franzke 571 Dec 30, 2022
Chakra UI Animations is a dependancy which offers you pre-built animations for your Chakra UI components.

Chakra UI Animations Chakra UI Animations is a dependancy which offers you pre-built animations for your Chakra UI components. Installation yarn add @

Code Chemistry Inc. 9 Nov 19, 2022
Fancytree - JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkboxes, drag'n'drop, and lazy loading

Fancytree Fancytree (sequel of DynaTree 1.x) is a JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkb

Martin Wendt 2.6k Jan 9, 2023
Fallback JS - JavaScript library for dynamically loading CSS and JS files

Fallback JS - JavaScript library for dynamically loading CSS and JS files. Also provides the ability to load multiple files from a CDN with multiple fallback options and shimming!

Dolox Inc. 311 Nov 13, 2022
Lightweight Loading Animation

◼️ Features: ?? Easy to Use ?? Fast & Lightweight (0.5Kb) ?? No dependencies, built with VanillaJS ?? Tested in all modern browsers ◼️ Demo: https://b

Bruno Vieira 76 Sep 1, 2022
A simple way of loading inline es-modules on modern browser.

ES inline module A simple way of loading inline es-modules on modern browser. Usage Use inlineImport to dynamically import inline scripts. <script typ

稀土 40 Dec 22, 2022
Dynamically-loading WebComponent Assembly UI Framework

Golgi: Dynamically-loading WebComponent Assembly Framework Rob Tweed [email protected] 14 February 2022, M/Gateway Developments Ltd http://www.mgate

null 28 Dec 18, 2022
The progressive image loading library for great good!

AntiModerate The progressive image loading library for great good! Reduce loading time of page to less than a second on slow connections by loading an

Jett LaRue 1.5k Dec 20, 2022
A jQuery plugin that lets you attach callbacks to useful image loading events.

waitForImages Copyright (c) 2011-2018 Alexander Dickson @alexdickson Licensed under the MIT licenses. http://alexanderdickson.com Donate! Overview Pro

Alexander Dickson 1.3k Dec 28, 2022
A modern lazy loading library for images.

Layzr.js A modern lazy loading library for images. Demo Page Getting Started Follow these steps: Install Setup Images Instantiate Review Options Revie

Michael Cavalea 5.6k Dec 25, 2022
BttrLazyLoading is a Jquery plugin that allows your web application to defer image loading until images are scrolled to but not only

BttrLazyLoading.js BttrLazyLoading is a Jquery plugin that allows your web application to defer image loading until images are scrolled to but not onl

Julien Renaux 410 Dec 14, 2022
Simple and flexible, css only, content placeholder loading animation.

Placeholder loading Simple and flexible, css only, content placeholder loading animation. Demo https://zalog.github.io/placeholder-loading/ Take a loo

Catalin Zalog 1.4k Dec 30, 2022
A wrapper around IPFS for speeding up the loading of web3 frontend applications.

ipfs-wrapper A wrapper around ipfs-core for speeding up the loading of web3 frontend applications. Used on Blogchain. Requirements NodeJS v14.5.0 or h

Capsule Social 15 Sep 14, 2022
A progressive image loading library. Inspired by Medium’s similar technique.

Blurry Image Load Synopsis A lightweight, zero-dependency library that loads images on demand. Until the images are loaded, a very small version of ea

Dominic Brant 32 Dec 10, 2022
A table component for your Mantine data-rich applications, supporting asynchronous data loading, column sorting, custom cell data rendering, row context menus, dark theme, and more.

Mantine DataTable A "dark-theme aware" table component for your Mantine UI data-rich applications, featuring asynchronous data loading support, pagina

Ionut-Cristian Florescu 331 Jan 4, 2023
Bootstrap 5 spinner/loading/pending indicator for ,