-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo-mouseclick.html
59 lines (49 loc) · 1.49 KB
/
demo-mouseclick.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Undefined</title>
<script type="text/javascript" src="JSCodeSkulptor.js" ></script>
</head>
<body>
<div id="leftPanel" style="left:0; top:0; width:245px; height: 400px; float:left;"></div>
<canvas id="myCanvas" width="600" height="400" style="border:1px solid #000000; background-color:black;"></canvas>
<script>
////////////////////////////////////////////////////////////////////////////////
// Code Skulptor code below
// Examples of mouse input
// intialize globals
var WIDTH = 450;
var HEIGHT = 300;
var ball_pos = [WIDTH / 2, HEIGHT / 2];
var BALL_RADIUS = 15;
var ball_color = "Red";
// helper function
function distance(p, q) {
return Math.sqrt(Math.pow(p[0] - q[0], 2) + Math.pow(p[1] - q[1], 2));
}
// define event handler for mouse click, draw
function click(pos) {
if (distance(pos, ball_pos) < BALL_RADIUS) {
ball_color = "Green";
} else {
ball_pos = pos;
ball_color = "Red";
}
}
function draw(canvas) {
canvas.draw_circle(ball_pos, BALL_RADIUS, 1, "Black", ball_color);
}
// create frame
var frame = new simplegui.create_frame("Mouse selection", WIDTH, HEIGHT);
frame.set_canvas_background("White");
// register event handler
frame.set_mouseclick_handler(click);
frame.set_draw_handler(draw);
// start frame
frame.start();
// Code Skulptor code above
////////////////////////////////////////////////////////////////////////////////
</script>
</body>
</html>