caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 1 | <html> |
| 2 | <head> |
| 3 | <div style="height:0"> |
| 4 | <div id="test_1div"> |
| 5 | path.moveTo(213.673737, 413.292938); |
| 6 | path.lineTo(225.200134, 343.616821); |
| 7 | path.lineTo(236.726532, 273.940704); |
| 8 | path.lineTo(219.386414, 231.373322); |
| 9 | path.lineTo(213.673737, 413.292938); |
| 10 | path.close(); |
| 11 | path.moveTo(43.485352, 308.984497); |
| 12 | path.lineTo(122.610657, 305.950134); |
| 13 | path.lineTo(201.735962, 302.915802); |
| 14 | path.lineTo(280.861267, 299.881470); |
| 15 | path.lineTo(43.485352, 308.984497); |
| 16 | path.close(); |
| 17 | </div> |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame^] | 18 | <div id="test_2div"> |
| 19 | path.moveTo(-177.878387, 265.368988); |
| 20 | path.lineTo(-254.415771, 303.709961); |
| 21 | path.lineTo(-317.465363, 271.325562); |
| 22 | path.lineTo(-374.520386, 207.507660); |
| 23 | path.lineTo(-177.878387, 265.368988); |
| 24 | path.close(); |
| 25 | path.moveTo(-63.582489, -3.679123); |
| 26 | path.lineTo(-134.496841, 26.434566); |
| 27 | path.lineTo(-205.411209, 56.548256); |
| 28 | path.lineTo(-276.325562, 86.661942); |
| 29 | path.lineTo(-63.582489, -3.679123); |
| 30 | path.close(); |
| 31 | path.moveTo(-57.078423, 162.633453); |
| 32 | path.lineTo(-95.963928, 106.261139); |
| 33 | path.lineTo(-134.849457, 49.888824); |
| 34 | path.lineTo(-173.734955, -6.483480); |
| 35 | path.lineTo(-57.078423, 162.633453); |
| 36 | path.close(); |
| 37 | </div> |
| 38 | <div id="test_3div"> |
| 39 | path.moveTo(98.666489, -94.295059); |
| 40 | path.lineTo(156.584320, -61.939133); |
| 41 | path.lineTo(174.672974, -12.343765); |
| 42 | path.lineTo(158.622345, 52.028267); |
| 43 | path.lineTo(98.666489, -94.295059); |
| 44 | path.close(); |
| 45 | path.moveTo(-133.225616, -48.622055); |
| 46 | path.lineTo(-73.855499, -10.375397); |
| 47 | path.lineTo(-14.485367, 27.871277); |
| 48 | path.lineTo(44.884750, 66.117935); |
| 49 | path.lineTo(-133.225616, -48.622055); |
| 50 | path.close(); |
| 51 | path.moveTo( 9.030045, -163.413132); |
| 52 | path.lineTo(-19.605331, -89.588760); |
| 53 | path.lineTo(-48.240707, -15.764404); |
| 54 | path.lineTo(-76.876053, 58.059944); |
| 55 | path.lineTo( 9.030045, -163.413132); |
| 56 | path.close(); |
| 57 | </div> |
| 58 | <div id="test_4div"> |
| 59 | path.moveTo( -5503.40843,1749.49658); |
| 60 | path.lineTo(-5503.40843,1749.49718); |
| 61 | path.close(); |
| 62 | path.moveTo( -5503.40843,1749.49658); |
| 63 | path.lineTo(-5503.40729,1749.50314); |
| 64 | path.close(); |
| 65 | path.moveTo( -5503.40729,1749.50314); |
| 66 | path.lineTo(-5503.40729,1749.50361); |
| 67 | path.close(); |
| 68 | </div> |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 69 | </div> |
| 70 | |
| 71 | <script type="text/javascript"> |
| 72 | |
| 73 | var testDivs = [ |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame^] | 74 | test_4div, |
| 75 | test_3div, |
| 76 | test_2div, |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 77 | test_1div, |
| 78 | ]; |
| 79 | |
| 80 | var scale, columns, rows, xStart, yStart; |
| 81 | |
| 82 | var ticks = 0.1; |
| 83 | var at_x = 13 + 0.5; |
| 84 | var at_y = 13 + 0.5; |
| 85 | |
| 86 | var tests = []; |
| 87 | var testIndex = 0; |
| 88 | |
| 89 | var ctx; |
| 90 | |
| 91 | function parse(test) { |
| 92 | var contours = []; |
| 93 | var contourStrs = test.split("path.close();"); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame^] | 94 | var pattern = /-?\d+\.*\d*/g; |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 95 | for (var c in contourStrs) { |
| 96 | var points = contourStrs[c].match(pattern); |
| 97 | var pts = []; |
| 98 | for (var wd in points) { |
| 99 | var num = parseFloat(points[wd]); |
| 100 | if (isNaN(num)) continue; |
| 101 | pts.push(num ); |
| 102 | } |
| 103 | contours.push(pts); |
| 104 | } |
| 105 | tests.push(contours); |
| 106 | } |
| 107 | |
| 108 | function init(test) { |
| 109 | var canvas = document.getElementById('canvas'); |
| 110 | if (!canvas.getContext) return; |
| 111 | ctx = canvas.getContext('2d'); |
| 112 | var xmin = Infinity; |
| 113 | var xmax = -Infinity; |
| 114 | var ymin = Infinity; |
| 115 | var ymax = -Infinity; |
| 116 | for (pts in test) { |
| 117 | var pt = test[pts]; |
| 118 | for (i = 0; i < pt.length; i += 2) { |
| 119 | xmin = Math.min(xmin, pt[i]); |
| 120 | ymin = Math.min(ymin, pt[i + 1]); |
| 121 | xmax = Math.max(xmax, pt[i]); |
| 122 | ymax = Math.max(ymax, pt[i + 1]); |
| 123 | } |
| 124 | } |
| 125 | var subscale = 1; |
| 126 | while ((xmax - xmin) * subscale < 0.1 && (ymax - ymin) * subscale < 0.1) { |
| 127 | subscale *= 10; |
| 128 | } |
| 129 | columns = Math.ceil(xmax) - Math.floor(xmin) + 1; |
| 130 | rows = Math.ceil(ymax) - Math.floor(ymin) + 1; |
| 131 | xStart = Math.floor(xmin); |
| 132 | yStart = Math.floor(ymin); |
| 133 | var hscale = ctx.canvas.width / columns / ticks; |
| 134 | var vscale = ctx.canvas.height / rows / ticks; |
| 135 | scale = Math.floor(Math.min(hscale, vscale)) * subscale; |
| 136 | } |
| 137 | |
| 138 | function drawPoint(px, py, xoffset, yoffset, unit) { |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame^] | 139 | var label = px.toFixed(3) + ", " + py.toFixed(3); |
caryclark@google.com | 752b60e | 2012-03-22 21:11:17 +0000 | [diff] [blame] | 140 | var _px = px * unit + xoffset; |
| 141 | var _py = py * unit + yoffset; |
| 142 | ctx.beginPath(); |
| 143 | ctx.arc(_px, _py, 3, 0, Math.PI*2, true); |
| 144 | ctx.closePath(); |
| 145 | ctx.fill(); |
| 146 | ctx.fillText(label, _px + 5, _py); |
| 147 | } |
| 148 | |
| 149 | function draw(test, _at_x, _at_y, scale) { |
| 150 | var unit = scale * ticks; |
| 151 | ctx.lineWidth = 1; |
| 152 | var i; |
| 153 | for (i = 0; i <= rows * ticks; ++i) { |
| 154 | ctx.strokeStyle = (i % ticks) != 0 ? "rgb(160,160,160)" : "black"; |
| 155 | ctx.beginPath(); |
| 156 | ctx.moveTo(_at_x + 0, _at_y + i * scale); |
| 157 | ctx.lineTo(_at_x + unit * columns, _at_y + i * scale); |
| 158 | ctx.stroke(); |
| 159 | } |
| 160 | for (i = 0; i <= columns * ticks; ++i) { |
| 161 | ctx.strokeStyle = (i % ticks) != 0 ? "rgb(160,160,160)" : "black"; |
| 162 | ctx.beginPath(); |
| 163 | ctx.moveTo(_at_x + i * scale, _at_y + 0); |
| 164 | ctx.lineTo(_at_x + i * scale, _at_y + unit * rows); |
| 165 | ctx.stroke(); |
| 166 | } |
| 167 | |
| 168 | var xoffset = xStart * -unit + _at_x; |
| 169 | var yoffset = yStart * -unit + _at_y; |
| 170 | |
| 171 | ctx.fillStyle = "rgb(40,80,60)" |
| 172 | for (i = 0; i <= columns; i += (1 / ticks)) |
| 173 | { |
| 174 | num = (xoffset - _at_x) / -unit + i; |
| 175 | ctx.fillText(num.toFixed(0), i * unit + _at_y - 5, 10); |
| 176 | } |
| 177 | for (i = 0; i <= rows; i += (1 / ticks)) |
| 178 | { |
| 179 | num = (yoffset - _at_x) / -unit + i; |
| 180 | ctx.fillText(num.toFixed(0), 0, i * unit + _at_y + 0); |
| 181 | } |
| 182 | ctx.strokeStyle = "red"; |
| 183 | for (pts in test) { |
| 184 | var pt = test[pts]; |
| 185 | var x = pt[0]; |
| 186 | var y = pt[1]; |
| 187 | ctx.beginPath(); |
| 188 | ctx.moveTo(xoffset + x * unit, yoffset + y * unit); |
| 189 | for (i = 2; i < pt.length; i += 2) { |
| 190 | x = pt[i]; |
| 191 | y = pt[i + 1]; |
| 192 | ctx.lineTo(xoffset + x * unit, yoffset + y * unit); |
| 193 | } |
| 194 | ctx.stroke(); |
| 195 | } |
| 196 | |
| 197 | ctx.fillStyle="blue"; |
| 198 | for (pts in test) { |
| 199 | var pt = test[pts]; |
| 200 | for (i = 0; i < pt.length; i += 2) { |
| 201 | x = pt[i]; |
| 202 | y = pt[i + 1]; |
| 203 | drawPoint(x, y, xoffset, yoffset, unit); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | var mouseX = Infinity, mouseY; |
| 209 | |
| 210 | function calcXY() { |
| 211 | var e = window.event; |
| 212 | var tgt = e.target || e.srcElement; |
| 213 | var left = tgt.offsetLeft; |
| 214 | var top = tgt.offsetTop; |
| 215 | var unit = scale * ticks; |
| 216 | mouseX = (e.clientX - left - Math.ceil(at_x) + 1) / unit + xStart; |
| 217 | mouseY = (e.clientY - top - Math.ceil(at_y)) / unit + yStart; |
| 218 | } |
| 219 | |
| 220 | function handleMouseOver() { |
| 221 | calcXY(); |
| 222 | var num = mouseX.toFixed(3) + ", " + mouseY.toFixed(3); |
| 223 | ctx.beginPath(); |
| 224 | ctx.rect(300,100,200,10); |
| 225 | ctx.fillStyle="white"; |
| 226 | ctx.fill(); |
| 227 | ctx.fillStyle="black"; |
| 228 | ctx.fillText(num, 300, 108); |
| 229 | } |
| 230 | |
| 231 | function handleMouseClick() { |
| 232 | calcXY(); |
| 233 | // drawInset(); |
| 234 | } |
| 235 | |
| 236 | function drawTop() { |
| 237 | init(tests[testIndex]); |
| 238 | redraw(); |
| 239 | } |
| 240 | |
| 241 | function redraw() { |
| 242 | ctx.beginPath(); |
| 243 | ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 244 | ctx.fillStyle="white"; |
| 245 | ctx.fill(); |
| 246 | draw(tests[testIndex], at_x, at_y, scale); |
| 247 | // if (insetScale != scale && mouseX != Infinity) |
| 248 | // drawInset(); |
| 249 | } |
| 250 | |
| 251 | function doKeyPress(evt) { |
| 252 | var char = String.fromCharCode(evt.charCode); |
| 253 | switch (char) { |
| 254 | case 'N': |
| 255 | case 'n': |
| 256 | if (++testIndex >= tests.length) |
| 257 | testIndex = 0; |
| 258 | // insetScale = scale; |
| 259 | mouseX = Infinity; |
| 260 | drawTop(); |
| 261 | break; |
| 262 | case 'T': |
| 263 | case 't': |
| 264 | // drawTs(testIndex); |
| 265 | break; |
| 266 | case '-': |
| 267 | // if (--insetScale < 1) |
| 268 | // insetScale = 1; |
| 269 | // else |
| 270 | redraw(); |
| 271 | break; |
| 272 | case '=': |
| 273 | case '+': |
| 274 | // ++insetScale; |
| 275 | redraw(); |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | function start() { |
| 281 | for (i = 0; i < testDivs.length; ++i) { |
| 282 | var str = testDivs[i].firstChild.data; |
| 283 | parse(str); |
| 284 | } |
| 285 | drawTop(); |
| 286 | window.addEventListener('keypress', doKeyPress, true); |
| 287 | } |
| 288 | |
| 289 | </script> |
| 290 | </head> |
| 291 | |
| 292 | <body onLoad="start();"> |
| 293 | <canvas id="canvas" width="1500" height="1000" |
| 294 | onmousemove="handleMouseOver()" |
| 295 | onclick="handleMouseClick()" |
| 296 | ></canvas > |
| 297 | </body> |
| 298 | </html> |