Skip to content

Commit ee141ca

Browse files
committed
Improve Division Raster example.
1 parent c9e95d2 commit ee141ca

1 file changed

Lines changed: 31 additions & 45 deletions

File tree

examples/Paperjs.org/DivisionRaster.html

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,53 @@
1515
// Make the raster invisible:
1616
raster.visible = false;
1717

18-
// Create a seperate layer that the paths will be placed on:
19-
var layer = new Layer();
18+
var lastPos = view.center;
19+
function moveHandler(event) {
20+
if (lastPos.getDistance(event.point) < 10)
21+
return;
22+
lastPos = event.point;
2023

21-
// The mouse has to drag at least 5pt until the next onMouseDrag
22-
// event is fired:
23-
tool.minDistance = 5;
24+
var size = this.bounds.size.clone();
25+
var isLandscape = size.width > size.height;
2426

25-
function onMouseMove(event) {
26-
// Each drag event, iterate through the children of group:
27-
for (var i = 0; i < layer.children.length; i++) {
28-
var child = layer.children[i];
29-
var bounds = child.bounds;
27+
// If the path is in landscape orientation, we're going to
28+
// split the path horizontally, otherwise vertically:
3029

31-
if (bounds.contains(event.point)) {
32-
// If the mouse position intersects with the bounding
33-
// box of the path, we're going to split it into two paths:
30+
size /= isLandscape ? [2, 1] : [1, 2];
3431

35-
var size = bounds.size;
36-
var isLandscape = size.width > size.height;
37-
38-
// If the path is in landscape orientation, we're going to
39-
// split the path horizontally, otherwise vertically:
40-
if (isLandscape) {
41-
size.width /= 2;
42-
} else {
43-
size.height /= 2;
44-
}
45-
46-
var topLeft = bounds.topLeft.floor();
47-
var path = new Path.Rectangle(topLeft, size.ceil());
48-
path.fillColor = raster.getAverageColor(path);
49-
path.insertBelow(child);
50-
51-
var secondPath = path.clone();
52-
size = size.floor();
53-
secondPath.position += isLandscape
54-
? [size.width, 0]
55-
: [0, size.height];
56-
secondPath.fillColor = raster.getAverageColor(secondPath);
57-
secondPath.insertBelow(path);
32+
var path = new Path.Rectangle({
33+
point: this.bounds.topLeft.floor(),
34+
size: size.ceil(),
35+
onMouseMove: moveHandler
36+
});
37+
path.fillColor = raster.getAverageColor(path);
5838

59-
// Remove the path which was split:
60-
child.remove();
39+
var path = new Path.Rectangle({
40+
point: isLandscape
41+
? this.bounds.topCenter.ceil()
42+
: this.bounds.leftCenter.ceil(),
43+
size: size.floor(),
44+
onMouseMove: moveHandler
45+
});
46+
path.fillColor = raster.getAverageColor(path);
6147

62-
// Avoid continuing looping through the rest of the items:
63-
return;
64-
}
65-
}
48+
this.remove();
6649
}
6750

6851
function onResize(event) {
69-
layer.removeChildren();
52+
project.activeLayer.removeChildren();
7053

7154
// Transform the raster so that it fills the bounding rectangle
7255
// of the view:
7356
raster.fitBounds(view.bounds, true);
7457

7558
// Create a path that fills the view, and fill it with
7659
// the average color of the raster:
77-
var path = new Path.Rectangle(view.bounds);
78-
path.fillColor = raster.getAverageColor(path);
60+
new Path.Rectangle({
61+
rectangle: view.bounds,
62+
fillColor: raster.getAverageColor(view.bounds),
63+
onMouseMove: moveHandler
64+
});
7965
}
8066
</script>
8167
</head>

0 commit comments

Comments
 (0)