-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
279 lines (222 loc) · 7.82 KB
/
script.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
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
var protonNumber = 0, neutronNumber = 0, electronNumber = 0;
var $canvas = $("<canvas>");
var ctx = $canvas[0].getContext("2d");
var $emptyTableRow = $("#emptySpace");
// Set the canvas size
$canvas.attr("width", 300);
$canvas.attr("height", 300);
// canvas parent
var container = $canvas.parentNode;
// Global variables
var radius = 50, lineWidth = 1;
var x = 0;
var y = radius - 10 - lineWidth / 2;
var angle = 0;
var currentElementName = "";
var isFinishedLoading = false;
$(window).on("load", function() {
var generateEmptySpace = function(){
let emptyTableRow = "";
for (let x = 0; x < 19; x++){
emptyTableRow += "<td colspan='0'>ㅤ</td>";
}
$("#emptySpace").append(emptyTableRow);
// CSS stuff
$("#emptySpace td").css("font-size", "1.2em");
$("#emptySpace td").css("color", "black");
// Compatability Stuff
$("#emptySpace td").css("user-select", "none");
$("#emptySpace td").css("pointer-events", "none");
}
generateEmptySpace();
//override default
$('.lanthanides').css('background-color', '#f82882');
$('.actinides').css('background-color', '#35b14c');
let $bohrModel = $("#bohr-model");
$bohrModel.append($canvas);
drawCanvas();
// Click Functions
$('#actinides-hover').click(function() {
changeColor('.actinides', '#c6fdbb');
});
$('#lanthanides-hover').click(function() {
changeColor('.lanthanides', '#ff82b8');
});
$("#exit-button").click(function(){
$("#element-info-modal").hide();
});
$('td:not([colspan])').click(function() {
// Get Proton, Neutron and Electron Number
var text = $(this).text().split(' ')[0];
var number = parseInt(text);
protonNumber = number, neutronNumber = number, electronNumber = number;
// Get element name
var elementName = $(this).find(".element-name").text();
if (elementName) {
currentElementName = elementName;
$("#current-element-name").text(currentElementName);
}
$("#loading-holder").text("Loading...");
$("#electroneg-holder").text("");
$("#atomicnum-holder").text("");
$("#element-info-wiki").text("");
openElementInfoModal();
// Scrape data off the internet because I'm lazy
getData("https://www.periodictable.co.za/", function(data) {
let container = $(data).find("#element-list-section");
let element = container.find('td:contains("' + currentElementName.trim() + '")').not(".react");
let electronegativity = element.nextAll().eq(2).text();
let atomicWeight = element.nextAll().eq(1).text();
var elementData = element.text();
$("#element-info-wiki").text("Stealing from wikipedia... ");
if(electronegativity && atomicWeight && elementData){
$("#loading-holder").text("");
$("#electroneg-holder").text("Electronegativity: " + electronegativity);
$("#atomicnum-holder").text("Atomic Weight: " + atomicWeight);
isFinishedLoading = true;
} else isFinishedLoading = false;
});
getData("https://en.wikipedia.org/wiki/" + currentElementName, function(data) {
// Check if previous data was not loaded
if(!isFinishedLoading){
$("#loading-holder").text("Can't load previous data ://");
}
var elementInfo = $(data).find('p:contains("' + currentElementName.trim() + '")').first().text();
if(elementInfo){
// Clear out any citations
elementInfo = elementInfo.replace(/\[\d+\]|\[\w+\]/g, "");
$("#element-info-wiki").text(elementInfo);
}
});
});
$("#element-info-modal").resize(function() {
$canvas.width = container.clientWidth;
$canvas.height = container.clientHeight;
});
// loop
setInterval(function(){
updateCanvas();
}, 30);
});
function changeColor(selector, color) {
var originalColor = $(selector).css('background-color');
originalColor = rgbToHex(originalColor);
$(selector).css('background-color', color);
var interval = setInterval(function() {
if (originalColor) {
$(selector).css('transition', 'background-color 0.25s');
$(selector).css('background-color', originalColor);
clearInterval(interval);
}
}, 100);
}
function getData(url, successHandler) {
$.ajax({
type: "GET",
url: "https://cors-bypasser.glitch.me/bypass/" + url, // this removes every annoying CORS block so I can steal from them
success: successHandler
});
}
function rgbToHex(rgb) {
var hexValues = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(hexValues[1]) + hex(hexValues[2]) + hex(hexValues[3]);
}
function openElementInfoModal() {
//$("#periodic-table-container").animate({ "margin-right": 300 }, "slow");
$("#element-info-modal").css("display", "block");
}
function getNumberOfRings(atomicNumber) {
// start with the lowest energy level and fill it with the maximum number of electrons it can hold
// (2 for the first energy level, 8 for the second, and so on)
let energyLevel = 1;
let electronsPlaced = 0;
while (atomicNumber > 0) {
// get the maximum number of electrons that the current energy level can hold
let maxElectrons = 0;
if (energyLevel === 1) {
maxElectrons = 2;
} else if (energyLevel === 2) {
maxElectrons = 8;
} else {
maxElectrons = 18;
}
// subtract the number of electrons in the current energy level from the atomic number
electronsPlaced += Math.min(atomicNumber, maxElectrons);
atomicNumber -= Math.min(atomicNumber, maxElectrons);
// move to the next energy level if all of the electrons in the current one have been placed
if (electronsPlaced === maxElectrons) {
energyLevel++;
electronsPlaced = 0;
}
}
// return the number of rings
return energyLevel;
}
// Deprecated but keeping it cause it looks cool
function getNumberOfElectronsInRing(energyLevel) {
return energyLevel === 1 ? 2 : 2 * energyLevel;
}
function drawCanvas(){
var circleX = 150, circleY = 150;
// Draw the circle
ctx.beginPath();
ctx.arc(circleX, circleY, 40, 0, 2 * Math.PI);
ctx.fillStyle = "lightgray";
ctx.fill();
ctx.stroke();
var ringRadius = 50;
var circleRadius = 2;
var lineWidth = 1;
numRings = getNumberOfRings(protonNumber);
ctx.translate(circleX, circleY);
// Set the font and text alignment
ctx.font = "14px Arial";
ctx.textAlign = "center";
ctx.fillStyle = "black";
ctx.fillText(protonNumber + " P", 0, -20);
ctx.fillText(neutronNumber + " N", 0, 30);
//draw the rings
drawRings();
var angles = [];
var numCircles = 1;
for (var i = 0; i < numRings; i++) {
for (var j = 0; j < numCircles; j++) {
if(!angles[i]){
angles[i] = [];
}
if(!angles[i][j]){
angles[i][j] = Math.random()*2*Math.PI;
}
var x = ringRadius * Math.cos(angles[i][j]);
var y = ringRadius * Math.sin(angles[i][j]);
ctx.beginPath();
ctx.arc(x, y, circleRadius, 0, 2 * Math.PI);
ctx.fillStyle = "black";
ctx.fill();
angles[i][j] += 0.1;
//console.log(angles[i][j]);
}
ringRadius += lineWidth * 10;
}
// Restore the context state
ctx.restore();
};
function updateCanvas(){
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, 1000, 1000);
drawCanvas();
}
function drawRings(){
ringRadius = 50;
for (var i = 0; i < numRings; i++) {
ctx.beginPath();
ctx.arc(0, 0, ringRadius, 0, 2 * Math.PI);
ctx.lineWidth = lineWidth;
ctx.strokeStyle = 'black';
ctx.stroke();
ringRadius += lineWidth * 10;
}
};