Improve Division Raster example.

This commit is contained in:
Jonathan Puckey 2013-03-09 18:51:04 +01:00
parent c9e95d2c01
commit ee141ca98d

View file

@ -15,58 +15,41 @@ var raster = new Raster('mona');
// Make the raster invisible: // Make the raster invisible:
raster.visible = false; raster.visible = false;
// Create a seperate layer that the paths will be placed on: var lastPos = view.center;
var layer = new Layer(); function moveHandler(event) {
if (lastPos.getDistance(event.point) < 10)
return;
lastPos = event.point;
// The mouse has to drag at least 5pt until the next onMouseDrag var size = this.bounds.size.clone();
// event is fired: var isLandscape = size.width > size.height;
tool.minDistance = 5;
function onMouseMove(event) { // If the path is in landscape orientation, we're going to
// Each drag event, iterate through the children of group: // split the path horizontally, otherwise vertically:
for (var i = 0; i < layer.children.length; i++) {
var child = layer.children[i];
var bounds = child.bounds;
if (bounds.contains(event.point)) { size /= isLandscape ? [2, 1] : [1, 2];
// If the mouse position intersects with the bounding
// box of the path, we're going to split it into two paths:
var size = bounds.size; var path = new Path.Rectangle({
var isLandscape = size.width > size.height; point: this.bounds.topLeft.floor(),
size: size.ceil(),
onMouseMove: moveHandler
});
path.fillColor = raster.getAverageColor(path);
// If the path is in landscape orientation, we're going to var path = new Path.Rectangle({
// split the path horizontally, otherwise vertically: point: isLandscape
if (isLandscape) { ? this.bounds.topCenter.ceil()
size.width /= 2; : this.bounds.leftCenter.ceil(),
} else { size: size.floor(),
size.height /= 2; onMouseMove: moveHandler
} });
path.fillColor = raster.getAverageColor(path);
var topLeft = bounds.topLeft.floor(); this.remove();
var path = new Path.Rectangle(topLeft, size.ceil());
path.fillColor = raster.getAverageColor(path);
path.insertBelow(child);
var secondPath = path.clone();
size = size.floor();
secondPath.position += isLandscape
? [size.width, 0]
: [0, size.height];
secondPath.fillColor = raster.getAverageColor(secondPath);
secondPath.insertBelow(path);
// Remove the path which was split:
child.remove();
// Avoid continuing looping through the rest of the items:
return;
}
}
} }
function onResize(event) { function onResize(event) {
layer.removeChildren(); project.activeLayer.removeChildren();
// Transform the raster so that it fills the bounding rectangle // Transform the raster so that it fills the bounding rectangle
// of the view: // of the view:
@ -74,8 +57,11 @@ function onResize(event) {
// Create a path that fills the view, and fill it with // Create a path that fills the view, and fill it with
// the average color of the raster: // the average color of the raster:
var path = new Path.Rectangle(view.bounds); new Path.Rectangle({
path.fillColor = raster.getAverageColor(path); rectangle: view.bounds,
fillColor: raster.getAverageColor(view.bounds),
onMouseMove: moveHandler
});
} }
</script> </script>
</head> </head>