-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrickbreak.js
69 lines (60 loc) · 1.37 KB
/
brickbreak.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var canvas = null;
var context = null;
var batwidth=100;
var batheight=20;
var batx = 0;
var baty = 0;
var ballx = 0;
var bally = 0;
var ballrad = 10;
var velx = 0;
var vely = 0;
var started=false;
function update()
{
ballx += velx*100./1000.;
bally -= vely*100./1000.;
draw();
setTimeout(update, 50);
}
function draw()
{
context.clearRect(0,0,canvas.width,canvas.height);
context.fillRect(batx, baty, batwidth, batheight);
context.beginPath();
context.arc(ballx, bally, ballrad, 0, 2*Math.PI);
context.stroke();
context.strokeText("x: " + batx + "y: " + baty, 10, 10);
}
function mouseMove(e)
{
var halfwidth = batwidth/2;
var halfheight = batheight/2;
batx=e.pageX-halfwidth;
if ( batx < 0 )
batx = 0;
if ( batx > canvas.width-batwidth )
batx=canvas.width-batwidth;
draw();
}
function mouseDown()
{
if ( !started ) {
started=true;
velx=vely=100;
setTimeout(update, 100);
}
}
function init()
{
canvas = document.getElementById("canvas");
canvas.style.cursor="none";
baty=canvas.height-batheight;
ballx = canvas.width/2-ballrad;
bally = baty-ballrad;
context = canvas.getContext("2d");
context.fillStyle="red";
jQuery("#canvas").mousemove(mouseMove);
jQuery("#canvas").click(mouseDown);
}
init();