Layer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. var util = require("./core/util");
  2. var _config = require("./config");
  3. var devicePixelRatio = _config.devicePixelRatio;
  4. var Style = require("./graphic/Style");
  5. var Pattern = require("./graphic/Pattern");
  6. /**
  7. * @module zrender/Layer
  8. * @author pissang(https://www.github.com/pissang)
  9. */
  10. function returnFalse() {
  11. return false;
  12. }
  13. /**
  14. * 创建dom
  15. *
  16. * @inner
  17. * @param {string} id dom id 待用
  18. * @param {Painter} painter painter instance
  19. * @param {number} number
  20. */
  21. function createDom(id, painter, dpr) {
  22. var newDom = util.createCanvas();
  23. var width = painter.getWidth();
  24. var height = painter.getHeight();
  25. var newDomStyle = newDom.style;
  26. if (newDomStyle) {
  27. // In node or some other non-browser environment
  28. newDomStyle.position = 'absolute';
  29. newDomStyle.left = 0;
  30. newDomStyle.top = 0;
  31. newDomStyle.width = width + 'px';
  32. newDomStyle.height = height + 'px';
  33. newDom.setAttribute('data-zr-dom-id', id);
  34. }
  35. newDom.width = width * dpr;
  36. newDom.height = height * dpr;
  37. return newDom;
  38. }
  39. /**
  40. * @alias module:zrender/Layer
  41. * @constructor
  42. * @extends module:zrender/mixin/Transformable
  43. * @param {string} id
  44. * @param {module:zrender/Painter} painter
  45. * @param {number} [dpr]
  46. */
  47. var Layer = function (id, painter, dpr) {
  48. var dom;
  49. dpr = dpr || devicePixelRatio;
  50. if (typeof id === 'string') {
  51. dom = createDom(id, painter, dpr);
  52. } // Not using isDom because in node it will return false
  53. else if (util.isObject(id)) {
  54. dom = id;
  55. id = dom.id;
  56. }
  57. this.id = id;
  58. this.dom = dom;
  59. var domStyle = dom.style;
  60. if (domStyle) {
  61. // Not in node
  62. dom.onselectstart = returnFalse; // 避免页面选中的尴尬
  63. domStyle['-webkit-user-select'] = 'none';
  64. domStyle['user-select'] = 'none';
  65. domStyle['-webkit-touch-callout'] = 'none';
  66. domStyle['-webkit-tap-highlight-color'] = 'rgba(0,0,0,0)';
  67. domStyle['padding'] = 0; // eslint-disable-line dot-notation
  68. domStyle['margin'] = 0; // eslint-disable-line dot-notation
  69. domStyle['border-width'] = 0;
  70. }
  71. this.domBack = null;
  72. this.ctxBack = null;
  73. this.painter = painter;
  74. this.config = null; // Configs
  75. /**
  76. * 每次清空画布的颜色
  77. * @type {string}
  78. * @default 0
  79. */
  80. this.clearColor = 0;
  81. /**
  82. * 是否开启动态模糊
  83. * @type {boolean}
  84. * @default false
  85. */
  86. this.motionBlur = false;
  87. /**
  88. * 在开启动态模糊的时候使用,与上一帧混合的alpha值,值越大尾迹越明显
  89. * @type {number}
  90. * @default 0.7
  91. */
  92. this.lastFrameAlpha = 0.7;
  93. /**
  94. * Layer dpr
  95. * @type {number}
  96. */
  97. this.dpr = dpr;
  98. };
  99. Layer.prototype = {
  100. constructor: Layer,
  101. __dirty: true,
  102. __used: false,
  103. __drawIndex: 0,
  104. __startIndex: 0,
  105. __endIndex: 0,
  106. incremental: false,
  107. getElementCount: function () {
  108. return this.__endIndex - this.__startIndex;
  109. },
  110. initContext: function () {
  111. this.ctx = this.dom.getContext('2d');
  112. this.ctx.dpr = this.dpr;
  113. },
  114. createBackBuffer: function () {
  115. var dpr = this.dpr;
  116. this.domBack = createDom('back-' + this.id, this.painter, dpr);
  117. this.ctxBack = this.domBack.getContext('2d');
  118. if (dpr !== 1) {
  119. this.ctxBack.scale(dpr, dpr);
  120. }
  121. },
  122. /**
  123. * @param {number} width
  124. * @param {number} height
  125. */
  126. resize: function (width, height) {
  127. var dpr = this.dpr;
  128. var dom = this.dom;
  129. var domStyle = dom.style;
  130. var domBack = this.domBack;
  131. if (domStyle) {
  132. domStyle.width = width + 'px';
  133. domStyle.height = height + 'px';
  134. }
  135. dom.width = width * dpr;
  136. dom.height = height * dpr;
  137. if (domBack) {
  138. domBack.width = width * dpr;
  139. domBack.height = height * dpr;
  140. if (dpr !== 1) {
  141. this.ctxBack.scale(dpr, dpr);
  142. }
  143. }
  144. },
  145. /**
  146. * 清空该层画布
  147. * @param {boolean} [clearAll]=false Clear all with out motion blur
  148. * @param {Color} [clearColor]
  149. */
  150. clear: function (clearAll, clearColor) {
  151. var dom = this.dom;
  152. var ctx = this.ctx;
  153. var width = dom.width;
  154. var height = dom.height;
  155. var clearColor = clearColor || this.clearColor;
  156. var haveMotionBLur = this.motionBlur && !clearAll;
  157. var lastFrameAlpha = this.lastFrameAlpha;
  158. var dpr = this.dpr;
  159. if (haveMotionBLur) {
  160. if (!this.domBack) {
  161. this.createBackBuffer();
  162. }
  163. this.ctxBack.globalCompositeOperation = 'copy';
  164. this.ctxBack.drawImage(dom, 0, 0, width / dpr, height / dpr);
  165. }
  166. ctx.clearRect(0, 0, width, height);
  167. if (clearColor && clearColor !== 'transparent') {
  168. var clearColorGradientOrPattern; // Gradient
  169. if (clearColor.colorStops) {
  170. // Cache canvas gradient
  171. clearColorGradientOrPattern = clearColor.__canvasGradient || Style.getGradient(ctx, clearColor, {
  172. x: 0,
  173. y: 0,
  174. width: width,
  175. height: height
  176. });
  177. clearColor.__canvasGradient = clearColorGradientOrPattern;
  178. } // Pattern
  179. else if (clearColor.image) {
  180. clearColorGradientOrPattern = Pattern.prototype.getCanvasPattern.call(clearColor, ctx);
  181. }
  182. ctx.save();
  183. ctx.fillStyle = clearColorGradientOrPattern || clearColor;
  184. ctx.fillRect(0, 0, width, height);
  185. ctx.restore();
  186. }
  187. if (haveMotionBLur) {
  188. var domBack = this.domBack;
  189. ctx.save();
  190. ctx.globalAlpha = lastFrameAlpha;
  191. ctx.drawImage(domBack, 0, 0, width, height);
  192. ctx.restore();
  193. }
  194. }
  195. };
  196. var _default = Layer;
  197. module.exports = _default;