Skip to content

Instantly share code, notes, and snippets.

View mflux's full-sized avatar

Michael Chang mflux

View GitHub Profile
function traverseGroup( group, fn ){
for( var i in group.children ){
var child = group.children[ i ];
fn( child );
traverseGroup( child, fn );
}
}
@mflux
mflux / fixgrouptoshapes.js
Created September 26, 2014 01:49
TWO.js center group on objects
function fixGroupToShapes( g ){
var bb = g.getBoundingClientRect();
var cx = bb.width * 0.5 + bb.left;
var cy = bb.height * 0.5 + bb.top;
g.center();
g.translation.x = cx;
g.translation.y = cy;
}
@mflux
mflux / gist:86dac07cdc6a16baafe1
Created July 23, 2014 23:20
A small hacky module for THREE.js that forces a mesh and its texture to be uploaded into the GPU regardless of it's seen on-screen or not, preventing lag when the object comes into view.
ForceGPULoad = (function(){
var renderer;
var scene;
var camera;
return {
buffer: function( object3D ){
// push
var originalPosition = object3D.position.clone();
object3D.position.set( 0,0,0 );
@mflux
mflux / isometric unproject
Created August 28, 2014 10:46
Phaser Isometric Unprojector
Phaser.Plugin.Isometric.Projector.prototype.unproject = function (point, out) {
if (typeof out === "undefined") {
out = new Phaser.Plugin.Isometric.Point3();
}
var px = point.x;
var py = point.y;
px *= this.projectionRatio;
@mflux
mflux / ec.js
Created September 23, 2015 23:50
import R from 'ramda';
function newId(){
let d = new Date().getTime();
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
import R from 'ramda';
function newId(){
let d = new Date().getTime();
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
import R from 'ramda';
import * as Debug from './debug';
export function create(){
const events = {
componentCreations: [],
componentChanges: [],
componentDeletions: []
};
@mflux
mflux / ec.js
Created June 26, 2018 22:07
Entity Components
import R from 'ramda';
import * as Perf from '../cpcs/performance';
import fast from '../../node_modules/fast.js/index';
/**
* @return {string}
*/
function newId(){
let d = new Date().getTime();
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
// save camera views
const savedCamera = JSON.parse( localStorage.getItem( 'savedCamera' ) );
if( savedCamera ){
camera.position.copy( savedCamera.cameraPosition );
orbitControls.target.copy( savedCamera.targetPosition );
}
$(window).unload( function(){
localStorage.savedCamera = JSON.stringify({
cameraPosition: camera.position,
@mflux
mflux / twojshack.js
Created September 25, 2014 05:55
TWO.js text hack
/*
Goes after
Polygon.MakeObservable(Polygon.prototype);
//...
*/
var Text = Two.Text = function( text ) {