Skip to content

Instantly share code, notes, and snippets.

@arvind-iyer
Created March 1, 2016 11:17
Show Gist options
  • Save arvind-iyer/05cd2e0b5735288057a8 to your computer and use it in GitHub Desktop.
Save arvind-iyer/05cd2e0b5735288057a8 to your computer and use it in GitHub Desktop.
jqEzZy
<canvas id="canvas" width="640" height="360"></canvas>
<div class="test">
</div>
document.addEventListener("DOMContentLoaded", init, false);
function init()
{
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", getPosition, false);
}
function getPosition(event)
{
var x = new Number();
var y = new Number();
var canvas = document.getElementById("canvas");
if (event.x != undefined && event.y != undefined)
{
x = event.x;
y = event.y;
}
else // Firefox method to get the position
{
x = event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = event.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
var test = $('.test');
test.text("x: " + x + " y: " + y);
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
#canvas {
background-color: #000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment