Source: core/renderers/SystemRenderer.js

core/renderers/SystemRenderer.js

  1. import { sayHello, hex2string, hex2rgb } from '../utils';
  2. import { Matrix, Rectangle } from '../math';
  3. import { RENDERER_TYPE } from '../const';
  4. import settings from '../settings';
  5. import Container from '../display/Container';
  6. import RenderTexture from '../textures/RenderTexture';
  7. import EventEmitter from 'eventemitter3';
  8. //引入小程序补丁
  9. import { documentAlias } from '@ali/pixi-miniprogram-adapter';
  10. const tempMatrix = new Matrix();
  11. /**
  12. * The SystemRenderer is the base for a PixiJS Renderer. It is extended by the {@link PIXI.CanvasRenderer}
  13. * and {@link PIXI.WebGLRenderer} which can be used for rendering a PixiJS scene.
  14. *
  15. * @abstract
  16. * @class
  17. * @extends EventEmitter
  18. * @memberof PIXI
  19. */
  20. export default class SystemRenderer extends EventEmitter
  21. {
  22. // eslint-disable-next-line valid-jsdoc
  23. /**
  24. * @param {string} system - The name of the system this renderer is for.
  25. * @param {object} [options] - The optional renderer parameters
  26. * @param {number} [options.width=800] - the width of the screen
  27. * @param {number} [options.height=600] - the height of the screen
  28. * @param {HTMLCanvasElement} [options.view] - the canvas to use as a view, optional
  29. * @param {boolean} [options.transparent=false] - If the render view is transparent, default false
  30. * @param {boolean} [options.autoResize=false] - If the render view is automatically resized, default false
  31. * @param {boolean} [options.antialias=false] - sets antialias (only applicable in chrome at the moment)
  32. * @param {number} [options.resolution=1] - The resolution / device pixel ratio of the renderer. The
  33. * resolution of the renderer retina would be 2.
  34. * @param {boolean} [options.preserveDrawingBuffer=false] - enables drawing buffer preservation,
  35. * enable this if you need to call toDataUrl on the webgl context.
  36. * @param {boolean} [options.clearBeforeRender=true] - This sets if the renderer will clear the canvas or
  37. * not before the new render pass.
  38. * @param {number} [options.backgroundColor=0x000000] - The background color of the rendered area
  39. * (shown if not transparent).
  40. * @param {boolean} [options.roundPixels=false] - If true PixiJS will Math.floor() x/y values when rendering,
  41. * stopping pixel interpolation.
  42. */
  43. constructor(system, options, arg2, arg3)
  44. {
  45. super();
  46. sayHello(system);
  47. // Support for constructor(system, screenWidth, screenHeight, options)
  48. if (typeof options === 'number')
  49. {
  50. options = Object.assign({
  51. width: options,
  52. height: arg2 || settings.RENDER_OPTIONS.height,
  53. }, arg3);
  54. }
  55. // Add the default render options
  56. options = Object.assign({}, settings.RENDER_OPTIONS, options);
  57. /**
  58. * The supplied constructor options.
  59. *
  60. * @member {Object}
  61. * @readOnly
  62. */
  63. this.options = options;
  64. /**
  65. * The type of the renderer.
  66. *
  67. * @member {number}
  68. * @default PIXI.RENDERER_TYPE.UNKNOWN
  69. * @see PIXI.RENDERER_TYPE
  70. */
  71. this.type = RENDERER_TYPE.UNKNOWN;
  72. /**
  73. * Measurements of the screen. (0, 0, screenWidth, screenHeight)
  74. *
  75. * Its safe to use as filterArea or hitArea for whole stage
  76. *
  77. * @member {PIXI.Rectangle}
  78. */
  79. this.screen = new Rectangle(0, 0, options.width, options.height);
  80. /**
  81. * The canvas element that everything is drawn to
  82. *
  83. * @member {HTMLCanvasElement}
  84. */
  85. this.view = options.view || documentAlias.createElement('canvas');
  86. /**
  87. * The resolution / device pixel ratio of the renderer
  88. *
  89. * @member {number}
  90. * @default 1
  91. */
  92. this.resolution = options.resolution || settings.RESOLUTION;
  93. /**
  94. * Whether the render view is transparent
  95. *
  96. * @member {boolean}
  97. */
  98. this.transparent = options.transparent;
  99. /**
  100. * Whether css dimensions of canvas view should be resized to screen dimensions automatically
  101. *
  102. * @member {boolean}
  103. */
  104. this.autoResize = options.autoResize || false;
  105. /**
  106. * Tracks the blend modes useful for this renderer.
  107. *
  108. * @member {object<string, mixed>}
  109. */
  110. this.blendModes = null;
  111. /**
  112. * The value of the preserveDrawingBuffer flag affects whether or not the contents of
  113. * the stencil buffer is retained after rendering.
  114. *
  115. * @member {boolean}
  116. */
  117. this.preserveDrawingBuffer = options.preserveDrawingBuffer;
  118. /**
  119. * This sets if the CanvasRenderer will clear the canvas or not before the new render pass.
  120. * If the scene is NOT transparent PixiJS will use a canvas sized fillRect operation every
  121. * frame to set the canvas background color. If the scene is transparent PixiJS will use clearRect
  122. * to clear the canvas every frame. Disable this by setting this to false. For example if
  123. * your game has a canvas filling background image you often don't need this set.
  124. *
  125. * @member {boolean}
  126. * @default
  127. */
  128. this.clearBeforeRender = options.clearBeforeRender;
  129. /**
  130. * If true PixiJS will Math.floor() x/y values when rendering, stopping pixel interpolation.
  131. * Handy for crisp pixel art and speed on legacy devices.
  132. *
  133. * @member {boolean}
  134. */
  135. this.roundPixels = options.roundPixels;
  136. /**
  137. * The background color as a number.
  138. *
  139. * @member {number}
  140. * @private
  141. */
  142. this._backgroundColor = 0x000000;
  143. /**
  144. * The background color as an [R, G, B] array.
  145. *
  146. * @member {number[]}
  147. * @private
  148. */
  149. this._backgroundColorRgba = [0, 0, 0, 0];
  150. /**
  151. * The background color as a string.
  152. *
  153. * @member {string}
  154. * @private
  155. */
  156. this._backgroundColorString = '#000000';
  157. this.backgroundColor = options.backgroundColor || this._backgroundColor; // run bg color setter
  158. /**
  159. * This temporary display object used as the parent of the currently being rendered item
  160. *
  161. * @member {PIXI.DisplayObject}
  162. * @private
  163. */
  164. this._tempDisplayObjectParent = new Container();
  165. /**
  166. * The last root object that the renderer tried to render.
  167. *
  168. * @member {PIXI.DisplayObject}
  169. * @private
  170. */
  171. this._lastObjectRendered = this._tempDisplayObjectParent;
  172. }
  173. /**
  174. * Same as view.width, actual number of pixels in the canvas by horizontal
  175. *
  176. * @member {number}
  177. * @readonly
  178. * @default 800
  179. */
  180. get width()
  181. {
  182. return this.view.width;
  183. }
  184. /**
  185. * Same as view.height, actual number of pixels in the canvas by vertical
  186. *
  187. * @member {number}
  188. * @readonly
  189. * @default 600
  190. */
  191. get height()
  192. {
  193. return this.view.height;
  194. }
  195. /**
  196. * Resizes the screen and canvas to the specified width and height
  197. * Canvas dimensions are multiplied by resolution
  198. *
  199. * @param {number} screenWidth - the new width of the screen
  200. * @param {number} screenHeight - the new height of the screen
  201. */
  202. resize(screenWidth, screenHeight)
  203. {
  204. this.screen.width = screenWidth;
  205. this.screen.height = screenHeight;
  206. this.view.width = screenWidth * this.resolution;
  207. this.view.height = screenHeight * this.resolution;
  208. if (this.autoResize)
  209. {
  210. this.view.style.width = `${screenWidth}px`;
  211. this.view.style.height = `${screenHeight}px`;
  212. }
  213. }
  214. /**
  215. * Useful function that returns a texture of the display object that can then be used to create sprites
  216. * This can be quite useful if your displayObject is complicated and needs to be reused multiple times.
  217. *
  218. * @param {PIXI.DisplayObject} displayObject - The displayObject the object will be generated from
  219. * @param {number} scaleMode - Should be one of the scaleMode consts
  220. * @param {number} resolution - The resolution / device pixel ratio of the texture being generated
  221. * @param {PIXI.Rectangle} [region] - The region of the displayObject, that shall be rendered,
  222. * if no region is specified, defaults to the local bounds of the displayObject.
  223. * @return {PIXI.Texture} a texture of the graphics object
  224. */
  225. generateTexture(displayObject, scaleMode, resolution, region)
  226. {
  227. region = region || displayObject.getLocalBounds();
  228. const renderTexture = RenderTexture.create(region.width | 0, region.height | 0, scaleMode, resolution);
  229. tempMatrix.tx = -region.x;
  230. tempMatrix.ty = -region.y;
  231. this.render(displayObject, renderTexture, false, tempMatrix, !!displayObject.parent);
  232. return renderTexture;
  233. }
  234. /**
  235. * Removes everything from the renderer and optionally removes the Canvas DOM element.
  236. *
  237. * @param {boolean} [removeView=false] - Removes the Canvas element from the DOM.
  238. */
  239. destroy(removeView)
  240. {
  241. if (removeView && this.view.parentNode)
  242. {
  243. this.view.parentNode.removeChild(this.view);
  244. }
  245. this.type = RENDERER_TYPE.UNKNOWN;
  246. this.view = null;
  247. this.screen = null;
  248. this.resolution = 0;
  249. this.transparent = false;
  250. this.autoResize = false;
  251. this.blendModes = null;
  252. this.options = null;
  253. this.preserveDrawingBuffer = false;
  254. this.clearBeforeRender = false;
  255. this.roundPixels = false;
  256. this._backgroundColor = 0;
  257. this._backgroundColorRgba = null;
  258. this._backgroundColorString = null;
  259. this._tempDisplayObjectParent = null;
  260. this._lastObjectRendered = null;
  261. }
  262. /**
  263. * The background color to fill if not transparent
  264. *
  265. * @member {number}
  266. */
  267. get backgroundColor()
  268. {
  269. return this._backgroundColor;
  270. }
  271. set backgroundColor(value) // eslint-disable-line require-jsdoc
  272. {
  273. this._backgroundColor = value;
  274. this._backgroundColorString = hex2string(value);
  275. hex2rgb(value, this._backgroundColorRgba);
  276. }
  277. }