Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An Angular2/Angular4 library for HTML Canvas.

NotificationsYou must be signed in to change notification settings

patrimart/ngvas

Repository files navigation

An Angular2 / Angular4 Module for HTML Canvas

Thengvas library allows you to control the<canvas> element from within Angular 2. Or is it 4 now?

Look in the/demo directory for a working example.

Experiment withngvas in aPlunker.


Example Angular Module:

import{NgvasModule,tweens,hitAreas}from"ngvas";@NgModule({imports:[BrowserModule,NgvasModule],declarations:[AppComponent],bootstrap:[AppComponent]})classAppModule{}

Example HTML with Ngvas components:

<ngvas[width]="500"[height]="500"(ready)="onNgvasInit()"><templatengForlet-rect[ngForOf]="rects"><ngvas-rectangle[fill]="rect.color"[translate]="rect.xy"[width]="rect.w"[height]="rect.h"origin="center"></ngvas-rectangle></template><ngvas-circle[fill]="'#0000ff'"[x]="100"[y]="100"[radius]="50"origin="center"(click)="onClickHandler($event)"[hitArea]="PixelHitArea"></ngvas-circle></ngvas>

Components

<ngvas>: NgvasComponent Input Bindings

@Input("name")Data TypeDescription
activebooleanSets if animation in the canvas is active or not.
widthnumberSets the width of the<canvas> element.
heightnumberSets the height of the<canvas> element.

<ngvas>: NgvasComponent Event Bindings

@Output("name")Data TypeDescription
readyNgvasComponentFires once when the component is initialized.

<ngvas-*>: NgvasBaseComponent Input Bindings

These input bindings are available on all<ngvas-*> components.

@Input("name")Data TypeDescription
namestringThe name of the component. Not used at this point.
activebooleanSets if the animation is active on the component.
visiblebooleanSets if the component is visible on the canvas.
xnumberSets the x coordinate of the shape.
ynumberSets the y coordinate of the shape.
origin`[number, number]"center"`
widthnumberSets the width of the shape. Not applicable for many shapes.
heightnumberSets the height of the shape. Not applicable for many shapes.
rotationnumberSets the rotation of the shape in degrees.
scaleXnumberSets the x scale of the shape.
scaleYnumberSets the y scale of the shape in degrees.
skewXnumberSets the x skew of the shape in degrees.
skewYnumberSets the y skew of the shape.
scaleTweenInput<S, [number, number]>Scale multiplier.
sizeTweenInput<S, [number, number]>Sets the width and height of the shape.
skewTweenInput<S, [number, number]>Sets the X and Y skew of the shape.
rotateTweenInput<S, number>Rotates the shape by the specified degrees.
translateTweenInput<S, [number, number]>Moves the shape the specified X and Y coordinates.
animate`((shape: S) => boolean)undefined`
constrain`ConstraintFunction[]undefined`
hitAreatypeof PixelHitAreaA function to calculate if the current mouse coordinates are within a shape.
opacitynumberSets the overall opacity of the shape. It's proabably easier to use a fill or strokeColorStyle with opacity.
compose{ alpha?: number, overlay?: ComposeOverlay }Sets how the shape is overlayed on the canvas.CanvasRenderingContext2D.globalCompositeOperation
fillTweenInput<S, ColorStyle>Sets the fill color, gradient or pattern of the shape.CanvasRenderingContext2D.fillStyle
strokeTweenInput<S, { width:number, style:ColorStyle, join?:LineJoin, cap?:LineCap, dashOffset?:number, miterLimit?:number }>Sets the stroke settings of the shape.
shadowTweenInput<S, { blur:number, color:string, offsetX:number, offsetY:number }>Sets the shadow settings of the shape.

<ngvas-*>: NgvasBaseComponent Event Bindings

These event bindings are available on all<ngvas-*> components.Note: a hit area is required for mouse events to work.

@Output("name")Data TypeDescription
clickMouseEventEmits a MouseEvent if the shape is clicked.
dblclickMouseEventEmits a MouseEvent if the shape is double-clicked.
wheelWheelEventEmits a WheelEvent if the scroll wheel is used over the shape.
mouseenterMouseEventEmits a MouseEvent if the mouse pointer enters the shape.
mouseleaveMouseEventEmits a MouseEvent if the mouse pointer leaves the shape.
shapeBaseShape & BaseStyleOn init, emits the underlying class of the shape.

<ngvas-arc>: NgvasArcComponent Input Bindings

Draws a portion of a circle.connectToCenter to make a pie-chart shape.

@Input("name")Data TypeDescription
angleTweenInput<ArcShape, number>The ending angle of the arc in degrees.
radiusTweenInput<ArcShape, number>The radius of the arc.
connectToCenterbooleanIftrue, connects the arc to the center point like a pie slice.

Example:

<ngvas-arcfill="#ff0000"[x]="250"[y]="250"[radius]="50"[angle]="270"origin="center"[connectToCenter]="true"></ngvas-arc>

<ngvas-bezier>: NgvasBezierCurveComponent Input Bindings

Draws one or more connected bezier curves.

@Input("name")Data TypeDescription
curvesBezierCurve[]An array of connected bezier curves.
<ngvas-bezier[stroke]="{ width: 4 }"[x]="50"[y]="50"[curves]="[[ [100, 100], [150, 450], [400, 300], [400, 400] ]]"></ngvas-bezier>

<ngvas-circle>: NgvasCircleComponent Input Bindings

Draws a circle. Basically, an arc with a 360 degree angle.

@Input("name")Data TypeDescription
radiusTweenInput<CircleShape, number>The radius of the circle.
<ngvas-circlefill="#ff0000"[x]="250"[y]="250"[radius]="50"origin="center"></ngvas-circle>

