Skip to content

Commit

Permalink
Update dependencies. Modernize syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpeiris committed Dec 21, 2019
1 parent 76b4702 commit 1ddc32e
Show file tree
Hide file tree
Showing 18 changed files with 5,374 additions and 4,010 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2015 Brian Peiris and contributors
Copyright (c) 2014-2019 Brian Peiris and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
60 changes: 27 additions & 33 deletions js/File.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
define([
],
function (
) {
var File = function (name, contents) {
this.name = name || 'Example';
export default class File {
constructor(name, contents) {
this.name = name || "Example";
this.contents = contents;
this.selected = true;
};
File.prototype.findNumberAt = function (index) {
}
findNumberAt(index) {
var match = this.contents.substring(index).match(/-?\d+\.?\d*/);
if (match) { return match[0]; }
};
File.prototype.spinNumber = function (number, direction, amount) {
if (number.indexOf('.') === -1) {
return (parseInt(number, 10) + direction * amount).toString();
if (match) {
return match[0];
}
else {
}
spinNumber(number, direction, amount) {
if (number.indexOf(".") === -1) {
return (parseInt(number, 10) + direction * amount).toString();
} else {
return (parseFloat(number) + direction * amount).toFixed(2);
}
};
File.prototype.spinNumberAt = function (
index, direction, amount, originalNumber
) {
}
spinNumberAt(index, direction, amount, originalNumber) {
var number = this.findNumberAt(index);
if (number === undefined) { return; }
if (number === undefined) {
return;
}
originalNumber = originalNumber || number;
var newNumber = this.spinNumber(originalNumber, direction, amount);
this.contents = (
this.contents.substring(0, index) +
newNumber +
this.contents.substring(index + number.length)
);
};
File.prototype.recordOriginalNumberAt = function (index) {
this.contents = this.contents.substring(0, index) + newNumber + this.contents.substring(index + number.length);
}
recordOriginalNumberAt(index) {
var number = this.findNumberAt(index);
if (number === undefined) { return; }
if (number === undefined) {
return;
}
this.originalNumber = number;
this.originalIndex = index;
};
File.prototype.offsetOriginalNumber = function (offset) {
}
offsetOriginalNumber(offset) {
this.spinNumberAt(this.originalIndex, 1, offset, this.originalNumber);
};
return File;
});

}
}
69 changes: 26 additions & 43 deletions js/Files/Behaviors.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,50 @@
var flockingBehavior = function (
boid, world, hands
) {
var flockingBehavior = function(boid, world, hands) {
var closestBoids = [];

var getLookAtQuaternion = function (otherBoid) {
var getLookAtQuaternion = function(otherBoid) {
var q = new t3.Quaternion();
q.copy(boid.obj.quaternion);
var m1 = new t3.Matrix4();
m1.lookAt(
otherBoid.obj.position,
boid.obj.position,
boid.obj.up);
m1.lookAt(otherBoid.obj.position, boid.obj.position, boid.obj.up);
q.setFromRotationMatrix(m1);
return q;
};

var separate = function (otherBoid) {
if (
otherBoid.obj.position.distanceTo(
boid.obj.position) < 0.1
) { return; }
var q = getLookAtQuaternion(
otherBoid);
var separate = function(otherBoid) {
if (otherBoid.obj.position.distanceTo(boid.obj.position) < 0.1) {
return;
}
var q = getLookAtQuaternion(otherBoid);
q.conjugate();

var SEPARATION = 0.1;

boid.obj.quaternion.slerp(
q, SEPARATION
);
boid.obj.quaternion.slerp(q, SEPARATION);
};

var align = function (otherBoid) {
boid.obj.quaternion.slerp(
otherBoid.obj.quaternion, 0.01
);
var align = function(otherBoid) {
boid.obj.quaternion.slerp(otherBoid.obj.quaternion, 0.01);
};

var adhere = function (otherBoid) {
if (
otherBoid.obj.position.distanceTo(
boid.obj.position) > 0.2
) { return; }
var adhere = function(otherBoid) {
if (otherBoid.obj.position.distanceTo(boid.obj.position) > 0.2) {
return;
}
var q = getLookAtQuaternion(otherBoid);
boid.obj.quaternion.slerp(
q, 0.005
);
boid.obj.quaternion.slerp(q, 0.005);
};

var attractTo = function (hands) {
var attractTo = function(hands) {
var hand = hands[0] || hands[1];
if (!hand) { return; }
var q = getLookAtQuaternion({obj: {position: hand}});
boid.obj.quaternion.slerp(
q, 0.05
);
if (!hand) {
return;
}
var q = getLookAtQuaternion({ obj: { position: hand } });
boid.obj.quaternion.slerp(q, 0.05);
};

world.boids.forEach(function (otherBoid) {
if (
otherBoid.obj.position.distanceTo(
boid.obj.position) < 0.5
) {
world.boids.forEach(function(otherBoid) {
if (otherBoid.obj.position.distanceTo(boid.obj.position) < 0.5) {
separate(otherBoid);
align(otherBoid);
adhere(otherBoid);
Expand All @@ -72,11 +55,11 @@ var flockingBehavior = function (
});
};

var moveBehavior = function (boid) {
var moveBehavior = function(boid) {
boid.obj.translateZ(0.05);
};

var returnToOrigin = function (boid) {
var returnToOrigin = function(boid) {
if (boid.obj.position.distanceTo(scene.position) > 4) {
boid.obj.lookAt(scene.position);
}
Expand Down
29 changes: 10 additions & 19 deletions js/Files/Boid.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@

var Boid = function (world) {
var Boid = function(world) {
this.world = world;
this.behaviors = [
moveBehavior,
flockingBehavior,
returnToOrigin
];
this.behaviors = [moveBehavior, flockingBehavior, returnToOrigin];
var color = new t3.Color();
color.setHSL(Math.random(), 1, 0.5);
this.visual = new t3.Mesh(
new t3.TetrahedronGeometry(0.06, 0),
new t3.MeshLambertMaterial({color: color})
);
this.visual.quaternion.set(
0.2798, -0.3648, -0.1163, 0.8804);
this.visual = new t3.Mesh(new t3.TetrahedronGeometry(0.06, 0), new t3.MeshLambertMaterial({ color: color }));
this.visual.quaternion.set(0.2798, -0.3648, -0.1163, 0.8804);

var boid = new t3.Object3D();
boid.add(this.visual);
Expand All @@ -22,10 +13,10 @@ var Boid = function (world) {
scene.add(boid);
this.obj = boid;
};
Boid.prototype.step = function (hands) {
this.behaviors.forEach(function (behavior) {
behavior(this, this.world, hands);
}.bind(this));
Boid.prototype.step = function(hands) {
this.behaviors.forEach(
function(behavior) {
behavior(this, this.world, hands);
}.bind(this)
);
};


6 changes: 3 additions & 3 deletions js/Files/Cube.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ var light = new t3.PointLight();
light.position.set(1, 1, 1);
scene.add(light);

function makeCube (x, y, z) {
function makeCube(x, y, z) {
var cube = new t3.Mesh(
new t3.BoxGeometry(1, 1, 1),
new t3.MeshLambertMaterial({
color: 'red'
color: "red"
})
);
cube.position.set(x, y, z);
Expand All @@ -18,7 +18,7 @@ var cube = makeCube(0, 0, 0);
scene.add(cube);

var i = 0;
return function () {
return function() {
cube.rotation.y = i;
i += 0.02;
};
57 changes: 25 additions & 32 deletions js/Files/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,36 @@ scene.add(light);

scene.add(new t3.AmbientLight(0xaaaaaa));

var makeCube = function () {
var cube = new t3.Mesh(
new t3.BoxGeometry(1, 1, 1),
new t3.MeshLambertMaterial({color: 'red'})
);
var makeCube = function() {
var cube = new t3.Mesh(new t3.BoxGeometry(1, 1, 1), new t3.MeshLambertMaterial({ color: "red" }));
cube.position.set(1, 1, 1);
scene.add(cube);
return cube;
};

var Watch = function (obj, name) {
var Watch = function(obj, name) {
this.canvasSize = 200;
var canvas = document.createElement('canvas');
var canvas = document.createElement("canvas");
canvas.width = this.canvasSize;
canvas.height = this.canvasSize / 2;

this.context = canvas.getContext('2d');
this.context.font = '40px Inconsolata,monospace';
this.context = canvas.getContext("2d");
this.context.font = "40px Inconsolata,monospace";
// this.context.globalCompositeOperation = 'darker';
this.textTexture = new THREE.Texture(canvas);
this.textTexture.needsUpdate = true;
var textAreaMat = new THREE.MeshBasicMaterial(
{map: this.textTexture, side: THREE.DoubleSide});
var textAreaMat = new THREE.MeshBasicMaterial({ map: this.textTexture, side: THREE.DoubleSide });

var watch = new THREE.Mesh(
new THREE.PlaneBufferGeometry(1, 0.5),
new THREE.MeshBasicMaterial(textAreaMat));
var watch = new THREE.Mesh(new THREE.PlaneBufferGeometry(1, 0.5), new THREE.MeshBasicMaterial(textAreaMat));
watch.rotation.y = Math.PI;

scene.add(watch);

this.update = function () {
this.update = function() {
this.context.clearRect(0, 0, this.canvasSize, this.canvasSize);
this.context.fillStyle = 'white';
this.context.fillStyle = "white";
this.context.fillRect(0, 0, this.canvasSize, this.canvasSize);
this.context.fillStyle = 'black';
this.context.fillStyle = "black";
this.context.fillText(obj[name], 0, 40);
this.textTexture.needsUpdate = true;
};
Expand All @@ -53,32 +47,31 @@ var world = {
var numBoids = 10;

var positions = [
new t3.Vector3(0.00, 1.60, -1.60),
new t3.Vector3(0.10, 1.50, -1.60),
new t3.Vector3(0.70, 1.80, -1.80),
new t3.Vector3(0.40, 1.60, -1.60),
new t3.Vector3(-0.40, 1.30, -2.00),
new t3.Vector3(0.00, 1.30, -1.20),
new t3.Vector3(-0.20, 1.20, -1.60),
new t3.Vector3(0.00, 1.40, -2.60),
new t3.Vector3(0.00, 1.60, -2.60),
new t3.Vector3(0.60, 1.20, -1.60)
new t3.Vector3(0.0, 1.6, -1.6),
new t3.Vector3(0.1, 1.5, -1.6),
new t3.Vector3(0.7, 1.8, -1.8),
new t3.Vector3(0.4, 1.6, -1.6),
new t3.Vector3(-0.4, 1.3, -2.0),
new t3.Vector3(0.0, 1.3, -1.2),
new t3.Vector3(-0.2, 1.2, -1.6),
new t3.Vector3(0.0, 1.4, -2.6),
new t3.Vector3(0.0, 1.6, -2.6),
new t3.Vector3(0.6, 1.2, -1.6)
];

for (var i = 0; i < numBoids; i++) {
var boid = new Boid(world);
var boid = new Boid(world);
boid.obj.position.copy(positions[i]);
boid.visual.material.color.setHSL(i / 10, 1, 0.5);
world.boids.push(boid);
}

return function loop () {
return function loop() {
for (var i = 0; i < numBoids; i++) {
var boid = world.boids[i];
if (typeof hands !== 'undefined') {
if (typeof hands !== "undefined") {
boid.step(hands);
}
else {
} else {
boid.step();
}
}
Expand Down
Loading

0 comments on commit 1ddc32e

Please sign in to comment.