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=en 11 * @class CacheMixin A mixin that contains cache method.You can mix cache method to the target by use Class.mix(target, CacheMixin). 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=en 23 * Cache the view. 24 * @param {Boolean} forceUpdate is force update cache. 25 */ 26 cache: function(forceUpdate){ 27 if(forceUpdate || this._cacheDirty || !this._cacheImage){ 28 this.updateCache(); 29 } 30 }, 31 /** 32 * @language=en 33 * Update the cache. 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=en 48 * set the cache state diry. 49 */ 50 setCacheDirty:function(dirty){ 51 this._cacheDirty = dirty; 52 } 53 };