jqplot.shadowRenderer.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  8. * Copyright (c) 2009-2013 Chris Leonello
  9. * jqPlot is currently available for use in all personal or commercial projects
  10. * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
  11. * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
  12. * choose the license that best suits your project and use it accordingly.
  13. *
  14. * Although not required, the author would appreciate an email letting him
  15. * know of any substantial use of jqPlot. You can reach the author at:
  16. * chris at jqplot dot com or see http://www.jqplot.com/info.php .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author Ash Searle
  25. * http://hexmen.com/blog/2007/03/printf-sprintf/
  26. * http://hexmen.com/js/sprintf.js
  27. * The author (Ash Searle) has placed this code in the public domain:
  28. * "This code is unrestricted: you are free to use it however you like."
  29. *
  30. */
  31. (function($) {
  32. // class: $.jqplot.shadowRenderer
  33. // The default jqPlot shadow renderer, rendering shadows behind shapes.
  34. $.jqplot.ShadowRenderer = function(options){
  35. // Group: Properties
  36. // prop: angle
  37. // Angle of the shadow in degrees. Measured counter-clockwise from the x axis.
  38. this.angle = 45;
  39. // prop: offset
  40. // Pixel offset at the given shadow angle of each shadow stroke from the last stroke.
  41. this.offset = 1;
  42. // prop: alpha
  43. // alpha transparency of shadow stroke.
  44. this.alpha = 0.07;
  45. // prop: lineWidth
  46. // width of the shadow line stroke.
  47. this.lineWidth = 1.5;
  48. // prop: lineJoin
  49. // How line segments of the shadow are joined.
  50. this.lineJoin = 'miter';
  51. // prop: lineCap
  52. // how ends of the shadow line are rendered.
  53. this.lineCap = 'round';
  54. // prop; closePath
  55. // whether line path segment is closed upon itself.
  56. this.closePath = false;
  57. // prop: fill
  58. // whether to fill the shape.
  59. this.fill = false;
  60. // prop: depth
  61. // how many times the shadow is stroked. Each stroke will be offset by offset at angle degrees.
  62. this.depth = 3;
  63. this.strokeStyle = 'rgba(0,0,0,0.1)';
  64. // prop: isarc
  65. // whether the shadow is an arc or not.
  66. this.isarc = false;
  67. $.extend(true, this, options);
  68. };
  69. $.jqplot.ShadowRenderer.prototype.init = function(options) {
  70. $.extend(true, this, options);
  71. };
  72. // function: draw
  73. // draws an transparent black (i.e. gray) shadow.
  74. //
  75. // ctx - canvas drawing context
  76. // points - array of points or [x, y, radius, start angle (rad), end angle (rad)]
  77. $.jqplot.ShadowRenderer.prototype.draw = function(ctx, points, options) {
  78. ctx.save();
  79. var opts = (options != null) ? options : {};
  80. var fill = (opts.fill != null) ? opts.fill : this.fill;
  81. var fillRect = (opts.fillRect != null) ? opts.fillRect : this.fillRect;
  82. var closePath = (opts.closePath != null) ? opts.closePath : this.closePath;
  83. var offset = (opts.offset != null) ? opts.offset : this.offset;
  84. var alpha = (opts.alpha != null) ? opts.alpha : this.alpha;
  85. var depth = (opts.depth != null) ? opts.depth : this.depth;
  86. var isarc = (opts.isarc != null) ? opts.isarc : this.isarc;
  87. var linePattern = (opts.linePattern != null) ? opts.linePattern : this.linePattern;
  88. ctx.lineWidth = (opts.lineWidth != null) ? opts.lineWidth : this.lineWidth;
  89. ctx.lineJoin = (opts.lineJoin != null) ? opts.lineJoin : this.lineJoin;
  90. ctx.lineCap = (opts.lineCap != null) ? opts.lineCap : this.lineCap;
  91. ctx.strokeStyle = opts.strokeStyle || this.strokeStyle || 'rgba(0,0,0,'+alpha+')';
  92. ctx.fillStyle = opts.fillStyle || this.fillStyle || 'rgba(0,0,0,'+alpha+')';
  93. for (var j=0; j<depth; j++) {
  94. var ctxPattern = $.jqplot.LinePattern(ctx, linePattern);
  95. ctx.translate(Math.cos(this.angle*Math.PI/180)*offset, Math.sin(this.angle*Math.PI/180)*offset);
  96. ctxPattern.beginPath();
  97. if (isarc) {
  98. ctx.arc(points[0], points[1], points[2], points[3], points[4], true);
  99. }
  100. else if (fillRect) {
  101. if (fillRect) {
  102. ctx.fillRect(points[0], points[1], points[2], points[3]);
  103. }
  104. }
  105. else if (points && points.length){
  106. var move = true;
  107. for (var i=0; i<points.length; i++) {
  108. // skip to the first non-null point and move to it.
  109. if (points[i][0] != null && points[i][1] != null) {
  110. if (move) {
  111. ctxPattern.moveTo(points[i][0], points[i][1]);
  112. move = false;
  113. }
  114. else {
  115. ctxPattern.lineTo(points[i][0], points[i][1]);
  116. }
  117. }
  118. else {
  119. move = true;
  120. }
  121. }
  122. }
  123. if (closePath) {
  124. ctxPattern.closePath();
  125. }
  126. if (fill) {
  127. ctx.fill();
  128. }
  129. else {
  130. ctx.stroke();
  131. }
  132. }
  133. ctx.restore();
  134. };
  135. })(jQuery);