Published
Edited
Aug 20, 2020
Insert cell
md`# 3D Draggable Audio Heads`
Insert cell
vidya = html`<div font-family="Roboto" style="font-weight:bold">Drag Me!!!!!</div><br>Get closer to either Brent or Oscar to listen in on their conversation.<br><br>
<div id="video-room" style="font-weight:bold; font-family:sans-serif">video is not in a room</div>
<div id="oscar-room" style="font-weight:bold; font-family:sans-serif">oscar is not in a room</div>
<div id="brent-room" style="font-weight:bold; font-family:sans-serif">brent is not in a room</div>
<div class="app-container">

<div id="add-room" class="button cool-border">+</div>
<div class="room cool-border" id="room-1"></div>

<span id="brent"><img class="speaking" src="${await FileAttachment("butt.png").url()}"></span>

<span id="video"><video class="object cool-border" draggable="true" ondragstart="event.dataTransfer.setData('text/plain',null)" autoplay></video>
</span>

<span id="oscar"><img class="speaking" src="${await FileAttachment("oscar.png").url()}"></span>

</div>`
Insert cell
{
vidya;
var video = document.getElementById('video').firstChild;

if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true, audio: false }).then(function(stream) {
video.srcObject = stream;
video.play();
});
}
}
Insert cell
audya = html`
<audio id="brent_noise">
<source src=${await FileAttachment("brent.mp3").url()} type="audio/mpeg">
</audio>

<audio id="oscar_noise">
<source src=${await FileAttachment("oscar.mp3").url()} type="audio/mpeg">
</audio>
`
Insert cell
{
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var myAudio = document.querySelector('#oscar_noise');
var source = audioCtx.createMediaElementSource(myAudio);
source.crossOrigin = "anonymous";

var analyser = audioCtx.createAnalyser();
analyser.fftSize = 2048;
var bufferLength = analyser.frequencyBinCount;
var dataArray = new Float32Array(bufferLength);
analyser.getFloatTimeDomainData(dataArray);

console.log(dataArray)
}
Insert cell
{
audya;
document.getElementById("oscar_noise").play();
document.getElementById("brent_noise").play();

document.getElementById("oscar_noise").volume = 1.0//100 / 900;
document.getElementById("brent_noise").volume = 1.0//(900 - 100) / 900;
}
Insert cell
my_css = html`<style>

.app-container {
height:700px;
}

img, .object {
width: 100px;
height: 100px;
border-radius:50%;
object-fit: cover;
position: absolute;
left: 300px;
top: 140px;
}

.room{
border-radius:50%;
position: relative;
left: 200px;
top: 200px;
width: 300px;
height: 300px;
visibility:hidden;
}

.button {
border-radius:50%;
position: relative;
left: 405px;
top: 50px;
width: 20px;
height: 20px;
text-align:center;
color:CornflowerBlue;
}

#brent > img {
left: 500px;
}

#oscar > img {
left: 100px;
}

#add-room {
padding-left:1px;
padding-bottom:1px;
}

.cool-border, img {
border-color:CornflowerBlue;
border-style:solid;
border-width:1.5px;
}

.speaking {
animation: pulseoutline 1s infinite;
animation-direction: alternate ;
border: solid 5px #a9c4f5;
margin: 5px;
}

@keyframes pulseoutline {
0% {margin: 5px; border: solid 5px #a9c4f5;}
50% {margin: 0px; border: solid 10px #a9c4f5;}
}

</style>
`
Insert cell
room = Object({video:0, brent:0, oscar:0})
Insert cell
{
vidya; audya;
var video = document.getElementById('video');
var initial_coords = [0,0]
//https://stackoverflow.com/questions/38655964/how-to-remove-dragghost-image#
document.addEventListener("dragstart", function( e ) {
var img = new Image();
img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=';
e.dataTransfer.setDragImage(img, 0, 0);
initial_coords[0] = e.clientX - e.target.offsetLeft;
initial_coords[1] = e.clientY - e.target.offsetTop;
e.target.style.cursor = 'none'
}, false);
var drag_handler = function(e) {
e.preventDefault();
if (e.clientX != 0) {
// TODO - what is the appropriate transformation from e.clientX, e.clientY to the corner of the image?
e.target.style.left = e.clientX - initial_coords[0] + "px";
e.target.style.top = e.clientY - initial_coords[1] + "px";
//console.log(this.id);
//TODO: fix disgusting hack
document.getElementById("add-room").style.left = e.clientX - initial_coords[0] + 105 + "px";
document.getElementById("add-room").style.top = e.clientY - initial_coords[1] - 85 + "px";
var room_coords = [350,500]
var target_coords = [+e.target.style.left.slice(0,-2) + 50, +e.target.style.top.slice(0,-2) + 50]
let name = this.id;
if (Math.sqrt((room_coords[0] - target_coords[0])**2 + (room_coords[1] - target_coords[1])**2) > 150) {
document.querySelector("#"+name+"-room").textContent = name+" is not in a room."
room[name] = 0;
}
else {
document.querySelector("#"+name+"-room").textContent = name+" is in room 1."
room[name] = 1;
}
if (room["video"] == room["oscar"]) {
document.getElementById("oscar_noise").volume = 1.0;
}
else {
document.getElementById("oscar_noise").volume = 0.0;
}
if (room["video"] == room["brent"]) {
document.getElementById("brent_noise").volume = 1.0;
}
else {
document.getElementById("brent_noise").volume = 0.0;
}
}
}
video.addEventListener("drag", drag_handler);
document.getElementById('oscar').addEventListener("drag", drag_handler);
document.getElementById('brent').addEventListener("drag", drag_handler);
document.getElementById('add-room').addEventListener("click", function () {document.getElementById("room-1").style.visibility = "visible"});
document.addEventListener('dragover', function(e) {
e.preventDefault();
return false;
});
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more