vml-path-element.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. jvm.VMLPathElement = function(config, style){
  2. var scale = new jvm.VMLElement('skew');
  3. jvm.VMLPathElement.parentClass.call(this, 'shape', config, style);
  4. this.node.coordorigin = "0 0";
  5. scale.node.on = true;
  6. scale.node.matrix = '0.01,0,0,0.01,0,0';
  7. scale.node.offset = '0,0';
  8. this.node.appendChild(scale.node);
  9. };
  10. jvm.inherits(jvm.VMLPathElement, jvm.VMLShapeElement);
  11. jvm.VMLPathElement.prototype.applyAttr = function(attr, value){
  12. if (attr === 'd') {
  13. this.node.path = jvm.VMLPathElement.pathSvgToVml(value);
  14. } else {
  15. jvm.VMLShapeElement.prototype.applyAttr.call(this, attr, value);
  16. }
  17. };
  18. jvm.VMLPathElement.pathSvgToVml = function(path) {
  19. var result = '',
  20. cx = 0, cy = 0, ctrlx, ctrly;
  21. path = path.replace(/(-?\d+)e(-?\d+)/g, '0');
  22. return path.replace(/([MmLlHhVvCcSs])\s*((?:-?\d*(?:\.\d+)?\s*,?\s*)+)/g, function(segment, letter, coords, index){
  23. coords = coords.replace(/(\d)-/g, '$1,-')
  24. .replace(/^\s+/g, '')
  25. .replace(/\s+$/g, '')
  26. .replace(/\s+/g, ',').split(',');
  27. if (!coords[0]) coords.shift();
  28. for (var i=0, l=coords.length; i<l; i++) {
  29. coords[i] = Math.round(100*coords[i]);
  30. }
  31. switch (letter) {
  32. case 'm':
  33. cx += coords[0];
  34. cy += coords[1];
  35. return 't'+coords.join(',');
  36. break;
  37. case 'M':
  38. cx = coords[0];
  39. cy = coords[1];
  40. return 'm'+coords.join(',');
  41. break;
  42. case 'l':
  43. cx += coords[0];
  44. cy += coords[1];
  45. return 'r'+coords.join(',');
  46. break;
  47. case 'L':
  48. cx = coords[0];
  49. cy = coords[1];
  50. return 'l'+coords.join(',');
  51. break;
  52. case 'h':
  53. cx += coords[0];
  54. return 'r'+coords[0]+',0';
  55. break;
  56. case 'H':
  57. cx = coords[0];
  58. return 'l'+cx+','+cy;
  59. break;
  60. case 'v':
  61. cy += coords[0];
  62. return 'r0,'+coords[0];
  63. break;
  64. case 'V':
  65. cy = coords[0];
  66. return 'l'+cx+','+cy;
  67. break;
  68. case 'c':
  69. ctrlx = cx + coords[coords.length-4];
  70. ctrly = cy + coords[coords.length-3];
  71. cx += coords[coords.length-2];
  72. cy += coords[coords.length-1];
  73. return 'v'+coords.join(',');
  74. break;
  75. case 'C':
  76. ctrlx = coords[coords.length-4];
  77. ctrly = coords[coords.length-3];
  78. cx = coords[coords.length-2];
  79. cy = coords[coords.length-1];
  80. return 'c'+coords.join(',');
  81. break;
  82. case 's':
  83. coords.unshift(cy-ctrly);
  84. coords.unshift(cx-ctrlx);
  85. ctrlx = cx + coords[coords.length-4];
  86. ctrly = cy + coords[coords.length-3];
  87. cx += coords[coords.length-2];
  88. cy += coords[coords.length-1];
  89. return 'v'+coords.join(',');
  90. break;
  91. case 'S':
  92. coords.unshift(cy+cy-ctrly);
  93. coords.unshift(cx+cx-ctrlx);
  94. ctrlx = coords[coords.length-4];
  95. ctrly = coords[coords.length-3];
  96. cx = coords[coords.length-2];
  97. cy = coords[coords.length-1];
  98. return 'c'+coords.join(',');
  99. break;
  100. }
  101. return '';
  102. }).replace(/z/g, 'e');
  103. };