-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb-lepracursor.html
188 lines (116 loc) · 4.79 KB
/
pb-lepracursor.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
<link rel="import" href="../polymer/polymer.html">
<!--
This polymer component changes mouse pointer behavior. With each click new pointers are added to the page.
Just add <pb-lepracursor></pb-lepracursor> tag inside body tag.
##### Example
<pb-lepracursor></pb-lepracursor>
@element pb-lepracursor
@blurb This polymer component changes mouse pointer behavior. With each click new pointers are added to the page.
@status alpha
@homepage http://sepans.github.io/pb-lepracursor
-->
<polymer-element name="pb-lepracursor" attributes="notitle author">
<template>
<link rel="stylesheet" href="pb-lepracursor.css" />
</template>
<script src="../d3/d3.min.js"></script>
<script>
Polymer({
_nodes: [],
_links: [],
_node: null,
_link: null,
_width: 4000,
_height: 4000,
_force: null,
_svg: null,
cursor: null,
ready: function() {
//remove the actual cursor
var body = document.querySelector('body');
body.style.cursor = 'none';
var html = document.querySelector('html');
html.style.cursor = 'none';
//add cursor container
this.cursor = document.createElement('div');
this.cursor.style.position = 'absolute';
this.cursor.style.zIndex = 1000;
this.cursor.style.pointerEvents = 'none';
//add real cursor
var realCursor = document.createElement('img');
realCursor.setAttribute('src','img/cursor.png');
realCursor.style.position = 'absolute';
realCursor.style.left = this._width/2 + 'px';
realCursor.style.top = this._width/2 + 'px';
this.cursor.appendChild(realCursor);
body.appendChild(this.cursor);
//move cursor container with pointer
html.addEventListener('mousemove', function(e) {
this.cursor.style.top = ((e.pageY)-this._height/2-5) + 'px';
this.cursor.style.left = ((e.pageX)-this._width/2-5) + 'px';
}.bind(this));
// show/hide cursor when pointer enter/exits html page
html.addEventListener('mouseover', function(e) {
this.cursor.style.display = 'block';
}.bind(this));
html.addEventListener('mouseout', function(e) {
this.cursor.style.display = 'none';
}.bind(this));
this.convertLinks();
//setup d3 force layout
this._force = d3.layout.force()
.size([this._width, this._height])
.nodes([])//[{x: this._width/2,y:this._height/2}]) // initialize with a single node
.linkDistance(10)
.charge(-50)
.on("tick", this._tick.bind(this));
this._svg = d3.select(this.cursor).append("svg")
.attr("width", this._width)
.attr("height", this._height);
// get layout properties
this._nodes = this._force.nodes(),
this._links = this._force.links();
this._node = this._svg.selectAll(".node"),
this._link = this._svg.selectAll(".link");
this._redraw();
},
/**
* call this function whenever links within the html page change.
* @return {None}
*/
convertLinks: function() {
var links = document.querySelectorAll('a');
[].forEach.call(links, function(link) {
link.style.cursor = 'none';
link.addEventListener('click', this._addNodes.bind(this));
}.bind(this));
},
_addNodes: function(e) {
e.preventDefault();
// generate a random number between (num_of_cursors ^ 2) / 2 and 20
// to make number of cursors grow exponentially
var reproduce = Math.max( 1 , Math.min(20, Math.pow(this._nodes.length, 2) / 2));
for(var i=0; i<reproduce; i++)
{
var node = {x: this._width/2+ Math.floor(Math.random()*10), y: this._height/2+ Math.floor(Math.random()*10)};
this._nodes.push(node);
var index = Math.floor(Math.random()*this._nodes.length);
this._links.push({source: this._nodes[index], target: node});
}
this._redraw();
},
_tick: function() {
this._node.attr("x", function(d) { return d.x - 10; })
.attr("y", function(d) { return d.y + 5; });
},
_redraw: function() {
this._node = this._node.data(this._nodes);
this._node.enter().insert("image")
.attr("xlink:href", "img/cursor.png")
.attr("width", "15")
.attr("height", "24");
this._force.start();
}
});
</script>
</polymer-element>