Source: core/renderers/canvas/utils/CanvasRenderTarget.js

core/renderers/canvas/utils/CanvasRenderTarget.js

  1. import settings from '../../../settings';
  2. import {windowAlias, documentAlias} from '@ali/pixi-miniprogram-adapter';
  3. /**
  4. * Creates a Canvas element of the given size.
  5. *
  6. * @class
  7. * @memberof PIXI
  8. */
  9. export default class CanvasRenderTarget
  10. {
  11. /**
  12. * @param {number} width - the width for the newly created canvas
  13. * @param {number} height - the height for the newly created canvas
  14. * @param {number} [resolution=1] - The resolution / device pixel ratio of the canvas
  15. */
  16. constructor(width, height, resolution)
  17. {
  18. /**
  19. * The Canvas object that belongs to this CanvasRenderTarget.
  20. *
  21. * @member {HTMLCanvasElement}
  22. */
  23. this.canvas = documentAlias.createElement('canvas');
  24. /**
  25. * A CanvasRenderingContext2D object representing a two-dimensional rendering context.
  26. *
  27. * @member {CanvasRenderingContext2D}
  28. */
  29. this.context = this.canvas.getContext('2d');
  30. this.resolution = resolution || settings.RESOLUTION;
  31. this.resize(width, height);
  32. }
  33. /**
  34. * Clears the canvas that was created by the CanvasRenderTarget class.
  35. *
  36. * @private
  37. */
  38. clear()
  39. {
  40. this.context.setTransform(1, 0, 0, 1, 0, 0);
  41. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  42. }
  43. /**
  44. * Resizes the canvas to the specified width and height.
  45. *
  46. * @param {number} width - the new width of the canvas
  47. * @param {number} height - the new height of the canvas
  48. */
  49. resize(width, height)
  50. {
  51. this.canvas.width = width * this.resolution;
  52. this.canvas.height = height * this.resolution;
  53. }
  54. /**
  55. * Destroys this canvas.
  56. *
  57. */
  58. destroy()
  59. {
  60. this.context = null;
  61. this.canvas = null;
  62. }
  63. /**
  64. * The width of the canvas buffer in pixels.
  65. *
  66. * @member {number}
  67. */
  68. get width()
  69. {
  70. return this.canvas.width;
  71. }
  72. set width(val) // eslint-disable-line require-jsdoc
  73. {
  74. this.canvas.width = val;
  75. }
  76. /**
  77. * The height of the canvas buffer in pixels.
  78. *
  79. * @member {number}
  80. */
  81. get height()
  82. {
  83. return this.canvas.height;
  84. }
  85. set height(val) // eslint-disable-line require-jsdoc
  86. {
  87. this.canvas.height = val;
  88. }
  89. }