happydom.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { GlobalRegistrator } from "@happy-dom/global-registrator"
  2. GlobalRegistrator.register()
  3. const originalGetContext = HTMLCanvasElement.prototype.getContext
  4. // @ts-expect-error - we're overriding with a simplified mock
  5. HTMLCanvasElement.prototype.getContext = function (contextType: string, _options?: unknown) {
  6. if (contextType === "2d") {
  7. return {
  8. canvas: this,
  9. fillStyle: "#000000",
  10. strokeStyle: "#000000",
  11. font: "12px monospace",
  12. textAlign: "start",
  13. textBaseline: "alphabetic",
  14. globalAlpha: 1,
  15. globalCompositeOperation: "source-over",
  16. imageSmoothingEnabled: true,
  17. lineWidth: 1,
  18. lineCap: "butt",
  19. lineJoin: "miter",
  20. miterLimit: 10,
  21. shadowBlur: 0,
  22. shadowColor: "rgba(0, 0, 0, 0)",
  23. shadowOffsetX: 0,
  24. shadowOffsetY: 0,
  25. fillRect: () => {},
  26. strokeRect: () => {},
  27. clearRect: () => {},
  28. fillText: () => {},
  29. strokeText: () => {},
  30. measureText: (text: string) => ({ width: text.length * 8 }),
  31. drawImage: () => {},
  32. save: () => {},
  33. restore: () => {},
  34. scale: () => {},
  35. rotate: () => {},
  36. translate: () => {},
  37. transform: () => {},
  38. setTransform: () => {},
  39. resetTransform: () => {},
  40. createLinearGradient: () => ({ addColorStop: () => {} }),
  41. createRadialGradient: () => ({ addColorStop: () => {} }),
  42. createPattern: () => null,
  43. beginPath: () => {},
  44. closePath: () => {},
  45. moveTo: () => {},
  46. lineTo: () => {},
  47. bezierCurveTo: () => {},
  48. quadraticCurveTo: () => {},
  49. arc: () => {},
  50. arcTo: () => {},
  51. ellipse: () => {},
  52. rect: () => {},
  53. fill: () => {},
  54. stroke: () => {},
  55. clip: () => {},
  56. isPointInPath: () => false,
  57. isPointInStroke: () => false,
  58. getTransform: () => ({}),
  59. getImageData: () => ({
  60. data: new Uint8ClampedArray(0),
  61. width: 0,
  62. height: 0,
  63. }),
  64. putImageData: () => {},
  65. createImageData: () => ({
  66. data: new Uint8ClampedArray(0),
  67. width: 0,
  68. height: 0,
  69. }),
  70. } as unknown as CanvasRenderingContext2D
  71. }
  72. return originalGetContext.call(this, contextType as "2d", _options)
  73. }