mtm.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function () {
  2. window.mtm = {};
  3. function Stage(canvasId) {
  4. //canvas object
  5. this.canvas = document.getElementById(canvasId);
  6. //canvas context aka graphics
  7. this.ctx = this.canvas.getContext("2d");
  8. //canvas width
  9. this.width = this.canvas.width;
  10. //canvas height
  11. this.height = this.canvas.height;
  12. //things to render
  13. this.shapes = [];
  14. //ghost canvas - used for selection
  15. this._gc = document.createElement("canvas");
  16. this._gc.width = this.width;
  17. this._gc.height = this.height;
  18. this._gctx = this._gc.getContext("2d");
  19. /*
  20. this.selected
  21. this._timer - main loop timer
  22. this._playFn - main loop callback
  23. */
  24. }
  25. Stage.prototype = {
  26. constructor: Stage,
  27. //iterate over each shape and call render
  28. //passing the stage's context as paramater
  29. render: function () {
  30. for (var i = 0; i < this.shapes.length; i++) {
  31. this.shapes[i].render(this.ctx);
  32. }
  33. },
  34. //clear canvas
  35. clear: function (ctx) {
  36. ctx = ctx || this.ctx;
  37. ctx.clearRect(0, 0, this.width, this.height);
  38. },
  39. //do a clear than a render
  40. cleanRender: function () {
  41. this.clear();
  42. this.render();
  43. },
  44. //main loop
  45. play: function (fn, settings) {
  46. var me = this;
  47. settings = settings || {};
  48. settings.framerate = settings.framerate || 24;
  49. settings._ms = 1000/settings.framerate;
  50. this._settings = settings;
  51. this._playFn = fn || this._playFn || function(){};
  52. this._timer = setInterval(function() {
  53. me._playFn(me.ctx);
  54. if (me._settings.auto !== false) {
  55. me.cleanRender();
  56. };
  57. }, this._settings._ms);
  58. },
  59. //stop main loop
  60. stop: function () {
  61. this._timer = clearInterval(this._timer);
  62. },
  63. //check if a point is inside a shape
  64. hitPoint: function (x, y) {
  65. clear(this._gctx);
  66. for (var i = this.shapes.length-1; i >= 0; i--) {
  67. var shape = this.shapes[i];
  68. shape.render(this._gctx);
  69. if(this._gctx.isPointInPath(x, y)) {
  70. shape._offsetX = x - shape.x;
  71. shape._offsetY = y - shape.y;
  72. shape.x = x - shape._offsetX;
  73. shape.y = y - shape._offsetY;
  74. clear(this._gctx);
  75. return shape;
  76. }
  77. }
  78. }
  79. };
  80. mtm.draggable = {
  81. color: "#000",
  82. isDrag: false,
  83. startDrag: function() {
  84. this.isDrag = true;
  85. if(this.selectedShape) {
  86. c.removeEventListener('mousemove', selectedShape._drag, false);
  87. }
  88. selectedShape = this;
  89. c.addEventListener("mousemove", (function(selected) {
  90. selected._drag = function(e) {
  91. var mx = e.offsetX || e.clientX;
  92. var my = e.offsetY || e.clientY;
  93. selected.x = mx - selected.offsetX;
  94. selected.y = my - selected.offsetY;
  95. }
  96. return selected._drag;
  97. })(this), false);
  98. },
  99. stopDrag: function() {
  100. this.isDrag = false;
  101. c.removeEventListener('mousemove', selectedShape._drag, false);
  102. selectedShape = undefined;
  103. }
  104. };
  105. mtm.Stage = Stage;
  106. })();