<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body onload="init();">
<canvas id="canvas"></canvas>
<a href="javascript:clear()">clear</a>
<a id="erase" href="javascript:erase()">erase</a>
<a id="done" href="javascript:done()">done</a>
</body>
</html>
canvas {
height: 200px;
width: 300px;
cursor: pointer;
border: 1px red solid;
}
var canvas;
var gco;
var context;
var paintcolor = "black";
var background = "white";
function init() {
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
gco = context.globalCompositeOperation;
context.globalCompositeOperation = "destination-over";
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = gco;
canvas.onmousedown = draw_start;
canvas.onmouseup = draw_stop;
}
function draw_start(e) {
e.preventDefault();
x = e.pageX;
y = e.pageY;
draw(x-30, y-30, 5, 5);
canvas.onmousemove = getmxy;
canvas.onmousedown = getmxy;
}
function draw_stop() {
canvas.onmousemove = null;
canvas.onmousedown = draw_start;
}
function getmxy(e) {
x = e.pageX;
y = e.pageY;
draw(x, y, 5, 5);
}
function draw(a,b,c,d,color) {
if (!color)
color = paintcolor;
context.fillStyle = color;
context.fillRect(a, b, c, d);
}
function erase() {
erase_link = document.getElementById("erase");
if (paintcolor == "black") {
//context.globalCompositeOperation = "destination-out";
paintcolor = "white";
erase_link.innerHTML = "paint";
}
else {
paintcolor = "black";
erase_link.innerHTML = "erase";
context.globalCompositeOperation = "source-over";
}
}
function clear() {
context.globalCompositeOperation = "destination-out";
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = "source-over";
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
}
function done() {
done_link = document.getElementById("done");
if (canvas.onmousedown) {
canvas.onmousedown = null;
canvas.onmouseup = null;
canvas.onmousemove = null;
done_link.innerHTML = 'draw';
} else {
done_link.innerHTML = 'done';
init();
}
}
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. |