Skip to content

Instantly share code, notes, and snippets.

View kishalmi's full-sized avatar
🤘
ready to rock

LMG kishalmi

🤘
ready to rock
View GitHub Profile
@kishalmi
kishalmi / gist:db5060b30de5fa9cbae0
Created May 21, 2014 14:19
trapping TAB key in ember component input-helper
<script type="text/x-handlebars" id="components/chat-input">
{{input id="tiChatInput" action="chatInputEnter"}}
</script>
MyApp.ChatInputComponent = Ember.Component.extend({
didInsertElement: function () {
var self = this,
ctx = this.$('input').keydown(function (event) {
if (event.which === 9) {
// [TAB]
@kishalmi
kishalmi / gist:665fe7a8953a5b91cc3a
Created April 30, 2014 14:59
reverse (copy of) webaudio buffer
request.onload = function () {
context.decodeAudioData(request.response, function (buf) {
// store as forward buffer
buffer = buf;
// create a reversed copy
bufferReversed = context.createBuffer(buf.numberOfChannels, buf.length, buf.sampleRate);
for (var i = 0; i < buf.numberOfChannels; i++)
bufferReversed.getChannelData(i).set(
Array.prototype.reverse.call(new Float32Array(buf.getChannelData(i)))
);
@kishalmi
kishalmi / main.js
Last active August 29, 2015 13:55
spawn a webworker without the need for a seperate file, including requirejs functionality
/*
assuming a rather standard requirejs script tag referring to main(.js)
<script type="text/javascript" data-main="main" src="js/require.min.js"></script>
*/
var w = newRequireWorker({
shim: { three: { exports: 'THREE' } },
baseUrl: 'js',
paths: { three: 'three.min' }
},
@kishalmi
kishalmi / gist:8452247
Created January 16, 2014 09:42
three.js - split a (loaded) model into mesh parts by material index
/**
* split a (loaded) model into mesh parts by material index
* @param {THREE.Geometry} geometry
* @param {Array<THREE.Material>} materials
* @returns {Array<THREE.Mesh>}
*/
var splitByMaterial = function(geometry,materials) {
var parts = [],
geo, vMap, iMat,
addPart = function() {
@kishalmi
kishalmi / HexGeometry.js
Created December 9, 2013 12:22
THREE hexagon geometry - evenly subdivided
/**
* hexagon geometry - evenly subdivided
* @param radius
* @param {int=} segments - subdivisions
* @param {Number=} theta - default "pointy top" (Math.PI/2 for "flat top")
* @constructor
* @author lmg / https://github.com/kishalmi
*/
THREE.HexGeometry = function (radius, segments, theta) {
@kishalmi
kishalmi / main.js
Last active December 26, 2015 21:58
auto updating uniform float time
uniforms.time = { type: 'f', get value() { return performance.now()/1000 } };
@kishalmi
kishalmi / main.js
Created October 23, 2013 07:53
create a geometry suitable for a THREE.Line of type THREE.LineStrip from a sphere (not displaying triangle-edges of a sphere wireframe)
/**
* create a geometry suitable for a THREE.Line of type THREE.LineStrip from a sphere
* thereby not displaying triangle-edges of a sphere wireframe.
* @param {THREE.SphereGeometry} geoSphere
* @returns {THREE.Geometry}
*/
function lineStripGeometryFromSphereGeometry(geoSphere) {
var geoLine = new THREE.Geometry(),
w = geoSphere.widthSegments, h = geoSphere.heightSegments,
x, y;
@kishalmi
kishalmi / gist:6951786
Created October 12, 2013 16:14
get ImageData from a WebGL renderer
var img = document.createElement('img');
img.src = renderer.domElement.toDataURL('image/png');
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = renderer.domElement.width;
canvas.height = renderer.domElement.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, canvas.width, canvas.height);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
}
@kishalmi
kishalmi / main_rods.js
Created October 4, 2013 20:30
three.js traverse scenegraph and trigger event from all objects found
function render() {
var t = new Date().getTime() / 1000; // unixtime
var dt = clock.getDelta();
dispatchSceneGraphEvent(scene, {type: 'preRender', t: t, dt: dt});
renderer.clear(true, true, true);
renderer.render(scene, camera);
}
@kishalmi
kishalmi / kishalmi.js
Last active December 23, 2015 15:59
simple require using jquery
/**
* cheap require (using jquery)
* loads a script (or scripts, if given an array) and executes it
* callbacks for progress and success
* won't load a script twice if already loaded (or failed)
* @param files
* @param callbackProgress
* @param callbackSuccess
*/
function require(files, callbackProgress, callbackSuccess) {