Skip to content

Instantly share code, notes, and snippets.

@very
Last active December 20, 2015 10:59
Show Gist options
  • Save very/6119816 to your computer and use it in GitHub Desktop.
Save very/6119816 to your computer and use it in GitHub Desktop.
var behaviours = {
select: {
onstart: function(p) {
this.from = alignPos(this, p);
this.workspace.viewer.unselect();
},
onresume: function(p) {
if (this.from) {
this.mousemoved = true;
updateSelection(this, p);
}
},
onfinish: function(p) {
if (this.from) {
if (this.mousemoved) {
updateSelection(this, p);
} else {
this.workspace.unselect();
}
delete this.mousemoved;
delete this.from;
}
}
}
};
function alignPos(ctx, p) {
var gridSize = ctx.workspace.viewer.gridSize;
return {
x: p.x - (p.x % gridSize),
y: p.y - (p.y % gridSize)
};
}
function updateSelection(ctx, p) {
var viewer = ctx.workspace.viewer;
var gridSize = viewer.gridSize;
var compo = viewer.compo;
var to = alignPos(ctx, p);
if (from.x <= to.x) {
to.x += gridSize;
}
if (to.x < 0) {
to.x = 0;
} else if (compo.width < to.x) {
to.x = compo.width - compo.width % gridSize;
}
if (from.y <= to.y) {
to.y += gridSize;
}
if (to.y < 0) {
to.y = 0;
} else if (compo.height < to.y) {
to.y = compo.height - compo.height % gridSize;
}
viewer.select(from.x, from.y, to.x, to.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment