Debian Codegraph @

js/jquery.geo.geographics.js

raw symbols SCIP
1 (function ($, window, undefined) {
2 var _ieVersion = ( function () {
3 var v = 5, div = document.createElement("div"), a = div.all || [];
4 do {
5 div.innerHTML = "<!--[if gt IE " + (++v) + "]><br><![endif]-->";
6 } while ( a[0] );
7 return v > 6 ? v : !v;
8 }() );
9
10 $.widget("geo.geographics", {
11 _$elem: undefined,
12 _options: {},
13 _trueCanvas: true,
14 _trueDoubleBuffer: true,
15
16 _width: 0,
17 _height: 0,
18
19 _$canvas: undefined,
20 _context: undefined,
21
22 _$canvasSceneFront: undefined, //< if _trueCanvas, where canvas images get written (front buffer)
23 _$canvasSceneBack: undefined, //< if _trueCanvas, where canvas images get written (back buffer)
24 _$canvasSceneStale: undefined, //< if _trueCanvas, scene to hide next
25 _$canvasSceneDrawn: undefined, //< if _trueCanvas, scene to show next
26 _timeoutEnd: null,
27 _requireFlip: false,
28
29 _blitcanvas: undefined,
30 _blitcontext: undefined,
31
32 _$labelsContainerFront: undefined,
33 _$labelsContainerBack: undefined,
34 _labelsHtml: "",
35
36 options: {
37 style: {
38 borderRadius: "8px",
39 color: "#7f0000",
40 //fill: undefined,
41 fillOpacity: 0.2,
42 height: "8px",
43 opacity: 1,
44 //stroke: undefined,
45 strokeOpacity: 1,
46 strokeWidth: "2px",
47 visibility: "visible",
48 width: "8px"
49 },
50
51 doubleBuffer: true
52 },
53
54 _create: function () {
55 this._$elem = this.element;
56 this._options = this.options;
57
58 this._$elem.css( {
59 webkitTransform: "translateZ(0)",
60 display: "inline-block",
61 overflow: "hidden",
62 textAlign: "left"
63 } );
64
65 if (this._$elem.css("position") === "static") {
66 this._$elem.css("position", "relative");
67 }
68
69 this._$elem.addClass( "geo-graphics" );
70
71 this._width = this._$elem.width();
72 this._height = this._$elem.height();
73
74 if (!(this._width && this._height)) {
75 this._width = parseInt(this._$elem.css("width"), 10);
76 this._height = parseInt(this._$elem.css("height"), 10);
77 }
78
79 var posCss = 'position:absolute;left:0;top:0;margin:0;padding:0;',
80 sizeCss = 'width:' + this._width + 'px;height:' + this._height + 'px;',
81 sizeAttr = 'width="' + this._width + '" height="' + this._height + '"';
82
83 this._blitcanvas = document.createElement( "canvas" );
84
85 if ( this._blitcanvas.getContext ) {
86 this._$canvas = $( window.toStaticHTML( '<canvas ' + sizeAttr + ' style="-webkit-transform:translateZ(0);' + posCss + '"></canvas>' ) );
87
88 // test _trueDoubleBuffer
89 this._blitcanvas.width = 1;
90 this._blitcanvas.height = 1;
91 this._trueDoubleBuffer = this._blitcanvas.toDataURL().length > 6;
92
93 if ( !(this._options.doubleBuffer && this._trueDoubleBuffer) ) {
94 this._$elem.append( this._$canvas );
95 }
96
97 this._context = this._$canvas[0].getContext("2d");
98
99 this._blitcanvas.width = this._width;
100 this._blitcanvas.height = this._height;
101 this._blitcontext = this._blitcanvas.getContext("2d");
102
103 // create our front & back buffers
104 // though, at any time either one can be in front
105 this._$canvasSceneFront = $( window.toStaticHTML( '<img id="scene0" style="-webkit-transform:translateZ(0);' + posCss + sizeCss + '" />' ) ).on( 'load', $.proxy( this._canvasImgLoad, this ) );
106 this._$canvasSceneBack = $( window.toStaticHTML( '<img id="scene1" style="-webkit-transform:translateZ(0);' + posCss + sizeCss + '" />' ) ).on( 'load', $.proxy( this._canvasImgLoad, this ) );
107
108 } else if (_ieVersion <= 8) {
109 this._trueCanvas = false;
110 this._$elem.append( '<div ' + sizeAttr + ' style="' + posCss + sizeCss + '"></div>');
111 this._$canvas = this._$elem.children(':last');
112
113 G_vmlCanvasManager.initElement(this._$canvas[0]);
114 this._context = this._$canvas[0].getContext("2d");
115 this._$canvas.children().css({ backgroundColor: "transparent", width: this._width, height: this._height });
116 }
117
118 // create our front & back label containers
119 this._$labelsContainerFront = $( window.toStaticHTML( '<div class="geo-labels-container" style="-webkit-transform:translateZ(0);' + posCss + sizeCss + '"></div>' ) );
120 this._$labelsContainerBack = $( window.toStaticHTML( '<div class="geo-labels-container" style="-webkit-transform:translateZ(0);' + posCss + sizeCss + '"></div>' ) );
121 },
122
123 _setOption: function (key, value) {
124 if (key === "style") {
125 value = $.extend({}, this._options.style, value);
126 }
127 $.Widget.prototype._setOption.apply(this, arguments);
128 },
129
130 destroy: function () {
131 $.Widget.prototype.destroy.apply(this, arguments);
132 this._$elem.html("");
133 this._$elem.removeClass( "geo-graphics" );
134 },
135
136 clear: function () {
137 this._context.clearRect(0, 0, this._width, this._height);
138 this._labelsHtml = "";
139 this._end( );
140 },
141
142 drawArc: function (coordinates, startAngle, sweepAngle, style) {
143 style = this._getGraphicStyle(style);
144
145 if (style.visibility !== "hidden" && style.opacity > 0 && style.widthValue > 0 && style.heightValue > 0) {
146 var r = Math.min(style.widthValue, style.heightValue) / 2;
147
148 startAngle = (startAngle * Math.PI / 180);
149 sweepAngle = (sweepAngle * Math.PI / 180);
150
151 this._context.save();
152 this._context.translate(coordinates[0], coordinates[1]);
153 if (style.widthValue > style.heightValue) {
154 this._context.scale(style.widthValue / style.heightValue, 1);
155 } else {
156 this._context.scale(1, style.heightValue / style.widthValue);
157 }
158
159 this._context.beginPath();
160 this._context.arc(0, 0, r, startAngle, sweepAngle, false);
161
162 if (this._trueCanvas) {
163 this._context.restore();
164 }
165
166 if (style.doFill) {
167 this._context.fillStyle = style.fill;
168 this._context.globalAlpha = style.opacity * style.fillOpacity;
169 this._context.fill();
170 }
171
172 if (style.doStroke) {
173 this._context.lineJoin = "round";
174 this._context.lineWidth = style.strokeWidthValue;
175 this._context.strokeStyle = style.stroke;
176
177 this._context.globalAlpha = style.opacity * style.strokeOpacity;
178 this._context.stroke();
179 }
180
181 if (!this._trueCanvas) {
182 this._context.restore();
183 }
184 }
185
186 this._end( );
187 },
188
189 drawPoint: function (coordinates, style) {
190 style = this._getGraphicStyle(style);
191 if (style.widthValue === style.heightValue && style.heightValue === style.borderRadiusValue) {
192 this.drawArc(coordinates, 0, 360, style);
193 } else if (style.visibility !== "hidden" && style.opacity > 0) {
194 style.borderRadiusValue = Math.min(Math.min(style.widthValue, style.heightValue) / 2, style.borderRadiusValue);
195 coordinates[0] -= style.widthValue / 2;
196 coordinates[1] -= style.heightValue / 2;
197 this._context.beginPath();
198 this._context.moveTo(coordinates[0] + style.borderRadiusValue, coordinates[1]);
199 this._context.lineTo(coordinates[0] + style.widthValue - style.borderRadiusValue, coordinates[1]);
200 this._context.quadraticCurveTo(coordinates[0] + style.widthValue, coordinates[1], coordinates[0] + style.widthValue, coordinates[1] + style.borderRadiusValue);
201 this._context.lineTo(coordinates[0] + style.widthValue, coordinates[1] + style.heightValue - style.borderRadiusValue);
202 this._context.quadraticCurveTo(coordinates[0] + style.widthValue, coordinates[1] + style.heightValue, coordinates[0] + style.widthValue - style.borderRadiusValue, coordinates[1] + style.heightValue);
203 this._context.lineTo(coordinates[0] + style.borderRadiusValue, coordinates[1] + style.heightValue);
204 this._context.quadraticCurveTo(coordinates[0], coordinates[1] + style.heightValue, coordinates[0], coordinates[1] + style.heightValue - style.borderRadiusValue);
205 this._context.lineTo(coordinates[0], coordinates[1] + style.borderRadiusValue);
206 this._context.quadraticCurveTo(coordinates[0], coordinates[1], coordinates[0] + style.borderRadiusValue, coordinates[1]);
207 this._context.closePath();
208
209 if (style.doFill) {
210 this._context.fillStyle = style.fill;
211 this._context.globalAlpha = style.opacity * style.fillOpacity;
212 this._context.fill();
213 }
214
215 if (style.doStroke) {
216 this._context.lineJoin = "round";
217 this._context.lineWidth = style.strokeWidthValue;
218 this._context.strokeStyle = style.stroke;
219
220 this._context.globalAlpha = style.opacity * style.strokeOpacity;
221
222 this._context.stroke();
223 }
224
225 this._end( );
226 }
227 },
228
229 drawLineString: function (coordinates, style) {
230 this._drawLines([coordinates], false, style);
231 },
232
233 drawPolygon: function (coordinates, style) {
234 if ( !this._trueCanvas || coordinates.length === 1 ) {
235 // either we don't have fancy rendering or there's no need for it (no holes)
236 this._drawLines( coordinates, true, style );
237 } else {
238 if ( !coordinates || !coordinates.length || coordinates[ 0 ].length < 3 ) {
239 // this is not a Polygon or it doesn't have a proper outer ring
240 return;
241 }
242
243 style = this._getGraphicStyle(style);
244 this._blitcontext.globalCompositeOperation = "copy";
245
246 var pixelBbox, i, j;
247
248 if ( style.visibility !== "hidden" && style.opacity > 0 ) {
249 this._blitcontext.clearRect(0, 0, this._width, this._height);
250
251 if ( style.doFill ) {
252 if ( coordinates.length > 1 ) {
253 // stencil inner rings
254 this._blitcontext.globalCompositeOperation = "source-out";
255 this._blitcontext.globalAlpha = 1;
256
257 this._blitcontext.beginPath();
258
259 for ( i = 1; i < coordinates.length; i++ ) {
260 // start drawing inner rings, i
261 this._blitcontext.moveTo( coordinates[ i ][ 0 ][ 0 ], coordinates[ i ][ 0 ][ 1 ] );
262
263 for ( j = 0; j < coordinates[ i ].length; j++ ) {
264 this._blitcontext.lineTo( coordinates[ i ][ j ][ 0 ], coordinates[ i ][ j ][ 1 ] );
265 }
266 }
267
268 this._blitcontext.closePath();
269
270 this._blitcontext.fill( );
271 }
272 }
273
274 // path outer ring
275 this._blitcontext.beginPath();
276 this._blitcontext.moveTo( coordinates[ 0 ][ 0 ][ 0 ], coordinates[ 0 ][ 0 ][ 1 ] );
277
278 pixelBbox = [ coordinates[ 0 ][ 0 ][ 0 ] - style.strokeWidthValue, coordinates[ 0 ][ 0 ][ 1 ] - style.strokeWidthValue, coordinates[ 0 ][ 0 ][ 0 ] + style.strokeWidthValue, coordinates[ 0 ][ 0 ][ 1 ] + style.strokeWidthValue ];
279
280 for ( i = 1; i < coordinates[ 0 ].length - 1; i++ ) {
281 this._blitcontext.lineTo( coordinates[ 0 ][ i ][ 0 ], coordinates[ 0 ][ i ][ 1 ] );
282
283 pixelBbox[ 0 ] = Math.min( coordinates[ 0 ][ i ][ 0 ] - style.strokeWidthValue, pixelBbox[ 0 ] );
284 pixelBbox[ 1 ] = Math.min( coordinates[ 0 ][ i ][ 1 ] - style.strokeWidthValue, pixelBbox[ 1 ] );
285 pixelBbox[ 2 ] = Math.max( coordinates[ 0 ][ i ][ 0 ] + style.strokeWidthValue, pixelBbox[ 2 ] );
286 pixelBbox[ 3 ] = Math.max( coordinates[ 0 ][ i ][ 1 ] + style.strokeWidthValue, pixelBbox[ 3 ] );
287 }
288
289 this._blitcontext.closePath();
290
291 if ( style.doFill ) {
292 // fill outer ring
293 this._blitcontext.fillStyle = style.fill;
294 this._blitcontext.globalAlpha = style.opacity * style.fillOpacity;
295 this._blitcontext.fill( );
296 }
297
298 this._blitcontext.globalCompositeOperation = "source-over";
299 if ( style.doStroke ) {
300 // stroke outer ring
301 this._blitcontext.lineCap = this._blitcontext.lineJoin = "round";
302 this._blitcontext.lineWidth = style.strokeWidthValue;
303 this._blitcontext.strokeStyle = style.stroke;
304
305 this._blitcontext.globalAlpha = style.opacity * style.strokeOpacity;
306 this._blitcontext.stroke( );
307
308 if ( coordinates.length > 1 ) {
309 // stroke inner rings
310 for ( i = 1; i < coordinates.length; i++ ) {
311 this._blitcontext.beginPath();
312 this._blitcontext.moveTo( coordinates[ i ][ 0 ][ 0 ], coordinates[ i ][ 0 ][ 1 ] );
313 for ( j = 1; j < coordinates[ i ].length; j++ ) {
314 this._blitcontext.lineTo( coordinates[ i ][ j ][ 0 ], coordinates[ i ][ j ][ 1 ] );
315 }
316 this._blitcontext.closePath();
317
318 this._blitcontext.stroke( );
319 }
320 }
321 }
322
323 // blit
324 pixelBbox[ 0 ] = Math.min( Math.max( pixelBbox[ 0 ], 0), this._width );
325 pixelBbox[ 1 ] = Math.min( Math.max( pixelBbox[ 1 ], 0), this._height );
326 pixelBbox[ 2 ] = Math.min( Math.max( pixelBbox[ 2 ], 0), this._width );
327 pixelBbox[ 3 ] = Math.min( Math.max( pixelBbox[ 3 ], 0), this._height );
328
329 if ( pixelBbox[ 0 ] !== pixelBbox[ 2 ] && pixelBbox[ 1 ] !== pixelBbox[ 3 ] ) {
330 this._context.drawImage(this._blitcanvas, pixelBbox[ 0 ], pixelBbox[ 1 ], pixelBbox[ 2 ] - pixelBbox[ 0 ], pixelBbox[ 3 ] - pixelBbox[ 1 ], pixelBbox[ 0 ], pixelBbox[ 1 ], pixelBbox[ 2 ] - pixelBbox[ 0 ], pixelBbox[ 3 ] - pixelBbox[ 1 ] );
331
332 this._end( );
333 }
334 }
335 }
336 },
337
338 drawBbox: function (bbox, style) {
339 this._drawLines([[
340 [bbox[0], bbox[1]],
341 [bbox[0], bbox[3]],
342 [bbox[2], bbox[3]],
343 [bbox[2], bbox[1]],
344 [bbox[0], bbox[1]]
345 ]], true, style);
346 },
347
348 drawLabel: function( coordinates, label ) {
349 this._labelsHtml += '<div class="geo-label" style="-webkit-transform:translateZ(0);position:absolute; left:' + ( coordinates[ 0 ] / this._width * 100 ) + '%; top:' + ( coordinates[ 1 ] / this._height * 100 ) + '%;">' + label + '</div>';
350 },
351
352 resize: function( dx, dy ) {
353 this._width = this._$elem.width();
354 this._height = this._$elem.height();
355
356 if (!(this._width && this._height)) {
357 this._width = parseInt(this._$elem.css("width"), 10);
358 this._height = parseInt(this._$elem.css("height"), 10);
359 }
360
361 if ( this._trueCanvas ) {
362 this._$canvas[0].width = this._width;
363 this._$canvas[0].height = this._height;
364
365 this._$canvasSceneFront.css( {
366 left: dx,
367 top: dy
368 } );
369
370 this._$canvasSceneBack.css( {
371 width: this._width,
372 height: this._height
373 } );
374 } else {
375 this._$canvas.css( {
376 width: this._width,
377 height: this._height
378 } );
379 }
380
381 var labelPositionFront = this._$labelsContainerFront.position();
382
383 this._$labelsContainerFront.css( {
384 left: labelPositionFront.left + dx,
385 top: labelPositionFront.top + dy,
386 width: this._width,
387 height: this._height
388 } );
389
390 this._$labelsContainerBack.css( {
391 width: this._width,
392 height: this._height
393 } );
394 },
395
396 interactiveTransform: function( origin, scale ) {
397 if ( this._timeoutEnd ) {
398 clearTimeout( this._timeoutEnd );
399 this._timeoutEnd = null;
400 }
401
402 if ( this._trueCanvas && this._options.doubleBuffer && this._trueDoubleBuffer ) {
403 this._canvasSceneLoad( );
404
405 // transform a finished scene, can assume no drawing during these calls
406 this._$canvasSceneFront.css( {
407 left: Math.round( origin[ 0 ] ),
408 top: Math.round( origin[ 1 ] ),
409 width: this._width * scale,
410 height: this._height * scale
411 } );
412 } else {
413 this._context.clearRect(0, 0, this._width, this._height);
414 }
415
416 // transform labels
417 this._$labelsContainerFront.css( {
418 left: Math.round( origin[ 0 ] ),
419 top: Math.round( origin[ 1 ] ),
420 width: this._width * scale,
421 height: this._height * scale
422 } );
423 },
424
425 _canvasImgLoad: function( ) {
426 var geographics = this;
427 geographics._$canvasSceneFront = geographics._$canvasSceneDrawn.prependTo( geographics._$elem );
428 geographics._$canvasSceneBack = geographics._$canvasSceneStale;
429 geographics._$canvasSceneBack.detach();
430 },
431
432 _canvasSceneLoad: function() {
433 var geographics = this;
434 if ( geographics._requireFlip ) {
435 geographics._requireFlip = false;
436 geographics._$canvasSceneStale = geographics._$canvasSceneFront;
437
438 geographics._$canvasSceneDrawn = geographics._$canvasSceneBack.css( {
439 left: 0,
440 top: 0,
441 width: geographics._width,
442 height: geographics._height
443 } ).prop( "src", geographics._$canvas[ 0 ].toDataURL( ) );
444 }
445 },
446
447 _endCallback: function() {
448 var geographics = this;
449
450 if ( !geographics._timeoutEnd ) {
451 // something has canceled the draw
452 return;
453 }
454
455 if ( geographics._trueCanvas && geographics._options.doubleBuffer && geographics._trueDoubleBuffer ) {
456 this._canvasSceneLoad( );
457 }
458
459 geographics._$labelsContainerBack.html( window.toStaticHTML( geographics._labelsHtml ) ).find("a").css({
460 position: "relative",
461 zIndex: 1,
462 display: "inline-block",
463 webkitTransform: "translateZ(0)"
464 });
465
466 var oldLabelsContainer = geographics._$labelsContainerFront;
467
468 geographics._$labelsContainerFront = geographics._$labelsContainerBack.css( {
469 left: 0,
470 top: 0,
471 width: geographics._width,
472 height: geographics._height
473 } ).appendTo( geographics._$elem );
474
475 geographics._$labelsContainerBack = oldLabelsContainer.detach();
476
477 geographics._timeoutEnd = null;
478 },
479
480 _end: function( ) {
481 // end/finalize a scene
482 if ( this._timeoutEnd ) {
483 clearTimeout( this._timeoutEnd );
484 this._timeoutEnd = null;
485 }
486
487 this._requireFlip = true;
488
489 this._timeoutEnd = setTimeout( $.proxy(this._endCallback, this), 20 );
490 },
491
492 _getGraphicStyle: function (style) {
493 function safeParse(value) {
494 value = parseInt(value, 10);
495 return (+value + '') === value ? +value : value;
496 }
497
498 style = $.extend({}, this._options.style, style);
499 style.borderRadiusValue = safeParse(style.borderRadius);
500 style.fill = style.fill || style.color;
501 style.doFill = style.fill && style.fillOpacity > 0;
502 style.stroke = style.stroke || style.color;
503 style.strokeWidthValue = safeParse(style.strokeWidth);
504 style.doStroke = style.stroke && style.strokeOpacity > 0 && style.strokeWidthValue > 0;
505 style.widthValue = safeParse(style.width);
506 style.heightValue = safeParse(style.height);
507 return style;
508 },
509
510 _drawLines: function (coordinates, close, style) {
511 if (!coordinates || !coordinates.length || coordinates[0].length < 2) {
512 return;
513 }
514
515 var i, j;
516 style = this._getGraphicStyle(style);
517
518 if (style.visibility !== "hidden" && style.opacity > 0) {
519 this._context.beginPath();
520
521 for (i = 0; i < coordinates.length; i++) {
522 this._context.moveTo(coordinates[i][0][0], coordinates[i][0][1]);
523 for (j = 1; j < coordinates[i].length; j++) {
524 this._context.lineTo(coordinates[i][j][0], coordinates[i][j][1]);
525 }
526 }
527
528 if (close) {
529 this._context.closePath();
530 }
531
532 if (close && style.doFill) {
533 this._context.fillStyle = style.fill;
534 this._context.globalAlpha = style.opacity * style.fillOpacity;
535 this._context.fill();
536 }
537
538 if (style.doStroke) {
539 this._context.lineCap = this._context.lineJoin = "round";
540 this._context.lineWidth = style.strokeWidthValue;
541 this._context.strokeStyle = style.stroke;
542
543 this._context.globalAlpha = style.opacity * style.strokeOpacity;
544 this._context.stroke();
545 }
546
547 this._end( );
548 }
549 }
550 });
551 }(jQuery, window));
552