<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body onload="init();">
<canvas width="500" height="500" id="myCanvas" style="border: 2px solid black"></canvas>
</body>
</html>
var canvas, ctx, mousePos = {x:0, y:0};
var angle = 0;
// pos du monstre
var x=0, y=0;
var speed = 1;
function init() {
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext('2d');
// mouse listener
canvas.addEventListener('mousemove', function (evt) {
mousePos = getMousePos(canvas, evt);
}, false);
canvas.addEventListener('mousedown', function (evt) {
speed = 10;
});
canvas.addEventListener('mouseup', function (evt) {
speed = 1;
});
animate();
}
function animate() {
// 1 clear screen
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 2 move monster in the direction of the mouse,
// advance by speed pixels
var dx = x - mousePos.x;
var dy = y - mousePos.y;
var angle = Math.atan2(dy, dx);
// some basic trigonometry
x -= speed*Math.cos(angle);
y -= speed*Math.sin(angle);
// pos and scale, angle
drawMonster(x, y, 0.5, 0.5, angle+Math.PI/2);
requestAnimationFrame(animate);
}
function drawMonster(x, y, sx, sy, angle) {
ctx.save();
// Move the monster to x, y, center and rotate the coordinate system
ctx.translate(x, y);
ctx.rotate(angle);
ctx.scale(sx, sy);
// This one is for repositionning the coordinate system at
// the center of the rectangle instead of top left corner
ctx.translate(-250, -250);
drawHead();
drawEyes();
drawMouth();
ctx.restore();
}
function drawHead() {
// always save context at the beginning of a drawing function
ctx.save();
ctx.shadowColor = "#bbbbbb";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 15;
ctx.shadowOffsetY = 15;
ctx.lineWidth = 10;
ctx.strokeStyle = "red";
ctx.beginPath();
ctx.arc(250, 250, 200, 0, 2*Math.PI);
ctx.stroke();
// restore context at the end
ctx.restore();
}
function drawEyes() {
ctx.save();
// black rectangles
ctx.save();
ctx.shadowColor = "#bbbbbb";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 15;
ctx.shadowOffsetY = 15;
ctx.fillRect(100, 140, 100, 100);
ctx.fillRect(250, 140, 100, 100);
ctx.restore();
ctx.fillStyle = "yellow";
// yellow circles
ctx.beginPath();
ctx.arc(130, 210, 25, 0, 2*Math.PI);
ctx.arc(280, 210, 25, 0, 2*Math.PI);
ctx.fill();
// small black circles
ctx.beginPath();
ctx.fillStyle = "black";
ctx.arc(130, 220, 10, 0, 2*Math.PI);
ctx.arc(280, 220, 10, 0, 2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.restore();
}
function drawMouth() {
ctx.save();
ctx.shadowColor = "#bbbbbb";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 15;
ctx.shadowOffsetY = 15;
ctx.lineWidth=10;
ctx.beginPath();
ctx.moveTo(160, 340);
ctx.lineTo(310, 330);
ctx.stroke();
// left tooth
ctx.beginPath();
ctx.moveTo(170, 340);
ctx.lineTo(190, 370);
ctx.lineTo(210, 335);
// right tooth
ctx.moveTo(300, 330);
ctx.lineTo(290,363);
ctx.lineTo(265, 335);
ctx.fill();
ctx.restore();
}
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
};
}
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. |