-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ClipPath, SymbolElement and TwoPointPattern
- Loading branch information
Showing
12 changed files
with
383 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Box2 } from '@daign/math'; | ||
import { GraphicNode } from '@daign/2d-pipeline'; | ||
|
||
import { StyledGraphicNode } from '../styledGraphicNode'; | ||
|
||
/** | ||
* Class for a clip path element. | ||
*/ | ||
export class ClipPath extends StyledGraphicNode { | ||
/** | ||
* Constructor. | ||
*/ | ||
public constructor() { | ||
super(); | ||
|
||
this.baseClass = 'clip-path'; | ||
} | ||
|
||
/** | ||
* Get the bounding box of all child elements of the clip path. | ||
* @returns The bounding box. | ||
*/ | ||
public getBox(): Box2 { | ||
const box = new Box2(); | ||
this.children.forEach( ( child: GraphicNode ): void => { | ||
if ( child instanceof StyledGraphicNode ) { | ||
box.expandByBox( child.getBoxTransformed() ); | ||
} | ||
} ); | ||
return box; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Box2 } from '@daign/math'; | ||
import { GraphicNode } from '@daign/2d-pipeline'; | ||
|
||
import { StyledGraphicNode } from '../styledGraphicNode'; | ||
|
||
/** | ||
* Class for a symbol element. | ||
*/ | ||
export class SymbolElement extends StyledGraphicNode { | ||
/** | ||
* Constructor. | ||
*/ | ||
public constructor() { | ||
super(); | ||
|
||
this.baseClass = 'symbol'; | ||
} | ||
|
||
/** | ||
* Get the bounding box of all child elements of the symbol. | ||
* @returns The bounding box. | ||
*/ | ||
public getBox(): Box2 { | ||
const box = new Box2(); | ||
this.children.forEach( ( child: GraphicNode ): void => { | ||
if ( child instanceof StyledGraphicNode ) { | ||
box.expandByBox( child.getBoxTransformed() ); | ||
} | ||
} ); | ||
return box; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Box2, Matrix3, Vector2 } from '@daign/math'; | ||
|
||
import { StyledGraphicNode } from '../styledGraphicNode'; | ||
|
||
/** | ||
* Class for a pattern element, whose size is defined by two points. | ||
*/ | ||
export class TwoPointPattern extends StyledGraphicNode { | ||
public get start(): Vector2 { | ||
return this.points.getByName( 'start' ); | ||
} | ||
|
||
public set start( position: Vector2 ) { | ||
this.points.getByName( 'start' ).copy( position ); | ||
} | ||
|
||
public get end(): Vector2 { | ||
return this.points.getByName( 'end' ); | ||
} | ||
|
||
public set end( position: Vector2 ) { | ||
this.points.getByName( 'end' ).copy( position ); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public constructor() { | ||
super(); | ||
|
||
this.baseClass = 'pattern'; | ||
this.points.initializeElements( 2 ); | ||
this.points.assignName( 'start', 0 ); | ||
this.points.assignName( 'end', 1 ); | ||
} | ||
|
||
/** | ||
* Calculate the start after a given transformation. | ||
* @param transformation - The transformation to apply. | ||
* @returns The transformed start position. | ||
*/ | ||
public getStartTransformed( transformation: Matrix3 ): Vector2 { | ||
const area = new Box2(); | ||
const startPoint = this.start.clone().transform( transformation ); | ||
area.expandByPoint( startPoint ); | ||
const endPoint = this.end.clone().transform( transformation ); | ||
area.expandByPoint( endPoint ); | ||
return area.min; | ||
} | ||
|
||
/** | ||
* Calculate the end after a given transformation. | ||
* @param transformation - The transformation to apply. | ||
* @returns The transformed end position. | ||
*/ | ||
public getEndTransformed( transformation: Matrix3 ): Vector2 { | ||
const area = new Box2(); | ||
const startPoint = this.start.clone().transform( transformation ); | ||
area.expandByPoint( startPoint ); | ||
const endPoint = this.end.clone().transform( transformation ); | ||
area.expandByPoint( endPoint ); | ||
return area.max; | ||
} | ||
|
||
/** | ||
* Calculate the size after a given transformation. | ||
* @param transformation - The transformation to apply. | ||
* @returns The transformed size. | ||
*/ | ||
public getSizeTransformed( transformation: Matrix3 ): Vector2 { | ||
const area = new Box2(); | ||
const startPoint = this.start.clone().transform( transformation ); | ||
area.expandByPoint( startPoint ); | ||
const endPoint = this.end.clone().transform( transformation ); | ||
area.expandByPoint( endPoint ); | ||
return area.size; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { Vector2 } from '@daign/math'; | ||
import { GraphicNode } from '@daign/2d-pipeline'; | ||
|
||
import { ClipPath, Line } from '../../lib'; | ||
|
||
describe( 'ClipPath', (): void => { | ||
describe( 'constructor', (): void => { | ||
it( 'should set the base class clip-path', (): void => { | ||
// Act | ||
const clipPath = new ClipPath(); | ||
|
||
// Assert | ||
expect( ( clipPath as any ).classNames.getByName( 'baseClass' ).value ) | ||
.to.equal( 'clip-path' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getBox', (): void => { | ||
it( 'should get the bounding box of the clip path', (): void => { | ||
// Arrange | ||
const clipPath = new ClipPath(); | ||
|
||
const line1 = new Line(); | ||
line1.start = new Vector2( 7, 2 ); | ||
line1.end = new Vector2( 3, 8 ); | ||
clipPath.appendChild( line1 ); | ||
|
||
const line2 = new Line(); | ||
line2.start = new Vector2( 5, 6 ); | ||
line2.end = new Vector2( 1, 0 ); | ||
clipPath.appendChild( line2 ); | ||
|
||
// Act | ||
const box = clipPath.getBox(); | ||
|
||
// Assert | ||
expect( box.min.equals( new Vector2( 1, 0 ) ) ).to.be.true; | ||
expect( box.max.equals( new Vector2( 7, 8 ) ) ).to.be.true; | ||
} ); | ||
|
||
it( 'should ignore children that are not StyledGraphicNodes', (): void => { | ||
// Arrange | ||
const clipPath = new ClipPath(); | ||
|
||
const graphicNode = new GraphicNode(); | ||
clipPath.appendChild( graphicNode ); | ||
|
||
// Act | ||
const box = clipPath.getBox(); | ||
|
||
// Assert | ||
expect( box.isEmpty ).to.be.true; | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { Vector2 } from '@daign/math'; | ||
import { GraphicNode } from '@daign/2d-pipeline'; | ||
|
||
import { SymbolElement, Line } from '../../lib'; | ||
|
||
describe( 'SymbolElement', (): void => { | ||
describe( 'constructor', (): void => { | ||
it( 'should set the base class symbol', (): void => { | ||
// Act | ||
const symbolElement = new SymbolElement(); | ||
|
||
// Assert | ||
expect( ( symbolElement as any ).classNames.getByName( 'baseClass' ).value ) | ||
.to.equal( 'symbol' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getBox', (): void => { | ||
it( 'should get the bounding box of the symbol element', (): void => { | ||
// Arrange | ||
const symbolElement = new SymbolElement(); | ||
|
||
const line1 = new Line(); | ||
line1.start = new Vector2( 7, 2 ); | ||
line1.end = new Vector2( 3, 8 ); | ||
symbolElement.appendChild( line1 ); | ||
|
||
const line2 = new Line(); | ||
line2.start = new Vector2( 5, 6 ); | ||
line2.end = new Vector2( 1, 0 ); | ||
symbolElement.appendChild( line2 ); | ||
|
||
// Act | ||
const box = symbolElement.getBox(); | ||
|
||
// Assert | ||
expect( box.min.equals( new Vector2( 1, 0 ) ) ).to.be.true; | ||
expect( box.max.equals( new Vector2( 7, 8 ) ) ).to.be.true; | ||
} ); | ||
|
||
it( 'should ignore children that are not StyledGraphicNodes', (): void => { | ||
// Arrange | ||
const symbolElement = new SymbolElement(); | ||
|
||
const graphicNode = new GraphicNode(); | ||
symbolElement.appendChild( graphicNode ); | ||
|
||
// Act | ||
const box = symbolElement.getBox(); | ||
|
||
// Assert | ||
expect( box.isEmpty ).to.be.true; | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.