Socket
Socket
Sign inDemoInstall

@ng-web-apis/canvas

Package Overview
Dependencies
6
Maintainers
3
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ng-web-apis/canvas

A library for declarative use of Canvas API with Angular


Version published
Weekly downloads
10
increased by25%
Maintainers
3
Created
Weekly downloads
 

Readme

Source

Canvas API for Angular

Part of Web APIs for Angular

npm version npm bundle size Travis (.com) Coveralls github angular-open-source-starter

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 class="canvas">
    <ng-container
        waCanvasFillRect
        [x]="0"
        [y]="0"
        [width]="100"
        [height]="50"
    ></ng-container>
</canvas>

Directives

There are 3 types of directives you can use:

  1. Context directives
  2. Method directives
  3. Properties directives:

Context

These are applied to an HTMLCanvasElement and are used to defined scope of a particular context:

  • waCanvas2d. Supports following attributes (see contextAttributes for 2D context):
    • opaqueboolean attribute to set alpha to false
    • desynchronizedboolean attribute to set desynchronized to true

Method

These are your typical directives to draw things on canvas. They are declarative counterparts of methods existing on CanvasRenderingContext2D:

Properties

These directives set properties of CanvasRenderingContext2D. They must be added 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.

Example

Combining properties and method directive can be examine on the following example. Consider drawing two rectangles with native commands:

function drawTwoRectangles(context) {
    context.fillStyle = 'red';
    context.fillRect(0, 0, 100, 50);
    context.fillStyle = 'green';
    context.globalCompositeOperation = 'screen';
    context.fillRect(25, 25, 100, 50);
    context.fillStyle = '#000';
    context.globalCompositeOperation = 'source-over';
}

This is equivalent to the following HTML

<canvas waCanvas2d>
    <ng-container
        waCanvasFillRect
        waCanvasFillStyle="red"
        [x]="0"
        [y]="0"
        [width]="100"
        [height]="50"
    ></ng-container>
    <ng-container
        waCanvasFillRect
        waCanvasFillStyle="green"
        waCanvasGlobalCompositeOperation="screen"
        [x]="25"
        [y]="25"
        [width]="100"
        [height]="50"
    ></ng-container>
</canvas>

And it will give you this:

canvas.png

Development status

Canvas API is a vast topic. This library was created because of personal need for a few methods and properties on a 2D context. There is no particular schedule for implementing the rest of the API. If you need something that is missing it is rather easy to add given existing examples of method and property directives so PRs are very welcome. Alternatively, feel free to file an issue and we will get to it when we have time.

See also

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

Open-source

Do you also want to open-source something, but hate the collateral work? Check out this Angular Open-source Library Starter we’ve created for our projects. It got you covered on continuous integration, pre-commit checks, linting, versioning + changelog, code coverage and all that jazz.

Keywords

FAQs

Last updated on 09 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc