<body>
<canvas id="mine" width="500px" height="500px"></canvas>
</body>
canvas {
cursor: none;
border: 1px solid black;
}
window.onload=function start() {
canvas = document.getElementById('mine');
cxt = canvas.getContext('2d');
canvas.addEventListener("mousemove",move,false);
window.addEventListener("keydown",doKeyDown,false);
offsetXY = canvas.getBoundingClientRect();
}
var z=0,y=250,x=250,l=50,h=70,s,w,centerX,centerY,nomove=false,add=0;
setInterval(draw,30);
function draw() {
if(z==0){
xmove = getRand();
ymove = getRand();
z++;
}
cxt.clearRect(0,0,500,500)
//if((x+xmove) - centerX+12.5 < 25) xmove*=-1;//30 is distance between both centers
//if((y+ymove) - centerY < 30) ymove+=-1;
if(nomove==false) {
if(y+ymove>=w && y+ymove<=w+h && x>=s && x<=s+l /*&& centerY+17.5 < y+ymove+5 && centerX+12.5 < x+xmove+5*/) ymove*=-1; //top and bottom, extra +5 for radius
if(x+xmove>=s && x+xmove<=s+l && y+ymove<=w+h && y+ymove>=w /*&& centerX+12.5 < x+xmove+5 && centerY+17.5 < y+ymove+5*/) xmove*=-1; //left and right
}
//if(
if(y+ymove<=0 || y+ymove>=500) ymove*=-1;
if(x+xmove>=500 || x+xmove<=0) xmove*=-1;
cxt.beginPath();
cxt.arc(x+=xmove,y+=ymove,10,0,2*Math.PI);
cxt.stroke();
if(nomove==false) {
cxt.beginPath();
cxt.fillRect(s,w,l,h); //s->w \/
cxt.stroke();
}
//console.log(x + ' y: ' + y);
}
function move(e) {
console.log('IN');
w=e.clientY-offsetXY.top;
s=e.clientX-offsetXY.left;
centerX = w/2,centerY = s/2;
console.log('centX ' + centerX + ' centY ' + centerY);
console.log('w ' + w + ' s ' + s);
}
function doKeyDown(e) {
add++;
switch(e.keyCode) {
case 87:
if(add % 2 !==0){
nomove=true;
}else{
nomove=false;
}
}
}
function getRand() {
while((r = Math.round(Math.random()*2-1))==0);
if(r>0){
return 5;
}else{
return -5;
}
}
Output
300px
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |