3.0.2 • Published 2 months ago

@wethegit/preact-stickerbook v3.0.2

Weekly downloads
470
License
ISC
Repository
github
Last release
2 months ago

preact-stickerbook

Easily create collage apps that are accessible by default.

Demo

Basic usage

This is the most simplistic way of using it, it's an artboard with the stickers. No fuzz.
Most likely you will want more control, you will want to generate downloads, add and remove stickers, and more. Check out the full demo on Codepen.

import { h, render } from "preact";
import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";

const App = () => {
  const stickers = [
    {
      key: "my-id-1",
      image: "https://media.giphy.com/media/10mgrhuEWNasNO/giphy.gif",
      order: 0,
    },
  ];

  return (
    <Stickerbook outputWidth={500} outputHeight={500}>
      {stickers.map((sticker) => (
        <Sticker {...sticker} />
      ))}
    </Stickerbook>
  );
};

render(<App />, document.body);

Stickerbook

Your main artboard. Responsible for containing and providing the Stickers with a context and all the required references so they can properly scale.
Apart from the Stickers, the Stickerbook can also have a background, foreground and a frame.

Props

outputHeight

The height of your artboard.

Integer default 500

outputWidth

The width of your artboard.

Integer default 500

background

Object | optional

image | String - Path to an image, can also be a base64 string or blob url.
alt | String default "" - Alternate text.
type | String - A background can be of two types:

  • scene: behaves like background-size: cover
  • pattern: repeats until it fills the artboard.
{
  image: "path-to/background.jpg",
  type: "scene",
  alt: "A paper crumble texture"
}

foreground

foreground will appear be on top of all Stickers.

Object | optional

image | String - Path to an image, can also be a base64 string or blob url.
alt | String default "" - Alternate text.

{
  image: "path-to/foreground.png",
  alt: "My company's logo"
}

frame

frame will appear on top of background but behind Stickers. Useful for borders.

Object | optional

image | String - Path to an image, can also be a base64 string or blob url.
alt | String default "" - Alternate text.

{
  image: "path-to/border.png",
  alt: "Ornate painting-like frame"
}

stickerModifiers

stickerModifiers allows you to pass an Array of variants ("modifiers") for your image-based sticker assets. This will append a control button to the sticker, which will allow the user to cycle through the available modifiers.

Array | optional

Each Array item must be an Object containing the following properties:
controlStyle | Object - the values with which to style to the Sticker's modifier control button.
fileSuffix | String - the specific filename suffix for this modifier item's image asset. For example, if your image's filename is my-sticker-blue.png, the suffix should be -blue.

[
  { controlStyle: { backgroundColor: "#ff0000" }, fileSuffix: "-red" },
  { controlStyle: { backgroundColor: "#ffc700" }, fileSuffix: "-yellow" },
  { controlStyle: { backgroundColor: "#00ffff" }, fileSuffix: "-blue" },
];

Sticker

All of the elements that form the collage. At the very minimum a sticker element MUST have:

  • key: A unique identifier. An easy way to get a unique key is by using Date.now(). This is require to avoid rendering and reordering issues.
  • image: Path to a image, can be a base64 string or a blob url.
  • order: the order of the element on the DOM, think z-index.
const sticker = {
  key: "sticker-d47s7@##s",
  image: "path-to/image.png",
  order: 0,
};

Props

image

Path to an image, can also be a base64 string or blob url.

String

alt

Alternate text.

String default ""

order

Order represents the z-index of the element on the DOM.

Integer default 0

initialScale

Initial scale value of the sticker when it's first mounted.
This is similar to css scale() but it doesn't take x and y just a single value.

Float|null | optional

initialRotation

Initial rotation value of the sticker when it's first mounted.
The value needs to be a valid css <angle> in radians but without the unit notation.

Float|null | optional

initialPosition

Initial position value of the sticker when it's first mounted.
The value needs to be a Vec2 instance from the wtc-math library.

Vec2|null | optional

defaultScale

If no initialScale is provided defaultScale will be used.

number default 0.3 | optional

onDelete

A callback function to be called when the delete button is clicked.
It's importante to note that if no function is provided, then there delete button won't show.
Note: use the deleteSticker helper function.

Function | optional

import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";
import { deleteSticker } from "@wethegit/preact-stickerbook/helpers";

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);

  return (
    <Stickerbook>
      {stickers.map((sticker, index) => (
        <Sticker
          {...sticker}
          onDelete={() => {
            setStickers((stickers) => deleteSticker(stickers, index));
          }}
        />
      ))}
    </Stickerbook>
  );
};

onReorder

