import { FloatArray } from './types' ; import { convertToPathData , IPathSource } from './getPath' ; import { round } from './utilities/math' ; import { DRAW_CURVE_CUBIC_BEZIER , MOVE_CURSOR , SPACE } from './constants' ; export class Path { private data : FloatArray [ ] ; private stringData : string | undefined ; constructor ( pathSelectorOrElement ) { const { data , stringData } = convertToPathData ( pathSelectorOrElement ) ; this . data = data ; this . stringData = stringData ; } public getData ( ) { return this . data ; } public getStringData ( ) { return this . stringData || ( this . stringData = this . render ( ) ) ; } public render ( formatter = round ) { const pathData = this . data ; let result = [ ] ; for ( let i = 0 ; i < pathData . length ; i ++ ) { const n = pathData [ i ] ; result . push ( MOVE_CURSOR , formatter ( n [ 0 ] ) , formatter ( n [ 1 ] ) , DRAW_CURVE_CUBIC_BEZIER ) ; let lastResult ; for ( let f = 2 ; f < n . length ; f += 6 ) { const p0 = formatter ( n [ f ] ) ; const p1 = formatter ( n [ f + 1 ] ) ; const p2 = formatter ( n [ f + 2 ] ) ; const p3 = formatter ( n [ f + 3 ] ) ; const dx = formatter ( n [ f + 4 ] ) ; const dy = formatter ( n [ f + 5 ] ) ; const isPoint = p0 == dx && p2 == dx && p1 == dy && p3 == dy ; if ( ! isPoint || lastResult != ( lastResult = ( '' + p0 + p1 + p2 + p3 + dx + dy ) ) ) { result . push ( p0 , p1 , p2 , p3 , dx , dy ) ; } } } return result . join ( SPACE ) ; } }