Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created April 17, 2014 08:29
Show Gist options
  • Save jhurliman/10964644 to your computer and use it in GitHub Desktop.
Save jhurliman/10964644 to your computer and use it in GitHub Desktop.
famo.us RenderPool. Work in progres...
define(function(require, exports, module) {
var RenderNode = require('famous/core/RenderNode');
var Modifier = require('famous/core/Modifier');
var Surface = require('famous/core/Surface');
function RenderPool() {
this._node = new RenderNode();
this._children = {};
this._unusedChildren = [];
}
/** @const */
RenderPool.SIZE_HIDDEN = [0.0001, 0.0001];
RenderPool.OPACITY_HIDDEN = 1e-6;
RenderPool.prototype.create = function create(id, modifierOptions, surfaceOptions) {
var modifier;
var surface;
var child = this._children[id] || this._unusedChildren.pop();
if (child) {
modifier = child.get();
surface = child._child.get();
_updateModifier(modifier, modifierOptions);
surface.setOptions(surfaceOptions);
return child;
}
modifier = new Modifier(modifierOptions);
surface = new Surface(surfaceOptions);
child = this._node.add(modifier);
child.add(surface);
this._children[id] = child;
return child;
};
RenderPool.prototype.get = function get(id) {
return this._children[id];
};
RenderPool.prototype.remove = function remove(id) {
var child = this._children[id];
if (!child) return;
delete this._children[id];
child._object.sizeFrom(RenderPool.SIZE_HIDDEN);
child._object.opacityFrom(RenderPool.OPACITY_HIDDEN);
this._unusedChildren.push(child);
};
RenderPool.prototype.clear = function clear() {
Object.keys(this._children).forEach(this.remove, this);
};
RenderPool.prototype.getSize = function getSize() {
return this._node.getSize();
};
RenderPool.prototype.commit = function commit(context) {
return this._node.commit(context);
};
RenderPool.prototype.render = function render() {
return this._node.render();
};
function _updateModifier(modifier, options) {
options = options || {};
if (options.transform) modifier.transformFrom(options.transform);
if (options.origin) modifier.originFrom(options.origin);
if (options.size) modifier.sizeFrom(options.size);
else modifier.sizeFrom(null);
if (options.opacity !== undefined) modifier.opacityFrom(options.opacity);
else modifier.opacityFrom(1);
}
module.exports = RenderPool;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment