Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<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 x
public
Bin info
anonymouspro
0viewers