Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Utility class for drawing canvas shapes.
  3. */
  4. var CanvasUtil = (function(my) {
  5. /**
  6. * Draws a round rectangle with a glow. The glowWidth indicates the depth
  7. * of the glow.
  8. *
  9. * @param drawContext the context of the canvas to draw to
  10. * @param x the x coordinate of the round rectangle
  11. * @param y the y coordinate of the round rectangle
  12. * @param w the width of the round rectangle
  13. * @param h the height of the round rectangle
  14. * @param glowColor the color of the glow
  15. * @param glowWidth the width of the glow
  16. */
  17. my.drawRoundRectGlow
  18. = function(drawContext, x, y, w, h, r, glowColor, glowWidth) {
  19. // Save the previous state of the context.
  20. drawContext.save();
  21. if (w < 2 * r) r = w / 2;
  22. if (h < 2 * r) r = h / 2;
  23. // Draw a round rectangle.
  24. drawContext.beginPath();
  25. drawContext.moveTo(x+r, y);
  26. drawContext.arcTo(x+w, y, x+w, y+h, r);
  27. drawContext.arcTo(x+w, y+h, x, y+h, r);
  28. drawContext.arcTo(x, y+h, x, y, r);
  29. drawContext.arcTo(x, y, x+w, y, r);
  30. drawContext.closePath();
  31. // Add a shadow around the rectangle
  32. drawContext.shadowColor = glowColor;
  33. drawContext.shadowBlur = glowWidth;
  34. drawContext.shadowOffsetX = 0;
  35. drawContext.shadowOffsetY = 0;
  36. // Fill the shape.
  37. drawContext.fill();
  38. drawContext.save();
  39. drawContext.restore();
  40. // 1) Uncomment this line to use Composite Operation, which is doing the
  41. // same as the clip function below and is also antialiasing the round
  42. // border, but is said to be less fast performance wise.
  43. // drawContext.globalCompositeOperation='destination-out';
  44. drawContext.beginPath();
  45. drawContext.moveTo(x+r, y);
  46. drawContext.arcTo(x+w, y, x+w, y+h, r);
  47. drawContext.arcTo(x+w, y+h, x, y+h, r);
  48. drawContext.arcTo(x, y+h, x, y, r);
  49. drawContext.arcTo(x, y, x+w, y, r);
  50. drawContext.closePath();
  51. // 2) Uncomment this line to use Composite Operation, which is doing the
  52. // same as the clip function below and is also antialiasing the round
  53. // border, but is said to be less fast performance wise.
  54. // drawContext.fill();
  55. // Comment these two lines if choosing to do the same with composite
  56. // operation above 1 and 2.
  57. drawContext.clip();
  58. drawContext.clearRect(0, 0, 277, 200);
  59. // Restore the previous context state.
  60. drawContext.restore();
  61. };
  62. /**
  63. * Clones the given canvas.
  64. *
  65. * @return the new cloned canvas.
  66. */
  67. my.cloneCanvas = function (oldCanvas) {
  68. /*
  69. * FIXME Testing has shown that oldCanvas may not exist. In such a case,
  70. * the method CanvasUtil.cloneCanvas may throw an error. Since audio
  71. * levels are frequently updated, the errors have been observed to pile
  72. * into the console, strain the CPU.
  73. */
  74. if (!oldCanvas)
  75. return oldCanvas;
  76. //create a new canvas
  77. var newCanvas = document.createElement('canvas');
  78. var context = newCanvas.getContext('2d');
  79. //set dimensions
  80. newCanvas.width = oldCanvas.width;
  81. newCanvas.height = oldCanvas.height;
  82. //apply the old canvas to the new one
  83. context.drawImage(oldCanvas, 0, 0);
  84. //return the new canvas
  85. return newCanvas;
  86. };
  87. return my;
  88. })(CanvasUtil || {});