• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

random blob generation and animation

Legacy documentation

Install

$ npm install blobs
import * as blobs2 from "blobs/v2";
import * as blobs2Animate from "blobs/v2/animate";

OR

<script src="https://unpkg.com/blobs/v2"></script>
<script src="https://unpkg.com/blobs/v2/animate"></script>

SVG Path

const svgPath = blobs2.svgPath({
    seed: Math.random(),
    extraPoints: 8,
    randomness: 4,
    size: 256,
});
doSomething(svgPath);

SVG

const svgString = blobs2.svg(
    {
        seed: Math.random(),
        extraPoints: 8,
        randomness: 4,
        size: 256,
    },
    {
        fill: "white", // 🚨 NOT SANITIZED
        stroke: "black", // 🚨 NOT SANITIZED
        strokeWidth: 4,
    },
);
container.innerHTML = svgString;

Canvas

const path = blobs2.canvasPath(
    {
        seed: Math.random(),
        extraPoints: 16,
        randomness: 2,
        size: 128,
    },
    {
        offsetX: 16,
        offsetY: 32,
    },
);
ctx.stroke(path);

Canvas Animation

const ctx = /* ... */;
const animation = blobs2Animate.canvasPath();

TODO wiggle

// Set up "requestAnimationFrame" rendering loop.
const renderAnimation = () => {
    ctx.clearRect(0, 0, width, height);
    ctx.fill(animation.renderFrame());
    requestAnimationFrame(renderAnimation);
};
requestAnimationFrame(renderAnimation);

// Keyframe loop.
const loopAnimation = () => {
    animation.transition({
        duration: 4000,
        timingFunction: "ease",
        callback: loopAnimation,
        blobOptions: {...},
    });
};

// Initial frame.
animation.transition({
    duration: 0, // Render immediately.
    callback: loopAnimation,
    blobOptions: {...},
});

// Toggle play/pause animation on canvas click.
ctx.canvas.onclick = () => {
    animation.playPause();
};

Complete API

"blobs/v2"

export interface BlobOptions {
    // A given seed will always produce the same blob.
    // Use `Math.random()` for pseudorandom behavior.
    seed: string | number;
    // Actual number of points will be `3 + extraPoints`.
    extraPoints: number;
    // Increases the amount of variation in point position.
    randomness: number;
    // Size of the bounding box.
    size: number;
}

export interface CanvasOptions {
    // Coordinates of top-left corner of the blob.
    offsetX?: number;
    offsetY?: number;
}
export const canvasPath: (blobOptions: BlobOptions, canvasOptions?: CanvasOptions) => Path2D;

export interface SvgOptions {
    fill?: string; // Default: "#ec576b".
    stroke?: string; // Default: "none".
    strokeWidth?: number; // Default: 0.
}
export const svg: (blobOptions: BlobOptions, svgOptions?: SvgOptions) => string;
export const svgPath: (blobOptions: BlobOptions) => string;

"blobs/v2/animate"

interface Keyframe {
    // Duration of the keyframe animation in milliseconds.
    duration: number;
    // Delay before animation begins in milliseconds.
    // Default: 0.
    delay?: number;
    // Controls the speed of the animation over time.
    // Default: "linear".
    timingFunction?:
        | "linear"
        | "easeEnd"
        | "easeStart"
        | "ease"
        | "elasticEnd0"
        | "elasticEnd1"
        | "elasticEnd2"
        | "elasticEnd3";
    // Called after keyframe end-state is reached or passed.
    // Called exactly once when the keyframe end-state is rendered.
    // Not called if the keyframe is preempted by a new transition.
    callback?: () => void;
    // Standard options, refer to "blobs/v2" documentation.
    canvasOptions?: {
        offsetX?: number;
        offsetY?: number;
    };
}

export interface CanvasKeyframe extends Keyframe {
    // Standard options, refer to "blobs/v2" documentation.
    blobOptions: {
        seed: number | string;
        randomness: number;
        extraPoints: number;
        size: number;
    };
}

