Skip to content

Instantly share code, notes, and snippets.

@seb-thomas
Created June 6, 2012 17:04
Show Gist options
  • Save seb-thomas/2883317 to your computer and use it in GitHub Desktop.
Save seb-thomas/2883317 to your computer and use it in GitHub Desktop.
Code to generate a path object for https://github.com/JoelBesada/scrollpath from an array, using coords in px relational to current xy
//Code to generate a path object from an array, using coords in px relational to current xy
function do_with_args(func, args1, args2){
args = jQuery.merge(args1, args2);
console.log(func, args)
return func.apply(null, args);
}
var OPERATIONS = {
lineTo:
function (path, xy, dx, dy){
var x=xy[0], y=xy[1];
var newx = x+dx;
newy = y+dy;
path.lineTo(newx, newy);
return [newx, newy];
},
arc:
function (path, xy, radius, rstart, rend, dir){
var x=xy[0], y=xy[1],
radians = rend - rstart;
var chord_len = radius * Math.sin(radians/2),
chord_angle = ((rstart - rend)/2) + (Math.PI/2);
var dx = chord_len * Math.sin(chord_angle),
dy = chord_len * Math.cos(chord_angle);
var newx = x+dx,
newy = y+dy;
path.arc(newx, newy, radius, rstart, rend, dir)
return [newx, newy];
}
};
var START = [0, 0];
operations = [
{method:'lineTo', args:[400, 0]},
{method:'arc', args:[400, -Math.PI/2, Math.PI/2, false]}
];
CURRENT_POSITION = START;
var PATH = $.fn.scrollPath("getPath");
jQuery.each(operations, function(i, el){
var method = el.method;
var args = el.args;
func = OPERATIONS[method];
console.log([PATH, CURRENT_POSITION], args);
CURRENT_POSITION = do_with_args(func, [PATH, CURRENT_POSITION], args);
})
// todo: use a vector class like this
function Vec2(x, y){
// v = new Vec2(100, 200)
this.x = x;
this.y = y;
}
function V(x, y){
// v = V(100, 200)
// no "new" required
return new Vec2(x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment