Giter VIP home page Giter VIP logo

canvasimo's Introduction

Canvasimo CircleCI

An HTML5 canvas drawing library, with 150+ useful methods, jQuery-like fluent interface, and cross-browser compatibility enhancements.

Full documentation and examples available on canvasimo.com

Installation

Install Canvasimo with npm (add --save to add to your package.json)

npm install canvasimo

...or download from GitHub

Getting started

Load Canvasimo

Canvasimo can be bundled with all major build tools, or loaded with a script tag and used as a global

With an ES6 bundler / TypeScript (recommended)

import Canvasimo from 'canvasimo';
// Or
import { Canvasimo } from 'canvasimo';

Both of these will import the Canvasimo class as it is both a default and named export.

With an ES5 bundler / commonjs

var canvasimo = require('canvasimo');
var Canvasimo = canvasimo.Canvasimo;

As a global

<script type="text/javascript" src="path/to/canvasimo.js">

Now Canvasimo is accessible globally like so (to allow for ES6 and TypeScript default imports)

const Canvasimo = canvasimo.Canvasimo;

Create a Canvasimo instance

// Get your canvas element
const element = document.getElementById('canvas');

// Create Canvasimo instance
const canvas = new Canvasimo(element);

Begin drawing

Here's a simple chart example

const margin = 20;
const width = 600;
const height = 200;
const start = 0;
const end = 11;
const colors = ['red', 'green', 'blue'];
const data = [
  [3, 7, 2, 8, 3, 8, 5, 4, 4, 7],
  [7, 5, 6, 7, 8, 4, 5, 3, 2, 3],
  [9, 8, 7, 5, 3, 6, 4, 5, 2, 5]
];

canvas
  // Set the canvas size
  .setSize(width, height)
  // Set some initial fill and stroke styles
  .setFill('black')
  .setStroke('black')
  .setStrokeWidth(1)
  // Setup fonts for the axis labels
  .setTextAlign('center')
  .setTextBaseline('middle')
  .setFontFamily('arial')
  .setFontSize(10)
  // Draw the axis lines
  .beginPath()
  .strokeLine(margin, margin, margin, height - margin)
  .beginPath()
  .strokeLine(margin, height - margin, width - margin, height - margin)
  // Draw the x axis labels
  .repeat(start, end, (index) => {
    canvas
      .fillText(index, margin / 2, height - margin - (height - margin * 2) / 10 * index)
  })
  // Loop over our data
  .forEach(data, (dataSet, index) => {
    const verticalScale = (height - margin * 2) / (end - 1);
    const horizontalScale = (width - margin * 2) / (dataSet.length - 1);

    // Map our values to our chart area
    const values = dataSet.map((value, index) => [index * horizontalScale, -value * verticalScale]);

    canvas
      // Save the current canvas state
      .save()
      // Move to the bottom left corner of the chart area
      .translate(margin, height - margin)
      // Draw a data set as a path
      .beginPath()
      .strokePath(values, colors[index])
      // Restore canvas to its previous state
      .restore()
  });

TypeScript support

As of version 0.7.0 Canvasimo only officially supports TypeScript 3.1.6 and upwards.

Canvasimo may work with older versions of TypeScript, but due to a change in TypeScript's native lib types you will need to create a global type alias:

type Canvas2DContextAttributes = CanvasRenderingContext2DSettings;

canvasimo's People

Contributors

jakesidsmith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

canvasimo's Issues

Basic example code in docs

const element = document.getElementById('canvas');
const canvas = new Canvasimo(element);

canvas
  .setDensity(2)
  .setSize({width: 100, height: 100})
  .fillCanvas('#EEEEEE')
  .tap(() => {
    const { width, height } = canvas.getSize();

    canvas
      .fillCircle(width / 2, height / 2, 20, false, 'red');
  })
  .repeat(10, (index) => {
    const y = canvas.map(index, 0, 10, 0, canvas.getHeight());
    canvas.strokeLine(10, y, canvas.getWidth() - 10, y, 'green');
  })
  .beginPath()
  .strokePath([{x: 10, y: 80}, {x: 20, y: 70}, {x: 30, y: 90}], 'blue');

Chart demo

Demonstrate how easy it is to generate charts using canvasimo

Update readme

  • Add some more info to the readme
  • Update description to match others
  • Update package.json description
  • Basic example of chaining (highlight roundedRect / font methods / forEach)
  • Direct people toward canvasimo.com
  • Add basic setup instructions & example to readme
  • Add basic setup instructions & example to docs
  • Add images?

Docs - additional info

  • Add basic chaining example
  • Link to Mozzila documentation for references to original methods
  • Link to data types on Mozzila
  • Find nicer way to display data types
  • Add a way to demonstrate different uses of arguments (e.g. that clip can have none, 1 or 2 arguments, or how drawImage handles cropping)
  • Add information about possible strings that can be used (e.g. text alignment)
  • Explain the points arrays that can be passed to path methods
  • Add demo code to methods that are not obvious

Compatibility issues

  • Do not directly bind original canvas methods
  • Add bind polyfill
  • Do not rely on ellipse for circle methods - use arc
  • Try catch every original canvas method?

Add event listeners

Add .on and .off (alias .bind and .unbind) methods for listening to mouse events etc.

These should return custom events with the mouse position relative to the canvas (taking density into account).

These should warn if many event listeners are added within a short period of time (to prevent adding listeners within a draw loop).

Plugins

Allow defining plugins to add additional methods e.g. for drawing chart stuff

Ensure that plugins cannot override default methods, or each other.

Create custom snapshot renderer

This should stub the CanvasRenderingContext2D and create an ascii representation that is written to a file to check for changes later.

expect(context).toMatchCanvasSnapshot();
expect.extend({
  toMatchCanvasSnapshot(received, argument) {
    const pass = //;

    if (pass) {
      return {
        message: () => `expected ${received} not to match snapshot ${snapshot}`,
        pass: true,
      };
    } else {
      return {
        message: () => `expected ${received} to match snapshot ${snapshot}`,
        pass: false,
      };
    }
  }
});

Version method

Add a method to return the installed version of canvasimo (and log this to the console).

Import from package.json

Text does not draw on iOS

  • No text renders on iOS due to the maxWidth argument being undefined

Not sure how to fix this one... huge number?

typeof maxWidth === 'undefined' ? 1000000 : maxWidth;

SEO

  • Short description
  • Ordering of important content
  • Meta tags
  • Open Graph

Handling multiple optional arguments

Nicely handle optional arguments, so that undefined does not need to be passed e.g.

canvas.fillText('text', 0, 0, undefined, 'black');

// Should be able to be done in the following way
// without the need for a maxWidth

canvas.fillText('text', 0, 0, 'black');

Unsure if this will be possible with all methods

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.