Implement serialization of selection.

This commit is contained in:
Jürg Lehni 2013-10-17 12:04:11 +02:00
parent 3958d35f28
commit 9976033655
4 changed files with 54 additions and 2 deletions

View file

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Symbols</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper.js"></script>
<script type="text/paperscript" canvas="canvas1">
var path = new Path.Ellipse({
from: [10, 10],
to: [200, 100],
fillColor: 'red'
});
path.firstSegment.handleOut.selected = true;
var circle = new Path.Circle({
center: [50, 150],
radius: 25,
fillColor: 'blue'
});
circle.selected = true;
var rectangle = new Path.Rectangle({
from: [25, 200],
to: [100, 225],
fillColor: 'green'
});
var group = new Group(circle, rectangle);
window._json = project.exportJSON();
console.log(window._json);
</script>
<script type="text/paperscript" canvas="canvas2">
project.importJSON(window._json);
</script>
</head>
<body>
<canvas id="canvas1" width="500" height="500"></canvas>
<canvas id="canvas2" width="500" height="500"></canvas>
</body>
</html>

View file

@ -234,8 +234,7 @@ var Point = Base.extend(/** @lends Point# */{
var f = options.formatter;
// For speed reasons, we directly call formatter.number() here, instead
// of converting array through Base.serialize() which makes a copy.
return [f.number(this.x),
f.number(this.y)];
return [f.number(this.x), f.number(this.y)];
},
/**

View file

@ -57,6 +57,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
blendMode: 'normal',
opacity: 1,
guide: false,
selected: false,
clipMask: false,
data: {}
},

View file

@ -24,6 +24,7 @@ var SegmentPoint = Point.extend({
x = y = 0;
} else if ((x = point[0]) !== undefined) { // Array-like
y = point[1];
selected = point[2]; // See #_serialize()
} else {
// If not Point-like already, read Point from arguments
if ((x = point.x) === undefined) {
@ -47,6 +48,15 @@ var SegmentPoint = Point.extend({
return this;
},
_serialize: function(options) {
var f = options.formatter,
values = [f.number(this._x), f.number(this._y)];
// Included the selected state of the segment point
if (this.isSelected())
values.push(true);
return values;
},
getX: function() {
return this._x;
},