A callback function to be called when the Sticker should change its order.
This function receives two arguments indicating the direction as a string and a boolean indicating if Sticker should be brought before or after all the others.
Leaving this empty won't reorder the stickers when they are focused.
Note: use the reorderSticker helper function.

Function | optional

import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";
import { reorderSticker } from "@wethegit/preact-stickerbook/helpers";

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);

  return (
    <Stickerbook>
      {stickers.map((sticker, index) => (
        <Sticker
          {...sticker}
          onDelete={(direction, extreme) => {
            setStickers((stickers) =>
              reorderSticker({ direction, extreme, stickers, index })
            );
          }}
        />
      ))}
    </Stickerbook>
  );
};

onPosition

A callback function to be called when the position of the Sticker changed.
Note: use the patchSticker helper function.

Function | optional

import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";
import { patchSticker } from "@wethegit/preact-stickerbook/helpers";

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);

  return (
    <Stickerbook>
      {stickers.map((sticker, index) => (
        <Sticker
          {...sticker}
          onPosition={(value) => {
            setStickers((stickers) =>
              patchSticker({ stickers, prop: "position", value, index })
            );
          }}
        />
      ))}
    </Stickerbook>
  );
};

onScale

A callback function to be called when the scale of the Sticker changed.
Note: use the patchSticker helper function.

Function | optional

import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";
import { patchSticker } from "@wethegit/preact-stickerbook/helpers";

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);

  return (
    <Stickerbook>
      {stickers.map((sticker, index) => (
        <Sticker
          {...sticker}
          onScale={(value) => {
            setStickers((stickers) =>
              patchSticker({ stickers, prop: "scale", value, index })
            );
          }}
        />
      ))}
    </Stickerbook>
  );
};

onRotate

A callback function to be called when the rotation of the Sticker changed.
Note: use the patchSticker helper function.

Function | optional

import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";
import { patchSticker } from "@wethegit/preact-stickerbook/helpers";

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);

  return (
    <Stickerbook>
      {stickers.map((sticker, index) => (
        <Sticker
          {...sticker}
          onRotate={(value) => {
            setStickers((stickers) =>
              patchSticker({ stickers, prop: "rotation", value, index })
            );
          }}
        />
      ))}
    </Stickerbook>
  );
};

onModifierChange

A callback function to be called when the Sticker modifier is updated (clicked). The callback receives the sticker's local modifier index value, as well as its unique id.
Note: use the modifyStickerImageInPlace helper function.

Function | optional

import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";
import { modifyStickerImageInPlace } from "@wethegit/preact-stickerbook/helpers";

const MODIFIERS = [
  { controlStyle: { backgroundColor: "#ff0000" }, fileSuffix: "-red" },
  { controlStyle: { backgroundColor: "#ffc700" }, fileSuffix: "-yellow" },
  { controlStyle: { backgroundColor: "#00ffff" }, fileSuffix: "-blue" },
];

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);
  const [modifierIndex, setModifierIndex] = useState(0);

  const onModifierChange = (newModifierIndex, id) => {
    modifyStickerImageInPlace({
      stickers,
      id,
      modifiers: MODIFIERS,
      modifierIndex,
      newModifierIndex,
    });

    // update our global default modifier
    setModifierIndex(newModifierIndex);
  };

  return (
    <Stickerbook>
      {stickers.map((sticker, index) => (
        <Sticker {...sticker} onModifierChange={onModifierChange} />
      ))}
    </Stickerbook>
  );
};

Helpers

exportStickerbook

Returns a representation of the stickerbook in the chosen format.

async Function

options | Object
options.canvas | HTMLCanvasElement | optional - A canvas element to draw to.
options.background | Object | optional - A valid background object.
options. frame | Object | optional - A valid frame object.
options. foreground | Object | optional - A valid foreground object.
options.stickers | Array | optional - An array of valid sticker objects.
options.outputWidth | Integer default 500 - Output width.
options.outputHeight | Integer default 500 - Output height.
options.format | String default "image" - The returned value. Can be one of the following:

  • image: Will generate a url using window.URL.createObjectURL
  • canvas: Will just return the provided canvas or a new one
  • blob: Will return a Blob using HTMLCanvasElement.toBlob()
import { exportStickerbook } from "@wethegit/preact-stickerbook/helpers";

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);

  const getDownloadUrl = async (e) => {
    e.preventDefault();

    const newUrl = await exportStickerbook({
      stickers,
    });

    e.currentTarget.href = newUrl;
    e.currentTarget.click();
  };

  return (
    {/* your app */}
    <a href="#" download="Stickerbook.png" onClick={getDownloadUrl}>Download</a>
  )
}

reorderSticker

