Skip to content

Instantly share code, notes, and snippets.

View william-silversmith's full-sized avatar

William Silversmith william-silversmith

View GitHub Profile
@william-silversmith
william-silversmith / animation.js
Last active October 26, 2021 11:49
Animating an Object
/* Example animation function
*
* Interpolate between a start and end position.
*
* obj.x represents a position parameter (e.g. 12.2)
* end_pos is the value obj.x will have at the end of the animation
* msec is the number of milliseconds we want to run the animation for
* easing is a timing function that accepts a number between 0 to 1
* and returns the proportion of the interpolation between start and end to move the object to.
*
@william-silversmith
william-silversmith / overshootfactory.js
Last active December 21, 2015 06:05
Overshoot Factory
function clamp (x, min, max) {
return Math.min(Math.max(x, min), max);
}
function springFactory (zeta, k) {
if (zeta < 0 || zeta >= 1) {
throw new Error("Parameter 1 (zeta) must be in range [0, 1). Given: " + zeta);
}
if (Math.floor(k) !== k) {
var argv = require('yargs').argv,
gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
stylus = require('gulp-stylus'),
include = require('gulp-include'),
minifyCss = require('gulp-minify-css'),
autoprefixer = require('gulp-autoprefixer'),
rsync = require('gulp-rsync'),
print = require('gulp-print')
@william-silversmith
william-silversmith / webm-alpha-detect.js
Last active September 12, 2015 00:21
Detect Browser Support for Video Alpha Channels
// Requires jQuery.Deferred
function detectVideoAlphaSupport () {
var _this = this;
if (this.state.webm_alpha_support !== undefined) {
return $.Deferred().resolve(this.state.webm_alpha_support).promise();
}
var context = $('<canvas>')[0].getContext('2d');
@william-silversmith
william-silversmith / bouncefactory.js
Last active October 26, 2021 12:11
The Bounce Factory
/* bounceFactory
*
* Consult this article: https://medium.com/@willsilversmith/the-bounce-factory-3498de1e5262#.pn5rcjp15
* The variables below are annotate with comments that reference the article.
*
* Simulate a physical bouncing motion based on physics equations of motion.
*
* We assume mass and gravity = 1 as they are immaterial when we normalize both
* the y and t axis to 1. The length of the animation in msec will determine "gravity"
* and the elasticity will determine the number of bounces.
@william-silversmith
william-silversmith / sigmoidfactory.js
Last active October 26, 2021 12:15
An ease-in-out curve that has customizable steepness.
/* sigmoidFactory
*
* Generate an ease-in-out function with desired steepness.
* Accompanying article: https://medium.com/analytic-animations/ease-in-out-the-sigmoid-factory-c5116d8abce9
*
* Required:
* k: (float != 0), sharpness of ease
*
* Return: f(t), t in 0..1
*/
@william-silversmith
william-silversmith / springfactory.js
Last active August 25, 2023 12:24
Spring Factory
/* springFactory
*
* Generate a physically realistic easing curve for a damped mass-spring system.
*
* Required:
* damping (zeta): [0, 1)
* halfcycles: 0...inf
*
* Optional:
* initial_position: -1..1, default 1
@william-silversmith
william-silversmith / datacube.es6
Last active January 8, 2016 21:56
3D Image Data Cube Implementation Draft
// Volume needs to lease the data cube
class Volume {
constructor (args) {
this.channel_id = args.channel_id; // volume id as corresponding to the data server
this.segmentation_id = args.segmentation_id;
this.bounds = args.bounds;
@william-silversmith
william-silversmith / easeoutfactory.js
Last active June 29, 2017 15:20
Produces ease out curves using half of a sigmoid.
/* easeOutFactory
*
* Generate an ease-out function with desired steepness.
* Article: https://medium.com/analytic-animations/ease-out-the-half-sigmoid-7240df433d98#.yupto8l43
*
* Note: Values below 6 may not come to a smooth stop.
*
* Required:
* k: (float), sharpness of ease
*
@william-silversmith
william-silversmith / sqrt.styl
Last active May 31, 2016 05:05
Square Root function for Stylus
// Note: I found out stylus has math(x, 'sqrt') after the fact
sqrt(y)
if y < 0
error("Unable to process complex roots.")
if y == 0 // for floating point precision
return 0
else if y == 1 // for floating point precision
return 1