forked from svgdotjs/svg.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.js
More file actions
225 lines (192 loc) · 6.31 KB
/
Copy pathpath.js
File metadata and controls
225 lines (192 loc) · 6.31 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
212
213
214
215
216
217
describe('Path', function() {
var path
beforeEach(function() {
path = draw.path(svgPath)
})
afterEach(function() {
draw.clear()
})
describe('array()', function() {
it('returns an instance of SVG.PathArray', function() {
expect(path.array() instanceof SVG.PathArray).toBeTruthy()
})
it('returns the value stored in the private variable _array', function() {
expect(path.array()).toBe(path._array)
})
})
describe('x()', function() {
it('returns the value of x without an argument', function() {
expect(path.x()).toBe(0)
})
it('sets the value of x with the first argument', function() {
path.x(123)
var box = path.bbox()
expect(box.x).toBe(123)
})
})
describe('y()', function() {
it('returns the value of y without an argument', function() {
expect(path.y()).toBe(0)
})
it('sets the value of y with the first argument', function() {
path.y(345)
var box = path.bbox()
expect(box.y).toBe(345)
})
})
describe('cx()', function() {
it('returns the value of cx without an argument', function() {
expect(path.cx()).toBe(50)
})
it('sets the value of cx with the first argument', function() {
path.cx(123)
var box = path.bbox()
expect(box.cx).toBe(123)
})
})
describe('cy()', function() {
it('returns the value of cy without an argument', function() {
expect(path.cy()).toBe(50)
})
it('sets the value of cy with the first argument', function() {
path.cy(345)
var box = path.bbox()
expect(box.cy).toBe(345)
})
})
describe('move()', function() {
it('sets the x and y position', function() {
path.move(123,456)
var box = path.bbox()
expect(box.x).toBe(123)
expect(box.y).toBe(456)
})
it('sets the x and y position when scaled to half its size', function() {
path.scale(0.5, 0, 0).move(123,456)
var box = path.bbox()
expect(box.x).toBe(123)
expect(box.y).toBe(456)
})
})
describe('dx()', function() {
it('moves the x positon of the element relative to the current position', function() {
path.move(50,60)
path.dx(100)
var box = path.bbox()
expect(box.x).toBe(150)
})
})
describe('dy()', function() {
it('moves the y positon of the element relative to the current position', function() {
path.move(50, 60)
path.dy(120)
var box = path.bbox()
expect(box.y).toBe(180)
})
})
describe('dmove()', function() {
it('moves the x and y positon of the element relative to the current position', function() {
path.move(50,60)
path.dmove(80, 25)
var box = path.bbox()
expect(box.x).toBe(130)
expect(box.y).toBe(85)
})
})
describe('center()', function() {
it('sets the cx and cy position', function() {
path.center(321,567)
var box = path.bbox()
expect(box.x).toBe(271)
expect(box.y).toBe(517)
})
})
describe('width()', function() {
it('sets the width of the element', function() {
path.width(234)
var box = path.bbox()
expect(box.width).toBeCloseTo(234)
})
it('gets the width of the element aithout an agrument', function() {
path.width(456)
expect(path.width()).toBeCloseTo(456)
})
})
describe('height()', function() {
it('sets the height of the element', function() {
path.height(654)
var box = path.bbox()
expect(box.height).toBeCloseTo(654)
})
it('gets the height of the element aithout an agrument', function() {
path.height(321)
expect(path.height()).toBeCloseTo(321)
})
})
describe('size()', function() {
it('defines the width and height of the element', function() {
path.size(987,654)
var box = path.bbox()
expect(box.width).toBeCloseTo(987)
expect(box.height).toBeCloseTo(654)
})
it('defines the width and height proportionally with only the width value given', function() {
var box = path.bbox()
path.size(500)
expect(path.width()).toBe(500)
expect(path.width() / path.height()).toBe(box.width / box.height)
})
it('defines the width and height proportionally with only the height value given', function() {
var box = path.bbox()
path.size(null, 525)
expect(path.height()).toBe(525)
expect(path.width() / path.height()).toBe(box.width / box.height)
})
})
describe('scale()', function() {
it('should scale the element universally with one argument', function() {
var box1 = path.tbox()
, box2 = path.scale(2).tbox()
expect(box1.width * 2).toBe(box2.width)
expect(box1.height * 2).toBe(box2.height)
})
it('should scale the element over individual x and y axes with two arguments', function() {
var box1 = path.tbox()
, box2 = path.scale(2, 3.5).tbox()
expect(box1.width * 2).toBe(box2.width)
expect(box1.height * 3.5).toBe(box2.height)
})
})
describe('translate()', function() {
it('sets the translation of an element', function() {
path.transform({ x: 12, y: 12 })
expect(path.node.getAttribute('transform')).toBe('matrix(1,0,0,1,12,12)')
})
})
describe('plot()', function() {
it('falls back to a single point without an argument', function() {
path = draw.path()
expect(path.node.getAttribute('d')).toBe('M0 0 ')
})
})
describe('toString()', function() {
it('renders path array correctly to string', function() {
path = path.plot('M 50 60 A 60 60 0 0 0 50 -60 H 100 V 100 L 20 30 C 10 20 30 40 50 60 ')
expect(path.node.getAttribute('d')).toBe('M50 60A60 60 0 0 0 50 -60H100V100L20 30C10 20 30 40 50 60 ')
})
it('renders path array correctly to string', function() {
path = path.plot('M 50 60 A 60 60 1 1 0 50 -60 H 100 V 100 L 20 30 C 10 20 30 40 50 60 ')
expect(path.node.getAttribute('d')).toBe('M50 60A60 60 1 1 0 50 -60H100V100L20 30C10 20 30 40 50 60 ')
})
})
describe('length()', function() {
it('gets the total length of the path', function() {
expect(path.length()).toBe(path.node.getTotalLength())
})
})
describe('pointAt()', function() {
it('gets a point at given length', function() {
expect(path.pointAt(100)).toEqual(path.node.getPointAtLength(100))
})
})
})