Layer.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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; // 没append呢,请原谅我这样写,清晰~
  26. newDomStyle.position = 'absolute';
  27. newDomStyle.left = 0;
  28. newDomStyle.top = 0;
  29. newDomStyle.width = width + 'px';
  30. newDomStyle.height = height + 'px';
  31. newDom.width = width * dpr;
  32. newDom.height = height * dpr; // id不作为索引用,避免可能造成的重名,定义为私有属性
  33. newDom.setAttribute('data-zr-dom-id', id);
  34. return newDom;
  35. }
  36. /**
  37. * @alias module:zrender/Layer
  38. * @constructor
  39. * @extends module:zrender/mixin/Transformable
  40. * @param {string} id
  41. * @param {module:zrender/Painter} painter
  42. * @param {number} [dpr]
  43. */
  44. var Layer = function (id, painter, dpr) {
  45. var dom;
  46. dpr = dpr || devicePixelRatio;
  47. if (typeof id === 'string') {
  48. dom = createDom(id, painter, dpr);
  49. } // Not using isDom because in node it will return false
  50. else if (util.isObject(id)) {
  51. dom = id;
  52. id = dom.id;
  53. }
  54. this.id = id;
  55. this.dom = dom;
  56. var domStyle = dom.style;
  57. if (domStyle) {
  58. // Not in node
  59. dom.onselectstart = returnFalse; // 避免页面选中的尴尬
  60. domStyle['-webkit-user-select'] = 'none';
  61. domStyle['user-select'] = 'none';
  62. domStyle['-webkit-touch-callout'] = 'none';
  63. domStyle['-webkit-tap-highlight-color'] = 'rgba(0,0,0,0)';
  64. domStyle['padding'] = 0;
  65. domStyle['margin'] = 0;
  66. domStyle['border-width'] = 0;
  67. }
  68. this.domBack = null;
  69. this.ctxBack = null;
  70. this.painter = painter;
  71. this.config = null; // Configs
  72. /**
  73. * 每次清空画布的颜色
  74. * @type {string}
  75. * @default 0
  76. */
  77. this.clearColor = 0;
  78. /**
  79. * 是否开启动态模糊
  80. * @type {boolean}
  81. * @default false
  82. */
  83. this.motionBlur = false;
  84. /**
  85. * 在开启动态模糊的时候使用,与上一帧混合的alpha值,值越大尾迹越明显
  86. * @type {number}
  87. * @default 0.7
  88. */
  89. this.lastFrameAlpha = 0.7;
  90. /**
  91. * Layer dpr
  92. * @type {number}
  93. */
  94. this.dpr = dpr;
  95. };
  96. Layer.prototype = {
  97. constructor: Layer,
  98. elCount: 0,
  99. __dirty: true,
  100. initContext: function () {
  101. this.ctx = this.dom.getContext('2d');
  102. this.ctx.__currentValues = {};
  103. this.ctx.dpr = this.dpr;
  104. },
  105. createBackBuffer: function () {
  106. var dpr = this.dpr;
  107. this.domBack = createDom('back-' + this.id, this.painter, dpr);
  108. this.ctxBack = this.domBack.getContext('2d');
  109. this.ctxBack.__currentValues = {};
  110. if (dpr != 1) {
  111. this.ctxBack.scale(dpr, dpr);
  112. }
  113. },
  114. /**
  115. * @param {number} width
  116. * @param {number} height
  117. */
  118. resize: function (width, height) {
  119. var dpr = this.dpr;
  120. var dom = this.dom;
  121. var domStyle = dom.style;
  122. var domBack = this.domBack;
  123. domStyle.width = width + 'px';
  124. domStyle.height = height + 'px';
  125. dom.width = width * dpr;
  126. dom.height = height * dpr;
  127. if (domBack) {
  128. domBack.width = width * dpr;
  129. domBack.height = height * dpr;
  130. if (dpr != 1) {
  131. this.ctxBack.scale(dpr, dpr);
  132. }
  133. }
  134. },
  135. /**
  136. * 清空该层画布
  137. * @param {boolean} clearAll Clear all with out motion blur
  138. */
  139. clear: function (clearAll) {
  140. var dom = this.dom;
  141. var ctx = this.ctx;
  142. var width = dom.width;
  143. var height = dom.height;
  144. var clearColor = this.clearColor;
  145. var haveMotionBLur = this.motionBlur && !clearAll;
  146. var lastFrameAlpha = this.lastFrameAlpha;
  147. var dpr = this.dpr;
  148. if (haveMotionBLur) {
  149. if (!this.domBack) {
  150. this.createBackBuffer();
  151. }
  152. this.ctxBack.globalCompositeOperation = 'copy';
  153. this.ctxBack.drawImage(dom, 0, 0, width / dpr, height / dpr);
  154. }
  155. ctx.clearRect(0, 0, width, height);
  156. if (clearColor) {
  157. var clearColorGradientOrPattern; // Gradient
  158. if (clearColor.colorStops) {
  159. // Cache canvas gradient
  160. clearColorGradientOrPattern = clearColor.__canvasGradient || Style.getGradient(ctx, clearColor, {
  161. x: 0,
  162. y: 0,
  163. width: width,
  164. height: height
  165. });
  166. clearColor.__canvasGradient = clearColorGradientOrPattern;
  167. } // Pattern
  168. else if (clearColor.image) {
  169. clearColorGradientOrPattern = Pattern.prototype.getCanvasPattern.call(clearColor, ctx);
  170. }
  171. ctx.save();
  172. ctx.fillStyle = clearColorGradientOrPattern || clearColor;
  173. ctx.fillRect(0, 0, width, height);
  174. ctx.restore();
  175. }
  176. if (haveMotionBLur) {
  177. var domBack = this.domBack;
  178. ctx.save();
  179. ctx.globalAlpha = lastFrameAlpha;
  180. ctx.drawImage(domBack, 0, 0, width, height);
  181. ctx.restore();
  182. }
  183. }
  184. };
  185. var _default = Layer;
  186. module.exports = _default;