<html>
<canvas id="gameCanvas" width = "600" height = "250"></canvas>
</html>
//Calling technical stuff
var canvas;
var canvasContext;
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
// Initial values
var ballX = (canvas.width-20)/2;
var ballY = (canvas.height-20)/2;
var yDirection = 2;
var xDirection = 2;
var PositionY;
var drawEverythingCount = 0;
// Constants
var ballSize = 20;
var panelHeight = 100;
var panelWidth = 10;
window.onload = function(){
// Calls the main draw function and has the interval for repeating set to 10 ms
setInterval(function() {
drawEverything(PositionY);
}, 10);
}
//Adds the event for mousemove to record the Y position of the mouse
document.addEventListener("mousemove",function(event){
PositionY = (event.clientY-(panelHeight/2));
});
function drawEverything(PositionY) {
drawEverythingCount = drawEverythingCount + 1;
// Draws the canvas, ball and panel
canvasContext.fillStyle = 'black';
canvasContext.fillRect(0,0, canvas.width, canvas.height);
canvasContext.fillStyle = 'red';
canvasContext.fillRect(ballX, ballY, ballSize, ballSize);
canvasContext.fillStyle = 'green';
canvasContext.fillRect(canvas.width-panelWidth, PositionY, panelWidth, panelHeight);
// Resets the game if the ball goes out of the Right side of the canvas
if (ballX > canvas.width) {
alert('You scored:' + (xDirection - 2) + '!')
ballX = (canvas.width - 20)/2;
ballY = (canvas.height-20)/2;
yDirection = 2;
xDirection = 2;
}
// Checks to see if the ball is about to hit the left-hand wall
if (ballX - xDirection < 0) {
(xDirection = (xDirection*-1) + 1); ballX = 0;
};
// Checks to see whether or not the ball is about to go off of the canvas' y-axis and changes its
// direction
if ((ballY+20) > canvas.height || ballY < 0) {
if ((ballY+20) > canvas.height) {
yDirection = -1;
} else {
yDirection = 1;
}
}
// Checks to see if the ball is hitting the panel
if ((ballX + xDirection) > (canvas.width - 10)) {
var y;
for (y = PositionY; y < (PositionY + 101);y++) {
if(y == ballY) {
xDirection = xDirection * -1;
break;
}
}};
// Movement of the ball
ballY = ballY + yDirection;
ballX = ballX + xDirection;
}
Output
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. |