Added types for svg-intersections (#39899)

* Generated svg-intersections files using npx

* Added base typings for shape method with conditional second argument type

* Added missing types in SvgProperties

* Added basic Shape interface

* Added types for intersect function

* Refactored intersection to interface and added typings to Matrix2D

* Linted index.d.ts

* Added line test

* Added tests for rect, circle, ellipse & polygon

* Added test for path

* Rectangle rx&ry should be optional

* Added intersections test

* Replaced unnecessary typeof with generic type
This commit is contained in:
William Bergeron-Drouin 2019-11-01 18:01:47 -04:00 committed by Jesse Trinity
parent 13e6949a02
commit eb680b04ff
4 changed files with 120 additions and 0 deletions

85
types/svg-intersections/index.d.ts vendored Normal file
View File

@ -0,0 +1,85 @@
// Type definitions for svg-intersections 0.4
// Project: https://github.com/effektif/svg-intersections#readme
// Definitions by: xWiiLLz <https://github.com/xWiiLLz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
export type SvgElements = 'line' | 'rect' | 'circle' | 'ellipse' | 'polygon' | 'polyline' | 'path';
// Svg element properties
export interface LineProps {
x1: number;
y1: number;
x2: number;
y2: number;
}
export interface RectProps {
rx?: number;
ry?: number;
x: number;
y: number;
width: number;
height: number;
}
export interface CircleProps {
cx: number;
cy: number;
r: number;
}
export interface EllipseProps {
cx: number;
cy: number;
rx: number;
ry: number;
}
export interface PolygonProps {
points: string;
}
export type PolylineProps = PolygonProps;
export interface PathProps {
d: string;
}
export type SvgProperties<T extends SvgElements> =
T extends 'line' ? LineProps :
T extends 'rect' ? RectProps :
T extends 'circle' ? CircleProps :
T extends 'ellipse' ? EllipseProps :
T extends 'polygon' ? PolygonProps :
T extends 'polyline' ? PolylineProps :
T extends 'path' ? PathProps :
never;
export interface Shape {
type: string;
meta: object;
params: object;
}
export interface Matrix2D {
a: number;
b: number;
c: number;
d: number;
e: number;
f: number;
}
export interface Point2D {
x: number;
y: number;
}
export interface Intersection {
status: string;
points: Point2D[];
}
export function shape<T extends SvgElements>(svgElementName: T, svgProps: SvgProperties<T>): Shape;
export function intersect(shape1: Shape, shape2: Shape, m1?: Matrix2D, m2?: Matrix2D): Intersection;

View File

@ -0,0 +1,11 @@
import { intersect, shape } from 'svg-intersections';
const line = shape('line', {x1: 0, y1: 0, x2: 2, y2: 3});
const rect = shape('rect', {x: 0, y: 0, width: 200, height: 100});
const rectWithOptional = shape('rect', {x: 0, y: 0, width: 200, height: 100, rx: 4, ry: 4});
const circle = shape('circle', {cx: 0, cy: 0, r: 100});
const ellipse = shape('ellipse', {rx: 100, ry: 150, cx: 0, cy: 0});
const polygon = shape('polygon', {points: '-5,0 0,10 5,0 0,-10'});
const path = shape('path', {d: 'M 10 10 h 80 v 80 h -80 Z'});
const intersections = intersect(line, rect);

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"svg-intersections-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }