source: other-projects/nz-flag-design/trunk/design-2d/Original editor.method.ac/method-draw/extensions/ext-grid.js@ 29468

Last change on this file since 29468 was 29468, checked in by sjs49, 9 years ago

Initial commit for editor.method.ac for flag design

  • Property svn:executable set to *
File size: 5.1 KB
Line 
1/*
2 * ext-grid.js
3 *
4 * Licensed under the Apache License, Version 2
5 *
6 * Copyright(c) 2010 Redou Mine
7 * Copyright(c) 2010 Alexis Deveria
8 *
9 */
10
11// Dependencies:
12// 1) units.js
13// 2) everything else
14
15methodDraw.addExtension("view_grid", function(s) {
16 if (!document.getElementById("canvasGrid")){
17 var svgdoc = document.getElementById("svgcanvas").ownerDocument,
18 svgns = "http://www.w3.org/2000/svg",
19 dims = methodDraw.curConfig.dimensions,
20 svgroot = s.svgroot;
21 var svgCanvas = methodDraw.canvas;
22 var showGrid = false;
23 var assignAttributes = s.assignAttributes;
24
25 var hcanvas = document.createElement('canvas');
26 $(hcanvas).hide().appendTo('body');
27
28 var canvasgrid = svgdoc.createElementNS(svgns, "g");
29 assignAttributes(canvasgrid, {
30 'id': 'canvasGrid',
31 'width': '100%',
32 'height': '100%',
33 'x': 0,
34 'y': 0,
35 'overflow': 'visible',
36 'display': 'none'
37 });
38
39 var canvBG = $('#canvas_background');
40 canvBG.after(canvasgrid);
41
42
43
44 // grid-pattern
45 var gridPattern = svgdoc.createElementNS(svgns, "pattern");
46 assignAttributes(gridPattern, {
47 'id': 'gridpattern',
48 'patternUnits': 'userSpaceOnUse',
49 'x': 0, //-(value.strokeWidth / 2), // position for strokewidth
50 'y': 0, //-(value.strokeWidth / 2), // position for strokewidth
51 'width': 100,
52 'height': 100
53 });
54
55 var gridimg = svgdoc.createElementNS(svgns, "image");
56 assignAttributes(gridimg, {
57 'x': 0,
58 'y': 0,
59 'width': 100,
60 'height': 100
61 });
62
63 gridPattern.appendChild(gridimg);
64 $('#svgroot defs').append(gridPattern);
65
66 // grid-box
67 var gridBox = svgdoc.createElementNS(svgns, "rect");
68 assignAttributes(gridBox, {
69 'width': '100%',
70 'height': '100%',
71 'x': 0,
72 'y': 0,
73 'stroke-width': 0,
74 'stroke': 'none',
75 'fill': 'url(#gridpattern)',
76 'style': 'pointer-events: none; display:visible;'
77 });
78 $('#canvasGrid').append(gridBox);
79 }
80// });
81
82 function updateGrid(zoom) {
83 // TODO: Try this with <line> elements, then compare performance difference
84
85 var bgwidth = +canvBG.attr('width');
86 var bgheight = +canvBG.attr('height');
87
88 var units = svgedit.units.getTypeMap();
89 var unit = units[methodDraw.curConfig.baseUnit]; // 1 = 1px
90 var r_intervals = [.01, .1, 1, 10, 100, 1000];
91
92 var d = 0;
93 var is_x = (d === 0);
94 var dim = is_x ? 'x' : 'y';
95 var lentype = is_x?'width':'height';
96 var c_elem = svgCanvas.getContentElem();
97 var content_d = c_elem.getAttribute(dim)-0;
98
99 var hcanv = hcanvas;
100
101 var u_multi = unit * zoom;
102
103 // Calculate the main number interval
104 var raw_m = 100 / u_multi;
105 var multi = 1;
106 for(var i = 0; i < r_intervals.length; i++) {
107 var num = r_intervals[i];
108 multi = num;
109 if(raw_m <= num) {
110 break;
111 }
112 }
113
114 var big_int = multi * u_multi;
115
116 // Set the canvas size to the width of the container
117 hcanv.width = big_int;
118 hcanv.height = big_int;
119 var ctx = hcanv.getContext("2d");
120
121 var ruler_d = 0;
122 var cur_d = .5;
123
124 var part = big_int / 10;
125
126 ctx.globalAlpha = 0.2;
127 ctx.strokeStyle = "#000";
128 for(var i = 1; i < 10; i++) {
129 var sub_d = Math.round(part * i) + .5;
130// var line_num = (i % 2)?12:10;
131 var line_num = 0;
132 ctx.moveTo(sub_d, big_int);
133 ctx.lineTo(sub_d, line_num);
134 ctx.moveTo(big_int, sub_d);
135 ctx.lineTo(line_num ,sub_d);
136 }
137 ctx.stroke();
138 ctx.beginPath();
139 ctx.globalAlpha = 0.5;
140 ctx.moveTo(cur_d, big_int);
141 ctx.lineTo(cur_d, 0);
142
143 ctx.moveTo(big_int, cur_d);
144 ctx.lineTo(0, cur_d);
145 ctx.stroke();
146
147 var datauri = hcanv.toDataURL('image/png');
148 gridimg.setAttribute('width', big_int);
149 gridimg.setAttribute('height', big_int);
150 gridimg.parentNode.setAttribute('width', big_int);
151 gridimg.parentNode.setAttribute('height', big_int);
152 svgCanvas.setHref(gridimg, datauri);
153 }
154
155 return {
156 name: "view_grid",
157 zoomChanged: function(zoom) {
158 // update size
159 if(showGrid) updateGrid(zoom);
160 },
161
162 buttons: [{
163 id: "view_grid",
164 type: "menu",
165 after: "tool_wireframe",
166 panel: "view_menu",
167 title: "View Grid",
168 events: {
169 'click': function() {
170 var gr = !$('#view_grid').hasClass('push_button_pressed');
171 if (gr) {
172 methodDraw.curConfig.showGrid = showGrid = true;
173 $('#view_grid').addClass('push_button_pressed');
174 $('#canvasGrid').attr('display', 'inline');
175 updateGrid(svgCanvas.getZoom());
176 }
177 else {
178 methodDraw.curConfig.showGrid = showGrid = false;
179 $('#view_grid').removeClass('push_button_pressed');
180 $('#canvasGrid').attr('display', 'none');
181 }
182 }
183 }
184}]
185 };
186 });
Note: See TracBrowser for help on using the repository browser.