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

@ng-web-apis/canvas

v3.0.6

Published

A library for declarative use of Canvas API with Angular

Downloads

458

Readme

ng-web-apis logo Canvas API for Angular

npm version npm bundle size codecov

This is a library for declarative use of Canvas API with Angular.

Install

If you do not have @ng-web-apis/common:

npm i @ng-web-apis/common

Now install the package:

npm i @ng-web-apis/canvas

Usage

Add CanvasModule to your module declaration and use waCanvas2d directive on a canvas element to declare 2D context scope. Then use other directives to draw inside canvas:

<canvas waCanvas2d>
  <canvas-path fillStyle="red">
    <canvas-rect
      [x]="0"
      [y]="0"
      [width]="100"
      [height]="50"
    ></canvas-rect>
  </canvas-path>
</canvas>

Context directive supports the following attributes (see contextAttributes for 2D context):

  • opaqueboolean attribute to set alpha to false
  • desynchronizedboolean attribute to set desynchronized to true

Directives

There are 3 types of directives you can use:

  1. Method directives
  2. Properties directives
  3. Path directives

Method

These are basic directives to draw things on canvas.

Properties

These directives set properties of CanvasRenderingContext2D. They must be applied to a method directive and they change context property before calling the method. They also restore default value after drawing is performed so it will not interfere with the rest of picture.

Path

You can use following directives to draw path on Canvas. They must be children of canvas-path directive:

Example

Combining properties, method and path directives can be examined on the following case. Consider drawing two rectangles with native commands:

function drawTwoRectangles(context) {
  context.beginPath();
  context.fillStyle = 'red';
  context.rect(0, 0, 100, 50);
  context.fill();

  context.beginPath();
  context.fillStyle = 'green';
  context.globalCompositeOperation = 'screen';
  context.rect(25, 25, 100, 50);
  context.fill();

  context.fillStyle = '#000';
  context.globalCompositeOperation = 'source-over';
}

This is equivalent to the following HTML

<canvas waCanvas2d>
  <canvas-path fillStyle="red">
    <canvas-rect
      [x]="0"
      [y]="0"
      [width]="100"
      [height]="50"
    ></canvas-rect>
  </canvas-path>
  <canvas-path
    fillStyle="green"
    globalCompositeOperation="screen"
  >
    <canvas-rect
      [x]="25"
      [y]="25"
      [width]="100"
      [height]="50"
    ></canvas-rect>
  </canvas-path>
</canvas>

And both will give you this result:

canvas.png

Pipes

You can use Pipes to create some of the classes, required for particular Canvas operations:

Notes

  • Performance-wise it would of course be slower than performing imperative commands and optimizing them manually. But unless you are making a video game with heavy render cycle this shouldn't be noticeable.
  • Unlike raw canvas, default stroke color is transparent to align behavior with SVG.

See also

Other Web APIs for Angular by @ng-web-apis