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 * @class CacheMixin是一个包含cache功能的mixin。可以通过 Class.mix(target, CacheMixin) 来为target增加cache功能。 11 * @mixin 12 * @static 13 * @module hilo/view/CacheMixin 14 * @requires hilo/core/Hilo 15 * @requires hilo/core/Class 16 * @requires hilo/view/Drawable 17 */ 18 var CacheMixin = { 19 _cacheDirty:true, 20 /** 21 * 缓存到图片里。可用来提高渲染效率。 22 * @param {Boolean} forceUpdate 是否强制更新缓存 23 */ 24 cache: function(forceUpdate){ 25 if(forceUpdate || this._cacheDirty || !this._cacheImage){ 26 this.updateCache(); 27 } 28 }, 29 /** 30 * 更新缓存 31 */ 32 updateCache:function(){ 33 //TODO:width, height自动判断 34 _cacheCanvas.width = this.width; 35 _cacheCanvas.height = this.height; 36 this._draw(_cacheContext); 37 this._cacheImage = new Image(); 38 this._cacheImage.src = _cacheCanvas.toDataURL(); 39 this.drawable = this.drawable||new Drawable(); 40 this.drawable.init(this._cacheImage); 41 this._cacheDirty = false; 42 }, 43 /** 44 * 设置缓存是否dirty 45 */ 46 setCacheDirty:function(dirty){ 47 this._cacheDirty = dirty; 48 } 49 };