Giter Site home page Giter Site logo

leinwand's Introduction

leinwand

Code Climate Build Status devDependency Status Coverage Status

Method chaining for the canvas 2d api.

leinwand is a small library that wraps the canvas 2d api. It provides some helper functions as well as a chaining api for most methods that exist on CanvasRenderingContext2D.

Example usage

let canvas = document.getElementById('myCanvas');
let l = new Leinwand(canvas);

l
  .fillStyle('red')
  .mt(50, 50)
  .lt(250, 70)
  .lt(166, 99)
  .lt(166, 199)
  .closePath()
  .fill()
  .stroke()
  .beginPath()
  .fillStyle('blue')
  .circle(50, 50, 40)
  .fill()
  .stroke();

Chaining methods

The following methods can be called exactly like the ones of CanvasRenderingContext2D except that they return the Leinwand object.

  • arc
  • arcTo
  • beginPath
  • bezierCurveTo
  • clearRect
  • clip
  • closePath
  • createLinearGradient
  • createRadialGradient
  • drawImage
  • ellipse
  • fill
  • fillRect
  • fillText
  • lineTo
  • moveTo
  • quadraticCurveTo
  • rect
  • resetClip
  • restore
  • rotate
  • save
  • scale
  • setLineDash
  • setTransform
  • stroke
  • strokeRect
  • strokeText
  • transform
  • translate

Aliases

mt is an alias for moveTo. lt is an alias for lineTo.

Setters/Getters

If you want to set the fillStyle of your context you'd usually have to do something like this ctx.fillStyle = 'red'. To allow chaining your api calls in leinwand you do l.fillStyle('red'). This does the same as l.setFillStyle('red'). To read a property you can either use l.getFillStyle() or l.fillStyle(). This works with all of these properites:

  • fillStyle
  • font
  • globalAlpha
  • globalCompositeOperation
  • lineCap
  • lineDashOffset
  • lineJoin
  • lineWidth
  • miterLimit
  • strokeStyle
  • textAlign
  • textBaseline

Additionally this also works with width and height. So you can do l.setHeight(400) (or l.height(400)) to change the height of the canvas element to 400.

Passthrough methods

There are methods on CanvasRenderingContext2D that do return something. So we can't chain on these methods. They behave exactly as if they were called on the context.

  • getImageData
  • getLineDash
  • measureText
  • putImageData

Additional methods

l.clear()

Clears the canvas. If you have applied any transforms to the context this may not do what you want (try clearWithTransforms).

l.circle(x, y, r)

Draws a path in form of a circle at x/y with a radius of r.

l.strokeCircle(x, y, r)

Strokes a circle at x/y with a radius of r.

l.fillCircle(x, y, r)

Fills a circle at x/y with a radius of r.

l.rotateContextAt(x, y, r)

Rotates the context at x/y by r radians.

l.resetCanvas()

Resets the canvas.

l.resetTransforms()

Resets all the transforms.

l.clearWithTransforms()

Clears the canvas event if there have been transforms applied. The tansforms are preserved.

l.rectCenteredAt(x, y, w, h)

Draws a path of a rectangle centered at x/y with a widht of w and a hight of h.

l.fillRectCenteredAt(x, y, w, h)

Fills a rectangle centered at x/y with a widht of w and a hight of h.

l.strokeRectCenteredAt(x, y, w, h)

Strokes a rectangle centered at x/y with a widht of w and a hight of h.

l.fillTextCenteredAt(text, x, y)

Fills the the text text centered at x/y.

l.strokeTextCenteredAt(text, x, y)

Strokes the the text text centered at x/y.

l.drawImageCenteredAt(...)

Like drawImage on CanvasRenderingContext2D this method has 3 different signatures.

 l.drawCenteredAtImage(image, dx, dy);
 l.drawCenterdAtImage(image, dx, dy, dWidth, dHeight);
 l.drawCenteredAtImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

This does what drawImage does except that the image is centered at dy/dy. For more infos on the parameters hava a look at the mdn aritcle on drawImage.

l.getContext()

Get the CanvasRenderingContext2D. Just in cave leinwand does not provide some functionality.

l.getCanvas()

Get the underlying HTMLCanvasElement.

Release History

  • 2018-02-16   v0.6.0   implement getters and add more utility functions
  • 2018-02-09   v0.5.0   add more utility functions
  • 2016-07-12   v0.4.0   add drawImage method
  • 2015-12-05   v0.3.0   add more utility functions
  • 2015-09-24   v0.2.0   small bug fixes
  • 2014-11-02   v0.1.0   Initial version

leinwand's People

Contributors

dependabot[bot] avatar tillarnold avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

leinwand's Issues

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.