<html>
<head>
<script>
window.onload = function () {
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var moveGuy = 0;
context.lineWidth = 2;
context.strokeStyle = "black";
context.font = "30pt Calibri";
context.fillText('Survival Politics', 270, 40);
var guyHealth = 230;
//Draw separator block
context.fillRect(0,465,810,20);
// Create Data Boxes
context.font = "10pt Calibri";
context.strokeRect(180, 490, 40, 20);
context.fillText('Day: '+ moveGuy, 155, 505);
//Game format
context.beginPath();
context.moveTo(0,230);
context.lineTo(810, 230);
context.stroke();
context.beginPath();
context.lineTo(0,230);
context.font = "10pt Calibri";
context.fillText('Click on the above blue square', 30, 570);
context.fillText('to see the results of your decisions.', 30, 590);
context.fillStyle = "#FF0000";
context.fillText('Click the circular arrow in the top left of your screen for a new game.', 230, 710);
// Draw box lower to check for input
context.fillStyle = "blue";
context.fillRect(50, 500, 50, 50);
context.fillStyle = "#FF0000";
// Coordinates of hit boxs to check against
let hitBoxs = {
blueBox: {
x: 50,
y: 500,
width: 50,
height: 50
}
};
function hitCheck(event, hitBoxs){
// Get the x/y position of the mouse inside the canvas
let rect = event.target.getBoundingClientRect();
let x = event.clientX - rect.left;
let y = event.clientY - rect.top;
// Loop through the hitBoxs and check to see if the x/y is within the cords of the box
// If so then return the name of the hit box that was clicked
for (let box of Object.keys(hitBoxs)){
if (x > hitBoxs[box].x && x < hitBoxs[box].x+hitBoxs[box].width && y > hitBoxs[box].y && y < hitBoxs[box].y + hitBoxs[box].height) return box;
}
}
// Add a click event to canvas and check for hits within the hit boxs
canvas.addEventListener("click", function (event) {
// Check to see if a box was hit
let hitBox = hitCheck(event, hitBoxs);
// If a box was hit
if (hitBox){
// Then respond to which box was hit
if (hitBox=="blueBox"){
alert("You clicked on the blue Box. Now click this to advance one day.");
context.strokeStyle = "red";
moveGuy = moveGuy + 1;
context.fillText('Day: '+ moveGuy, 155, 505);
}
}
});
}
</script>
</head>
<body>
<div align="center">
<canvas id="myCanvas" width="800" height="720" style="border:1px solid black;">
</canvas>
</div>
</body>
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |