0.0.11 • Published 3 years ago

ngx-canvas v0.0.11

Weekly downloads
30
License
MIT
Repository
github
Last release
3 years ago

NgxCanvas

Table of contents

Quick example

<ngx-canvas
  width="375"
  height="667"
  [options]="options"
  (drawComplete)="drawComplete($event)"
></ngx-canvas>

Installation

To install ngx-canvas run the following command:

npm i ngx-canvas --save

Usage

First, import the NgxCanvasModule into the AppModule:

import { NgModule } from '@angular/core';
import { NgxCanvasModule } from 'ngx-canvas';

@NgModule({
  imports: [
    NgxCanvasModule.forRoot()
  ]
})
export class AppModule {}

Now you can simply use the ngx-canvas component and provide your custom options via the options binding:

import { Component, OnInit } from '@angular/core';
import { DrawProps, ImageProps, TextProps, PropagateProps, RectProps, LineProps, StepsProps, Types } from 'ngx-canvas';

@Component({
  selector: 'app-root',
  template: `
    <ngx-canvas [options]="options" (drawComplete)="drawComplete($event)"></ngx-canvas>
  `,
})
export class AppComponent implements OnInit {
  options: DrawProps;

  ngOnInit(): void {
    // draw image
    const imgs: ImageProps[] = [
      {
        type: Types.image,
        url: 'assets/images/bg.png',
        top: 0,
        left: 0,
        width: 375,
        height: 667
      }
    ];
    // draw text
    const txts: TextProps[] = [
      {
        type: Types.text,
        content: 'Have good time.',
        top: 100,
        left: 100,
        fontSize: 18,
        color: '#FFFFFF',
        lineHeight: 30,
        width: 200,
        lineNum: 1,
        fontFamily: 'Microsoft YaHei'
      }
    ];
    // draw rect
    const rects: RectProps[] = [
      {
        type: Types.rect,
        width: 375,
        height: 667,
        x: 0,
        y: 280,
        backgroundColor: '#FFFFFF',
        borderRadius: 30
      }
    ];
    // draw line
    const lines: LineProps[] = [
      {
        type: Types.line,
        color: '#999',
        startX: 10,
        startY: 200,
        endX: 350,
        endY: 200,
        width: 2,
        lineCap: 'round'
      }
    ];
    // draw steps
    const steps: StepsProps[] = [{
      type: Types.steps,
      circleX: 48,
      circleY: 530,
      lineHeight: 80,
      lineCount: 3,
      lineColor: '#FFF',
      circleColor: '#FFF',
      direction: 'ltr',
      circleStyle: 'solid',
      lineStyle: 'solid'
    }];
    // draw options
    this.options = {
      debug: true, // Used to debug the presentation canvas
      width: 375,
      height: 667,
      backgroundColor: '#47b785',
      views: [
        ...imgs,
        ...txts,
        ...rects,
        ...lines,
        ...steps
      ],
      extra: 'Extend the params and return after drawing.'
    };
  }

  drawComplete(propagate: PropagateProps): void {
    const { dataUrl, canvas, ctx, extra } = propagate;
    // downloading canvas element to an image
    const a = document.createElement('a');
    document.body.appendChild(a);
    a.setAttribute('style', 'display: none');
    a.href = dataUrl;
    a.download = 'filename';
    a.click();
    a.remove();
  }
}

Notice that you will need to import the NgxCanvasModule into other modules as it exports ngx-canvas component. But forRoot has to be called only once!

API

Bindings

@Input()TypeRequiredDescription
optionsDrawPropsrequiredOptions of the canvas.
widthnumberoptionalWidth of the canvas display.
heightnumberoptionalHeight of the canvas display.

Events

@Output()TypeRequiredDescription
drawComplete(propagate: PropagateProps) => unknownoptionalGets an data that is notified when the canvas is finished draw.

Types

DrawPropsTypeRequiredDescriptionDefault
widthnumberrequiredGenerate image width.null
heightnumberrequiredGenerated image height.null
backgroundColorstringoptionalCanvas background color.'#FFFFFF'
debugbooleanoptionalWhether to enable debugging mode.false
viewsArray<T>requiredCanvas drawing core data.null
extraanyoptionalSome extra data.null

ImagePropsTypeRequiredDescriptionDefault
typestring | TypesrequiredType of drawing.Fixed value 'image'
urlstringrequiredPicture path or remote address.null
topnumberrequiredY-axis coordinate in the destination canvas.0
leftnumberrequiredX-axis coordinate in the destination canvas.0
widthnumberrequiredWidth to draw the image in the destination canvas.0
heightnumberrequiredHeight to draw the image in the destination canvas.0
borderRadiusnumberoptionalImage borderRadius.0
borderWidthnumberoptionalImage borderWidth.0
borderColorstringoptionalImage borderColor.'rgba(255,255,255,0)'

