@zushah/chalkboard 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ /*
2
+ The Chalkboard Library ===> https://www.github.com/Zushah/Chalkboard
3
+ Version 1.5.0 Example Program: Quaternion Donut
4
+ Authored by Zushah ===> https://www.github.com/Zushah
5
+ */
6
+
7
+ // Get the JavaScript Canvas API
8
+ var ctx = document.getElementById("canvas").getContext("2d");
9
+ canvas.width = window.innerWidth;
10
+ canvas.height = window.innerHeight;
11
+
12
+ var cb = Chalkboard; // Initialize Chalkboard as cb
13
+
14
+ // Generate the donut's points with a parametric function (see: https://en.wikipedia.org/wiki/Torus)
15
+ var points = [];
16
+ for(var u = 0; u < cb.PI(2); u += cb.PI(1/16)) {
17
+ for(var v = 0; v < cb.PI(2); v += cb.PI(1/6)) {
18
+ var x = (75 + 30 * cb.trig.cos(v)) * cb.trig.cos(u);
19
+ var y = (75 + 30 * cb.trig.cos(v)) * cb.trig.sin(u);
20
+ var z = 30 * cb.trig.sin(v);
21
+ points.push(cb.vec3.new(x, y, z));
22
+ }
23
+ }
24
+ var theta = 0;
25
+ function main() {
26
+ ctx.fillStyle = "rgb(0, 0, 0)";
27
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
28
+
29
+ // Make the donut rotate with a rotation quaternion
30
+ var r = cb.quat.fromAxis(cb.vec3.normalize(cb.vec3.new(1, 1, 1)), theta);
31
+ var qoints = []; // We'll say that "qoints" are the new rotated points
32
+ for(var i = 0; i < points.length; i++) {
33
+ qoints.push(cb.quat.toRotation(r, points[i]));
34
+ }
35
+ theta += cb.trig.toRad(1);
36
+
37
+ ctx.save();
38
+ ctx.translate(canvas.width / 2, canvas.height / 2);
39
+ ctx.strokeStyle = "rgb(255, 255, 255)";
40
+ for(var i = 0; i < qoints.length; i++) {
41
+ for(var j = 0; j < qoints.length; j++) {
42
+ if(cb.vec3.dist(qoints[i], qoints[j]) < 25) {
43
+ // Draw lines between the donut's qoints to draw the donut
44
+ cb.geom.line3D(qoints[i].x, qoints[i].y, qoints[i].z, qoints[j].x, qoints[j].y, qoints[j].z);
45
+ }
46
+ }
47
+ }
48
+ ctx.restore();
49
+
50
+ window.requestAnimationFrame(main);
51
+ }
52
+ window.requestAnimationFrame(main);
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@zushah/chalkboard",
3
+ "version": "1.5.0",
4
+ "description": "The Chalkboard library provides a plethora of mathematical functionalities for its user.",
5
+ "main": "src/Chalkboard.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Zushah/Chalkboard.git"
12
+ },
13
+ "keywords": [
14
+ "mathematics",
15
+ "khan-academy",
16
+ "processing"
17
+ ],
18
+ "author": "Zushah",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/Zushah/Chalkboard/issues"
22
+ },
23
+ "homepage": "https://zushah.github.io/Chalkboard/home.html"
24
+ }