-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource-map.js
72 lines (51 loc) · 2.1 KB
/
resource-map.js
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
(function ($) {
var drawn = [];
var W=0, H=0, area=0, demand=0;
function Resource(relsize) {
return {
draw: function(svg) {
var area_target = (relsize / demand) * area;
var root = Math.sqrt(area_target);
var previous = drawn[drawn.length - 1] || {x: 0, y: 0, w: 0, h: 0};
function random_side() {
return Math.max(Math.random() * Math.sqrt(area_target), 50);
}
this.x = previous.x + previous.w;
this.y = previous.y;
this.h = previous.h || random_side();
this.w = area_target / this.h;
if (this.x + this.w > W){
this.x = 0;
this.y = previous.y + previous.h;
this.h = random_side();
this.w = area_target / this.h;
}
console.log(relsize, demand, area, area_target, this.w, this.h);
svg.rect(this.x, this.y, this.w, this.h, 0, 0,
{fill: '#0099FF', stroke: 'white', strokeWidth: 10});
svg.circle(this.x + (this.w / 2), this.y + (this.h / 2), 10,
{fill: '#0099FF', stroke: 'white', strokeWidth: 2});
drawn.push(this);
}
}
}
$.fn.resourceMap = function(resources, options) {
var options = $.extend({}, $.fn.resourceMap.defaults, options);
W = this.width();
H = this.height();
area = W * H;
this.addClass('resource-map');
this.svg();
var svg = this.svg('get');
// Randomize sort order of resources.
resources.sort(function(a, b) {return Math.random() - 0.5});
// Determine demand for space.
demand = resources.reduce(function(prev, b) {return prev + b.duration}, 0);
console.log(W, H, area, demand);
for (var i=0, resource; resource = resources[i]; i++)
Resource(resource.duration).draw(svg);
return this;
};
}(jQuery));
jQuery.fn.resourceMap.defaults = {
};