Turn audio into a shareable video. forked from nypublicradio/audiogram

index.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // var d3 = require("d3"),
  2. var patterns = require("./patterns.js"),
  3. textWrapper = require("./text-wrapper.js");
  4. module.exports = function(t) {
  5. var renderer = {},
  6. backgroundImage,
  7. wrapText,
  8. theme;
  9. renderer.backgroundImage = function(_) {
  10. if (!arguments.length) return backgroundImage;
  11. backgroundImage = _;
  12. return this;
  13. };
  14. renderer.theme = function(_) {
  15. if (!arguments.length) return theme;
  16. theme = _;
  17. // Default colors
  18. theme.backgroundColor = theme.backgroundColor || "#fff";
  19. theme.waveColor = theme.waveColor || theme.foregroundColor || "#000";
  20. theme.captionColor = theme.captionColor || theme.foregroundColor || "#000";
  21. // Default wave dimensions
  22. if (typeof theme.waveTop !== "number") theme.waveTop = 0;
  23. if (typeof theme.waveBottom !== "number") theme.waveBottom = theme.height;
  24. if (typeof theme.waveLeft !== "number") theme.waveLeft = 0;
  25. if (typeof theme.waveRight !== "number") theme.waveRight = theme.width;
  26. /*if (location.pathname === "/theme") {
  27. const tf = (theme.noPattern === undefined) ? false : theme.noPattern;
  28. d3.select("#chkNoPattern").property("checked", tf);
  29. }*/
  30. wrapText = textWrapper(theme);
  31. return this;
  32. };
  33. // Draw the frame
  34. renderer.drawFrame = function(context, options){
  35. context.patternQuality = "best";
  36. // Draw the background image and/or background color
  37. context.clearRect(0, 0, theme.width, theme.height);
  38. context.fillStyle = theme.backgroundColor;
  39. context.fillRect(0, 0, theme.width, theme.height);
  40. if (backgroundImage) {
  41. context.drawImage(backgroundImage, 0, 0, theme.width, theme.height);
  42. }
  43. if (!theme.noPattern) {
  44. patterns[theme.pattern || "wave"](context, options.waveform, theme);
  45. }
  46. // Write the caption
  47. if (options.caption) {
  48. wrapText(context, options.caption);
  49. }
  50. // Write subtitle
  51. if (options.subtitle) {
  52. wrapText(context, null, options.subtitle);
  53. }
  54. return this;
  55. };
  56. if (t) {
  57. renderer.theme(t);
  58. }
  59. return renderer;
  60. }