GithubHelp home page GithubHelp logo

spaceblocks / atrament.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jakubfiala/atrament

0.0 0.0 0.0 1.1 MB

A small JS library for beautiful drawing and handwriting on the HTML Canvas.

Home Page: http://fiala.space/atrament.js/demo

License: Other

JavaScript 100.00%

atrament.js's Introduction

Atrament

A small JS library for beautiful drawing and handwriting on the HTML Canvas


Build Status

Atrament is a library that enables the user to draw smooth, natural drawings and handwriting on the HTML canvas. The algorithm was originally developed about 2 weeks after I started learning JavaScript, as I wanted to build a collaborative drawing space on the web, which ended up being 1WALL. I wanted the drawing to feel natural and comfortable, and the result to be smooth and pleasing. Years later, I've taken the algorithm, improved it, rewrote it in ES6 and made it into a neat little library.

FEATURES:

  • Draw/Fill/Erase modes
  • Adjustable adaptive smoothing
  • Events tracking the drawing - this allows the app to "replay" or reconstruct the drawing, e.g. for undo functionality
  • Adjustable line thickness and colour

Here's a basic demo.

Enjoy!

Installation

as a module

If you're using a tool like webpack or browserify to bundle your code, you can install it using npm.

  • install atrament as a dependency using npm install --save atrament.
  • You can access the Atrament class using import { Atrament } from 'atrament';

script tag

Include the script located at dist/atrament.min.js in the <head> tag of your HTML.

Alternatively, you can use Bower: bower install atrament and include bower_components/atrament/dist/atrament.min.js as a script tag.

polymer

Thanks to rubenstolk, you can also use the sc-atrament Polymer element.

Usage

  • create a <canvas> tag, e.g.:
<canvas id="sketchpad" width="500" height="500"></canvas>
  • in your JavaScript, create an Atrament instance passing it your canvas object:
import { Atrament } from 'atrament';

const canvas = document.querySelector('#sketchpad');
const sketchpad = new Atrament(canvas);
  • you can also pass the width, height and default colour to the constructor:
const sketchpad = new Atrament(canvas, {
  width: 500,
  height: 500,
  color: 'orange',
});
  • that's it, happy drawing!

Options & config

  • clear the canvas:
sketchpad.clear();
  • change the line thickness:
sketchpad.weight = 20; //in pixels
  • change the color:
sketchpad.color = '#ff485e'; //just like CSS
  • toggle between modes:
sketchpad.mode = 'erase'; // eraser tool
sketchpad.mode = 'fill'; // click to fill area
sketchpad.mode = 'draw'; // default
sketchpad.mode = 'disabled'; // no modification to the canvas (will still fire stroke events)
  • tweak smoothing - higher values make the drawings look much better, lower values make drawing feel a bit more responsive. Set to 0.85 by default.
sketchpad.smoothing = 1.3;
  • toggle adaptive stroke, i.e. line width changing based on drawing speed for a more natural effect. true by default.
sketchpad.adaptiveStroke = false;
  • record stroke data (enables the strokerecorded event). false by default.
sketchpad.recordStrokes = true;
  • export as image:
//we have to get the dataURL of the image
const dataURL = sketchpad.toImage();
//then we can, for instance, open a new window with it
window.open(dataURL);

Events

Dirty/clean

These events fire when the canvas is first drawn on, and when it's cleared. The state is stored in the isDirty property.

sketchpad.addEventListener('dirty', () => console.info(sketchpad.isDirty));
sketchpad.addEventListener('clean', () => console.info(sketchpad.isDirty));

Stroke start/end

These events don't provide any data - they just inform that a stroke has started/finished.

sketchpad.addEventListener('strokestart', () => console.info('strokestart'));
sketchpad.addEventListener('strokeend', () => console.info('strokeend'));

Fill start/end

These only fire in fill mode. The fillstart event also contains x and y properties denoting the starting point of the fill operation (where the user has clicked).

sketchpad.addEventListener('fillstart', ({ x, y }) =>
  console.info(`fillstart ${x} ${y}`),
);
sketchpad.addEventListener('fillend', () => console.info('fillend'));

Stroke recording

strokerecorded fires at the same time as strokeend and contains data necessary for reconstructing the stroke. pointdrawn fires during stroke recording every time the draw method is called. It contains the same data as strokerecorded.

sketchpad.addEventListener('strokerecorded', ({ stroke }) =>
  console.info(stroke),
);
/*
{
  points: [
    {
      point: { x, y },
      time,
    }
  ],
  color,
  weight,
  smoothing,
  adaptiveStroke,
}
*/
sketchpad.addEventListener('pointdrawn', ({ stroke }) =>
  console.info(stroke),
);

Programmatic drawing

To enable functionality such as undo/redo, stroke post-processing, and SVG export in apps using Atrament, the library can be configured to record the "pen strokes".

The first step is to enable recordStrokes, and add a listener for the strokerecorded event:

atrament.recordStrokes = true;
atrament.addEventListener('strokerecorded', ({ stroke }) => {
  // store `stroke` somewhere
});

The stroke can then be reconstructed using methods of the Atrament class:

// set drawing options
atrament.mode = stroke.mode;
atrament.weight = stroke.weight;
atrament.smoothing = stroke.smoothing;
atrament.color = stroke.color;
atrament.adaptiveStroke = stroke.adaptiveStroke;

// don't want to modify original data
const points = stroke.points.slice();

const firstPoint = points.shift().point;
// beginStroke moves the "pen" to the given position and starts the path
atrament.beginStroke(firstPoint.x, firstPoint.y);

let prevPoint = firstPoint;
while (points.length > 0) {
  const point = points.shift().point;

  // the `draw` method accepts the current real coordinates
  // (i. e. actual cursor position), and the previous processed (filtered)
  // position. It returns an object with the current processed position.
  const { x, y } = atrament.draw(point.x, point.y, prevPoint.x, prevPoint.y);

  // the processed position is the one where the line is actually drawn to
  // so we have to store it and pass it to `draw` in the next step
  prevPoint = { x, y };
}

// endStroke closes the path
atrament.endStroke(prevPoint.x, prevPoint.y);

Development

To obtain the dependencies, cd into the atrament directory and run npm install. You should be able to then build atrament by simply running npm run build.

I didn't bother writing tests because it's such a small package. Contributions are welcome!

atrament.js's People

Contributors

jakubfiala avatar dependabot[bot] avatar reccanti avatar rubenstolk avatar dethe avatar ptkach avatar bencergazda avatar rubenanton90 avatar panglesd avatar future-cyborg avatar uriberto avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.