-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype3.html
More file actions
176 lines (162 loc) · 5.96 KB
/
prototype3.html
File metadata and controls
176 lines (162 loc) · 5.96 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bakeoff 2 - Double Tap</title>
<style type="text/css">
/* Basic styling */
body {
height: 100vh;
width: 100vw;
margin: 0;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: relative;
}
body.active {
border-left: 3px #f06 solid;
}
/* Canvas container */
#main {
position: relative;
z-index: 1;
}
#markers {
position: absolute;
top: 0;
left: 0;
z-index: 2;
pointer-events: none;
}
/* Button container overlays the bottom of the canvas,
but pointer events only work on the buttons, not the container */
.button-container {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
pointer-events: none; /* container ignores pointer events */
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 10px;
z-index: 2;
}
.button-container button {
pointer-events: auto; /* enable pointer events on buttons */
padding: 0.5em 1em;
font-size: 16px;
}
svg {
border: 1px #ddd solid;
}
</style>
<!-- Required scripts -->
<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0/dist/svg.min.js"></script>
<script src="https://dhcs-s25-bakeoff2.glitch.me/framework.js"></script>
</head>
<body>
<!-- SVG drawing area -->
<div id="main"></div>
</body>
<script type="text/javascript">
// =========== Required Bakeoff Setup ===========
const tasksLength = 10; // for the Bakeoff
let svg = SVG().addTo('#main').size(""+canvasSize+"px", ""+canvasSize+"px");
const judge = new Judge(tasksLength, svg, "mayooooooo");
// =========== End Required Setup ===========
// Colors and styling:
const targetStrokeColor = "#FFA500";
const playerColor = "navy";
const markerColor = "red";
// Groups for player's square, target square, and corner markers.
let manipulator = svg.group();
let goal = svg.group();
let cornerMarkersGroup = svg.group();
// -------------------------------
// Two-tap mechanism globals:
// -------------------------------
// twoTapState: "waitingForFirst", "waitingForSecond", "done"
let twoTapState = "waitingForFirst";
let pointA = null, pointC = null;
// -------------------------------
// Click handler to record taps and add red corner markers.
// -------------------------------
svg.on("click", (e) => {
// If a square was already finalized, then clear only the player's square.
if (twoTapState === "done") {
manipulator.clear();
twoTapState = "waitingForFirst";
pointA = pointC = null;
}
// Do not process clicks if all tasks are complete.
if (judge.getTaskNumber() >= tasksLength) return;
// Create a red marker (a small circle) at the tapped location.
let marker = svg.circle(8).center(e.offsetX, e.offsetY).fill(markerColor);
cornerMarkersGroup.add(marker);
if (twoTapState === "waitingForFirst") {
// First tap: record point A.
pointA = { x: e.offsetX, y: e.offsetY };
twoTapState = "waitingForSecond";
} else if (twoTapState === "waitingForSecond") {
// Second tap: record point C (diagonally opposite to point A).
pointC = { x: e.offsetX, y: e.offsetY };
finalizeSquare();
cornerMarkersGroup.clear();
twoTapState = "done";
}
});
// -------------------------------
// Function to finalize the square using points A and C.
// -------------------------------
function finalizeSquare() {
// Compute the center of the square as the midpoint of A and C.
let centerX = (pointA.x + pointC.x) / 2;
let centerY = (pointA.y + pointC.y) / 2;
// Compute the diagonal length.
let diagLength = Math.hypot(pointC.x - pointA.x, pointC.y - pointA.y);
// The side length is the diagonal divided by sqrt(2).
let side = diagLength / Math.SQRT2;
// Calculate the angle of the diagonal in degrees.
let diagAngleDeg = Math.atan2(pointC.y - pointA.y, pointC.x - pointA.x) * 180 / Math.PI;
// For a square, the rectangle rotation is the diagonal angle minus 45°.
let squareAngle = diagAngleDeg - 45;
// Create the player's square as a navy blue filled rectangle.
let newSquare = svg.rect(side, side)
.center(centerX, centerY)
.rotate(squareAngle)
.fill(playerColor);
// Ensure only one square exists: clear any previous square.
manipulator.clear();
manipulator.add(newSquare);
// Assign the new square as the start square for the current task.
let task = judge.getCurrentTask();
task.start.square = newSquare;
}
judge.on("newTask", () => {
manipulator.clear();
cornerMarkersGroup.clear();
let task = judge.getCurrentTask();
task.start.square.fill(playerColor);
newSquare = task.start.square;
manipulator.add(newSquare)
task.goal.square.fill("none");
task.goal.square.stroke({ color: targetStrokeColor, width: 2});
goal.add(task.goal.square);
// Reset the two-tap mechanism.
twoTapState = "waitingForFirst";
pointA = pointC = null;
});
judge.on("testOver", (data) => {
for (let i=1; i<data.scores.length; i++) { // starting on the second one, since the first one is the "learning" trial, which doesn't affect the score
let score = data.scores[i];
let summedScore = (8*score.distance) + (900*score.scaleFactor) + (4*score.rotationFactor); // using the weight values as listed on Canvas
console.log("Trial "+i+": "+summedScore);
}
});
judge.setup();
</script>
</html>