@@ -33,43 +33,44 @@ Example.svg = function() {
33
33
Runner . run ( runner , engine ) ;
34
34
35
35
// add bodies
36
- var svgs = [
37
- 'iconmonstr-check-mark-8-icon' ,
38
- 'iconmonstr-paperclip-2-icon' ,
39
- 'iconmonstr-puzzle-icon' ,
40
- 'iconmonstr-user-icon'
41
- ] ;
42
-
43
- if ( typeof $ !== 'undefined' ) {
44
- for ( var i = 0 ; i < svgs . length ; i += 1 ) {
45
- ( function ( i ) {
46
- $ . get ( './svg/' + svgs [ i ] + '.svg' ) . done ( function ( data ) {
47
- var vertexSets = [ ] ,
48
- color = Common . choose ( [ '#f19648' , '#f5d259' , '#f55a3c' , '#063e7b' , '#ececd1' ] ) ;
49
-
50
- $ ( data ) . find ( 'path' ) . each ( function ( i , path ) {
51
- var points = Svg . pathToVertices ( path , 30 ) ;
52
- vertexSets . push ( Vertices . scale ( points , 0.4 , 0.4 ) ) ;
53
- } ) ;
54
-
55
- World . add ( world , Bodies . fromVertices ( 100 + i * 150 , 200 + i * 50 , vertexSets , {
56
- render : {
57
- fillStyle : color ,
58
- strokeStyle : color ,
59
- lineWidth : 1
60
- }
61
- } , true ) ) ;
62
- } ) ;
63
- } ) ( i ) ;
64
- }
65
-
66
- $ . get ( './svg/svg.svg' ) . done ( function ( data ) {
67
- var vertexSets = [ ] ,
68
- color = Common . choose ( [ '#f19648' , '#f5d259' , '#f55a3c' , '#063e7b' , '#ececd1' ] ) ;
69
-
70
- $ ( data ) . find ( 'path' ) . each ( function ( i , path ) {
71
- vertexSets . push ( Svg . pathToVertices ( path , 30 ) ) ;
36
+ if ( typeof fetch !== 'undefined' ) {
37
+ var select = function ( root , selector ) {
38
+ return Array . prototype . slice . call ( root . querySelectorAll ( selector ) ) ;
39
+ } ;
40
+
41
+ var loadSvg = function ( url ) {
42
+ return fetch ( url )
43
+ . then ( function ( response ) { return response . text ( ) ; } )
44
+ . then ( function ( raw ) { return ( new window . DOMParser ( ) ) . parseFromString ( raw , 'image/svg+xml' ) ; } ) ;
45
+ } ;
46
+
47
+ ( [
48
+ './svg/iconmonstr-check-mark-8-icon.svg' ,
49
+ './svg/iconmonstr-paperclip-2-icon.svg' ,
50
+ './svg/iconmonstr-puzzle-icon.svg' ,
51
+ './svg/iconmonstr-user-icon.svg'
52
+ ] ) . forEach ( function ( path , i ) {
53
+ loadSvg ( path ) . then ( function ( root ) {
54
+ var color = Common . choose ( [ '#f19648' , '#f5d259' , '#f55a3c' , '#063e7b' , '#ececd1' ] ) ;
55
+
56
+ var vertexSets = select ( root , 'path' )
57
+ . map ( function ( path ) { return Vertices . scale ( Svg . pathToVertices ( path , 30 ) , 0.4 , 0.4 ) ; } ) ;
58
+
59
+ World . add ( world , Bodies . fromVertices ( 100 + i * 150 , 200 + i * 50 , vertexSets , {
60
+ render : {
61
+ fillStyle : color ,
62
+ strokeStyle : color ,
63
+ lineWidth : 1
64
+ }
65
+ } , true ) ) ;
72
66
} ) ;
67
+ } ) ;
68
+
69
+ loadSvg ( './svg/svg.svg' ) . then ( function ( root ) {
70
+ var color = Common . choose ( [ '#f19648' , '#f5d259' , '#f55a3c' , '#063e7b' , '#ececd1' ] ) ;
71
+
72
+ var vertexSets = select ( root , 'path' )
73
+ . map ( function ( path ) { return Svg . pathToVertices ( path , 30 ) ; } ) ;
73
74
74
75
World . add ( world , Bodies . fromVertices ( 400 , 80 , vertexSets , {
75
76
render : {
@@ -79,6 +80,8 @@ Example.svg = function() {
79
80
}
80
81
} , true ) ) ;
81
82
} ) ;
83
+ } else {
84
+ Common . warn ( 'Fetch is not available. Could not load SVG.' ) ;
82
85
}
83
86
84
87
World . add ( world , [
0 commit comments