Introduce Paper object that keeps track of documents and which documenti is active. Newly created items are automatically appended to the children list of the active document's activeLayer.

This commit is contained in:
Jonathan Puckey 2011-02-12 16:20:10 +01:00
parent be4681fecd
commit d7fc9cf481
23 changed files with 95 additions and 50 deletions

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -42,7 +43,6 @@
path.add(newPoint); path.add(newPoint);
} }
path.smooth(); path.smooth();
doc.activeLayer.appendTop(path);
} }
doc.activeLayer.children.reverse(); doc.activeLayer.children.reverse();
doc.redraw(); doc.redraw();

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -36,10 +37,8 @@
path.arcTo(point.add(vector), true); path.arcTo(point.add(vector), true);
} }
} }
doc.activeLayer.appendTop(path); doc.activeLayer.appendBottom(path);
} }
doc.activeLayer.children.reverse();
doc.redraw(); doc.redraw();
} }
</script> </script>

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -26,7 +27,6 @@
for(var i = 0; i < 70; i++) { for(var i = 0; i < 70; i++) {
var path = new Path.Circle(center, i * 5); var path = new Path.Circle(center, i * 5);
path.strokeColor = 'black'; path.strokeColor = 'black';
doc.activeLayer.appendTop(path);
} }
doc.redraw(); doc.redraw();
} }

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -38,7 +39,6 @@
new Segment(new Point(56.984375, 286.0546875), new Point(10.03076171875, 6.6875)) new Segment(new Point(56.984375, 286.0546875), new Point(10.03076171875, 6.6875))
); );
letterPath.fillColor = 'black'; letterPath.fillColor = 'black';
doc.activeLayer.appendTop(letterPath);
doc.redraw(); doc.redraw();
} }
</script> </script>

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -33,7 +34,6 @@
for(var i = 0; i < 180; i++) { for(var i = 0; i < 180; i++) {
var path = new Path.Line(point.add(vector.rotate(i * 2).normalize(30)), point.add(vector.rotate(i * 2).normalize(30 + Math.abs(Math.sin((i + count / 20)) * 300)))); var path = new Path.Line(point.add(vector.rotate(i * 2).normalize(30)), point.add(vector.rotate(i * 2).normalize(30 + Math.abs(Math.sin((i + count / 20)) * 300))));
path.strokeColor = i % 4 ? 'black' : 'red'; path.strokeColor = i % 4 ? 'black' : 'red';
doc.activeLayer.appendTop(path);
} }
doc.redraw(); doc.redraw();
} }

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -24,8 +25,6 @@
doc = new Doc(canvas); doc = new Doc(canvas);
var path = new Path.Rectangle([50, 50], [100, 100]); var path = new Path.Rectangle([50, 50], [100, 100]);
path.strokeColor = 'black'; path.strokeColor = 'black';
console.log(path);
doc.activeLayer.appendTop(path);
doc.redraw(); doc.redraw();
} }
</script> </script>

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -31,7 +32,6 @@
var size = Math.abs(Math.sin(count / 40)) * 150 + 10; var size = Math.abs(Math.sin(count / 40)) * 150 + 10;
var path = new Path.RoundRectangle(rect, size); var path = new Path.RoundRectangle(rect, size);
path.fillColor = 'black'; path.fillColor = 'black';
doc.activeLayer.appendTop(path);
doc.redraw(); doc.redraw();
} }
</script> </script>

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -35,9 +36,8 @@
path.add(newPoint); path.add(newPoint);
} }
path.smooth(); path.smooth();
doc.activeLayer.appendTop(path); doc.activeLayer.appendBottom(path);
} }
doc.activeLayer.children.reverse();
doc.redraw(); doc.redraw();
} }
</script> </script>

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -25,8 +26,36 @@
var path = new Path([100, 150], [150, 200], [200, 150]); var path = new Path([100, 150], [150, 200], [200, 150]);
path.strokeColor = 'black'; path.strokeColor = 'black';
path.strokeWidth = 30; path.strokeWidth = 30;
path.strokeJoin = 'round';
var path = new Path([240, 150], [290, 200], [340, 150]);
path.strokeColor = 'black';
path.strokeWidth = 30;
path.strokeJoin = 'bevel'; path.strokeJoin = 'bevel';
doc.activeLayer.appendTop(path);
var path = new Path([380, 150], [430, 200], [480, 150]);
path.strokeColor = 'black';
path.strokeWidth = 30;
path.strokeJoin = 'miter';
var path = new Path([100, 250], [150, 300], [200, 250]);
path.strokeColor = 'black';
path.strokeWidth = 30;
path.strokeJoin = 'round';
path.strokeCap = 'round';
var path = new Path([240, 250], [290, 300], [340, 250]);
path.strokeColor = 'black';
path.strokeWidth = 30;
path.strokeJoin = 'bevel';
path.strokeCap = 'square';
var path = new Path([380, 250], [430, 300], [480, 250]);
path.strokeColor = 'black';
path.strokeWidth = 30;
path.strokeJoin = 'miter';
path.strokeCap = 'butt';
doc.redraw(); doc.redraw();
} }
</script> </script>

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -23,16 +24,14 @@
window.onload = function() { window.onload = function() {
var canvas = document.getElementById('canvas'); var canvas = document.getElementById('canvas');
var doc = new Doc(canvas); var doc = new Doc(canvas);
var path;
var tool = new Tool({ var tool = new Tool({
onMouseDrag: function(event) { onMouseDrag: function(event) {
if(event.count != 1) if(path) path.remove();
doc.activeLayer.children.pop();
var distance = event.downPoint.subtract(event.point).length; var distance = event.downPoint.subtract(event.point).length;
var path = new Path.Circle(event.downPoint, distance); path = new Path.Circle(event.downPoint, distance);
path.strokeColor = 'black'; path.strokeColor = 'black';
path.fillColor = 'white'; path.fillColor = 'white';
doc.activeLayer.appendTop(path);
} }
}); });
tool.document = doc; tool.document = doc;

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -32,7 +33,6 @@
path.strokeJoin = 'round'; path.strokeJoin = 'round';
path.strokeCap = 'round'; path.strokeCap = 'round';
path.add(event.point); path.add(event.point);
doc.activeLayer.appendTop(path);
}, },
onMouseDrag: function(event) { onMouseDrag: function(event) {
path.arcTo(event.point, true); path.arcTo(event.point, true);

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -28,10 +29,6 @@
var minSize = 5; var minSize = 5;
var tool = new Tool({ var tool = new Tool({
onMouseDown: function(event) {
},
onMouseDrag: function(event) { onMouseDrag: function(event) {
// If the user dragged more then minSize: // If the user dragged more then minSize:
if (event.delta.length > minSize) { if (event.delta.length > minSize) {
@ -40,7 +37,6 @@
path = new Path(); path = new Path();
path.fillColor = 'black'; path.fillColor = 'black';
path.add(event.lastPoint); path.add(event.lastPoint);
doc.activeLayer.appendTop(path);
} }
var step = event.delta.divide(2); var step = event.delta.divide(2);

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -36,7 +37,6 @@
onMouseDown: function(event) { onMouseDown: function(event) {
path = new Path(); path = new Path();
path.fillColor = event.count % 2 ? 'red' : 'black'; path.fillColor = event.count % 2 ? 'red' : 'black';
doc.activeLayer.appendTop(path);
}, },
onMouseDrag: function(event) { onMouseDrag: function(event) {

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -39,7 +40,6 @@
path = new Path(); path = new Path();
path.strokeColor = 'black'; path.strokeColor = 'black';
path.add(point); path.add(point);
doc.activeLayer.appendTop(path);
}, },
onMouseDrag: function(event) { onMouseDrag: function(event) {
var p = getPos(event.point); var p = getPos(event.point);

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -37,7 +38,6 @@
for (var i = 0; i < values.lines; i++) { for (var i = 0; i < values.lines; i++) {
var path = new Path(); var path = new Path();
path.strokeColor = '#000000'; path.strokeColor = '#000000';
doc.activeLayer.appendTop(path);
paths.push(path); paths.push(path);
} }
}, },
@ -50,7 +50,6 @@
var path = paths[values.lines - 1 - i]; var path = paths[values.lines - 1 - i];
offset.length = lineSize * i + lineSize / 2; offset.length = lineSize * i + lineSize / 2;
path.add(event.middlePoint.add(offset)); path.add(event.middlePoint.add(offset));
//path.insert(0, event.middlePoint.subtract(offset));
path.smooth(); path.smooth();
} }
} }

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -23,7 +24,6 @@
window.onload = function() { window.onload = function() {
var canvas = document.getElementById('canvas'); var canvas = document.getElementById('canvas');
var doc = new Doc(canvas); var doc = new Doc(canvas);
var values = { var values = {
curviness: 0.5, curviness: 0.5,
distance: 10, distance: 10,
@ -37,12 +37,10 @@
onMouseDown: function(event) { onMouseDown: function(event) {
path = new Path(); path = new Path();
path.strokeColor = 'black'; path.strokeColor = 'black';
doc.activeLayer.appendTop(path);
}, },
onMouseDrag: function(event) { onMouseDrag: function(event) {
var step = event.delta.rotate(90 * mul); var step = event.delta.rotate(90 * mul);
if (!values.mouseOffset) if (!values.mouseOffset)
step.length = values.offset; step.length = values.offset;
var segment = new Segment(event.point.add(step)); var segment = new Segment(event.point.add(step));

View file

@ -5,6 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title> <title>Example</title>
<script type="text/javascript" src="../../src/Bootstrap.js"></script> <script type="text/javascript" src="../../src/Bootstrap.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/Item.js"></script> <script type="text/javascript" src="../../src/Item.js"></script>
<script type="text/javascript" src="../../src/PathItem.js"></script> <script type="text/javascript" src="../../src/PathItem.js"></script>
<script type="text/javascript" src="../../src/Point.js"></script> <script type="text/javascript" src="../../src/Point.js"></script>
@ -40,7 +41,6 @@
worm.fillColor = '#ffffff'; worm.fillColor = '#ffffff';
worm.strokeColor = '#000000'; worm.strokeColor = '#000000';
worm.add(event.point); worm.add(event.point);
doc.activeLayer.appendTop(worm);
}, },
onMouseDrag: function(event) { onMouseDrag: function(event) {
@ -77,7 +77,6 @@
// make a new line path from top to bottom // make a new line path from top to bottom
var line = new Path.Line(top, bottom); var line = new Path.Line(top, bottom);
line.strokeColor = '#000000'; line.strokeColor = '#000000';
doc.activeLayer.appendTop(line);
// smooth the segments of the path // smooth the segments of the path
worm.smooth(); worm.smooth();

View file

@ -5,9 +5,16 @@ Doc = Base.extend({
this.ctx = this.canvas.getContext('2d'); this.ctx = this.canvas.getContext('2d');
this.size = new Size(canvas.offsetWidth, canvas.offsetHeight); this.size = new Size(canvas.offsetWidth, canvas.offsetHeight);
} }
Paper.documents.push(this);
this.activate();
this.layers = [];
this.activeLayer = new Layer(); this.activeLayer = new Layer();
this.layers = [this.activeLayer];
}, },
activate: function() {
Paper.activateDocument(this);
},
redraw: function() { redraw: function() {
if(this.canvas) { if(this.canvas) {
this.ctx.clearRect(0, 0, this.size.width, this.size.height); this.ctx.clearRect(0, 0, this.size.width, this.size.height);

View file

@ -1,7 +1,8 @@
Item = Base.extend({ Item = Base.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {
this.parent = Paper.document.activeLayer;
this.parent.children.push(this);
}, },
/** /**
@ -154,6 +155,13 @@ Item = Base.extend({
this.parent.children.splice(this.index, 1); this.parent.children.splice(this.index, 1);
}, },
/**
* Removes the item.
*/
remove: function() {
this.removeFromParent();
},
/** /**
* {@grouptitle Hierarchy Operations} * {@grouptitle Hierarchy Operations}
* *
@ -172,8 +180,10 @@ Item = Base.extend({
* @param item The item that will be appended as a child * @param item The item that will be appended as a child
*/ */
appendTop: function(item) { appendTop: function(item) {
item.removeFromParent();
this.children.push(item); this.children.push(item);
item.parent = this; item.parent = this;
item.document = this.document;
}, },
/** /**
@ -192,8 +202,10 @@ Item = Base.extend({
* @param item The item that will be appended as a child * @param item The item that will be appended as a child
*/ */
appendBottom: function(item) { appendBottom: function(item) {
item.removeFromParent();
this.children.splice(0, 0, item); this.children.splice(0, 0, item);
item.parent = this; item.parent = this;
item.document = this.document;
}, },
/** /**
@ -225,6 +237,7 @@ Item = Base.extend({
this.removeFromParent(); this.removeFromParent();
item.parent.children.splice(item.index + 1, 0, this); item.parent.children.splice(item.index + 1, 0, this);
this.parent = item.parent; this.parent = item.parent;
this.document = item.document;
return true; return true;
}, },
@ -248,6 +261,7 @@ Item = Base.extend({
this.removeFromParent(); this.removeFromParent();
item.parent.children.splice(item.index - 1, 0, this); item.parent.children.splice(item.index - 1, 0, this);
this.parent = item.parent; this.parent = item.parent;
this.document = item.document;
return true; return true;
}, },

View file

@ -1,7 +1,13 @@
Layer = Item.extend({ Layer = Item.extend({
initialize: function() { initialize: function() {
this.base();
this.children = []; this.children = [];
this.document = this.parent = Paper.document;
this.document.layers.push(this);
this.activate();
},
activate: function() {
this.document.activeLayer = this;
}, },
draw: function(ctx) { draw: function(ctx) {

10
src/Paper.js Normal file
View file

@ -0,0 +1,10 @@
Paper = Base.extend({
statics: {
documents: [],
activateDocument: function(doc) {
var index = this.documents.indexOf(doc);
if(index != -1)
this.document = this.documents[index];
}
}
});

View file

@ -5,6 +5,7 @@
<link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen"> <link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen">
<!-- Libraries --> <!-- Libraries -->
<script type="text/javascript" src="../src/Bootstrap.js"></script> <script type="text/javascript" src="../src/Bootstrap.js"></script>
<script type="text/javascript" src="../src/Paper.js"></script>
<script type="text/javascript" src="../src/Item.js"></script> <script type="text/javascript" src="../src/Item.js"></script>
<script type="text/javascript" src="../src/PathItem.js"></script> <script type="text/javascript" src="../src/PathItem.js"></script>
<script type="text/javascript" src="../src/Point.js"></script> <script type="text/javascript" src="../src/Point.js"></script>

View file

@ -3,8 +3,8 @@ module('Item');
test('appendChild(item)', function() { test('appendChild(item)', function() {
var doc = new Doc(); var doc = new Doc();
var path = new Path(); var path = new Path();
// doc.activeLayer.appendChild(path); doc.activeLayer.appendChild(path);
// equals(doc.activeLayer.children.length, 1); equals(doc.activeLayer.children.length, 1);
}); });
test('item.parent / item.isChild', function() { test('item.parent / item.isChild', function() {
@ -16,7 +16,7 @@ test('item.parent / item.isChild', function() {
secondDoc.activeLayer.appendTop(path); secondDoc.activeLayer.appendTop(path);
equals(doc.activeLayer.isChild(path), false); equals(doc.activeLayer.isChild(path), false);
equals(secondDoc.activeLayer.isChild(path), true); equals(secondDoc.activeLayer.isChild(path), true);
equals(doc.activeLayer.children.indexOf(path) != -1, true); equals(doc.activeLayer.children.indexOf(path) == -1, true);
equals(secondDoc.activeLayer.children.indexOf(path) == 0, true); equals(secondDoc.activeLayer.children.indexOf(path) == 0, true);
}); });
@ -24,8 +24,6 @@ test('item.lastChild / item.firstChild', function() {
var doc = new Doc(); var doc = new Doc();
var path = new Path(); var path = new Path();
var secondPath = new Path(); var secondPath = new Path();
doc.activeLayer.appendTop(path);
doc.activeLayer.appendTop(secondPath);
equals(doc.activeLayer.firstChild == path, true); equals(doc.activeLayer.firstChild == path, true);
equals(doc.activeLayer.lastChild == secondPath, true); equals(doc.activeLayer.lastChild == secondPath, true);
}); });
@ -34,7 +32,6 @@ test('appendBottom(item)', function() {
var doc = new Doc(); var doc = new Doc();
var path = new Path(); var path = new Path();
var secondPath = new Path(); var secondPath = new Path();
doc.activeLayer.appendChild(path);
doc.activeLayer.appendBottom(secondPath); doc.activeLayer.appendBottom(secondPath);
equals(secondPath.index < path.index, true); equals(secondPath.index < path.index, true);
}); });
@ -43,8 +40,6 @@ test('moveAbove(item)', function() {
var doc = new Doc(); var doc = new Doc();
var path = new Path(); var path = new Path();
var secondPath = new Path(); var secondPath = new Path();
doc.activeLayer.appendChild(path);
doc.activeLayer.appendChild(secondPath);
path.moveAbove(secondPath); path.moveAbove(secondPath);
equals(doc.activeLayer.lastChild == path, true); equals(doc.activeLayer.lastChild == path, true);
}); });
@ -53,8 +48,6 @@ test('moveBelow(item)', function() {
var doc = new Doc(); var doc = new Doc();
var firstPath = new Path(); var firstPath = new Path();
var secondPath = new Path(); var secondPath = new Path();
doc.activeLayer.appendChild(firstPath);
doc.activeLayer.appendChild(secondPath);
equals(secondPath.index > firstPath.index, true); equals(secondPath.index > firstPath.index, true);
secondPath.moveBelow(firstPath); secondPath.moveBelow(firstPath);
equals(secondPath.index < firstPath.index, true); equals(secondPath.index < firstPath.index, true);
@ -63,7 +56,6 @@ test('moveBelow(item)', function() {
test('isDescendant(item)', function() { test('isDescendant(item)', function() {
var doc = new Doc(); var doc = new Doc();
var path = new Path(); var path = new Path();
doc.activeLayer.appendChild(firstPath);
equals(path.isDescendant(doc.activeLayer), true); equals(path.isDescendant(doc.activeLayer), true);
equals(doc.activeLayer.isDescendant(path), false); equals(doc.activeLayer.isDescendant(path), false);
}); });
@ -72,8 +64,6 @@ test('getPreviousSibling() / getNextSibling()', function() {
var doc = new Doc(); var doc = new Doc();
var firstPath = new Path(); var firstPath = new Path();
var secondPath = new Path(); var secondPath = new Path();
doc.activeLayer.appendTop(firstPath);
doc.activeLayer.appendTop(secondPath);
equals(firstPath.nextSibling == secondPath, true); equals(firstPath.nextSibling == secondPath, true);
equals(secondPath.previousSibling == firstPath, true); equals(secondPath.previousSibling == firstPath, true);
equals(secondPath.nextSibling == null, true); equals(secondPath.nextSibling == null, true);
@ -82,7 +72,6 @@ test('getPreviousSibling() / getNextSibling()', function() {
test('hidden', function() { test('hidden', function() {
var doc = new Doc(); var doc = new Doc();
var firstPath = new Path(); var firstPath = new Path();
doc.activeLayer.appendTop(firstPath);
firstPath.visible = false; firstPath.visible = false;
equals(firstPath.hidden, true); equals(firstPath.hidden, true);
}); });