<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
#myCanvas {
border: 1px solid #9C9898;
}
</style>
<script>
var targetX = 500, targetY = 200, targetRadius = 50;
var playerX=0, playerY=0;
function writeMessage(canvas, message) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = '18pt Calibri';
context.fillStyle = 'black';
context.fillText(message, 10, 25);
}
function getMousePos(canvas, evt) {
// get canvas position
var obj = canvas;
var top = 0;
var left = 0;
while (obj && obj.tagName != 'BODY') {
top += obj.offsetTop;
left += obj.offsetLeft;
obj = obj.offsetParent;
}
// return relative mouse position
var mouseX = evt.clientX - left + window.pageXOffset;
var mouseY = evt.clientY - top + window.pageYOffset;
return {
x:mouseX,
y:mouseY
};
}
function drawObstacles(canvas, context) {
context.fillRect(100+10*Math.random(),100+10*Math.random(), 200, 200);
}
function drawPlayer(canvas, context, x, y) {
context.fillRect(x,y, 20,20);
}
// Collisions entre rectangles
function rectsOverlap(x0, y0, w0, h0, x2, y2, w2, h2) {
if ((x0 > (x2 + w2)) || ((x0 + w0) < x2))
return false;
if ((y0 > (y2 + h2)) || ((y0 + h0) < y2))
return false;
return true;
}
// Collisions entre rectangle et cercle
function circRectsOverlap(x0, y0, w0, h0, cx, cy, r) {
var testX=cx;
var testY=cy;
if (testX < x0) testX=x0;
if (testX > (x0+w0)) testX=(x0+w0);
if (testY < y0) testY=y0;
if (testY > (y0+h0)) testY=(y0+h0);
return (((cx-testX)*(cx-testX)+(cy-testY)*(cy-testY))<r*r);
}
function drawTarget(canvas, context, x, y, r, fillColor) {
context.save();
context.beginPath();
context.arc(x, y, r, 0, 2*Math.PI, false);
context.fillStyle=fillColor;
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
context.restore();
}
window.onload = function () {
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var vitesseX=0; vitesseY = 0;
function checkArrowKeysUp(e){
var arrs= [], key= window.event? event.keyCode: e.keyCode;
arrs[37]= 'left';
arrs[38]= 'up';
arrs[39]= 'right';
arrs[40]= 'down';
if(arrs[key]) console.log(arrs[key]);
if(arrs[key] == 'left') {
vitesseX= 0;
}
if(arrs[key] == 'right') {
vitesseX=0;
}
if(arrs[key] == 'up') {
vitesseY=0;
}
if(arrs[key] == 'down') {
vitesseY=0;
}
}
function checkArrowKeysDown(e){
var arrs= [], key= window.event? event.keyCode: e.keyCode;
arrs[37]= 'left';
arrs[38]= 'up';
arrs[39]= 'right';
arrs[40]= 'down';
if(arrs[key]) console.log(arrs[key]);
if(arrs[key] == 'left') {
vitesseX= -4;
}
if(arrs[key] == 'right') {
vitesseX=4;
}
if(arrs[key] == 'up') {
vitesseY=-4;
}
if(arrs[key] == 'down') {
vitesseY=4;
}
}
document.onkeydown=checkArrowKeysDown;
document.onkeyup=checkArrowKeysUp;
function mainLoop() {
// Effacement, dessin, collisions, etc.
context.clearRect(0,0, canvas.width, canvas.height);
drawObstacles(canvas, context);
drawPlayer(canvas, context, playerX, playerY);
// On déplace le joueur
playerX += vitesseX; playerY +=vitesseY;
// Collision test avec l'obstacle rectangulaire. Oui, c'est pas beau et codé en dur !
if(rectsOverlap(playerX, playerY, 20, 20, 100, 100, 200,200)) {
// En vas de collision on dessine en rouge
context.fillStyle = 'red';
// Là aussi, la réaction en cas de collision DOIT être mieux gérée !
vitesseX = 2;
vitesseY = 2;
} else {
context.fillStyle ='black';
}
// Collision avec la cible
if(circRectsOverlap(playerX, playerY, 20, 20,
targetX, targetY, targetRadius)) {
drawTarget(canvas, context, targetX, targetY, targetRadius, 'yellow');
} else {
drawTarget(canvas, context, targetX, targetY, targetRadius, "#8ED6FF");
}
// On reexecute la fonction mainloop 60 fois/s
window.requestAnimationFrame(mainLoop);
}
// Appelé dès que la page est chargée
mainLoop();
};
</script>
</head>
<body>
<canvas id="myCanvas" width="578" height="400">
</canvas>
</body>
</html>
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. |