<ngvas-image>: NgvasImageComponent Input Bindings

Draws an image from the

@Input("name")Data TypeDescription
srcstringThe URL path to the image.
<ngvas-imagefill="rgba(0,0,0,0)"[x]="10"[y]="10"[width]="100"[height]="100"src="../test/bird.jpg"></ngvas-image>

Note: As a temporary fix,<ngvas-image> must setfill to make it visible.


<ngvas-line>: NgvasLineComponent Input Bindings

Draws one or more connected lines.

@Input("name")Data TypeDescription
linesLine[]An array of connected lines.
<ngvas-line[stroke]="{ width: 4 }"[lines]="[ [[100, 100], [200, 200]], [[200, 200], [300, 100]] ]"></ngvas-line>

<ngvas-polygon>: NgvasPolygonComponent Input Bindings

Draws a shape with any combination of lines, bezier curves and quadratic curves.

@Input("name")Data TypeDescription
sides`Array<LineBezierCurve
<ngvas-polygonfill="#ff0000"[x]="50"[y]="50"[sides]="sidesArray"></ngvas-polygon>

<ngvas-quadratic>: NgvasQuadraticCurveComponent Input Bindings

Draws one or more connected quadratic curves.

@Input("name")Data TypeDescription
curvesQuadraticCurve[]An array of connected quadratic curves.
<ngvas-quadratic[stroke]="{ width: 4 }"[x]="50"[y]="50"[curves]="[[ [100, 100], [150, 450], [400, 400] ]]"></ngvas-quadratic>

<ngvas-text>: NgvasTextComponent Input Bindings

Draws text.

@Input("name")Data TypeDescription
textstringThe text to display.
textStyle{ font?: string, align?: TextAlign, baseline?: TextBaseline }The style of the displayed text.
<ngvas-textfill="#0000ff"[textStyle]="{ font: '48px Arial' }"[x]="50"[y]="250"text="This is text."></ngvas-text>

Tweening Inputs and Functions

You can set almost any value of a component to tween from its current value to a target value over a specifiedperiod of time with specific easing.

import{tweens}from"ngvas";typeTweenFunc=(time:number,startValue:number,changeValue:number,duration:number)=>number;typeTweenInput<S,T>=T|[T,number,TweenFunc|undefined,((s:S)=>void)|undefined];// Set the amount of pixels to move, the tween duration in MS, and the easing function to use.consttranslateTween=[[250,250],1000,tweens.easings.easeInOutSine];

Example HTML:

<ngvas-circlefill="#ff0000"[x]="50"[y]="50"[radius]="50"[translate]="translateTween"origin="center"></ngvas-circle>

Built-in Tweens

  • easeLinear
  • easeInSine, easeOutSine, easeInOutSine
  • easeInQuint, easeOutQuint, easeInOutQuint
  • easeInQuart, easeOutQuart, easeInOutQuart
  • easeInQuad, easeOutQuad, easeInOutQuad
  • easeInExpo, easeOutExpo, easeInOutExpo
  • easeInElastic, easeOutElastic, easeInOutElastic
  • easeInCircular, easeOutCircular, easeInOutCircular
  • easeInBack, easeOutBack, easeInOutBack
  • easeInBounce, easeOutBounce, easeInOutBounce
  • easeInCubic, easeOutCubic, easeInOutCubic

Hit Area Functions

One function to calculate if the mouse coordinates are on a shape is available. It is pixel accurate and non-optimized.Use it judiciously until more effecient hit area functions become available.

import{hitAreas}from"ngvas";importPixelHitArea=hitAreas.PixelHitArea;

Types

Import interfaces:

import*astypesfrom"ngvas";
// Point = [ x, y ]typePoint=[number,number];// Line = [ startPoint, endPoint ]typeLine=[Point,Point];// QuadraticCurve = [ startPoint, controlPoint, endPoint ]typeQuadraticCurve=[Point,Point,Point];// BexierCurve = [ startPoint, controlPoint0, controlPoint1, endPoint ]typeBezierCurve=[Point,Point,Point,Point];// Example: Point | [ Point, durationInMS, TweenFunc]typeTweenInput<SextendsBaseStyle,T>=T|[T,number,TweenFunc|undefined,((s:S)=>void)|undefined]typeComposeOverlay="source-over"|"source-in"|"source-out"|"source-atop"|"destination-over"|"destination-in"|"destination-out"|"destination-atop"|"lighter"|"copy"|"xor"|"multiply"|"screen"|"overlay"|"darken"|"lighten"|"color-dodge"|"color-burn"|"hard-light"|"soft-light"|"difference"|"exclusion"|"hue"|"saturation"|"color"|"luminosity";// ColorStyle: 0xff9966, "#ff9966", "#ff9966ff", "rgb(255,200,150)", "rgba(255,200,150,0.5)typeColorStyle=number|string|CanvasGradient|CanvasPattern;typeLineJoin="miter"|"bevel"|"round";typeLineCap="butt"|"round"|"square";typeTextAlign="left"|"right"|"center"|"start"|"end";typeTextBaseline="top"|"hanging"|"middle"|"alphabetic"|"ideographic"|"bottom";

TODOs for 1.0

  • Improve docs.
  • Unit tests with >90% coverage.
  • ImproveoriginToCenter for curves and polygon.
  • Built-in Constraints.
  • Add mouse events: mousedown, mouseup
  • Add other hit area types: vector rectangle, circle.
  • Optimization.

TODOs for 1.x

  • Add drag and drop events.
  • Add HammerJS support.
  • Grouping components.
  • Optimization.

About

An Angular2/Angular4 library for HTML Canvas.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp