Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from ToeJamson/1.js
Last active November 15, 2017 21:06
Show Gist options
  • Save domadev812/875071a3d6ece986effb8f59724aa690 to your computer and use it in GitHub Desktop.
Save domadev812/875071a3d6ece986effb8f59724aa690 to your computer and use it in GitHub Desktop.
Creating Real-Time WebGL Visualizations
function handleMsg(msg) {
if (VISIBLE) {
addData(msg.pub, msg.subs);
}
}
pubnub = new PubNub({
publishKey : 'demo',
subscribeKey : 'demo'
})
pubnub.addListener({
message: function(message) {
handleMsg(message);
}
})
pubnub.subscribe({
channels : ["real-time-stats-geostats"],
});
el.addEventListener('mousedown', function (event) {
isDown = true;
IDLE = false;
clearTimeout(interval);
onDownMouse = {
x: event.clientX,
y: -event.clientY
};
targetDownMouse = {
x: target.x,
y: target.y
};
});
el.addEventListener('mouseup', function (event) {
isDown = false;
clearTimeout(interval);
interval = setTimeout(function () {
IDLE = true;
}, IDLE_TIME);
});
el.addEventListener('mousemove', function (event) {
if (isDown == true) {
var mouseX = event.clientX,
mouseY = -event.clientY;
target.x = targetDownMouse.x + (onDownMouse.x - mouseX) * 0.005;
target.y = targetDownMouse.y + (onDownMouse.y - mouseY) * 0.005;
target.y = target.y > PI_HALF ? PI_HALF : target.y;
target.y = target.y < -PI_HALF ? -PI_HALF : target.y;
}
});
el.addEventListener('mousewheel', function (event) {
target.zoom -= event.wheelDeltaY * 0.3;
if (target.zoom > 3000) target.zoom = 3000;
if (target.zoom < 1300) target.zoom = 1300;
event.preventDefault();
return false;
});
function render() {
// … other rendering code
rotation.x += (target.x - rotation.x) * 0.1;
rotation.y += (target.y - rotation.y) * 0.1;
DISTANCE += (target.zoom - DISTANCE) * 0.3;
checkIdle();
// Convert our 2d camera target into 3d world coords
camera.position.x = DISTANCE * Math.sin(rotation.x) * Math.cos(rotation.y);
camera.position.y = DISTANCE * Math.sin(rotation.y);
camera.position.z = DISTANCE * Math.cos(rotation.x) * Math.cos(rotation.y);
camera.lookAt( scene.position );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment