|
15 | 15 | // Make the raster invisible: |
16 | 16 | raster.visible = false; |
17 | 17 |
|
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; |
20 | 23 |
|
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; |
24 | 26 |
|
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: |
30 | 29 |
|
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]; |
34 | 31 |
|
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); |
58 | 38 |
|
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); |
61 | 47 |
|
62 | | - // Avoid continuing looping through the rest of the items: |
63 | | - return; |
64 | | - } |
65 | | - } |
| 48 | + this.remove(); |
66 | 49 | } |
67 | 50 |
|
68 | 51 | function onResize(event) { |
69 | | - layer.removeChildren(); |
| 52 | + project.activeLayer.removeChildren(); |
70 | 53 |
|
71 | 54 | // Transform the raster so that it fills the bounding rectangle |
72 | 55 | // of the view: |
73 | 56 | raster.fitBounds(view.bounds, true); |
74 | 57 |
|
75 | 58 | // Create a path that fills the view, and fill it with |
76 | 59 | // 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 | + }); |
79 | 65 | } |
80 | 66 | </script> |
81 | 67 | </head> |
|
0 commit comments