brush.ts 1.1 KB

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