ext-grid.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 = svgEditor.curConfig.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. function gridUpdate () {
  117. if (showGrid) {
  118. updateGrid(svgCanvas.getZoom());
  119. }
  120. $('#canvasGrid').toggle(showGrid);
  121. $('#view_grid').toggleClass('push_button_pressed tool_button');
  122. }
  123. return {
  124. name: 'view_grid',
  125. svgicons: svgEditor.curConfig.extPath + 'grid-icon.xml',
  126. zoomChanged: function(zoom) {
  127. if (showGrid) {updateGrid(zoom);}
  128. },
  129. callback: function () {
  130. if (showGrid) {
  131. gridUpdate();
  132. }
  133. },
  134. buttons: [{
  135. id: 'view_grid',
  136. type: 'context',
  137. panel: 'editor_panel',
  138. title: 'Show/Hide Grid',
  139. events: {
  140. click: function() {
  141. svgEditor.curConfig.showGrid = showGrid = !showGrid;
  142. gridUpdate();
  143. }
  144. }
  145. }]
  146. };
  147. });