Skip to content

Instantly share code, notes, and snippets.

@syntagmatic
Last active December 14, 2015 00:59
Show Gist options
  • Save syntagmatic/5003047 to your computer and use it in GitHub Desktop.
Save syntagmatic/5003047 to your computer and use it in GitHub Desktop.
Canvas Simple Mouse Interaction
<canvas id="picture" width=960 height=500></canvas>
<script>
var canvas = document.getElementById("picture");
var ctx = canvas.getContext("2d");
var width = canvas.width;
var height = canvas.height;
ctx.translate(width/2,height/2);
ctx.globalAlpha = 0.4;
ctx.globalCompositeOperation = "darker";
function render(x) {
ctx.fillStyle = "hsl(" + Math.round(x) + ",50%,50%)";
ctx.rotate(0.03);
ctx.fillRect(10,10,2,x);
};
canvas.addEventListener("mousemove", function(e) {
var x = 1000/Math.sqrt(Math.abs(e.pageX-width/2));
render(x);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment