You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CanvasUtils.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * Utility class for drawing canvas shapes.
  18. */
  19. var CanvasUtil = (function(my) {
  20. /**
  21. * Draws a round rectangle with a glow. The glowWidth indicates the depth
  22. * of the glow.
  23. *
  24. * @param drawContext the context of the canvas to draw to
  25. * @param x the x coordinate of the round rectangle
  26. * @param y the y coordinate of the round rectangle
  27. * @param w the width of the round rectangle
  28. * @param h the height of the round rectangle
  29. * @param glowColor the color of the glow
  30. * @param glowWidth the width of the glow
  31. */
  32. my.drawRoundRectGlow
  33. = function(drawContext, x, y, w, h, r, glowColor, glowWidth) {
  34. // Save the previous state of the context.
  35. drawContext.save();
  36. if (w < 2 * r) r = w / 2;
  37. if (h < 2 * r) r = h / 2;
  38. // Draw a round rectangle.
  39. drawContext.beginPath();
  40. drawContext.moveTo(x+r, y);
  41. drawContext.arcTo(x+w, y, x+w, y+h, r);
  42. drawContext.arcTo(x+w, y+h, x, y+h, r);
  43. drawContext.arcTo(x, y+h, x, y, r);
  44. drawContext.arcTo(x, y, x+w, y, r);
  45. drawContext.closePath();
  46. // Add a shadow around the rectangle
  47. drawContext.shadowColor = glowColor;
  48. drawContext.shadowBlur = glowWidth;
  49. drawContext.shadowOffsetX = 0;
  50. drawContext.shadowOffsetY = 0;
  51. // Fill the shape.
  52. drawContext.fill();
  53. drawContext.save();
  54. drawContext.restore();
  55. // 1) Uncomment this line to use Composite Operation, which is doing the
  56. // same as the clip function below and is also antialiasing the round
  57. // border, but is said to be less fast performance wise.
  58. // drawContext.globalCompositeOperation='destination-out';
  59. drawContext.beginPath();
  60. drawContext.moveTo(x+r, y);
  61. drawContext.arcTo(x+w, y, x+w, y+h, r);
  62. drawContext.arcTo(x+w, y+h, x, y+h, r);
  63. drawContext.arcTo(x, y+h, x, y, r);
  64. drawContext.arcTo(x, y, x+w, y, r);
  65. drawContext.closePath();
  66. // 2) Uncomment this line to use Composite Operation, which is doing the
  67. // same as the clip function below and is also antialiasing the round
  68. // border, but is said to be less fast performance wise.
  69. // drawContext.fill();
  70. // Comment these two lines if choosing to do the same with composite
  71. // operation above 1 and 2.
  72. drawContext.clip();
  73. drawContext.clearRect(0, 0, 277, 200);
  74. // Restore the previous context state.
  75. drawContext.restore();
  76. };
  77. /**
  78. * Clones the given canvas.
  79. *
  80. * @return the new cloned canvas.
  81. */
  82. my.cloneCanvas = function (oldCanvas) {
  83. /*
  84. * FIXME Testing has shown that oldCanvas may not exist. In such a case,
  85. * the method CanvasUtil.cloneCanvas may throw an error. Since audio
  86. * levels are frequently updated, the errors have been observed to pile
  87. * into the console, strain the CPU.
  88. */
  89. if (!oldCanvas)
  90. return oldCanvas;
  91. //create a new canvas
  92. var newCanvas = document.createElement('canvas');
  93. var context = newCanvas.getContext('2d');
  94. //set dimensions
  95. newCanvas.width = oldCanvas.width;
  96. newCanvas.height = oldCanvas.height;
  97. //apply the old canvas to the new one
  98. context.drawImage(oldCanvas, 0, 0);
  99. //return the new canvas
  100. return newCanvas;
  101. };
  102. return my;
  103. })(CanvasUtil || {});
  104. module.exports = CanvasUtil;