Skip to content

Instantly share code, notes, and snippets.

@very
Created July 31, 2013 06:32
Show Gist options
  • Save very/6119815 to your computer and use it in GitHub Desktop.
Save very/6119815 to your computer and use it in GitHub Desktop.
var behaviours = {
select: {
onstart: function(p) {
var viewer = this.workspace.viewer;
this.from = {
x: p.x - (p.x % viewer.gridSize),
y: p.y - (p.y % viewer.gridSize)
};
viewer.unselect();
},
onresume: function(p) {
var from = this.from;
if (from) {
var viewer = this.workspace.viewer;
var compo = viewer.compo;
this.mousemoved = true;
var to = {
x: p.x - (p.x % viewer.gridSize),
y: p.y - (p.y % viewer.gridSize)
};
if (from.x <= to.x) {
to.x += viewer.gridSize;
}
if (to.x < 0) {
to.x = 0;
} else if (compo.width < to.x) {
to.x = compo.width - compo.width % viewer.gridSize;
}
if (from.y <= to.y) {
to.y += viewer.gridSize;
}
if (to.y < 0) {
to.y = 0;
} else if (compo.height < to.y) {
to.y = compo.height - compo.height % viewer.gridSize;
}
viewer.select(from.x, from.y, to.x, to.y);
}
},
onfinish: function(p) {
var from = this.from;
if (from) {
var viewer = this.workspace.viewer;
if (this.mousemoved) {
var compo = viewer.compo;
var to = {
x: p.x - (p.x % viewer.gridSize),
y: p.y - (p.y % viewer.gridSize)
};
if (from.x <= to.x) {
to.x += viewer.gridSize;
}
if (to.x < 0) {
to.x = 0;
} else if (compo.width < to.x) {
to.x = compo.width - compo.width % viewer.gridSize;
}
if (from.y <= to.y) {
to.y += viewer.gridSize;
}
if (to.y < 0) {
to.y = 0;
} else if (compo.height < to.y) {
to.y = compo.height - compo.height % viewer.gridSize;
}
viewer.select(from.x, from.y, to.x, to.y);
} else {
viewer.unselect();
}
delete this.mousemoved;
delete this.from;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment