ext-grid.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*globals svgEditor, svgedit, svgCanvas, $*/
  2. /*jslint vars: true*/
  3. /*
  4. * ext-grid.js
  5. *
  6. * Licensed under the Apache License, Version 2
  7. *
  8. * Copyright(c) 2010 Redou Mine
  9. * Copyright(c) 2010 Alexis Deveria
  10. *
  11. */
  12. // Dependencies:
  13. // 1) units.js
  14. // 2) everything else
  15. svgEditor.addExtension('view_grid', function() { 'use strict';
  16. var NS = svgedit.NS,
  17. svgdoc = document.getElementById('svgcanvas').ownerDocument,
  18. showGrid = false,
  19. assignAttributes = svgCanvas.assignAttributes,
  20. hcanvas = document.createElement('canvas'),
  21. canvBG = $('#canvasBackground'),
  22. units = svgedit.units.getTypeMap(),
  23. intervals = [0.01, 0.1, 1, 10, 100, 1000];
  24. $(hcanvas).hide().appendTo('body');
  25. var canvasGrid = svgdoc.createElementNS(NS.SVG, 'svg');
  26. assignAttributes(canvasGrid, {
  27. 'id': 'canvasGrid',
  28. 'width': '100%',
  29. 'height': '100%',
  30. 'x': 0,
  31. 'y': 0,
  32. 'overflow': 'visible',
  33. 'display': 'none'
  34. });
  35. canvBG.append(canvasGrid);
  36. // grid-pattern
  37. var gridPattern = svgdoc.createElementNS(NS.SVG, 'pattern');
  38. assignAttributes(gridPattern, {
  39. 'id': 'gridpattern',
  40. 'patternUnits': 'userSpaceOnUse',
  41. 'x': 0, //-(value.strokeWidth / 2), // position for strokewidth
  42. 'y': 0, //-(value.strokeWidth / 2), // position for strokewidth
  43. 'width': 100,
  44. 'height': 100
  45. });
  46. var gridimg = svgdoc.createElementNS(NS.SVG, 'image');
  47. assignAttributes(gridimg, {
  48. 'x': 0,
  49. 'y': 0,
  50. 'width': 100,
  51. 'height': 100
  52. });
  53. gridPattern.appendChild(gridimg);
  54. $('#svgroot defs').append(gridPattern);
  55. // grid-box
  56. var gridBox = svgdoc.createElementNS(NS.SVG, 'rect');
  57. assignAttributes(gridBox, {
  58. 'width': '100%',
  59. 'height': '100%',
  60. 'x': 0,
  61. 'y': 0,
  62. 'stroke-width': 0,
  63. 'stroke': 'none',
  64. 'fill': 'url(#gridpattern)',
  65. 'style': 'pointer-events: none; display:visible;'
  66. });
  67. $('#canvasGrid').append(gridBox);
  68. function updateGrid(zoom) {
  69. var i;
  70. // TODO: Try this with <line> elements, then compare performance difference
  71. var unit = units[svgEditor.curConfig.baseUnit]; // 1 = 1px
  72. var u_multi = unit * zoom;
  73. // Calculate the main number interval
  74. var raw_m = 100 / u_multi;
  75. var multi = 1;
  76. for (i = 0; i < intervals.length; i++) {
  77. var num = intervals[i];
  78. multi = num;
  79. if (raw_m <= num) {
  80. break;
  81. }
  82. }
  83. var big_int = multi * u_multi;
  84. // Set the canvas size to the width of the container
  85. hcanvas.width = big_int;
  86. hcanvas.height = big_int;
  87. var ctx = hcanvas.getContext('2d');
  88. var cur_d = 0.5;
  89. var part = big_int / 10;
  90. ctx.globalAlpha = 0.2;
  91. ctx.strokeStyle = svgEditor.curConfig.gridColor;
  92. for (i = 1; i < 10; i++) {
  93. var sub_d = Math.round(part * i) + 0.5;
  94. // var line_num = (i % 2)?12:10;
  95. var line_num = 0;
  96. ctx.moveTo(sub_d, big_int);
  97. ctx.lineTo(sub_d, line_num);
  98. ctx.moveTo(big_int, sub_d);
  99. ctx.lineTo(line_num ,sub_d);
  100. }
  101. ctx.stroke();
  102. ctx.beginPath();
  103. ctx.globalAlpha = 0.5;
  104. ctx.moveTo(cur_d, big_int);
  105. ctx.lineTo(cur_d, 0);
  106. ctx.moveTo(big_int, cur_d);
  107. ctx.lineTo(0, cur_d);
  108. ctx.stroke();
  109. var datauri = hcanvas.toDataURL('image/png');
  110. gridimg.setAttribute('width', big_int);
  111. gridimg.setAttribute('height', big_int);
  112. gridimg.parentNode.setAttribute('width', big_int);
  113. gridimg.parentNode.setAttribute('height', big_int);
  114. svgCanvas.setHref(gridimg, datauri);
  115. }
  116. return {
  117. name: 'view_grid',
  118. svgicons: svgEditor.curConfig.extPath + 'grid-icon.xml',
  119. zoomChanged: function(zoom) {
  120. if (showGrid) {updateGrid(zoom);}
  121. },
  122. buttons: [{
  123. id: 'view_grid',
  124. type: 'context',
  125. panel: 'editor_panel',
  126. title: 'Show/Hide Grid',
  127. events: {
  128. click: function() {
  129. svgEditor.curConfig.showGrid = showGrid = !showGrid;
  130. if (showGrid) {
  131. updateGrid(svgCanvas.getZoom());
  132. }
  133. $('#canvasGrid').toggle(showGrid);
  134. $('#view_grid').toggleClass('push_button_pressed tool_button');
  135. }
  136. }
  137. }]
  138. };
  139. });