Layer.js 6.0 KB

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