-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
154 lines (130 loc) · 3.81 KB
/
main.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
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
//the require library is configuring paths
require.config({
paths: {
'jquery': 'libs/jquery/jquery',
'knockout': 'libs/knockout.js/knockout',
'Sammy': 'libs/sammy/lib/min/sammy-latest.min'
}
});
define(
['jquery', 'knockout', 'Sammy'], function ($, ko, Sammy) {
var onload = (function () {
function highlight (url) {
$link = $('#' + (url.path.replace(/[#\/]/g,'') || 'main') + '-link');
$link.parent().toggleClass('active');
$link.parent().siblings().removeClass('active')
}
function template (url) {
return $('#' + (url.path.replace(/[#\/]/g,'') || 'main') + '-template');
}
function reduceFn (previousValue, currentValue, index, array) {
// algorithm of oriented square multiplied by 2
return previousValue +
currentValue['X'] * (array[(index + 1) % array.length]['Y']) -
currentValue['Y'] * (array[(index + 1) % array.length]['X']);
}
function createPath (obj, index) {
path = document.createElementNS('http://www.w3.org/2000/svg',"path");
startObj = obj.shift();
svgData = 'M'+ startObj['X'] + ',' + startObj['Y'];
createPathExpr(obj);
path.setAttributeNS(null, 'd', svgData);
path.setAttributeNS(null, 'style', 'fill:none; stroke:black; stroke-width:1;');
paths.push(path)
$('svg').append(path)
}
function createPathExpr (arr) {
switch (arr.length)
{
case 1:
// 1 point case: plot Q with start
svgData += '\nQ' + arr[0]['X'] + ',' + arr[0]['Y'] + ' ' +
startObj['X'] + ',' + startObj['Y'];
return;
break;
case 2:
// 2 points case: plot C with start
svgData += '\nC' + arr[0]['X'] + ',' + arr[0]['Y'] + ' ' +
arr[1]['X'] + ',' + arr[1]['Y'] + ' ' +
startObj['X'] + ',' + startObj['Y'];
return;
break;
case 3:
// 3 points case: plot two Q
svgData += '\nQ' + arr[0]['X'] + ',' + arr[0]['Y'] + ' ' +
arr[1]['X'] + ',' + arr[1]['Y'] + ' ';
arr.shift();
arr.shift();
createPathExpr(arr);
return;
break;
}
if (arr.length > 3) {
// plot C with 3 dots
svgData += '\nC';
for (var i = 0; i< 3; i++) {
svgData += arr[0]['X'] + ',' + arr[0]['Y'] + ' ';
arr.shift();
}
createPathExpr(arr)
} else {
// array is empty
return;
}
}
var $link, App, app, main, self, curArr, startObj, path
paths = [],
arrayLen = 0,
svgData = '';
return function() {
main = $('#main');
App = function ContainerViewModel () {
self = this;
self.orientations = ko.observableArray();
// obtain orientation json
$.ajax({
url: 'data/orientation.json',
dataType: 'json',
success: function (res) {
res.forEach(function (obj, index) {
self.orientations.push(obj.reduce(reduceFn, 0));
});
}
});
// obtain bezier json
$.ajax({
url: 'data/Bezier.json',
dataType: 'json',
success: function (res) {
res.forEach(createPath);
}
});
this.paths = ko.observableArray(paths);
// this.htmlStructure = ko.observable($('#index').html());
this.openPage = function (url) {
highlight(url);
main.animate({opacity: 0}, 200, function () {
main.find('section').first().prependTo('body');
main.append(template(url))
main.animate({opacity: 1}, 200);
});
};
};
app = new App ();
Sammy('#main', function() {
// define a 'route'
this.get('#orientation', function (url) {
app.openPage(url);
});
this.get('#bezier', function (url) {
app.openPage(url);
});
this.get('', function (url) {
app.openPage(url);
});
}).run('#/');;
ko.applyBindings(app);
}
})();
$(onload);
})