Skip to content

Instantly share code, notes, and snippets.

@mkuklis
Created January 2, 2012 21:19
Show Gist options
  • Save mkuklis/1552169 to your computer and use it in GitHub Desktop.
Save mkuklis/1552169 to your computer and use it in GitHub Desktop.
more experiments with canvas
// demo: http://jsfiddle.net/7wG3a/14/
var c = document.getElementById('canvas');
var ctx = c.getContext('2d');
var drawing = false, dx, dy;
var x = 100, y = 100, w = 100, h = 100;
c.onmousedown = function (e) {
dx = e.clientX - x;
dy = e.clientY - y;
drawing = true;
}
c.onmousemove = function (e) {
if (drawing) {
ctx.clearRect(0, 0, 400, 400);
x = e.clientX - dx;
y = e.clientY - dy;
draw(ctx, x, y, 100, 100);
}
}
c.onmouseup = function (e) {
drawing = false;
}
function draw(ctx, x, y, w, h) {
ctx.save();
ctx.fillStyle = "#ff0000";
ctx.beginPath();
ctx.translate(x, y);
ctx.rotate(45 * Math.PI / 180);
ctx.fillRect(-w/2, -h/2, w, h);
ctx.restore();
}
ctx.clearRect(0, 0, 400, 400);
draw(ctx, x, y, w, h);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment