您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CanvasUtils.js 3.4KB

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