      1 /**
      2  * Hilo
      3  * Copyright 2015 alibaba.com
      4  * Licensed under the MIT License
      5  */
      6
      7 var _cacheCanvas, _cacheContext;
      8 /**
      9  * @class CacheMixin是一个包含cache功能的mixin。可以通过 Class.mix(target, CacheMixin) 来为target增加cache功能。
     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      * 缓存到图片里。可用来提高渲染效率。
     20      * @param {Boolean} forceUpdate 是否强制更新缓存
     21      */
     22     cache: function(forceUpdate){
     23         if(forceUpdate || this._cacheDirty || !this.drawable){
     24             this.updateCache();
     25         }
     26     },
     27     /**
     28      * 更新缓存
     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      * 设置缓存是否dirty
     50      * @param {Boolean} dirty 是否dirty
     51      */
     52     setCacheDirty:function(dirty){
     53         this._cacheDirty = dirty;
     54     }
     55 };