TextPropsTypeRequiredDescriptionDefault
typestring | TypesrequiredType of drawing.Fixed value 'text'
topnumberrequiredY-axis coordinate in the destination canvas.0
leftnumberrequiredX-axis coordinate in the destination canvas.0
contentstringrequiredText string to render into the context.null
fontSizenumberoptionalFont size.16
colorstringoptionalFont color.'#000'
baseLinestringoptionalCurrent text baseline used when drawing text.'top'
textAlignstringoptionalCurrent text alignment used when drawing text.'left'
opacitynumberoptionalFont transparency.1
widthnumberoptionalMaximum text width.null
lineNumnumberoptionalMaximum Text lines.1
lineHeightnumberoptionalFont line height.0
fontWeightnumberoptionalFont weight.'normal'
fontStylestringoptionalFont style.'normal'
fontFamilystringoptionalFont family.'Microsoft YaHei'

LinePropsTypeRequiredDescriptionDefault
typestring | TypesrequiredType of drawing.Fixed value 'line'
startXnumberrequiredX-axis coordinate of the line's start point.null
startYnumberrequiredY-axis coordinate of the line's start point.null
endXnumberrequiredX-axis coordinate of the line's end point.null
endYnumberrequiredY-axis coordinate of the line's end point.null
colorstringoptionalColor of the line.'#000'
widthnumberoptionalWidth of the line.1
lineCapstringoptionalShape used to draw the end points of lines.'butt'

RectPropsTypeRequiredDescriptionDefault
typestring | TypesrequiredType of drawing.Fixed value 'rect'
widthnumberrequiredRectangle's width.0
heightnumberrequiredRectangle's height.null
xnumberrequiredX-axis coordinate of the rectangle's start point.null
ynumberrequiredY-axis coordinate of the rectangle's start point.null
textTextPropsoptionalText string to render into the context.null
paddingLeftnumberoptionalPadding area to the left of rectangle's.0
paddingRightnumberoptionalPadding area to the right of rectangle's.0
borderWidthnumberoptionalWidth of rectangle's border.null
backgroundColorstringoptionalBackgroundColor of rectangle'snull
borderColorstringoptionalShape used to draw the end points of lines.null
borderRadiusnumberoptionalRadius of rectangle's border.0
opacitynumberoptionalRect transparency.1

StepsPropsTypeRequiredDescriptionDefault
typestring | TypesrequiredType of drawing.Fixed value 'steps'
leftnumberrequiredX-axis coordinate of the first circle start point.0
topnumberrequiredY-axis coordinate of the first circle start point.0
rnumberoptionalThe arc's radius. Must be positive.5
lineWidthnumberoptionalSets the thickness of lines.1
startAnglenumberoptionalThe angle at which the arc starts in radians.0
endAnglenumberoptionalThe angle at which the arc ends in radians.Math.PI * 2
strokeColorstringoptionalSpecifies the color to use for the strokes.'#CCCCCC'
modestringoptionalBrush color fill mode.'fill'
unfinishedColorstringoptionalThe color of the unfinished stroke.'#FF8478'
processColorstringoptionalThe color of the stroke in progress.'#3DA8F5'
finishedColorstringoptionalFinished stroke color.'#9ED979'
cableColorstringoptionalThe stroke color of the connecting line.'#EBEBEB'
listsStepsListProps[]optionalBasic data element of steps.'[]'
directionstringoptionalSpecifies the direction of steps.'column'

StepsListPropsTypeRequiredDescriptionDefault
statusnumberoptionalThe current state value of the step bar.0
spacingnumberoptionalThe spacing between circles.50

ProgressPropsTypeRequiredDescriptionDefault
typestring | TypesrequiredType of drawing.Fixed value 'progress'
startXnumberrequiredX-axis coordinate of the progress's start point.0
startYnumberrequiredY-axis coordinate of the progress's start point.0
endXnumberrequiredX-axis coordinate of the progress's end point.0
endYnumberrequiredY-axis coordinate of the progress's end point.0
percentnumberrequiredCurrent progress value.0
lineWidthnumberoptionalWidth of the stroke.12
linecapnumberoptionalShape used to draw the end points of lines.'round'
fromColornumberoptionalBackground color.'#ccc'
toColornumberoptionalActivated color.'#46b684'
fontLineWidthnumberoptionalWidth of the stroke.1
fontColornumberoptionalFont color.'#4a4a4a'
fontSizenumberoptionalFont size.16
fontWeightnumberoptionalFont weight.'normal'
fontStylenumberoptionalFont style.'normal'
fontFamilynumberoptionalFont family.'Microsoft YaHei'
infonumberoptionalValue display the end of the progress.''
infoMarginLeftnumberoptionalLeft margin.10

PropagatePropsTypeDescription
canvasHTMLCanvasElementCanvas Instance Object.
ctxCanvasRenderingContext2DCanvasRenderingContext2D interface Object.
dataUrlstringData URI containing a representation of the image.
extraanyAdditional parameters are returned after drawing.

Demo

You can clone this repo to your working copy and then launch the demo page in your local machine:

npm install
npm run build:prod
npm run start

The demo page server is listening on: http://localhost:4200

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago