brush.ts 1.1 KB

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