Hilo/docs/api-en/code/view/CacheMixin.js
2016-12-14 14:40:55 +08:00

49 lines
1.4 KiB
JavaScript

/**
* Hilo
* Copyright 2015 alibaba.com
* Licensed under the MIT License
*/
var _cacheCanvas = Hilo.createElement('canvas');
var _cacheContext = _cacheCanvas.getContext('2d');
/**
* @class CacheMixin A mixin that contains cache method.You can mix cache method to the target by use Class.mix(target, CacheMixin).
* @static
* @mixin
* @module hilo/view/CacheMixin
* @requires hilo/core/Hilo
* @requires hilo/view/Drawable
*/
var CacheMixin = /** @lends CacheMixin# */ {
_cacheDirty:true,
/**
* Cache the view.
* @param {Boolean} forceUpdate is force update cache.
*/
cache: function(forceUpdate){
if(forceUpdate || this._cacheDirty || !this._cacheImage){
this.updateCache();
}
},
/**
* Update the cache.
*/
updateCache:function(){
//TODO:width, height自动判断
_cacheCanvas.width = this.width;
_cacheCanvas.height = this.height;
this._draw(_cacheContext);
this._cacheImage = new Image();
this._cacheImage.src = _cacheCanvas.toDataURL();
this.drawable = this.drawable||new Drawable();
this.drawable.init(this._cacheImage);
this._cacheDirty = false;
},
/**
* set the cache state diry.
* @param {Boolean} dirty is cache dirty
*/
setCacheDirty:function(dirty){
this._cacheDirty = dirty;
}
};