npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

leinwand

v0.6.0

Published

Method chaining for the canvas 2d api

Downloads

12

Readme

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 infor 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