1 /** 2 * Hilo 3 * Copyright 2015 alibaba.com 4 * Licensed under the MIT License 5 */ 6 7 var _cacheCanvas = Hilo.createElement('canvas'); 8 var _cacheContext = _cacheCanvas.getContext('2d'); 9 /** 10 * @language=zh 11 * @class CacheMixin是一个包含cache功能的mixin。可以通过 Class.mix(target, CacheMixin) 来为target增加cache功能。 12 * @mixin 13 * @static 14 * @module hilo/view/CacheMixin 15 * @requires hilo/core/Hilo 16 * @requires hilo/core/Class 17 * @requires hilo/view/Drawable 18 */ 19 var CacheMixin = { 20 _cacheDirty:true, 21 /** 22 * @language=zh 23 * 缓存到图片里。可用来提高渲染效率。 24 * @param {Boolean} forceUpdate 是否强制更新缓存 25 */ 26 cache: function(forceUpdate){ 27 if(forceUpdate || this._cacheDirty || !this._cacheImage){ 28 this.updateCache(); 29 } 30 }, 31 /** 32 * @language=zh 33 * 更新缓存 34 */ 35 updateCache:function(){ 36 //TODO:width, height自动判断 37 _cacheCanvas.width = this.width; 38 _cacheCanvas.height = this.height; 39 this._draw(_cacheContext); 40 this._cacheImage = new Image(); 41 this._cacheImage.src = _cacheCanvas.toDataURL(); 42 this.drawable = this.drawable||new Drawable(); 43 this.drawable.init(this._cacheImage); 44 this._cacheDirty = false; 45 }, 46 /** 47 * @language=zh 48 * 设置缓存是否dirty 49 */ 50 setCacheDirty:function(dirty){ 51 this._cacheDirty = dirty; 52 } 53 };