1.0.12 • Published 8 months ago

@sketch-paper/core v1.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

🖍️ Sketch Paper

Sketch Paper is a web-component for creating drawing applications. It's the software behind crayon.town.

Check out the live demo at sketchpaper.ink to see Sketch Paper in action.

Installation

npm i @sketch-paper/core

Usage

Sketch-paper is a web component, meaning you use it like this:

// import the library
import '@sketch-paper/core';
<!-- add it to your html -->
<sketch-paper ...></sketch-paper>
// once <sketch-paper/> is mounted, you must initialize the component.
const sketchPaperElement = getElementsByTagName('sketch-paper')[0];
sketchPaperElement.initialize(settings);

Simple Example

Sketch paper works with all frameworks. Here's an example showing it with Vue.js.

<script setup lang="ts">
  import { ref, onMounted } from 'vue';

  import { BrushKinds } from '@sketch-paper/core';

  const sketchPaperRef = ref();
  onMounted(() => {
    sketchPaperRef.value?.initialize({
      tileCountX: 0, // 0 means infinite
      tileCountY: 0, // 0 means infinite
      brushes: [BrushKinds.Crayon],
    });
  });
</script>

<template>
  <sketch-paper
    ref="sketchPaperRef"
    style="width: 100vw; height: 100vh"
    :brush-color="brush.color"
    :brush-kind="brush.kind"
    :brush-size="brush.size"
  >
  </sketch-paper>
</template>

Check out /examples for more.

Settings

// Settings are passed to the component via
sketchPaperElement.initialize(settings);

All settings are optional.

fieldDefaultDescription
brushesBrushKinds.CrayonAn array of brushes to load.
minZoom0.25How far out the camera can zoom.
maxZoom10How far in the camera can zoom.
startX0Camera starting position X.
startY0Camera starting position Y.
tileCountX1Number of tiles in the x direction. 0 means infinite* tiles
tileCountY1Number of tiles in the y direction. 0 means infinite* tiles
tileWidth2048The tile width in pixels.
tileHeight2048The tile height in pixels.
baseUrl''Defines where Sketch Paper looks for tile images. Must be a public URL with the format${baseUrl}/${tileX}_${tileY}.png (tileX, tileY are the tile indices). Empty string means Sketch Paper will not try to load the tiles from remote images.
baseColor#C2BCB0Default color of the tiles.
backgroundColor#FFFFFFColor outside of the drawing surface.
allowUndotrueBoolean for undo/redo functionality. undo = ctrl+z, redo = ctrl+y or ctrl+shift+z
tileLoadingConcurrency3How many tiles can be loading at once, useful when you're using "baseUrl".
maxTiles20How many tiles can be loaded in memory at once.
loadingImg''A URL pointing to an image to show while a tile is loading. The loading image doesn't have to be the same resolution as the tile.
maxSegmentLength0How long a segment is a allowed to be. 0 means infinte.

* Infinite actually means 2^32 pixels, the canvas will loop after that.

Attributes

Add attributes directly to the HTML directive.

<sketch-paper ... brush-color="#ff0000" brush-size="50"></sketch-paper>

<sketch-paper> uses the following attributes. Unlike the settings, these can be updated during runtime. | Attribute | Default | Description | | -------- | ------- | ------- | | brush-color | #000000 | Hex string, it can also include opacity value, eg: #00000000 | brush-kind | BrushKinds.None | None, Crayon, Marker, Paint | brush-size | 10 | The width and height in pixels of the brush tip | action-left-mouse | PointerActions.Draw | None, Draw, Pan | action-middle-mouse | PointerActions.Pan | None, Draw, Pan | action-right-mouse | PointerActions.Pan | None, Draw, Pan | action-wheel | WheelActions.Zoom | None, Zoom, Scroll

Events

Stay informed by listening for events.

<sketch-paper ... @sp-move="handleMove" />
function handleMove(event: SpMoveEvent) {
    console.log(event.detail) // { x: 123; y: 456 }
}

The <sketch-paper> component emits the following events:

EventPayloadDescription
sp-drawDrawSegment[]Emitted each frame when a new segment is drawn. The payload includes a draw segment for each tile that was drawn on.
sp-move{ x: number; y: number }Emitted when the camera is translated.
sp-zoom{ zoom: number }Emitted when the camera is zoomed.
sp-tile-loadindex: number, numberEmitted when a tile image is loaded.
1.0.12

8 months ago

1.0.11

8 months ago

1.0.10

8 months ago

1.0.9

8 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago