Skip to content

Instantly share code, notes, and snippets.

View mendes5's full-sized avatar
📉
[object Object]

mendes5

📉
[object Object]
View GitHub Profile
@mendes5
mendes5 / tinyTimer.js
Last active April 3, 2017 04:04
simple timer to put on the game loop to avoid using setTimeout.
const tinyTimer = (function () {
const timers = []
function update() {
const time = performance.now()
let i = 0
while (i < timers.length) {
let timer = timers[i]
if (time > timer.start + timer.duration) {
timer.callback()
timers.splice(i, 1)
@mendes5
mendes5 / visualizer.js
Last active March 7, 2017 17:02
Simple audio visualization using web audio api
let x=0,y=0,tx=0,ty=0,r=150,a=0; //States
let context = new AudioContext(); //Audio context
let times = new Uint8Array(360); //Typed array to hold only 360 items
let canvas = document.createElement('canvas'); //Create the canvas
let ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
document.body.appendChild(canvas);
document.body.style.margin = '0px';
@mendes5
mendes5 / baseConverter.js
Last active August 29, 2019 17:27 — forked from faisalman/baseConverter.js
Convert From/To Binary/Decimal/Hexadecimal in JavaScript
// This is licensed under the terms of the WTFPL license
const convert = (from, to) => value => parseInt(value, from).toString(to);
// Works with any bases btw
// convert(2, 10)('111') // '7'
// convert(10, 16)('42') // '2a'
// convert(16, 2)('f8') // '11111000'
// convert(10, 2)('22') // '10110'
// You can even cache those
@mendes5
mendes5 / index.js
Created March 29, 2017 21:11
WebGL Simplest Example
//Create the canvas and get the WebGlRederingContext
let ctx = document.createElement('canvas')
let gl = ctx.getContext('webgl')
document.body.appendChild(ctx)
ctx.width = 600
ctx.height = 600
gl.viewport(0, 0, 600, 600)
gl.clearColor(0.0, 0.0, 0.0, 1.0)
//Vertex Shader steps
@mendes5
mendes5 / index.js
Last active April 14, 2017 15:15
Retrieve all uniforms, attributes and its types from an shader string and returns it in a javascript object
const getShaderData = (str) => {
let shaderData = { uniform: {}, attribute: {} }
str.match(/attribute.*;|uniform.*;/g).map(item => {
let [type1, type2, name] = item.split(' ')
name = name.slice(0,name.length-1)
shaderData[type1][name] = {}
shaderData[type1][name].location = name
shaderData[type1][name].type = type2
})
shaderData.isFragmentShader = /gl_FragColor/.test(str)
@mendes5
mendes5 / util.js
Created April 3, 2017 02:35
Chek if an item is equal to any in an array, coz a == '.png' || a == '.jpg' || a == '.gif' sucks
//Only work with primitives values
const isEqualToAny = (input, items = []) => {
let result = false
items.map(i => !result && (result = i === input))
return result
}
@mendes5
mendes5 / index.js
Created April 3, 2017 04:03
Very simple and functional keyboard listener sinppet
class KeyListener {
constructor(obj ,element = window){
element.addEventListener('keyup', this.keyUp.bind(this), false);
element.addEventListener('keydown', this.keyDown.bind(this), false);
this.elememt = element
this.keys = {}
for(let key in obj){
this.keys[key] = {
isDown:false,
press:obj[key].press,
@mendes5
mendes5 / index.js
Created April 6, 2017 05:43
Another keyboard listener
class BooleanicKeys {
constructor(obj, element = window) {
element.addEventListener('keyup', this.keyUp.bind(this), false);
element.addEventListener('keydown', this.keyDown.bind(this), false);
this.elememt = element
this.keys = {}
for (let key in obj) {
this.keys[key] = {
isDown: false,
press: obj[key],
@mendes5
mendes5 / glCamera.js
Last active April 7, 2017 23:41
WebGL camera that works
class Camera {
constructor(gl ,vMU, pMU) {
this.viewMatrix = mat4.create()
this.projMatrix = mat4.create()
this.gl = gl
this.viewMatrixUniform = vMU
this.projectionMatrixUniform = pMU
this.speed
@mendes5
mendes5 / snake.js
Created April 19, 2017 21:03
Simple tweening experiment
//From utils.js
Math.TWO_PI = Math.PI * 2
const random = (v) => Math.floor(Math.random() * v)
const range = (n, s = 0, f = 1) => new Array(n).fill(1).map(i => i = s += f)
const overlays = {
button(name,f){
const btn = document.createElement('input')