Background.jsx 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React from 'react'
  2. const wrapperStyle = {
  3. width: '100%',
  4. height: '100%',
  5. position: 'absolute',
  6. zIndex: 0,
  7. top: 0,
  8. left: 0,
  9. overflow: 'hidden',
  10. }
  11. const colors = [
  12. '#fff1f0',
  13. '#fff2e8',
  14. '#fff7e6',
  15. '#fffbe6',
  16. '#feffe6',
  17. '#fcffe6',
  18. '#f6ffed',
  19. '#e6fffb',
  20. '#e6f4ff',
  21. '#f0f5ff',
  22. '#f9f0ff',
  23. '#fff0f6',
  24. ]
  25. export default (props) => {
  26. const ref = React.useRef();
  27. const [color1, color2] = React.useMemo(() => {
  28. const inx1 = Math.floor(Math.random() * colors.length);
  29. const inx2 = Math.floor(Math.random() * colors.length);
  30. return [colors[inx1], colors[inx2]];
  31. }, []);
  32. React.useEffect(() => {
  33. const dom = ref.current;
  34. const canvas = dom.firstElementChild;
  35. canvas.width = dom.offsetWidth;
  36. canvas.height = dom.offsetHeight;
  37. const centerX = canvas.width / 2;
  38. const centerY = canvas.height / 2;
  39. const size = centerX / 3
  40. const ctx = canvas.getContext("2d");
  41. ctx.filter = `blur(200px)`;
  42. ctx.beginPath()
  43. ctx.fillStyle = color1; // "#ffd8bf";
  44. ctx.ellipse(centerX + 100, centerY + 80, size, size + Math.random() * 100, Math.PI / 4, 0, 2 * Math.PI);
  45. ctx.fill();
  46. ctx.closePath();
  47. ctx.beginPath()
  48. ctx.fillStyle = color2; // "#e6fffb";
  49. ctx.ellipse(centerX + 400, centerY + 120, size, size + Math.random() * 100, Math.PI / 2, 0, 2 * Math.PI);
  50. ctx.fill();
  51. ctx.closePath();
  52. }, []);
  53. return (
  54. <div ref={ref} style={wrapperStyle}>
  55. <canvas>
  56. </canvas>
  57. </div>
  58. )
  59. }