Skip to content

Instantly share code, notes, and snippets.

@whistlegraph
Created July 7, 2016 05:01
Show Gist options
  • Save whistlegraph/7729d009e605ffdc41cc180bcd56ba8a to your computer and use it in GitHub Desktop.
Save whistlegraph/7729d009e605ffdc41cc180bcd56ba8a to your computer and use it in GitHub Desktop.
def('surf.graph', function (act, viewport, size) {
'use strict';
var vec2 = use('lib.vec2'),
t = use('lib.transform')({
size: size,
anchor: vec2.scal(viewport.win, 0.5)
}),
worms = {},
worms_do = function (fun) {
Object.keys(worms).forEach(function (k) { fun(worms[k]); });
},
store = {};
act.on('surf.graph.untouch', function () { worms = {}; });
act.on('surf.graph.touch', function (touch) {
worms[touch.p.print] = use('lib.worm')(t.transform_v(touch.p.paw)).shrink();
if (touch.and) {
worms[touch.p.print].baby = false; // allows for 'drag only' drawing tools
touch.and({
p: t.localize_v(touch.p.paw),
id: touch.p.print,
surf_transform: t.get(),
size: viewport.can,
ratio: viewport.ratio
});
}
});
act.on('surf.graph.scratch', function (s) {
if (!s) { return; }
var w = worms[s.p.print];
if (!w) {
worms[s.p.print] = use('lib.worm')(t.transform_v(s.p.paw)).shrink();
w = worms[s.p.print];
}
if (w.baby) {
w.head = t.transform_v(s.p.paw);
w.baby = false;
} else {
w.grow(t.transform_v(s.p.paw));
}
s.and({
worm: use('lib.worm')(t.wrap_v(w.head), w.tail),
id: s.p.print,
surf_transform: t.get(),
size: viewport.can,
ratio: viewport.ratio
});
});
act.on('surf.graph.lift', function (p) {
delete worms[p.print];
//console.log('lift surf: ', t.localize_v(p.paw), p.print);
});
act.on('surf.graph.add', function () {
act.out('surf.renderer.predraw', t.get());
});
act.on('surf.graph.move', function (p) {
var reset = t.translate(t.rot_v(p));
worms_do(function (w) { w.head = vec2.sub(w.head, reset); });
act.out('surf.renderer.soil');
});
act.on('surf.graph.rotate', function (d) {
t.rotate(d);
act.out('surf.renderer.soil');
});
act.on('surf.graph.anchor', function (v) {
var offset = t.anchor(vec2.mul(v, viewport.win));
worms_do(function (w) { w.head = vec2.sub(w.head, offset); });
act.out('surf.renderer.soil');
});
act.on('surf.graph.scale', function (n) {
if (t.scale(n)) { worms_do(function (w) { w.baby = true; w.shrink(); }); }
act.out('surf.renderer.scaled', t.get());
act.out('surf.renderer.soil');
});
act.on('surf.graph.scale_base_set', function (n) {
t.scale_base_set(n);
act.out('surf.renderer.soil');
});
act.on('resize viewport (fixed)', function (vp) {
var rat = vec2.div(vp.new, size);
act.out('surf.graph.scale_base_set', rat[0]);
act.out('surf.graph.anchor', [0.5, 0.5]);
act.out(
'surf.graph.move',
vec2.mul(vec2.sub(vp.old, vp.new), vec2.div([0.5, 0.5], t.get().scale))
);
});
act.on('surf.graph.store_offset', function () {
store.offset = t.get().offset;
});
act.on('surf.graph.store_angle', function () {
store.angle = t.get().angle;
});
act.on('surf.graph.clear_offset', function () { delete store.offset; });
act.on('surf.graph.clear_angle', function () { delete store.angle; });
act.on('surf.graph.load_offset', function () {
if (store.offset) { t.set_offset(store.offset); }
});
act.on('surf.graph.load_angle', function () {
if (store.angle !== undefined) { t.set_angle(store.angle); }
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment