1 /**
  2  * Hilo
  3  * Copyright 2015 alibaba.com
  4  * Licensed under the MIT License
  5  */
  6 
  7 var _cacheCanvas, _cacheContext;
  8 /**
  9  * @class CacheMixin A mixin that contains cache method.You can mix cache method to the target by use Class.mix(target, CacheMixin).
 10  * @static
 11  * @mixin
 12  * @module hilo/view/CacheMixin
 13  * @requires hilo/view/Drawable
 14  * @requires hilo/util/browser
 15  */
 16 var CacheMixin = /** @lends CacheMixin# */ {
 17     _cacheDirty:true,
 18     /**
 19      * Cache the view.
 20      * @param {Boolean} forceUpdate is force update cache.
 21      */
 22     cache: function(forceUpdate){
 23         if(forceUpdate || this._cacheDirty || !this.drawable){
 24             this.updateCache();
 25         }
 26     },
 27     /**
 28      * Update the cache.
 29      */
 30     updateCache:function(){
 31         if(browser.supportCanvas){
 32             if(!_cacheCanvas){
 33                 _cacheCanvas = document.createElement('canvas');
 34                 _cacheContext = _cacheCanvas.getContext('2d');
 35             }
 36 
 37             //TODO:width, height自动判断
 38             _cacheCanvas.width = this.width;
 39             _cacheCanvas.height = this.height;
 40             this._draw(_cacheContext);
 41             this.drawable = this.drawable||new Drawable();
 42             this.drawable.init({
 43                 image:_cacheCanvas.toDataURL()
 44             });
 45             this._cacheDirty = false;
 46         }
 47     },
 48     /**
 49      * set the cache state diry.
 50      * @param {Boolean} dirty is cache dirty
 51      */
 52     setCacheDirty:function(dirty){
 53         this._cacheDirty = dirty;
 54     }
 55 };