Skip to content

Instantly share code, notes, and snippets.

Greggman greggman

Block or report user

Report or block greggman

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@greggman
greggman / mousepos.js
Created Aug 29, 2019
Get element relative mouse position
View mousepos.js
// use clientX, clientY
someElement.addEventListener('mousemove', (e) => {
const rect = someElement.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
}
// the problem with clientX, clientY is they don't work with CSS transforms.
@greggman
greggman / promise.js
Last active Jul 11, 2019
incomplete promise impl
View promise.js
function MyPromise(fn) {
let status = 0; // 0 = unfulfilled, 1 = resolved, 2 = rejected
function makeQueue(neededStatus) {
return {
neededStatus,
fns: [],
do() {
if (status === neededStatus) {
if (neededStatus === 2 && this.fns.length === 0) {
View coroutinerunner.js
function* waitSeconds(duration) {
while (duration > 0) {
duration -= globals.deltaTime;
yield;
}
}
function* waitFrames(numFrames) {
while (numFrames > 0) {
--numFrames;
@greggman
greggman / webglresourcetracker.js
Created Jun 17, 2019
Find any webgl resources leaks and where they were allocated
View webglresourcetracker.js
const WebGLResourceTracker = function() {
let webglResources = new Map();
const stack = [];
[
'Buffer',
'Renderbuffer',
'Texture',
'Framebuffer',
'Shader',
'Program',
@greggman
greggman / css.md
Last active Sep 6, 2019
CSS notes
View css.md
@greggman
greggman / userContent.css
Created Dec 4, 2018
Hide score on hacker news in firefox
View userContent.css
/*
For me I know my score on HN gamifies me into wanting to spend more time on HN.
Anytime I see my score go up feel a tiny hit of pleasure and click to see why.
Looking at my comments anytime I see the score I feel proud someone liked my
comment.
While that is probably the intent I feel it's compelling me in unhealthy ways
and promotes my addiction to HN.
@greggman
greggman / mousecapture.md
Last active May 22, 2019
Mouse Capture JavaScript
View mousecapture.md

Mouse Capture

Mouse capture in this document means you want it so when the user clicks and drags on some element that element continues to receive events until the user releases the mouse button even if they drag outside of the element or even outside the window.

Solution:

  1. On element mousedown add events to document or window for mousemove and mouseup.
@greggman
greggman / GameObjectPooler.cs
Created Apr 2, 2018
Generic GameObject pool for Unity
View GameObjectPooler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
This is an attempt at a generic GameObject Pool system.
To Use:
@greggman
greggman / tween.js
Created Oct 20, 2017
Tween functions
View tween.js
/*!
* TERMS OF USE - EASING EQUATIONS
* Open source under the BSD License.
* Easing Equations (c) 2003 Robert Penner, all rights reserved.
*/
function easeInQuad(pos) {
return Math.pow(pos, 2);
}
@greggman
greggman / example.js
Created Aug 3, 2017
mod your shader in js
View example.js
function getShaderSource(needsBGRA) {
const twizzle = needsBGRA ? '.bgra' : '';
return `
...
imageColor = texture2D(u_image, v_image_coordinates)${twizzle};
...
`;
}
You can’t perform that action at this time.