Returns a reordered copy of the provided stickers array.

Function

options | Object
options.index | Integer - The index of the sticker on the array that will be reordered.
options.direction | String default "up" - The order in which to move the sticker.
options.extreme | Boolean default false - If it should be brought to the edges of the array.
options.stickers | Array default [] - An array of valid sticker objects.

import Stickerbook, { Sticker } from "@wethegit/preact-stickerbook";
import { reorderSticker } from "@wethegit/preact-stickerbook/helpers";

const App = () => {
  const [stickers, setStickers] = useState([
    /* your stickers */
  ]);

  return (
    <Stickerbook>
      {stickers.map((sticker, index) => (
        <Sticker
          {...sticker}
          onDelete={(direction, extreme) => {
            setStickers((stickers) =>
              reorderSticker({ direction, extreme, stickers, index })
            );
          }}
        />
      ))}
    </Stickerbook>
  );
};

addSticker

Returns a copy of the provided stickers array with the new sticker containing the required required key and order fields.

Function

stickers | Array | optional - An array of valid sticker objects.
sticker | Object - A valid sticker object. Note key and order will be overwritten.

deleteSticker

Returns a copy of the provided stickers array without the selected sticker.

Function

stickers | Array - An array of valid sticker objects.
index | Integer - The index of the sticker on the array that will be reordered.

patchSticker

Returns a copy of the provided stickers array, with the specified prop of a given sticker updated.

Function

options | Object
options.stickers | Array - An array of valid sticker
options.index | Integer - The index of the sticker on the array that will be updated.
options.value | optional - The new value.
options.prop | String - The prop to be updated. Can be one of the folllwing:

  • position
  • scale
  • rotation
  • image

modifyStickerImageInPlace

Returns a copy of the provided stickers array, with the given sticker image modifier applied. Maintains position, scale, rotation, etc.

Function

options | Object
options.stickers | Array - An array of valid sticker
options.id | String|Number - The unique sticker identifier.
options.modifiers | Array - A valid stickerModifiers array.
options.modifierIndex | Number - The currently selected modifier array index.
options.newModifierIndex | Number - The newly updated modifier array index.

4.0.0-beta.2

2 months ago

4.0.0-beta.1

2 months ago

3.0.1-beta.0

10 months ago

3.0.1-beta.1

10 months ago

3.0.1-beta.2

10 months ago

3.0.1-beta.3

10 months ago

3.0.1-beta.4

10 months ago

3.0.2

8 months ago

3.0.1

10 months ago

2.0.2-beta.0

10 months ago

3.0.0

10 months ago

2.0.2

11 months ago

2.0.1

11 months ago

2.0.0-beta.27

11 months ago

2.0.0

1 year ago

2.0.0-beta.9

1 year ago

2.0.0-beta.8

1 year ago

2.0.0-beta.7

1 year ago

2.0.0-beta.2

1 year ago

2.0.0-beta.1

1 year ago

2.0.0-beta.0

1 year ago

2.0.0-beta.6

1 year ago

2.0.0-beta.5

1 year ago

2.0.0-beta.4

1 year ago

2.0.0-beta.3

1 year ago

2.0.0-beta.22

1 year ago

2.0.0-beta.21

1 year ago

2.0.0-beta.20

1 year ago

2.0.0-beta.24

1 year ago

2.0.0-beta.23

1 year ago

2.0.0-beta.11

1 year ago

2.0.0-beta.10

1 year ago

2.0.0-beta.15

1 year ago

2.0.0-beta.14

1 year ago

2.0.0-beta.13

1 year ago

2.0.0-beta.12

1 year ago

2.0.0-beta.19

1 year ago

2.0.0-beta.18

1 year ago

2.0.0-beta.17

1 year ago

2.0.0-beta.16

1 year ago

1.0.4

2 years ago

1.0.4-beta.0

2 years ago

1.0.4-beta.1

2 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.10.1

3 years ago

0.10.2

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.3

3 years ago

0.7.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.6.5

3 years ago

0.8.2

3 years ago

0.1.0

3 years ago

0.3.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.5.0

3 years ago

0.3.2

3 years ago

0.1.4

3 years ago

0.3.1

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.6.3

3 years ago

0.2.7

3 years ago

0.6.2

3 years ago

0.4.4

3 years ago

0.2.6

3 years ago

0.2.8

3 years ago

0.4.1

3 years ago

0.2.3

3 years ago

0.4.0

3 years ago

0.2.2

3 years ago

0.6.1

3 years ago

0.4.3

3 years ago

0.2.5

3 years ago

0.6.0

3 years ago

0.4.2

3 years ago

0.2.4

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.9

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago