summaryrefslogtreecommitdiff
path: root/static/html/space.packing.html
blob: 7f46061bd9700f2eab3febaa17ddad5e62463606 (plain)
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
<!DOCTYPE html>
<html>
  <head>
    <title>dump.fm image packing test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="description" content="dump.fm - Talk with pictures!" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

  <style>
  html,body,img,div{margin:0; border:0; padding:0}
  img { max-width: 400px; max-height: 400px;}
  h1 { display:inline-block; margin: 20px; font-size: 30px;}
  #content { position: relative; width: 100%; }
  fieldset { display: inline }
  p { margin-left: 20px; display: inline-block; }
  </style>

<script>

var marginWidth = 16
var marginHeight = 16

function go(){

  $("#content").html("")

  columnSize = parseInt($("#settings-columnSize").val()) || 300

  minImageSize = parseInt($("#settings-minSize").val()) || 16
  maxImageSize = columnSize


  columnSpacingStyle = $('input:radio[name=settings-column-spacing]:checked').val();

  getWindowSize()
  var numColumns = calcColumns()
  Columns = []
  for (var i = 0; i < numColumns; i++) {
    Columns.push({"height": 0})
  }

  if (columnSpacingStyle == "center") {
    marginWidth = 16
    columnSpacingAmt = (Width - (numColumns * (columnSize + marginWidth) + marginWidth)) / 2

  } else if (columnSpacingStyle == "justify")
    marginWidth = (Width - (numColumns * columnSize)) / (numColumns + 1)


  setTimeout(fill, 50)
}


function fill(){
  var img = randomImage()

  var colIndex = shortestColumn()
  var col = Columns[colIndex]

  if (col.height > 2 * Height) return;

  if (columnSpacingStyle == "center") {
    var colLeft = colIndex * (marginWidth + columnSize) + columnSpacingAmt
    var imgLeft = Math.floor((marginWidth / 2) + (columnSize / 2) - (parseInt(img.style.width) / 2)) + colLeft + "px"
  } else if (columnSpacingStyle == "justify") {
    var colLeft = (colIndex * (marginWidth + columnSize))
    var imgLeft = Math.floor((marginWidth / 2) + (columnSize / 2) - (parseInt(img.style.width) / 2)) + colLeft + "px"
  }



  img.style.position = 'absolute'
  img.style.top = col.height + marginHeight + "px"
  img.style.left = imgLeft

  col.height += marginHeight + parseInt(img.style.height)

  $("#content").append(img)

  setTimeout(fill, 50)

}

function shortestColumn(){
  var min = Infinity
  var mindex = 0
  for(var i = 0; i< Columns.length; i++){
      var col = Columns[i]
      if ( min > col.height) {
        min = col.height
        mindex = i
      }
  }

  return mindex


}



function randomImage(){

  var d = document.createElement("div")
  d.style.backgroundColor = "#" + rand16() + rand16() + rand16() + rand16() + rand16() + rand16()
  d.style.width = Math.floor(Math.random() * (maxImageSize - minImageSize) + minImageSize) + "px"
  d.style.height = Math.floor(Math.random() * (maxImageSize - minImageSize) + minImageSize) + "px"

  return d

}

function rand16(){
  return Math.floor(Math.random() * 16).toString(16)
 }




function calcColumns(){
  var minMargin = 16
  var columns = 0;
  var width = Width - minMargin;
  var columnSub = columnSize + minMargin
  while (width > columnSub) {
    width -= columnSub
    columns ++
  }

  return columns

}

function getWindowSize(){
  Width = $(document).width()
  Height = $(document).height()
}



</script>
</head>
  <body>

  <h1> space packing test. </h1>
  <p>
  <fieldset><legend>type: <select><option>columns</option></select></legend>
  <div>
  <br>column size <input name="settings-columnSize" id="settings-columnSize" type="text" value="200" size="4" />
  <br>min image size <input name="settings-minSize" id="settings-minSize" type="text" value="40" size="4" />
  <br>
    center <input name="settings-column-spacing" value="center" checked="checked" type="radio">
    justify <input name="settings-column-spacing" value="justify" type="radio">
  </div>
  <br><button onclick="go()">go</button>
  </fieldset>




  </p>





  <div id="content"></div>




  </body>
 </html>