raphael.primitives.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*!
  2. * Raphael Primitives Plugin 0.2
  3. *
  4. * Copyright (c) 2009 Dmitry Baranovskiy (http://raphaeljs.com)
  5. * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
  6. */
  7. Raphael.fn.star = function (cx, cy, r, r2, rays) {
  8. r2 = r2 || r * .382;
  9. rays = rays || 5;
  10. var points = ["M", cx, cy + r2, "L"],
  11. R;
  12. for (var i = 1; i < rays * 2; i++) {
  13. R = i % 2 ? r : r2;
  14. points = points.concat([(cx + R * Math.sin(i * Math.PI / rays)), (cy + R * Math.cos(i * Math.PI / rays))]);
  15. }
  16. points.push("z");
  17. return this.path(points.join());
  18. };
  19. Raphael.fn.flower = function (cx, cy, rout, rin, n) {
  20. rin = rin || rout * .5;
  21. n = +n < 3 || !n ? 5 : n;
  22. var points = ["M", cx, cy + rin, "Q"],
  23. R;
  24. for (var i = 1; i < n * 2 + 1; i++) {
  25. R = i % 2 ? rout : rin;
  26. points = points.concat([+(cx + R * Math.sin(i * Math.PI / n)).toFixed(3), +(cy + R * Math.cos(i * Math.PI / n)).toFixed(3)]);
  27. }
  28. points.push("z");
  29. return this.path(points);
  30. };
  31. Raphael.fn.spike = function (cx, cy, rout, rin, n) {
  32. rin = rin || rout * .5;
  33. n = +n < 3 || !n ? 5 : n;
  34. var points = ["M", cx, cy - rout, "Q"],
  35. R;
  36. for (var i = 1; i < n * 2 + 1; i++) {
  37. R = i % 2 ? rin : rout;
  38. points = points.concat([cx + R * Math.sin(i * Math.PI / n - Math.PI), cy + R * Math.cos(i * Math.PI / n - Math.PI)]);
  39. }
  40. points.push("z");
  41. return this.path(points);
  42. };
  43. Raphael.fn.polyline = function () {
  44. var points = "M".concat(arguments[0] || 0, ",", arguments[1] || 0, "L");
  45. for (var i = 2, ii = arguments.length - 1; i < ii; i++) {
  46. points += arguments[i] + "," + arguments[++i];
  47. }
  48. arguments[ii].toLowerCase() == "z" && (points += "z");
  49. return this.path(points);
  50. };
  51. Raphael.fn.polygon = function (cx, cy, r, n) {
  52. n = +n < 3 || !n ? 5 : n;
  53. var points = ["M", cx, cy - r, "L"],
  54. R;
  55. for (var i = 1; i < n; i++) {
  56. points = points.concat([cx + r * Math.sin(i * Math.PI * 2 / n - Math.PI), cy + r * Math.cos(i * Math.PI * 2 / n - Math.PI)]);
  57. }
  58. points.push("z");
  59. return this.path(points);
  60. };
  61. Raphael.fn.line = function (x1, y1, x2, y2) {
  62. return this.path(["M", x1, y1, "L", x2, y2]);
  63. };
  64. Raphael.fn.drawGrid = function (x, y, w, h, wv, hv, color) {
  65. color = color || "#000";
  66. var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
  67. rowHeight = h / hv,
  68. columnWidth = w / wv;
  69. for (var i = 1; i < hv; i++) {
  70. path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
  71. }
  72. for (var i = 1; i < wv; i++) {
  73. path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
  74. }
  75. return this.path(path.join(",")).attr({stroke: color});
  76. };
  77. Raphael.fn.square = function (cx, cy, r) {
  78. r = r * .7;
  79. return this.rect(cx - r, cy - r, 2 * r, 2 * r);
  80. };
  81. Raphael.fn.triangle = function (cx, cy, r) {
  82. r *= 1.75;
  83. return this.path("M".concat(cx, ",", cy, "m0-", r * .58, "l", r * .5, ",", r * .87, "-", r, ",0z"));
  84. };
  85. Raphael.fn.diamond = function (cx, cy, r) {
  86. return this.path(["M", cx, cy - r, "l", r, r, -r, r, -r, -r, r, -r, "z"]);
  87. };
  88. Raphael.fn.cross = function (cx, cy, r) {
  89. r = r / 2.5;
  90. return this.path("M".concat(cx - r, ",", cy, "l", [-r, -r, r, -r, r, r, r, -r, r, r, -r, r, r, r, -r, r, -r, -r, -r, r, -r, -r, "z"]));
  91. };
  92. Raphael.fn.plus = function (cx, cy, r) {
  93. r = r / 2;
  94. return this.path("M".concat(cx - r / 2, ",", cy - r / 2, "l", [0, -r, r, 0, 0, r, r, 0, 0, r, -r, 0, 0, r, -r, 0, 0, -r, -r, 0, 0, -r, "z"]));
  95. };
  96. Raphael.fn.arrow = function (cx, cy, r) {
  97. return this.path("M".concat(cx - r * .7, ",", cy - r * .4, "l", [r * .6, 0, 0, -r * .4, r, r * .8, -r, r * .8, 0, -r * .4, -r * .6, 0], "z"));
  98. };