1
+ 'use strict'
2
+
3
+ function generateDownloadFile ( ) {
4
+ const save = { }
5
+ save . codes = codes
6
+ save . layers = [ ]
7
+
8
+ for ( let i = 0 ; i < layers . length ; i ++ ) {
9
+ save . layers . push ( { arr : [ ] , hidden : layers [ i ] . hidden , name : layerDiv . children [ i ] . name . value } )
10
+
11
+ const layer = layers [ i ] . arr
12
+ for ( let j = 0 ; j < layer . length ; j ++ ) {
13
+ const shape = layer [ j ]
14
+ save . layers [ i ] . arr . push ( {
15
+ x : shape . x ,
16
+ y : shape . y ,
17
+ w : shape . w ,
18
+ h : shape . h ,
19
+ line : shape . line ,
20
+ lineThick : shape . lineThick ,
21
+ invertX : shape . invertX ,
22
+ invertY : shape . invertY ,
23
+ color : shape . color ,
24
+ hidden : shape . hidden ,
25
+ property : shape . property ,
26
+ activeRemix : shape . activeRemix ,
27
+ activePreset : shape . activePreset ,
28
+ remixes : shape . remixes
29
+ } )
30
+ }
31
+ }
32
+
33
+ const name = 'canvasCraft' + randomStr ( ) + '.json'
34
+ const data = JSON . stringify ( save )
35
+ const file = new File ( [ data ] , name , { type : 'application/json' } )
36
+
37
+ const a = document . createElement ( 'a' )
38
+ const url = URL . createObjectURL ( file )
39
+ a . href = url
40
+ a . download = name
41
+ a . click ( )
42
+ }
43
+
44
+ function generateUploadFile ( item ) {
45
+ if ( ! item . files . length ) return
46
+ const file = item . files [ 0 ]
47
+ const reader = new FileReader ( )
48
+ layers = [ ]
49
+
50
+ reader . onload = ( ) => {
51
+ const save = JSON . parse ( reader . result )
52
+ if ( ! save . layers . length ) return
53
+ codes = save . codes
54
+ layerDiv . innerHTML = ''
55
+
56
+ for ( let i = 0 ; i < save . layers . length ; i ++ ) {
57
+ const layer = save . layers [ i ]
58
+
59
+ const elem = addNewLayer ( save . layers . length )
60
+ elem . name . value = layer . name
61
+ if ( layer . hidden ) elem . hide . onmousedown ( )
62
+
63
+ for ( let j = 0 ; j < layer . arr . length ; j ++ ) {
64
+ const shape = layer . arr [ j ]
65
+ const newShape = new Shape ( shape . x , shape . y , shape . w , shape . h )
66
+ createShape ( newShape )
67
+
68
+ newShape . line = shape . line
69
+ newShape . lineThick = shape . lineThick
70
+ newShape . invertX = shape . invertX
71
+ newShape . invertY = shape . invertY
72
+ newShape . color = shape . color
73
+ if ( shape . hidden ) newShape . hideSelf ( )
74
+ newShape . property = shape . property
75
+ newShape . activeRemix = shape . activeRemix
76
+ newShape . activePreset = shape . activePreset
77
+ newShape . remixes = shape . remixes
78
+
79
+ newShape . drawOnShape ( )
80
+ newShape . draw ( )
81
+ }
82
+ }
83
+
84
+ updateScreen = true
85
+ }
86
+
87
+ reader . readAsText ( file )
88
+ }
0 commit comments