Dot组件

用 svg需要遍历生成,性能不太好,
不如直接用 canvas生成一个透明度的背景图,然后平铺
image.png

  1. import React from 'react';
  2. function GridDot(props) {
  3. const {
  4. width = 10,
  5. height = 10,
  6. space = 10,
  7. radius = 1.5,
  8. fill = '#000',
  9. } = props;
  10. const viewWidth = width * radius * 2 + (width - 1) * (space - radius * 2);
  11. const viewHeight = height * radius * 2 + (height - 1) * (space - radius * 2);
  12. let dots = [];
  13. for (let indexWidth = 0; indexWidth < width; indexWidth++) {
  14. for (let indexHeight = 0; indexHeight < height; indexHeight++) {
  15. dots.push(
  16. <circle
  17. key={`${indexWidth}-${indexHeight}`}
  18. cx={radius + indexWidth * space}
  19. cy={radius + indexHeight * space}
  20. r={radius}
  21. />
  22. );
  23. }
  24. }
  25. return (
  26. <svg
  27. width={viewWidth}
  28. height={viewHeight}
  29. viewBox={`0 0 ${viewWidth} ${viewHeight}`}
  30. version="1.1"
  31. >
  32. <g fill={fill}>{dots}</g>
  33. </svg>
  34. );
  35. }
  36. export default GridDot;