brush.ts 995 B

1234567891011121314151617181920212223242526272829
  1. type Coordinates = {
  2. x: number | null;
  3. y: number | null;
  4. };
  5. type CreatePolylineType = (
  6. (penCoordinates: Coordinates, color: string, strokeWidth: number) => SVGPolylineElement
  7. );
  8. type CompletePathType = (
  9. (pathElement: SVGPolylineElement | null, penCoordinates: Coordinates) => void
  10. );
  11. export const createPolyline: CreatePolylineType = (penCoordinates, color, strokeWidth) => {
  12. const path = document.createElementNS('http://www.w3.org/2000/svg', 'polyline');
  13. path.setAttribute('fill', 'none');
  14. path.setAttribute('stroke', color);
  15. path.setAttribute('stroke-width', strokeWidth.toString());
  16. path.setAttribute('points', `${penCoordinates.x},${penCoordinates.y}`);
  17. return path;
  18. };
  19. export const completePath: CompletePathType = (pathElement, penCoordinates) => {
  20. if (pathElement && penCoordinates.x) {
  21. let points = pathElement.getAttribute('points') || '';
  22. points += ` ${penCoordinates.x},${penCoordinates.y}`;
  23. pathElement.setAttribute('points', points);
  24. }
  25. };