drawing-app.js 622 B

1234567891011121314151617181920212223242526272829303132
  1. var stage = new mtm.Stage('c'),
  2. c = stage.canvas,
  3. ctx = stage.ctx;
  4. c.addEventListener("mouseup", mouseUp, false);
  5. c.addEventListener("mousedown", mouseDown, false);
  6. function mouseDown(e) {
  7. var m = getMousePosition(e);
  8. ctx.moveTo(m.x, m.y);
  9. c.addEventListener("mousemove", mouseMove, false);
  10. }
  11. function mouseUp(e) {
  12. c.removeEventListener("mousemove", mouseMove, false);
  13. }
  14. function mouseMove(e) {
  15. var m = getMousePosition(e);
  16. ctx.lineTo(m.x, m.y);
  17. ctx.stroke();
  18. }
  19. function getMousePosition(e) {
  20. var mx = e.offsetX || e.clientX,
  21. my = e.offsetY || e.clientY;
  22. return {x: mx, y: my};
  23. }