mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-07-09 19:24:00 -04:00
Make JSON serialization work for Shapes.
This commit is contained in:
parent
407a7fcde7
commit
7278ee4374
2 changed files with 82 additions and 25 deletions
39
examples/JSON/Shapes.html
Normal file
39
examples/JSON/Shapes.html
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<!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 ellipse = new Shape.Ellipse({
|
||||||
|
from: [10, 10],
|
||||||
|
to: [200, 100],
|
||||||
|
fillColor: 'red'
|
||||||
|
});
|
||||||
|
|
||||||
|
var circle = new Shape.Circle({
|
||||||
|
center: [50, 150],
|
||||||
|
radius: 25,
|
||||||
|
fillColor: 'blue'
|
||||||
|
});
|
||||||
|
|
||||||
|
var rectangle = new Shape.Rectangle({
|
||||||
|
from: [25, 200],
|
||||||
|
to: [100, 225],
|
||||||
|
fillColor: 'green'
|
||||||
|
});
|
||||||
|
rectangle.rotate(30);
|
||||||
|
|
||||||
|
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>
|
|
@ -21,14 +21,14 @@ var Shape = Item.extend(/** @lends Shape# */{
|
||||||
_class: 'Shape',
|
_class: 'Shape',
|
||||||
_transformContent: false,
|
_transformContent: false,
|
||||||
_boundsSelected: true,
|
_boundsSelected: true,
|
||||||
|
_serializeFields: {
|
||||||
|
shape: null,
|
||||||
|
size: null,
|
||||||
|
radius: null
|
||||||
|
},
|
||||||
|
|
||||||
// TODO: serialization
|
initialize: function Shape(props) {
|
||||||
|
this._initialize(props);
|
||||||
initialize: function Shape(shape, center, size, radius, props) {
|
|
||||||
this._shape = shape;
|
|
||||||
this._size = size;
|
|
||||||
this._radius = radius;
|
|
||||||
this._initialize(props, center);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
_equals: function(item) {
|
_equals: function(item) {
|
||||||
|
@ -39,10 +39,12 @@ var Shape = Item.extend(/** @lends Shape# */{
|
||||||
},
|
},
|
||||||
|
|
||||||
clone: function(insert) {
|
clone: function(insert) {
|
||||||
return this._clone(new Shape(this._shape, this.getPosition(true),
|
return this._clone(new Shape({
|
||||||
this._size.clone(),
|
shape: this._shape,
|
||||||
this._radius.clone ? this._radius.clone() : this._radius,
|
size: this._size,
|
||||||
{ insert: false }), insert);
|
radius: this._radius,
|
||||||
|
insert: false
|
||||||
|
}), insert);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,6 +57,10 @@ var Shape = Item.extend(/** @lends Shape# */{
|
||||||
return this._shape;
|
return this._shape;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
setShape: function(shape) {
|
||||||
|
this._shape = shape;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The size of the shape.
|
* The size of the shape.
|
||||||
*
|
*
|
||||||
|
@ -67,10 +73,13 @@ var Shape = Item.extend(/** @lends Shape# */{
|
||||||
},
|
},
|
||||||
|
|
||||||
setSize: function(/* size */) {
|
setSize: function(/* size */) {
|
||||||
var shape = this._shape,
|
var size = Size.read(arguments);
|
||||||
size = Size.read(arguments);
|
if (!this._size) {
|
||||||
if (!this._size.equals(size)) {
|
// First time, e.g. whean reading from JSON...
|
||||||
var width = size.width,
|
this._size = size.clone();
|
||||||
|
} else if (!this._size.equals(size)) {
|
||||||
|
var shape = this._shape,
|
||||||
|
width = size.width,
|
||||||
height = size.height;
|
height = size.height;
|
||||||
if (shape === 'rectangle') {
|
if (shape === 'rectangle') {
|
||||||
// Shrink radius accordingly
|
// Shrink radius accordingly
|
||||||
|
@ -114,15 +123,20 @@ var Shape = Item.extend(/** @lends Shape# */{
|
||||||
this._size.set(size, size);
|
this._size.set(size, size);
|
||||||
} else {
|
} else {
|
||||||
radius = Size.read(arguments);
|
radius = Size.read(arguments);
|
||||||
if (this._radius.equals(radius))
|
if (!this._radius) {
|
||||||
return;
|
// First time, e.g. whean reading from JSON...
|
||||||
this._radius.set(radius.width, radius.height);
|
this._radius = radius.clone();
|
||||||
if (shape === 'rectangle') {
|
} else {
|
||||||
// Grow size accordingly
|
if (this._radius.equals(radius))
|
||||||
var size = Size.max(this._size, radius.multiply(2));
|
return;
|
||||||
this._size.set(size.width, size.height);
|
this._radius.set(radius.width, radius.height);
|
||||||
} else if (shape === 'ellipse') {
|
if (shape === 'rectangle') {
|
||||||
this._size.set(radius.width * 2, radius.height * 2);
|
// Grow size accordingly
|
||||||
|
var size = Size.max(this._size, radius.multiply(2));
|
||||||
|
this._size.set(size.width, size.height);
|
||||||
|
} else if (shape === 'ellipse') {
|
||||||
|
this._size.set(radius.width * 2, radius.height * 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._changed(/*#=*/ Change.GEOMETRY);
|
this._changed(/*#=*/ Change.GEOMETRY);
|
||||||
|
@ -310,7 +324,11 @@ new function() { // Scope for _contains() and _hitTest() code.
|
||||||
// Mess with indentation in order to get more line-space below:
|
// Mess with indentation in order to get more line-space below:
|
||||||
statics: new function() {
|
statics: new function() {
|
||||||
function createShape(shape, point, size, radius, args) {
|
function createShape(shape, point, size, radius, args) {
|
||||||
return new Shape(shape, point, size, radius, Base.getNamed(args));
|
var item = new Shape(Base.getNamed(args));
|
||||||
|
item._shape = shape;
|
||||||
|
item._size = size;
|
||||||
|
item._radius = radius;
|
||||||
|
return item.translate(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
return /** @lends Shape */{
|
return /** @lends Shape */{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue