Coder Social home page Coder Social logo

adon988 / atrament.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jakubfiala/atrament

1.0 1.0 0.0 366 KB

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

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

License: Other

HTML 9.58% JavaScript 90.42%

atrament.js's Introduction

atrament.js

Tiny JS library for beautiful drawing and handwriting on the HTML Canvas


Atrament is a lightweight 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.

Here's a basic demo.

Enjoy!

Installation

script tag

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

npm

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.
  • require the package in a module using var atrament = require('atrament');
  • You can also access the Atrament class using var Atrament = require('atrament').Atrament;

Usage

  • create a <canvas> tag, e.g.:
<canvas id="mySketcher" width="500px" height="500px">
  • in your JavaScript, call atrament passing in the selector string of your canvas:
var sketcher = atrament('#mySketcher');
//or use a more object-oriented style
var sketcher = new Atrament('#mySketcher');
//you can also pass the canvas Node itself
var sketcher = new Atrament(document.querySelector('#mySketcher'));
  • you can also pass the width, height and default colour to the function:
var sketcher = atrament('#mySketcher', 500, 500, 'orange');
  • that's it, happy drawing!

Options & config

  • clear the canvas:
sketcher.clear();
  • change the line thickness:
sketcher.weight = 20; //in pixels
  • change the colour:
sketcher.colour = `#ff485e`; //just like CSS
  • toggle between modes:
sketcher.mode = `erase`; 	// eraser tool
sketcher.mode = `fill`; 	// click to fill area
sketcher.mode = `draw`; 	// default
  • toggle smoothing - having it on makes the drawings look much better, turning it off makes it feel a bit more responsive:
sketcher.smoothing = false;
  • change the opacity:
sketcher.opacity = 0.5; //number between 0-1
  • export as image:
//we have to get the dataURL of the image
var dataURL = sketcher.toImage();
//then we can, for instance, open a new window with it
window.open(dataURL);

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 reccanti avatar ptkach avatar

Stargazers

Adam Ou-Yang avatar

Watchers

Adam Ou-Yang 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.