Created
September 21, 2010 16:30
-
-
Save bebraw/589974 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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> | |
<html lang="en"> | |
<head> | |
<title>Paint</title> | |
<script type="text/javascript" src="http://rightjs.org/hotlink/right-2.0.0.js"></script> | |
<script type="text/javascript"> | |
Event.include({offset: function() { | |
return {x: this.pageX - this.target.position().x, | |
y: this.pageY - this.target.position().y}; | |
}}); | |
// simple mouse capture plugin inspired by http://benanne.net/code/?p=238 | |
var mouse = function(RightJS, window) { | |
var mouse = window.mouse || {}; | |
if (!('capture' in mouse)) { | |
var mousePressed; | |
mouse.capture = function(selector, cbs) { | |
selector.on({ | |
'mousedown': function(e) { | |
mousePressed = true; | |
if('down' in cbs) { | |
cbs.down(e); | |
} | |
}, | |
'mousemove': function(e) { | |
if(mousePressed) { | |
if('move' in cbs) { | |
cbs.move(e); | |
} | |
} | |
}, | |
'mouseup': function(e) { | |
mousePressed = false; | |
if('up' in cbs) { | |
cbs.up(e); | |
} | |
} | |
}); | |
}; | |
} | |
return mouse; | |
}(RightJS, window); | |
var prev; | |
mouse.capture('#myCanvas', {down: function() { | |
prev = null; | |
}, | |
move: function(e) { | |
var cur = e.offset(); | |
if(prev != null) { | |
var canvas = $('myCanvas')._; | |
var ctx = canvas.getContext('2d'); | |
ctx.strokeStyle = "black"; | |
ctx.lineWidth = 1; | |
ctx.beginPath(); | |
ctx.moveTo(prev.x, prev.y); | |
ctx.lineTo(cur.x, cur.y); | |
ctx.stroke(); | |
ctx.closePath(); | |
} | |
prev = cur; | |
}} | |
); | |
</script> | |
</head> | |
<body> | |
<canvas id="myCanvas" style="background-color:grey" width="300" height="300"></canvas> | |
</body> | |
</html> |
And instead of document.getElementById('myCanvas')
you could use $('myCanvas')._
I incorporated the proposed suggestions to the code. Thanks. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
couple of notes.
RightJS
on theselector
, you can call it directly likeselector.on
on
method, kinda like that