forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.js
More file actions
211 lines (184 loc) · 5.29 KB
/
Copy pathJSON.js
File metadata and controls
211 lines (184 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
module('JSON');
function testExportImportJSON(project) {
// Use higher precision than in comparissons, for bounds
var json = project.exportJSON({ precision: 8 });
var project2 = new Project();
project2.clear();
project2.importJSON(json);
compareProjects(project2, project);
}
test('Circles', function() {
var topLeft = new Point(200, 200);
var size = new Size(150, 100);
var rectangle = new Rectangle(topLeft, size);
var path = new Path.Ellipse(rectangle);
path.fillColor = 'black';
console.log('JSON', path.exportJSON());
var topLeft = new Point(5, 400);
var size = new Size(100, 50);
var rectangle = new Rectangle(topLeft, size);
var path = new Path.Ellipse(rectangle);
path.fillColor = 'yellow';
var path = new Path.Circle(new Point(50, 50), 25);
path.fillColor = 'red';
testExportImportJSON(paper.project);
});
test('CompoundPath', function() {
paper.project.currentStyle.fillColor = 'black';
var path1 = new Path.Rectangle([200, 200], [100, 100]);
var path2 = new Path.Rectangle([50, 50], [200, 200]);
var path3 = new Path.Rectangle([0, 0], [400, 400]);
new CompoundPath(path1, path2, path3);
testExportImportJSON(paper.project);
});
test('Empty Path', function() {
new Path();
testExportImportJSON(paper.project);
});
test('Gradients', function() {
var path = new Path.Circle([100, 100], 40);
var gradient = new Gradient(['yellow', 'red', 'black'], true);
var from = path.position;
var to = path.bounds.rightCenter;
var gradientColor = new Color(gradient, from, to);
path.fillColor = gradientColor;
path.strokeColor = 'black';
testExportImportJSON(paper.project);
});
test('Group transform', function() {
var circle1 = new Path.Circle([100, 100], 50);
circle1.fillColor = 'red';
var circle2 = new Path.Circle([200, 100], 50);
circle2.fillColor = 'blue';
var group = new Group(circle1, circle2);
group.translate([100, 100]);
group.scale(0.5);
group.rotate(10);
testExportImportJSON(paper.project);
});
test('Rectangle testing', function() {
var point1 = new Point(10, 10);
var size1 = new Size(50, 50);
var rectangle1 = new Rectangle(point1, size1);
var path1 = new Path.Rectangle(rectangle1);
path1.strokeColor = 'black';
path1.fillColor = 'red';
path1.name = 'square1';
path1.strokeCap = 'square';
path1.opacity = .1;
path1.dashArray = [5, 2];
path1.dashOffset = 0;
var point2 = new Point(75, 75);
var point22 = new Point(100, 100);
var path2 = new Path.Rectangle(point2, point22);
path2.strokeColor = 'red';
path2.strokeWidth = 4;
path2.fillColor = 'blue';
path2.name = 'square2';
path2.strokeCap = 'butt';
var point3 = new Point(150, 150);
var size3 = new Size(50, 50);
var rectangle3 = new Rectangle(point3, size3);
var path3 = new Path.Rectangle(rectangle3);
path3.strokeColor = 'blue';
var point4 = new Point(200, 200);
var size4 = new Size(100, 100);
var rectangle4 = new Rectangle(point4, size4);
var cornerSize4 = new Size(30, 30);
var path4 = new Path.Rectangle(rectangle4, cornerSize4);
path4.strokeColor= 'yellow';
path4.fillColor='purple';
testExportImportJSON(paper.project);
});
test('Symbols', function() {
var ellipse = new Path.Ellipse({
from: [0, 0],
to: [200, 100],
fillColor: 'red'
});
var symbol = new Symbol(ellipse);
var p1 = symbol.place([100, 100]);
p1.rotate(45);
var p2 = symbol.place([300, 200]);
p2.rotate(-30);
testExportImportJSON(paper.project);
});
test('PointText testing', function() {
var text = new PointText(new Point(50, 100));
text.fillColor = 'black';
text.content = 'This is a test';
var text = new PointText(new Point(100, 150));
text.fillColor = 'red';
text.strokeWidth = '4';
text.content = 'This is also a test';
text.rotate(45);
text.shear(0.85, 0.15);
text.scale(0.85, 2);
testExportImportJSON(paper.project);
});
test('transform test 1', function() {
var circlePath = new Path.Circle(new Point(280, 100), 25);
circlePath.strokeColor = 'black';
circlePath.fillColor = 'white';
var clones = 30;
var angle = 360 / clones;
for(var i = 0; i < clones; i++) {
var clonedPath = circlePath.clone();
clonedPath.rotate(angle * i, circlePath.bounds.topLeft);
};
testExportImportJSON(paper.project);
});
test('transform test 2', function() {
var path = new Path.Rectangle(new Point(50, 50), new Size(100, 50));
path.style = {
fillColor: 'white',
strokeColor: 'black'
};
var copy = path.clone();
copy.strokeColor = 'red';
copy.rotate(-45);
copy.scale(0.5);
testExportImportJSON(paper.project);
});
test('Item#name', function() {
var path = new Path({
name: 'dave'
});
testExportImportJSON(paper.project);
});
test('Item#data', function() {
var path = new Path();
path.data = {
string: '----',
number: 1234,
array: ['a ray', 'some rays'],
bool: true,
nil: null,
point: new Point(12, 34),
size: new Size(12, 34),
rectangle: new Rectangle([12, 34], [56, 78]),
deep: {
deeper: {
deepest: true
}
}
};
testExportImportJSON(paper.project);
});
test('Color', function() {
var path = new Path({
fillColor: new Color(1, 1, 0, 0.5)
});
testExportImportJSON(paper.project);
});