Giter Club home page Giter Club logo

shapes.js's Introduction

shapes is an easy-to-use shape drawing API for the HTML canvas.

  • Simple: One shape = function call
  • Super tiny: under 3 KB unminified & uncompressed
  • No external dependencies
  • TypeScript support (definitions included)

Quick guide

npm i veridict/shapes.js --save

Shapes are drawn by calling different functions on the shape API. To create a new shape API use:

const shape = shapes(canvas);

Then use it, easy as pie:

shape.circle({
  radius: 100,
  x: 50,
  y: 50,
  style: '#009688'
});

If you need to offset the drawing origin (offset from top left), you can use:

shape.offset(50, 50);

You can also draw layers:

const shape = shapes(ctx);
const text = 'Layers are easy!';
const font = '14px sans-serif';
const pad = 14;
const width = shape.textWidth(text, font) + pad * 2;
const height = pad * 3;

shape.layers([
  // Contents (z-index 0)
  {
    'rect background': {
      x: 0,
      y: 0,
      width,
      height,
      style: '#000000DD'
    },
    'text contents': {
      x: pad,
      y: pad + pad / 2,
      value: text,
      style: '#fff'
    }
  },
  // Details (z-index 1)
  {
    'rect bottom border': {
      x: pad,
      y: pad * 2,
      width: width - pad * 2,
      height: 2,
      style: '#fff'
    }
  }
]);

Each layer contains a set of named commands, they describe:

  • The draw-command, e.g. rect
  • A descriptive name, e.g. background (optional)

The name works like a discriminator between commands within a layer. As JavaScript-objects require unique property names, having two rect commands would not work.

Layers are useful for combining draw calls, as you can keep your layers and draw logic separated.

Check out the basic example.

shapes.js's People

Contributors

mausworks avatar adamostgaard avatar

Stargazers

ciolt avatar

Watchers

James Cloos avatar ciolt 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.