Movimentando bola e Div

classic Clássica list Lista threaded Em Árvore
1 mensagem Opções
Responder | Em Árvore
Abra essa mensagem na visão em árvore
|

Movimentando bola e Div

jsmoraes
Administrador
Texto
Texto do Div Texto do Div Texto do Div

Código fonte:
<nabble_embed><canvas id="myCanvas" width="480" height="320"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("myCanvas");
var meudiv = document.getElementById("nomedodiv")
var ctx = canvas.getContext("2d");
var ballRadius = 20;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;

function drawBall() {
    ctx.beginPath();
    ctx.arc(x, y, ballRadius, 0, Math.PI*2);
    ctx.fillStyle = "#0095DD";
    ctx.fill();
    ctx.closePath();
meudiv.style.left = x-30 + "px";
meudiv.style.top = y-328 +"px";
}

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawBall();
    
    if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
        dx = -dx;
    }
    if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
        dy = -dy;
    }
    
    x += dx;
    y += dy;
}

setInterval(draw, 15);
</script>
    <style type="text/css">
      canvas { border: 1px solid white; background: #eee; }
    </style>
   
GSO 305 mm
NEQ6 Pro - Roda 5 Filtros Manual
Guia: OAG TSOAG9T2 - ASI120MC
Cannon EOS T3 - QHY163M
ASI120MC, DSI-1 Meade e SPC880