export interface CanvasCustomKeyframe extends Keyframe {
    // List of point coordinates that produce a single, closed shape.
    points: Point[];
}

export interface Animation {
    // Renders the current state of the animation.
    renderFrame: () => Path2D;
    // Immediately begin animating through the given keyframes.
    // Non-rendered keyframes from previous transitions are cancelled.
    transition: (...keyframes: (CanvasKeyframe | CanvasCustomKeyframe)[]) => void;
    // Resume a paused animation. Has no effect if already playing.
    play: () => void;
    // Pause a playing animation. Has no effect if already paused.
    pause: () => void;
    // Toggle between playing and pausing the animation.
    playPause: () => void;
}

// Function that returns the current timestamp. This value will be used for all
// duration/delay values and will be used to interpolate between keyframes. It
// must produce values increasing in size.
// Default: `Date.now`.
export interface TimestampProvider {
    (): number;
}
export const canvasPath: (timestampProvider?: TimestampProvider) => Animation;

export interface WiggleOptions {
    // Speed of the wiggle movement. Higher is faster.
    speed: number;
    // Delay before the first wiggle frame.
    // Default: 0
    initialDelay?: number;
    // Length of the transition from the current state to the wiggle blob.
    // Default: 0
    initialTransition?: number;
    // Interpolation function.
    // Default: linear
    initialTimingFunction?: Keyframe["timingFunction"];
}
// Preset animation that produces natural-looking random movement.
// The wiggle animation will continue indefinitely until the next transition.
export const wigglePreset = (
    animation: Animation,
    blobOptions: BlobOptions,
    canvasOptions: CanvasOptions,
    wiggleOptions: WiggleOptions,
)

License

MIT

More Repositories

1

npmfs

javascript package inspector
Go
101
star
2

sekstant

graphql interface for the kubernetes api
JavaScript
53
star
3

rickety

minimal typescript rpc library
TypeScript
12
star
4

targetblank

organize your links
TypeScript
10
star
5

ence

automation friendly json schemas
TypeScript
8
star
6

website

personal website generator running in the cloud
Go
7
star
7

slurry

automagically curry function arguments
JavaScript
6
star
8

svgsaurus

svg generation by query string
Go
6
star
9

gothrough

searchable index of exported members in the Go standard library and a selection of hosted packages
Go
5
star
10

coco

track project stats
Go
4
star
11

ayto

TypeScript
3
star
12

open-source-logos

custom logos for open-source projects
3
star
13

project-plans

woodworking projects index
TypeScript
3
star
14

beefypotatoes

TypeScript
2
star
15

SOEN343

Team 6️⃣
PHP
2
star
16

cards-amusing-humanity

Python
2
star
17

backend-challenge

Go
1
star
18

cover-gen

cover page pdf generator
Go
1
star
19

tomas

portfolio recommendation engine
HTML
1
star
20

sixsixtythree

TypeScript
1
star
21

emn

command line string replacement
JavaScript
1
star
22

tic-tac-toilet-paper-holder

1
star
23

keyboards

personal archive of keyboard layouts and firmware files
1
star
24

SOEN487A2

Python
1
star
25

corne

firmware builder for corne keyboard
1
star
26

http

partial http/1.0 client and server implementations
Go
1
star
27

blanket-ladder

1
star
28

euro-box-shelves

1
star
29

game-of-life

performant implementation of Conway's game-of-life using the HTML5 canvas
JavaScript
1
star
30

advent-of-code-2021

TypeScript
1
star
31

superpermutations

calculate and check superpermutations
Go
1
star
32

transactions

TypeScript
1
star
33

retro-dog-house

1
star
34

micro-workbench

1
star
35

existing-tv-stand

1
star
36

edelweiss

Go
1
star
37

topper-block

1
star
38

contributions

1
star
39

leatherman

TypeScript
1
star
40

console-table-drawer-box

1
star
41

beatmap

interactive music data exploration
Python
1
star
42

keeb.info

TypeScript
1
star