forked from AlloyTeam/gopng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·1281 lines (1235 loc) · 39.6 KB
/
index.html
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--<html manifest="cache.manifest">-->
<html>
<head>
<meta charset="utf-8" />
<meta author="duwei pandorid[at]gmail.com" />
<meta version="0.1.1alpha" />
<meta name="description" content="html5 css sprite tool gopng" />
<title>GoPng</title>
<link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://qplus1.idqqimg.com/module/common/scrollbar/scrollstyle.css">
<style>
#canvas {
border:1px solid #c3c3c3;
}
img {
border:1px solid yellow;
}
#layout {
position: relative;
overflow: auto;
}
#grid_canvas{
position: absolute;
o:0px;
top:0px;
}
#zoom_canvas{
position: absolute;
o:0px;
top:0px;
display: none;
}
#css_html_template, #png_out, #css_out, #export{
display: none;
}
.png_and_css{
-webkit-transition:background-color .5s ease-in-out;
transition:all .5s ease-in-out;
}
.yellow_inform {
background-color: yellow;
}
#png_link, #css_link{
display: none;
}
#absolute_path, #relative_path, #css_template{
width: 580px;
}
.github_icon{
width: 18px;
height: 18px;
background:url(./img/github_icon.png) no-repeat 0px 2px;
float:left;
}
#ta{
display: none;
}
</style>
</head>
<body>
<p></p>
<div class="container">
<div class="row">
<div class="span8">
<ul class="nav nav-tabs">
<li class="active" data-toggle="layout" >
<a href="###">Canvas</a>
</li>
<li class="" data-toggle="css_html_template">
<a href="###">Css Template</a>
</li>
<li class="" data-toggle="png_out" >
<a href="###" class="png_and_css">Png</a>
</li>
<li class="" data-toggle="css_out" >
<a href="###" class="png_and_css">CSS</a>
</li>
<li class="" data-toggle="export" >
<a href="###" class="png_and_css">Export</a>
</li>
</ul>
<div id="layout">
<canvas id="grid_canvas">
You browser does not support the canvas elements.
</canvas>
<canvas id="canvas">
You browser does not support the canvas elements.
</canvas>
<canvas id="zoom_canvas">
You browser does not support the canvas elements.
</canvas>
</div>
<div id="css_html_template">
<form class="form-horizontal">
<fieldset>
<h6>Css template</h6>
<div class="control-group">
<label class="control-label">Sprite absolute path</label>
<div class="controls docs-input-sizes">
<input class="input-xlarge" type="text" placeholder='' id="absolute_path" />
</div>
</div>
<div class="control-group">
<label class="control-label">Sprite relative path</label>
<div class="controls docs-input-sizes">
<input class="input-xlarge" type="text" placeholder='' id="relative_path" />
</div>
</div>
<div class="control-group">
<label class="control-label">Css Template</label>
<div class="controls">
<textarea class="input-xlarge" id="css_template" rows="25">
</textarea>
<p class="help-block">Using John Resig's Micro-Templating</p>
<p class="help-block">Variables can be uesd: relative_path, absolute_path, x, y, w, h, name</p>
<p>
<img src="./img/cord.png" title="about x,y,w,h" />
</p>
<p>name for fileName without ext</p>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="css_template_apply">Apply changes</button>
<button class="btn" id="css_template_reset">Reset</button>
</div>
</fieldset>
</form>
</div>
<div id="png_out">
<div class="row">
<div class="span2">
<p>
<a id="png_link" download="sprite.png" target="_blank" title="download png" href="###">
<button class="btn btn-info">
<i class="icon-file icon-white"></i>Download
</button>
</a>
</p>
</div>
<div class="span2">
</div>
</div>
<div id="img"></div>
</div>
<div id="css_out">
<p>
<a class="btn btn-info" id="css_link" download="style.css" target="_blank" title="download css" href="###"><i class="icon-file icon-white"></i>Download</a>
</p>
<h6>css code:</h6>
<pre id="target_css">
</pre>
</div>
<div id="export">
<div class="hero-unit">
<h1>Export!</h1>
<p>Export and save the file, then you can recover your workspace by drop it on the canvas next time!</p>
<p>
<a class="btn btn-primary btn-large" download="export.json" id="export_workspace" href="###">Export</a>
</p>
</div>
</div>
<p>
<div class="alert">
<button class="close" data-dismiss="alert" id="browser_notice">×</button>
<strong>Notice!</strong> This HTML5 app is only tested for Chrome 18+(recommend) and Firefox 10+ now.
</div>
</p>
</div>
<div class="span4">
<div class="well">
<div class="row">
<div class="span1">
<h3>Go!PNG</h3>
</div>
<div class="offset2">
<button class="btn btn-success" id="make">Make Png & css</button>
</div>
</div>
<form class="form-horizontal">
<fieldset>
<h6>Canvas sizes</h6>
<div class="control-group">
<label class="control-label">Canvas width</label>
<div class="controls docs-input-sizes">
<input class="input-small" type="text" placeholder='' id="canvas_width" />
</div>
</div>
<div class="control-group">
<label class="control-label">Canvas Height</label>
<div class="controls docs-input-sizes">
<input class="input-small" type="text" placeholder='' id="canvas_height" />
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="canvas_sizes_apply">Apply changes</button>
</div>
</fieldset>
<fieldset>
<h6>Packer modes</h6>
<div class="control-group">
<label class="control-label">Fixed Size</label>
<div class="controls">
<div class="btn-group" id="packer_modes_fixed">
<button class="btn btn-primary" id="vertical">Vertical</button>
<button class="btn btn-primary" id="bintree">BinTree</button>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Auto Grow</label>
<div class="controls">
<div class="btn-group" id="packer_modes_grow">
<!--<button class="btn btn-primary disabled" id="verticalGrow" onclick="return false;">Vertical</button>-->
<button class="btn btn-primary" id="bintreeGrow" onclick="return false;">BinTree</button>
<button class="btn btn-primary" id="Minimize" onclick="return false;">Minimize</button>
</div>
</div>
</div>
<div class="form-actions">
</div>
</fieldset>
<fieldset>
<h6>Zoom setting</h6>
<div class="control-group">
<label class="control-label">Zoom mode</label>
<div class="controls">
<div class="btn-group" id="zoom_factors">
<button class="btn btn-primary" id="1x">1X</button>
<button class="btn btn-primary" id="2x">2X</button>
<button class="btn btn-primary" id="4x">4X</button>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Zoom raduis</label>
<div class="controls docs-input-sizes">
<input class="input-small" type="text" placeholder='' id="zoom_setting_value" />
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="zoom_setting_apply">Apply changes</button>
</div>
</fieldset>
<fieldset>
<h6>Magnet setting</h6>
<div class="control-group">
<label class="control-label">Magnet</label>
<div class="controls">
<button class="btn btn-primary" id="magnet">Magnet!</button>
</div>
</div>
<div class="control-group">
<label class="control-label">Magnet distance</label>
<div class="controls docs-input-sizes">
<input class="input-small" type="text" placeholder='' id="magnet_setting_value"/>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="magnet_setting_apply">Apply changes</button>
</div>
</fieldset>
</form>
<p>
<a class="btn" style="margin-left:100px;" title="view on github" href="https://github.com/AlloyTeam/gopng" target="_blank"><span class="github_icon"></span>View On GitHub</a>
</p>
<p>
<a class="btn" style="margin-left:100px;" title="view on github" href="http://www.alloyteam.com/2012/05/gopng%E9%9B%AA%E7%A2%A7%E5%9B%BE%E5%90%88%E6%88%90%E5%B7%A5%E5%85%B7-%E5%8F%A6%E4%B8%80%E4%B8%AAhtml5-app/" target="_blank"><i class="icon-book"></i> Get some help </a>
</p>
</div>
</div>
</div>
</div>
<script>
;(function(namespace) {
//pandorid[at]gmail.com
var namespace = window[namespace] = {};
var config = {
//layout
canvas_width: 600,
canvas_height: 600,
//packer
packer_mode : 'bintree',
//magnet
if_magnet : true,
magnet_distance: 8,
//zoom
if_zoom : true,
zoom_pixel : true,
zoom: 2,
zoom_radius: 200,
//css template
absolute_path: 'http://gopng/static/images/sprite.png',
relative_path: '',
template: '#gopng_<%=name%>_outer {\n\
height: <%=h%>px;\n\
width: <%=w%>px;\n\
overflow: hidden;\n\
}\n\
#gopng_<%=name%> {\n\
background: url(<%=absolute_path%>) -<%=x%>px -<%=y%>px no-repeat;\n\
width: 100%;\n\
height: 100%;\n\
_background: none;\n\
_padding-left: <%=x%>px;\n\
_margin-left: -<%=x%>px;\n\
_padding-top: <%=y%>px;\n\
_margin-top: -<%=y%>px;\n\
_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=crop,src="<%=absolute_path%>");\n\
}\n\
'
};
var picture_height, picture_width;
var canvas = document.getElementById('canvas');
var grid_canvas = document.getElementById('grid_canvas');
var grid_context = grid_canvas.getContext("2d");
var zoom_canvas = document.getElementById("zoom_canvas");
var zoom_context = zoom_canvas.getContext("2d");
var context = canvas.getContext("2d");
var imgObjects = []; //x y w h ix iy iw ih...
//Zoomer
var zoomer = (function() {
var l, t; //mouse position
var init = function() {
zoom_canvas.style.display = 'block';
};
var pixelZoom = function(sx, sy, sw, sh, tx, ty, tw, th) {
var sdata = context.getImageData(sx, sy, sw, sh).data;
var target = context.createImageData(tw, th);
var tdata = target.data;
var mapx = [];
var ratiox = sw / tw, px = 0;
for (var i = 0; i < tw; ++i) {
mapx[i] = 4 * Math.floor(px);
px += ratiox;
}
var mapy = [];
var ratioy = sh / th, py = 0;
for (var i = 0; i < th; ++i) {
mapy[i] = 4 * sw * Math.floor(py);
py += ratioy;
}
var tp = 0;
for (py = 0; py < th; ++py) {
for (px = 0; px < tw; ++px) {
var sp = mapx[px] + mapy[py];
tdata[tp++] = sdata[sp++];
tdata[tp++] = sdata[sp++];
tdata[tp++] = sdata[sp++];
tdata[tp++] = sdata[sp++];
}
}
zoom_context.putImageData(target, tx, ty);
}
var snap = function() {
if(!mousedown || !config.if_zoom) {
return;
}
var r = config.zoom_radius;
var z = config.zoom;
zoomer.clear();
if(config.zoom_pixel) {
var sr = Math.floor(r / z);
var sw = sr * 2;
var sh = sr * 2;
var tw = 2 * r; //reduce the target width and target height to make it faster
var th = 2 * r; //reduce the target width and target height to make it faster
var shiftX = l - sr;
var shiftY = t - sr;
pixelZoom(shiftX, shiftY, sw, sh, l, t, tw, th);//now the loop always be 2R
}
zoom_context.globalCompositeOperation = 'destination-in'; //set
zoom_context.beginPath();
zoom_context.arc(l + r, t + r, r, 2*Math.PI, false);
zoom_context.fill();
zoom_context.globalCompositeOperation = 'source-over'; //restore
}
var clear = function(){
zoom_context.clearRect(0, 0, config.canvas_width * config.zoom, config.canvas_height * config.zoom);
};
var setXY = function(x, y){
l = parseInt(x);
t = parseInt(y);
};
var destory = function(){
clear();
zoom_canvas.style.display = 'none';
zoom_canvas_bg.style.display = 'none';
};
return {
setXY : setXY,
snap: snap,
destory: destory,
clear: clear,
init: init
}
})();
var refresh = function() {
clear();
imgObjects.map(function(o) {
context.drawImage(o.el, o.x, o.y);
});
if(drawCallBack) {
drawCallBack();
}
zoomer.snap();
};
var resetCanvas = function() {
canvas.height = config.canvas_height;
canvas.width = config.canvas_width;
zoom_canvas.height = config.canvas_height * 2;
zoom_canvas.width = config.canvas_width * 2;
refresh();
}
var makeImgAndCss = function() {
if(!imgObjects.length) {
return;
}
canvasSizeCaculate();
resetCanvasSize();
refresh();
dataURL = canvas.toDataURL();
var img = new Image();
img.src = dataURL;
getEl('png_link').href = dataURL.replace('data:image/','data:image/');
getEl('png_link').style.display = 'inline';
document.getElementById('img').innerHTML = '';
document.getElementById('img').appendChild(img);
resetCanvasSize(config.canvas_width, config.canvas_height);
refresh();
var css_pices = [];
imgObjects.map(function(o){
var css_pice = template(config.template, {w:o.w, h:o.h, x:o.x, y:o.y, absolute_path:config.absolute_path, name:o.name.split('.')[0], relative_path:config.relative_path});
css_pices.push(css_pice);
});
var css_text = css_pices.join('').split('[enter]').join('\n')
getEl('css_link').href = 'data:text/css;base64,' + encodeBase64(css_text);
getEl('target_css').innerHTML = XssDefender(css_text);
getEl('css_link').style.display = 'inline';
}
var XssDefender = function(s) {
s = s.replace(/&/g,"&").replace(/>/g,">").replace(/</g,"<").replace(/"/g,""").replace(/'/g,"'");
return s;
};
var resetCanvasSize = function(w, h) {
if (!w && !h) {
canvas.width = picture_width;
canvas.height = picture_height;
}else {
canvas.width = w;
canvas.height = h;
}
};
var clear = function() {
context.clearRect(0, 0, canvas.width, canvas.height);
};
var save = function() {
};
var posCaculate = function(){};
var packers = (function(){
var binPacker = function(w, h) {
this.init(w, h);
};
binPacker.prototype = {
init: function(w, h) {
this.root = {x:0, y:0, w:w, h:h};
},
fit: function(objs) {
var node, _this = this;
var s;
objs.map(function(o, i){
if (node = _this.findNode(_this.root, o.w, o.h)) {
var fit = _this.splitNode(node, o.w, o.h);
o.x = fit.x;
o.y = fit.y;
}else {
s = true;
}
});
if(s) {
return {error:'notfind'}
}else {
return {};
}
},
findNode: function(node, w, h) {
if (node.used) {
return this.findNode(node.right, w, h) || this.findNode(node.down, w, h);
}
else if ((w <= node.w) && (h <= node.h)) {
return node;
}
else {
return null;
}
},
splitNode: function(node, w, h) {
node.used = true;
node.down = {x:node.x, y:node.y + h, w:node.w, h:node.h - h};
node.right = {x:node.x+w, y:node.y, w: node.w-w, h:h};
return node;
}
};
var binGrowPacker = function() { };
binGrowPacker.prototype = {
fit: function(objs) {
var node, len = objs.length, _this = this;
var w = len? objs[0].w : 0;
var h = len? objs[0].h : 0;
this.root = { x: 0, y: 0, w: w, h: h };
objs.map(function(o, i){
if (node = _this.findNode(_this.root, o.w, o.h)) {
var fit = _this.splitNode(node, o.w, o.h);
o.x = fit.x;
o.y = fit.y;
}
else {
var fit = _this.growNode(o.w, o.h);
o.x = fit.x;
o.y = fit.y;
}
});
return this.root;
},
findNode: function(node, w, h) {
if (node.used) {
return this.findNode(node.right, w, h) || this.findNode(node.down, w, h);
}
else if ((w <= node.w) && (h <= node.h)) {
return node;
}
else {
return null;
}
},
splitNode: function(node, w, h) {
node.used = true;
node.down = { x: node.x, y: node.y + h, w: node.w, h: node.h - h };
node.right = { x: node.x + w, y: node.y, w: node.w - w, h: h };
return node;
},
growNode: function(w, h) {
var canGrowRight = (h <= this.root.h);
var canGrowDown = (w <= this.root.w);
var shouldGrowRight = canGrowRight && (this.root.h >= (this.root.w + w));
var shouldGrowDown = canGrowDown && (this.root.w >= (this.root.h + h));
if (shouldGrowRight) {
return this.growRight(w, h);
}
else if (shouldGrowDown) {
return this.growDown(w, h);
}
else if (canGrowRight) {
return this.growRight(w, h);
}
else if (canGrowDown) {
return this.growDown(w, h);
}
},
growRight: function(w, h) {
this.root = {
used: true,
x: 0,
y: 0,
w: this.root.w + w,
h: this.root.h,
down: this.root.down,
right: { x: this.root.w, y: 0, w: w, h: this.root.h }
};
node = this.findNode(this.root, w, h);
return this.splitNode(node, w, h);
},
growDown: function(w, h) {
this.root = {
used: true,
x: 0,
y: 0,
w: this.root.w,
h: this.root.h + h,
down: { x: 0, y: this.root.h, w: this.root.w, h: h },
right: this.root.right
};
node = this.findNode(this.root, w, h);
return this.splitNode(node, w, h);
}
};
return {
'bintree': function() {
resetCanvas();
var packer = new binPacker(config.canvas_width, config.canvas_height);
imgObjects.sort(function(a,b){
if(b.h-a.h!=0) {
return b.h-a.h;
}else {
return a.w-b.w;
}
})
var ret = packer.fit(imgObjects);
if(ret.error) {
alert('not fit!');
}
},
'vertical': function() {
resetCanvas();
var h_diff = 0;
imgObjects.map(function(o, i, all) {
o.y = h_diff;
o.x = 0;
h_diff += o.h;
});
if(h_diff > config.canvas_height) {
alert('not fit!');
}
},
'bintreeGrow': function() {
var packer = new binGrowPacker();
if(!imgObjects.length) return;
imgObjects.sort(function(a,b){
if(b.h-a.h!=0) {
return b.h-a.h;
}else {
return a.w-b.w;
}
})
var root = packer.fit(imgObjects);
config.canvas_width = root.w;
config.canvas_height = root.h;
resetCanvas();
getEl('canvas_width').value = config.canvas_width;
getEl('canvas_height').value = config.canvas_height;
},
'Minimize': function() {
resetCanvas();
var autoPacker = new binGrowPacker();
if(!imgObjects.length) return;
imgObjects.sort(function(a,b){
if(b.h-a.h!=0) {
return b.h-a.h;
}else {
return a.w-b.w;
}
});
var maxWidth = imgObjects.reduce(function(a, b){
if(a.w > b.w) {
return a;
}
return b;
}).w;
var firstTry = autoPacker.fit(imgObjects);
var totalWidth = firstTry.w;
var totalHeight = firstTry.h;
var area = totalHeight * totalWidth;
var tw, th;
var p, ret;
while(totalWidth-- > maxWidth) {
p = new binPacker(totalWidth, totalHeight);
ret = p.fit(imgObjects);
if(ret.error) {
while(ret.error) {
p = new binPacker(totalWidth, totalHeight);
ret = p.fit(imgObjects);
totalHeight++;
}
}
if(totalWidth * totalHeight < area) {
area = totalHeight * totalWidth;
tw = totalWidth;
th = totalHeight;
}
}
(new binPacker(tw, th)).fit(imgObjects);
config.canvas_width = tw;
config.canvas_height = th;
resetCanvas();
getEl('canvas_width').value = config.canvas_width;
getEl('canvas_height').value = config.canvas_height;
}
}
})();
var canvasSizeCaculate = function() {
var max_width = imgObjects.reduce(function(a, b) {
return a.w + a.x > b.w + b.x ? a : b;
});
var max_height = imgObjects.reduce(function(a, b) {
return a.h + a.y > b.h + b.y ? a : b;
});
picture_height = max_height.h + max_height.y;
picture_width = max_width.w + max_width.x;
}
zoom_canvas.ondragenter = canvas.ondragenter = function() {
return false;
};
zoom_canvas.ondragover= canvas.ondragover = function() {
return false;
};
zoom_canvas.ondrop = canvas.ondrop = function(e) {
var files = e.dataTransfer.files;
for(var i=0;i<files.length;i++) {
var f = files[i];
var numbers = files.length;
if(f.type.indexOf('image') == 0) {
(function(f) {
var name = f.name,
type = f.type;
if(typeof FileReader == 'undefined') {
alert('Sorry, FileReader() not supported, switch to Chrome and try again.')
}
var reader = new FileReader();
reader.onload = function(e) {
var dataURL = e.target.result;
var imgEL = new Image();
imgEL.onload = function(e) {
var w,h;
w = parseInt(imgEL.width);
h = parseInt(imgEL.height);
imgObjects.push({el: imgEL, w: w, h: h, x:0, y:0, name:name, type:type, base64:dataURL});
--numbers;
if(numbers === 0) {
posCaculate();
refresh();
}
}
imgEL.src = dataURL;
};
reader.readAsDataURL(f);
})(f);
}else {
var reader = new FileReader();
reader.readAsText(f);
reader.onload = function(e) {
try{
var infile = JSON.parse(e.target.result);
}catch(e) {
alert('wrong file!');
}
if(infile.config) {
config = infile.config;
initialActionsMenu();
resetCanvas();
}
if(infile.objects) {
imgObjects = [];
infile.objects.map(function(o){
var img = new Image();
img.onload = (function(o) {
getEl('img').appendChild(img);
imgObjects.push({x:o.x, y:o.y, h:o.h, w:o.w, base64:o.base64, el:img, name:o.name});
})(o);
img.src = o.base64;
});
refresh();
}
}
}
}
e.stopPropagation();
e.preventDefault();
};
var ifhit = function(movex, movey, width, height, name) {
if(arguments.length == 2) {
return imgObjects.filter(function(o) {
return movex > o.x && movex < o.x + o.w && movey > o.y && movey < o.y + o.h;
})[0];
}else if(arguments.length == 5) {
return imgObjects.some(function(o) {
if(o.name == name) {
return false;
}
return Math.abs(Math.min(o.x, movex) - Math.max(o.x+o.w, movex+width)) < o.w + width &&
Math.abs(Math.min(o.y, movey) - Math.max(o.y+o.h, movey+height)) < o.h + height
});
}
};
var ifMagnet = function(movex, movey, width, height, name) {
if(!config.if_magnet) {
return false;
}
var d = config.magnet_distance;
var objects = imgObjects.filter(function(o){
return o.name != name;
});
var magnetLinesX = [];
var magnetLinesY = [];
//create magnetLines...
objects.map(function(o){
magnetLinesX.push({x:o.x});
magnetLinesX.push({x:o.x+o.w});
});
objects.map(function(o){
magnetLinesY.push({y:o.y});
magnetLinesY.push({y:o.y+o.h});
});
magnetLinesX.map(function(o) {
Math.abs(o.x - movex) - Math.abs(o.x - movex - width) <= 0 ?
o.setx = o.x : o.setx = o.x - width;
o.distance = Math.min(Math.abs(o.x - movex), Math.abs(o.x - movex - width));
});
magnetLinesY.map(function(o) {
Math.abs(o.y - movey) - Math.abs(o.y - movey - height) <= 0 ?
o.sety = o.y : o.sety = o.y - height;
o.distance = Math.min(Math.abs(o.y - movey), Math.abs(o.y - movey - height));
});
var nearx = magnetLinesX.reduce(function(a, b){
return a.distance<=b.distance?a:b;
});
var neary = magnetLinesY.reduce(function(a, b){
return a.distance<=b.distance?a:b;
});
if( nearx.distance<d || neary.distance<d ) {
return {
nearx : nearx.distance<d && nearx,
neary : neary.distance<d && neary
};
}
};
var drawCallBack = null;
var drawLines = function(m) {
drawCallBack = null;
drawCallBack = function() {
if(m.nearx !== false) {
context.beginPath();
context.moveTo(m.nearx.x, 0);
context.lineTo(m.nearx.x, config.canvas_height);
context.stroke();
}
if(m.neary !== false) {
context.beginPath();
context.moveTo(0, m.neary.y);
context.lineTo(config.canvas_width, m.neary.y);
context.stroke();
}
context.fillStyle = "red";
if(m.nearx !== false && m.neary !== false) {
context.fillText([m.nearx.x, m.neary.y].join(','), m.nearx.x + 3, m.neary.y - 3);
context.beginPath();
context.arc(m.nearx.x, m.neary.y, 3, 2*Math.PI, false);
context.fill();
}else if(m.nearx !== false) {
context.fillText([m.nearx.x, target.y].join(','), m.nearx.x + 3, target.y - 3);
context.beginPath();
context.arc(m.nearx.x, target.y, 3, 2*Math.PI, false);
context.fill();
}else if(m.neary !== false) {
context.fillText([target.x, m.neary.y].join(','), target.x + 3, m.neary.y - 3);
context.beginPath();
context.arc(target.x, m.neary.y, 3, 2*Math.PI, false);
context.fill();
}
}
};
var emptyLines = function() {
drawCallBack = null;
}
var target;
var mousedown = 0;
var lastmovex, lastmovey;
zoom_canvas.onmousemove = canvas.onmousemove = function(e) {
var movex = e.offsetX || e.layerX;
var movey = e.offsetY || e.layerY;
if(target) {
zoomer.setXY(movex, movey);
var newx = target.x + (movex-lastmovex);
var newy = target.y + (movey-lastmovey);
if(ifhit(newx, newy, target.w, target.h, target.name)) {
return;
}
target.x = newx;
target.y = newy;
lastmovex = movex;
lastmovey = movey;
var m = ifMagnet(newx, newy, target.w, target.h, target.name);
if(m) {
//draw lines...
drawLines(m);
}else {
emptyLines();
}
refresh();
}else {
var mouseOverTarget = ifhit(movex, movey);
if(mouseOverTarget) {
document.body.style.cursor = 'pointer';
}else {
document.body.style.cursor = '';
}
}
};
zoom_canvas.onmousedown = canvas.onmousedown = function(e) {
mousedown = 1;
lastmovex = e.offsetX || e.layerX;
lastmovey = e.offsetY || e.layerY;
target = ifhit(lastmovex, lastmovey);
zoomer.setXY(lastmovex, lastmovey);
refresh();
document.body.style.cursor = 'pointer';
};
zoom_canvas.onmouseup = canvas.onmouseup = function(e) {
mousedown = 0;
document.body.style.cursor = 'normal';
zoomer.clear();
if(target) {
var m = ifMagnet(target.x, target.y, target.w, target.h, target.name);
if(m) {
m.nearx !== false ? (target.x = m.nearx.setx) : null;
m.neary !== false ? (target.y = m.neary.sety) : null;
}
emptyLines();
refresh();
target = null;
}
};
var hasClass = function(el, className) {
var reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
return el.className.match(reg);
}
var addClass = function(el, className) {
if(hasClass(el, className)) {
return;
}
el.className += ' '+className;
};
var removeClass = function(el, className) {
var reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
el.className = el.className.replace(reg, '');
};
var getEl = function(id_or_class) {
if(id_or_class.substring(0,1) == '.') {
return document.getElementsByClassName(id_or_class.substring(1));
}else if (id_or_class.substring(0,1) == '#') {
return document.getElementById(id_or_class.substring(1));
}else {
return document.getElementById(id_or_class);
}
};
var nodeList2Array = function(nodelist) {
return Array.prototype.concat.apply([], nodelist);
};
var loadConfigFromLocal = function() {}; //later
var saveConfigToLocal = function() {}; //later
var initialActionsMenu = function() {
//canvas sizes
getEl('canvas_width').placeholder = config.canvas_width;
getEl('canvas_height').placeholder = config.canvas_height;
//packer mode
nodeList2Array(getEl('packer_modes_fixed').getElementsByTagName('button')).concat(nodeList2Array(getEl('packer_modes_grow').getElementsByTagName('button'))).map(function(o){
removeClass(o, 'active');
});
switch(config.packer_mode) {
case 'bintree':
case 'vertical':
case 'bintreeGrow':
case 'Minimize':
posCaculate = packers[config.packer_mode];
addClass(getEl(config.packer_mode), 'active');
}
//zoom setting
nodeList2Array(getEl('zoom_factors').getElementsByTagName('button')).map(function(o){
removeClass(o, 'active');
});
if(!config.if_zoom) {
addClass(getEl('1x'), 'active');
}else if(config.zoom == 2) {
addClass(getEl('2x'), 'active');
}else if(config.zoom == 4) {
addClass(getEl('4x'), 'active');
}
getEl('zoom_setting_value').placeholder = config.zoom_radius;
//magnet
if(config.if_magnet) {