Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Chang mflux

View GitHub Profile
@mflux
mflux / ec.js
Created Jun 26, 2018
Entity Components
View ec.js
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) {
View stateevents.js
import R from 'ramda';
import * as Debug from './debug';
export function create(){
const events = {
componentCreations: [],
componentChanges: [],
componentDeletions: []
};
View ec.js
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;
View ec.js
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;
View three.js localstorage camera
// 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 / fixgrouptoshapes.js
Created Sep 26, 2014
TWO.js center group on objects
View fixgrouptoshapes.js
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 / twojshack.js
Created Sep 25, 2014
TWO.js text hack
View twojshack.js
/*
Goes after
Polygon.MakeObservable(Polygon.prototype);
//...
*/
var Text = Two.Text = function( text ) {
View Two.js group traversal
function traverseGroup( group, fn ){
for( var i in group.children ){
var child = group.children[ i ];
fn( child );
traverseGroup( child, fn );
}
}
@mflux
mflux / isometric unproject
Created Aug 28, 2014
Phaser Isometric Unprojector
View isometric unproject
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 / gist:86dac07cdc6a16baafe1
Created Jul 23, 2014
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.
View gist:86dac07cdc6a16baafe1
ForceGPULoad = (function(){
var renderer;
var scene;
var camera;
return {
buffer: function( object3D ){
// push
var originalPosition = object3D.position.clone();
object3D.position.set( 0,0,0 );
You can’t perform that action at this time.