-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-triangles.js
More file actions
298 lines (243 loc) · 10 KB
/
Copy pathmake-triangles.js
File metadata and controls
298 lines (243 loc) · 10 KB
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
// Build svg equilateral triangles, based on Trianglify @qrohlf.
// Needs d3.js
function Triangles(options) {
if (typeof options === 'undefined') {
options = {};
}
function defaults(opt, def) {
return (typeof opt !== 'undefined') ? opt : def;
}
// defaults
this.options = {
side: defaults(options.side, 50),
transition: defaults(options.transition, null),
viewBox: defaults(options.viewBox, "50 50 600 100")
};
}
Triangles.prototype.generate = function (x,y) {
var options, svg, data;
options = this.options;
svg = this.buildTemplate(x,y); //svg element is returned from buildTemplate
data = this.buildData(x,y,options); //return data set to be used for triangle points and colors
this.displayTriangles(data,svg);
if(options.transition !== null) { //if transition is not null, set it
this.setTransition(options.transition, svg);
}
return data;
};
Triangles.prototype.generateFromJSArray = function (x,y,JSONData) {
var jsArray, options, svg;
jsArray = JSON.parse(JSONData);
options = this.options;
svg = this.buildTemplate(x,y); //svg element is returned from buildTemplate
this.displayTriangles(jsArray,svg);
if(options.transition !== null) { //if transition is not null, set it
this.setTransition(options.transition, svg)
}
};
Triangles.prototype.buildTemplate = function(x,y){
var wrapper, svgWrapper, svg;
wrapper = d3.select(".triangle-wrapper"); //element class named triangle wrapper must exist
svgWrapper = wrapper //create an svg wrapper
.append("div")
.attr("class", "svg-wrapper");
svg = svgWrapper //build the svg, and return it
.append("svg")
.attr("viewBox",this.options.viewBox)
.attr("version","1.1")
.attr("xmlns","http://www.w3.org/2000/svg");
return svg;
};
Triangles.prototype.buildData = function(x,y,options){
var columns, fillY, height, data, runnerX, runnerY;
columns = Math.ceil(x/(options.side*2)), //number of chevron columns that fit in grid
fillY = Math.ceil(y/(options.side*1.5)), //number of chevron triangle groups that will fit in 1 column
height = (options.side/2)*Math.sqrt(3), //calculate the height of the equilateral triangle
data = [], //data will be built in the following structure:
runnerX = 0,
runnerY = 0;
//Reproduce image on Canvas.
var cvs = document.createElement("canvas"),
img = document.getElementsByTagName("img")[0]; // your image goes here
cvs.width = img.width;
cvs.height = img.height;
var ctx = cvs.getContext("2d");
ctx.drawImage(img,0,0,cvs.width,cvs.height);
//Set up variables for canvas here.
var canvasRunnerX, canvasRunnerY,
canvasColumnWidth, canvasRowHeight,
triHeight, triSide,
pixelData, canvasString,
outerLeft, outerRight, innerLeft, innerRight;
canvasRunnerX = 0,
canvasRunnerY = 0,
canvasColumnWidth = cvs.width/columns,
canvasRowHeight = cvs.height/fillY,
triHeight = canvasColumnWidth/2,
triSide = canvasRowHeight/1.5,
pixelData = ctx.getImageData(canvasRunnerX,canvasRunnerY,1,1).data;
// End IMG Canvas variables
for(var i=0; i<columns; i++){
canvasRunnerY = 0;
runnerY = 0;
for(var j=0; j<fillY; j++) {
pixelData = ctx.getImageData((canvasRunnerX+(triHeight/2)),(canvasRunnerY+(triSide/2)),1,1).data;
canvasString = 'rgb(' + pixelData[0] + ',' + pixelData[1] + ',' + pixelData[2] +')';
outerLeft = {
point1x: runnerX,
point1y: runnerY,
point2x: runnerX + height,
point2y: runnerY + (options.side / 2),
point3x: runnerX,
point3y: runnerY + options.side,
color: canvasString,
column: i,
chevron: j
};
pixelData = ctx.getImageData((canvasRunnerX+(triHeight*1.5)),(canvasRunnerY+(triSide/2)),1,1).data;
canvasString = 'rgb(' + pixelData[0] + ',' + pixelData[1] + ',' + pixelData[2] +')';
outerRight = {
point1x: runnerX + height*2,
point1y: runnerY,
point2x: runnerX + height,
point2y: runnerY + (options.side / 2),
point3x: runnerX + height*2,
point3y: runnerY + options.side,
color: canvasString,
column: i,
chevron: 20+j
};
pixelData = ctx.getImageData(canvasRunnerX+(triHeight/2),(canvasRunnerY+(triSide)),1,1).data;
canvasString = 'rgb(' + pixelData[0] + ',' + pixelData[1] + ',' + pixelData[2] +')';
innerLeft = {
point1x: runnerX + height,
point1y: runnerY + (options.side / 2),
point2x: runnerX + height,
point2y: runnerY + options.side*1.5,
point3x: runnerX,
point3y: runnerY + options.side,
color: canvasString,
column: i,
chevron: 20+j
};
pixelData = ctx.getImageData(canvasRunnerX+(triHeight*1.5),(canvasRunnerY+triSide),1,1).data;
canvasString = 'rgb(' + pixelData[0] + ',' + pixelData[1] + ',' + pixelData[2] +')';
innerRight = {
point1x: runnerX + height,
point1y: runnerY + (options.side / 2),
point2x: runnerX + height,
point2y: runnerY + options.side*1.5,
point3x: runnerX + height*2,
point3y: runnerY + options.side,
color: canvasString,
column: i,
chevron: 20+j
};
data.push(outerLeft); //push triangles (that make up the chevron) into the dataset
data.push(outerRight);
data.push(innerLeft);
data.push(innerRight);
runnerY += options.side; //increment runner on y-axis, new chevron
canvasRunnerY += canvasRowHeight;
}
runnerX += height*2; //increment runner on x-axis, new column
canvasRunnerX += canvasColumnWidth;
}
return this.nestData(data);
};
Triangles.prototype.nestData = function(data){
// nest the data by column
var nest, nested;
nest = d3.nest()
.key(function(d) { return d.column; });
nested = nest.entries(data);
return nested;
};
Triangles.prototype.displayTriangles = function(data,svg){
var group = svg
.selectAll("g")
.data(data)
.enter()
.append("g");
var path = group.selectAll("path")
.data(function(d) { return d.values; })
.enter()
.append("path")
.attr("d", function(d){
return "M " + d.point1x + " " + d.point1y +
" L " + d.point2x + " " + d.point2y +
" L " + d.point3x + " " + d.point3y + " z";
})
.style("fill", function(d){
return d.color;
})
.style("stroke", function(d){
return d.color;
})
.style("stroke-width", function(d){
return 1;
});
};
Triangles.prototype.setTransition = function(transition, svg){
var column, paths, transformed;
svg
.on("mouseover", function() {
d3
.selectAll("g")
.each(function () {
column = d3.select(this);
transformed = column[0][0].getAttribute("transformed");
paths = column.selectAll("path");
if(transformed == "null" || transformed == "false"){
paths
.transition()
.delay(function (d, i) {
return i * 50 + (d.column * 50);
})
.attr("transform", function (d) {
return transition;
});
column
.attr("transformed", function (d) {
return "true";
});
}
else {
paths
.transition()
.delay(function (d, i) {
return i * 50 + (d.column * 50);
})
.attr("transform", function (d) {
return "";
});
column
.attr("transformed", function (d) {
return "false";
});
}
});
});
svg
.on("mouseleave", function() {
d3
.selectAll("g")
.each(function () {
column = d3.select(this);
transformed = column[0][0].getAttribute("transformed");
paths = column.selectAll("path");
paths
.transition()
.delay(function (d, i) {
return i * 50 + (d.column * 50);
})
.attr("transform", function (d) {
return "";
});
column
.attr("transformed", function (d) {
return "false";
});
});
});
};