<html>
<head>
<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<img src="http://www.stlukeseye.com/images/img-amsler-grid.gif" id="img" />
<div>
<button id="btn">Get image</button>
x: <span id="x">?</span> y: <span id="y">?</span>
<p>Please note that "Get image" button will work only if the image is hosted on the same domain as the script. So in JSBin this won't work.</p>
</div>
<div id="output"></div>
</body>
</html>
$(function() {
// retrieve the image
var img = $("#img");
// create a canvas element
var canvas = document.createElement("canvas");
// set dimensions to the width/height of img
canvas.width = img.width();
canvas.height = img.height();
// replace img element with canvas
img.replaceWith(canvas);
// get context and draw image
var ctx = canvas.getContext("2d");
ctx.drawImage(img[0], 0, 0);
var coordsArr = []; // [0] - coords of 1st click, [1] - coords of 2nd click
$(canvas).click(function(e) {
// get coordinates
var coords = getCoordinates(canvas, e);
// main logic here please :)
// =========================
// add coordinates to the array
coordsArr.push(coords);
// if there are 2 points (2 clicks)
if (coordsArr.length === 2) {
// draw the line
draw(ctx, coordsArr);
// reset the array
coordsArr = [];
}
// =========================
// write the coordinates FYI
$("span#x").text(coords.x);
$("span#y").text(coords.y);
});
// if btn is clicked, create an <img/>, put data from canvas into it and append it to div
$("#btn").click(function() {
$("<img />").attr("src", canvas.toDataURL("image/png")).appendTo("#output");
});
});
function draw(ctx, coordsArr) {
// here you can put the logic that will calculate position of the grid intersections
var newCoords = coordsArr; // currently nothing is done
// draw the line with new coordinates
drawLine(ctx, newCoords);
}
// get coordinates from the click event of the canvas
function getCoordinates(canvas, e) {
var x;
var y;
if (e.pageX || e.pageY) {
x = e.pageX;
y = e.pageY;
} else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
return { x : x, y: y };
}
// drawLine - draws a line on a canvas context from the start point to the end point
function drawLine(ctx, coordsArr) {
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(coordsArr[0].x, coordsArr[0].y);
ctx.lineTo(coordsArr[1].x, coordsArr[1].y);
ctx.closePath();
ctx.stroke();
}
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. |