3
3
* (c) 2012-2015 Andrew Brampton (bramp.net)
4
4
* Simplified BSD license.
5
5
*/
6
- /*global Diagram, Snap, _ */
6
+ /*global Diagram, Snap, WebFont _ */
7
7
// TODO Move defintion of font onto the <svg>, so it can easily be override at each level
8
- if ( Snap ) {
8
+ if ( typeof Snap != 'undefined' ) {
9
+
10
+ if ( typeof WebFont == 'undefined' ) {
11
+ throw new Error ( "WebFont is required (https://github.com/typekit/webfontloader)." ) ;
12
+ }
13
+
9
14
var xmlns = "http://www.w3.org/2000/svg" ;
10
15
11
16
var LINE = {
@@ -22,52 +27,7 @@ if (Snap) {
22
27
******************/
23
28
Snap . plugin ( function ( Snap , Element , Paper , global , Fragment ) {
24
29
25
- Element . prototype . wobble = function ( x1 , y1 , x2 , y2 ) {
26
- assert ( _ . all ( [ x1 , x2 , y1 , y2 ] , _ . isFinite ) , "x1,x2,y1,y2 must be numeric" ) ;
27
-
28
- var wobble = Math . sqrt ( ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) ) / 25 ;
29
-
30
- // Distance along line
31
- var r1 = Math . random ( ) ;
32
- var r2 = Math . random ( ) ;
33
-
34
- var xfactor = Math . random ( ) > 0.5 ? wobble : - wobble ;
35
- var yfactor = Math . random ( ) > 0.5 ? wobble : - wobble ;
36
-
37
- var p1 = {
38
- x : ( x2 - x1 ) * r1 + x1 + xfactor ,
39
- y : ( y2 - y1 ) * r1 + y1 + yfactor
40
- } ;
41
-
42
- var p2 = {
43
- x : ( x2 - x1 ) * r2 + x1 - xfactor ,
44
- y : ( y2 - y1 ) * r2 + y1 - yfactor
45
- } ;
46
-
47
- return "C" + p1 . x + "," + p1 . y +
48
- " " + p2 . x + "," + p2 . y +
49
- " " + x2 + "," + y2 ;
50
- } ;
51
30
52
- /**
53
- * Draws a wobbly (hand drawn) rect
54
- */
55
- Element . prototype . handRect = function ( x , y , w , h ) {
56
- assert ( _ . all ( [ x , y , w , h ] , _ . isFinite ) , "x, y, w, h must be numeric" ) ;
57
- return this . path ( "M" + x + "," + y +
58
- this . wobble ( x , y , x + w , y ) +
59
- this . wobble ( x + w , y , x + w , y + h ) +
60
- this . wobble ( x + w , y + h , x , y + h ) +
61
- this . wobble ( x , y + h , x , y ) ) ;
62
- } ;
63
-
64
- /**
65
- * Draws a wobbly (hand drawn) line
66
- */
67
- Element . prototype . handLine = function ( x1 , y1 , x2 , y2 ) {
68
- assert ( _ . all ( [ x1 , x2 , y1 , y2 ] , _ . isFinite ) , "x1,x2,y1,y2 must be numeric" ) ;
69
- return this . path ( "M" + x1 + "," + y1 + this . wobble ( x1 , y1 , x2 , y2 ) ) ;
70
- } ;
71
31
} ) ;
72
32
73
33
@@ -104,6 +64,7 @@ if (Snap) {
104
64
} ,
105
65
106
66
init_paper : function ( container ) {
67
+ // Container must be a SVG element. We assume it's a div, so lets create a SVG and insert
107
68
var svg = document . createElementNS ( xmlns , 'svg' ) ;
108
69
container . appendChild ( svg ) ;
109
70
@@ -113,8 +74,9 @@ if (Snap) {
113
74
this . _paper . addClass ( "sequence" ) ;
114
75
this . _paper . addClass ( this . _css_class ) ;
115
76
116
- this . clear_group ( ) ;
77
+ this . begin_group ( ) ;
117
78
79
+ // TODO Perhaps only include the markers if we actually use them.
118
80
var a = this . arrow_markers = { } ;
119
81
var arrow = this . _paper . path ( "M 0 0 L 5 2.5 L 0 5 z" ) ;
120
82
a [ ARROWTYPE . FILLED ] = arrow . marker ( 0 , 0 , 5 , 5 , 5 , 2.5 )
@@ -143,22 +105,26 @@ if (Snap) {
143
105
text_bbox : function ( text , font ) {
144
106
var t = this . create_text ( text , font ) ;
145
107
var bb = t . getBBox ( ) ;
108
+ console . log ( bb ) ;
146
109
t . remove ( ) ;
147
110
return bb ;
148
111
} ,
149
112
113
+ // For each drawn element, push onto the stack, so it can be wrapped in a single outer element
150
114
push_to_stack : function ( element ) {
151
115
this . _stack . push ( element ) ;
152
116
return element ;
153
117
} ,
154
118
155
- clear_group : function ( ) {
119
+ // Begin a group of elements
120
+ begin_group : function ( ) {
156
121
this . _stack = [ ] ;
157
122
} ,
158
123
124
+ // Finishes the group, and returns the <group> element
159
125
finish_group : function ( ) {
160
126
var g = this . _paper . group . apply ( this . _paper , this . _stack ) ;
161
- this . clear_group ( ) ;
127
+ this . begin_group ( ) ; // Reset the group
162
128
return g ;
163
129
} ,
164
130
@@ -202,7 +168,6 @@ if (Snap) {
202
168
* align (string) ALIGN_LEFT or ALIGN_CENTER
203
169
*/
204
170
draw_text : function ( x , y , text , font , background , align ) {
205
- var paper = this . _paper ;
206
171
var t = this . create_text ( text , font ) ;
207
172
var bb = t . getBBox ( ) ;
208
173
@@ -213,6 +178,7 @@ if (Snap) {
213
178
214
179
// draw a rect behind it
215
180
if ( background ) {
181
+ var paper = this . _paper ;
216
182
var r = paper . rect ( x , y , bb . width , bb . height ) ;
217
183
r . attr ( RECT ) . attr ( { 'stroke' : 'none' } ) ;
218
184
this . push_to_stack ( r ) ;
@@ -228,31 +194,31 @@ if (Snap) {
228
194
} ,
229
195
230
196
draw_title : function ( ) {
231
- this . clear_group ( ) ;
197
+ this . begin_group ( ) ;
232
198
BaseTheme . prototype . draw_title . call ( this ) ;
233
199
return this . finish_group ( ) . addClass ( 'title' ) ;
234
200
} ,
235
201
236
202
draw_actor : function ( actor , offsetY , height ) {
237
- this . clear_group ( ) ;
203
+ this . begin_group ( ) ;
238
204
BaseTheme . prototype . draw_actor . call ( this , actor , offsetY , height ) ;
239
205
return this . finish_group ( ) . addClass ( 'actor' ) ;
240
206
} ,
241
207
242
208
draw_signal : function ( signal , offsetY ) {
243
- this . clear_group ( ) ;
209
+ this . begin_group ( ) ;
244
210
BaseTheme . prototype . draw_signal . call ( this , signal , offsetY ) ;
245
211
return this . finish_group ( ) . addClass ( 'signal' ) ;
246
212
} ,
247
213
248
214
draw_self_signal : function ( signal , offsetY ) {
249
- this . clear_group ( ) ;
215
+ this . begin_group ( ) ;
250
216
BaseTheme . prototype . draw_self_signal . call ( this , signal , offsetY ) ;
251
217
return this . finish_group ( ) . addClass ( 'signal' ) ;
252
218
} ,
253
219
254
220
draw_note : function ( note , offsetY ) {
255
- this . clear_group ( ) ;
221
+ this . begin_group ( ) ;
256
222
BaseTheme . prototype . draw_note . call ( this , note , offsetY ) ;
257
223
return this . finish_group ( ) . addClass ( 'note' ) ;
258
224
} ,
@@ -268,15 +234,33 @@ if (Snap) {
268
234
269
235
// Take the standard SnapTheme and make all the lines wobbly
270
236
_ . extend ( SnapHandTheme . prototype , SnapTheme . prototype , {
237
+
238
+ draw : function ( container ) {
239
+ var that = this ;
240
+ WebFont . load ( {
241
+ custom : {
242
+ families : [ 'daniel' ] // TODO replace this with somethign that reads the css
243
+ } ,
244
+ classes : false , // No need to place classes on the DOM, just use JS Events
245
+ active : function ( ) {
246
+ SnapTheme . prototype . draw . call ( that , container ) ;
247
+ } ,
248
+ inactive : function ( ) {
249
+ // If we fail to fetch the font, still continue.
250
+ SnapTheme . prototype . draw . call ( that , container ) ;
251
+ } ,
252
+ } ) ;
253
+ } ,
254
+
271
255
init_font : function ( ) {
272
- this . _font = {
256
+ this . _font = { // TODO is this needed? CSS should handle?
273
257
'font-size' : 16 ,
274
258
'font-family' : 'daniel'
275
259
} ;
276
260
} ,
277
261
278
262
draw_line : function ( x1 , y1 , x2 , y2 , linetype , arrowhead ) {
279
- var line = this . _paper . handLine ( x1 , y1 , x2 , y2 ) . attr ( LINE ) ;
263
+ var line = this . _paper . path ( handLine ( x1 , y1 , x2 , y2 ) ) . attr ( LINE ) ;
280
264
if ( arrowhead !== undefined ) {
281
265
line . attr ( 'markerEnd' , this . arrow_markers [ arrowhead ] ) ;
282
266
}
@@ -287,7 +271,7 @@ if (Snap) {
287
271
} ,
288
272
289
273
draw_rect : function ( x , y , w , h ) {
290
- var rect = this . _paper . handRect ( x , y , w , h ) . attr ( RECT ) ;
274
+ var rect = this . _paper . path ( handRect ( x , y , w , h ) ) . attr ( RECT ) ;
291
275
return this . push_to_stack ( rect ) ;
292
276
} ,
293
277
} ) ;
0 commit comments