vcanvas-base.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Setup a very simple "virtual canvas" to make drawing the few shapes we need easier
  2. // This is accessible as $(foo).simpledraw()
  3. VShape = createClass({
  4. init: function (target, id, type, args) {
  5. this.target = target;
  6. this.id = id;
  7. this.type = type;
  8. this.args = args;
  9. },
  10. append: function () {
  11. this.target.appendShape(this);
  12. return this;
  13. }
  14. });
  15. VCanvas_base = createClass({
  16. _pxregex: /(\d+)(px)?\s*$/i,
  17. init: function (width, height, target) {
  18. if (!width) {
  19. return;
  20. }
  21. this.width = width;
  22. this.height = height;
  23. this.target = target;
  24. this.lastShapeId = null;
  25. if (target[0]) {
  26. target = target[0];
  27. }
  28. $.data(target, '_jqs_vcanvas', this);
  29. },
  30. drawLine: function (x1, y1, x2, y2, lineColor, lineWidth) {
  31. return this.drawShape([[x1, y1], [x2, y2]], lineColor, lineWidth);
  32. },
  33. drawShape: function (path, lineColor, fillColor, lineWidth) {
  34. return this._genShape('Shape', [path, lineColor, fillColor, lineWidth]);
  35. },
  36. drawCircle: function (x, y, radius, lineColor, fillColor, lineWidth) {
  37. return this._genShape('Circle', [x, y, radius, lineColor, fillColor, lineWidth]);
  38. },
  39. drawPieSlice: function (x, y, radius, startAngle, endAngle, lineColor, fillColor) {
  40. return this._genShape('PieSlice', [x, y, radius, startAngle, endAngle, lineColor, fillColor]);
  41. },
  42. drawRect: function (x, y, width, height, lineColor, fillColor) {
  43. return this._genShape('Rect', [x, y, width, height, lineColor, fillColor]);
  44. },
  45. getElement: function () {
  46. return this.canvas;
  47. },
  48. /**
  49. * Return the most recently inserted shape id
  50. */
  51. getLastShapeId: function () {
  52. return this.lastShapeId;
  53. },
  54. /**
  55. * Clear and reset the canvas
  56. */
  57. reset: function () {
  58. alert('reset not implemented');
  59. },
  60. _insert: function (el, target) {
  61. $(target).html(el);
  62. },
  63. /**
  64. * Calculate the pixel dimensions of the canvas
  65. */
  66. _calculatePixelDims: function (width, height, canvas) {
  67. // XXX This should probably be a configurable option
  68. var match;
  69. match = this._pxregex.exec(height);
  70. if (match) {
  71. this.pixelHeight = match[1];
  72. } else {
  73. this.pixelHeight = $(canvas).height();
  74. }
  75. match = this._pxregex.exec(width);
  76. if (match) {
  77. this.pixelWidth = match[1];
  78. } else {
  79. this.pixelWidth = $(canvas).width();
  80. }
  81. },
  82. /**
  83. * Generate a shape object and id for later rendering
  84. */
  85. _genShape: function (shapetype, shapeargs) {
  86. var id = shapeCount++;
  87. shapeargs.unshift(id);
  88. return new VShape(this, id, shapetype, shapeargs);
  89. },
  90. /**
  91. * Add a shape to the end of the render queue
  92. */
  93. appendShape: function (shape) {
  94. alert('appendShape not implemented');
  95. },
  96. /**
  97. * Replace one shape with another
  98. */
  99. replaceWithShape: function (shapeid, shape) {
  100. alert('replaceWithShape not implemented');
  101. },
  102. /**
  103. * Insert one shape after another in the render queue
  104. */
  105. insertAfterShape: function (shapeid, shape) {
  106. alert('insertAfterShape not implemented');
  107. },
  108. /**
  109. * Remove a shape from the queue
  110. */
  111. removeShapeId: function (shapeid) {
  112. alert('removeShapeId not implemented');
  113. },
  114. /**
  115. * Find a shape at the specified x/y co-ordinates
  116. */
  117. getShapeAt: function (el, x, y) {
  118. alert('getShapeAt not implemented');
  119. },
  120. /**
  121. * Render all queued shapes onto the canvas
  122. */
  123. render: function () {
  124. alert('render not implemented');
  125. }
  126. });