Home Reference Source

cables_dev/cables/src/core/embedding.js

  1. import Patch from "./core_patch.js";
  2. import { generateUUID } from "./utils.js";
  3.  
  4. const EMBED = {};
  5.  
  6. /**
  7. * add patch into html element (will create canvas and set size to fill containerElement)
  8. * @name CABLES.EMBED#addPatch
  9. * @param {object|string} _element containerElement dom element or id of element
  10. * @param {object} options patch options
  11. * @function
  12. */
  13. EMBED.addPatch = function (_element, options)
  14. {
  15. let el = _element;
  16. let id = generateUUID();
  17. if (typeof _element == "string")
  18. {
  19. id = _element;
  20. el = document.getElementById(id);
  21.  
  22. if (!el)
  23. {
  24. console.error(id + " Polyshape Container Element not found!");
  25. return;
  26. }
  27. }
  28.  
  29. const canvEl = document.createElement("canvas");
  30. canvEl.id = "glcanvas_" + id;
  31. canvEl.width = el.clientWidth;
  32. canvEl.height = el.clientHeight;
  33.  
  34. window.addEventListener(
  35. "resize",
  36. function ()
  37. {
  38. this.setAttribute("width", el.clientWidth);
  39. this.height = el.clientHeight;
  40. }.bind(canvEl),
  41. );
  42.  
  43. el.appendChild(canvEl);
  44.  
  45. options = options || {};
  46. options.glCanvasId = canvEl.id;
  47.  
  48. if (!options.onError)
  49. {
  50. options.onError = function (err)
  51. {
  52. console.error(err);
  53. };
  54. }
  55.  
  56. CABLES.patch = new Patch(options);
  57. return canvEl;
  58. };
  59.  
  60. export { EMBED };