Created
May 29, 2012 21:31
-
-
Save KrofDrakula/2830853 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Canvas bootstrap</title> | |
<style>body { text-align: center; } canvas { border: 1px solid black; margin: auto;}</style> | |
<script src="raf.js"></script> | |
</head> | |
<body> | |
<canvas id="canvas" width="500" height="500"></canvas> | |
<script> | |
var helpers = { | |
getMouse: function(e, canvas) { | |
return { | |
x: e.pageX - canvas.offsetLeft, | |
y: e.pageY - canvas.offsetTop | |
} | |
} | |
}; | |
var canvas = document.getElementById("canvas"), | |
context = canvas.getContext('2d'), | |
lastPosition = null, | |
currentPosition = null; | |
canvas.addEventListener('mousemove', function(ev) { | |
currentPosition = helpers.getMouse(ev, canvas); | |
}); | |
draw(); | |
function draw() { | |
requestAnimationFrame(draw); | |
if (!currentPosition) return; | |
if (!lastPosition) | |
lastPosition = currentPosition; | |
var d = dist(lastPosition, currentPosition); | |
context.beginPath(); | |
context.moveTo(lastPosition.x, lastPosition.y); | |
context.lineTo(currentPosition.x, currentPosition.y); | |
context.strokeStyle = rndColor(); | |
context.lineWidth = Math.max(1, d); | |
context.lineCap = 'butt'; | |
context.lineJoin = 'bevel'; | |
context.stroke(); | |
context.closePath(); | |
lastPosition = currentPosition; | |
} | |
function rndColor() { | |
var s = '#', n; | |
for (var i = 0; i < 3; i++) { | |
n = Math.floor(Math.random() * 256).toString(16); | |
s += (n.length == 1 ? '0' : '') + n; | |
} | |
return s; | |
} | |
function dist(a, b) { | |
return Math.sqrt(Math.pow(b.x - a.x, 2) + (Math.pow(b.y - a.y, 2))); | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var lastTime = 0; | |
var vendors = ['ms', 'moz', 'webkit', 'o']; | |
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { | |
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; | |
window.cancelAnimationFrame = | |
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; | |
} | |
if (!window.requestAnimationFrame) | |
window.requestAnimationFrame = function(callback, element) { | |
var currTime = new Date().getTime(); | |
var timeToCall = Math.max(0, 16 - (currTime - lastTime)); | |
var id = window.setTimeout(function() { callback(currTime + timeToCall); }, | |
timeToCall); | |
lastTime = currTime + timeToCall; | |
return id; | |
}; | |
if (!window.cancelAnimationFrame) | |
window.cancelAnimationFrame = function(id) { | |
clearTimeout(id); | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment