Home Reference Source

cables_dev/cables/src/core/cg/cg_canvas.js

  1. class CgCanvas
  2. {
  3. constructor(options)
  4. {
  5. if (!options)
  6. {
  7. console.error("CgCanvas no options");
  8. }
  9. else
  10. {
  11. this._canvasEle = options.canvasEle;
  12. }
  13.  
  14. if (!options.cg)console.error("CgCanvas options has no cg");
  15. if (!options.canvasEle)console.error("CgCanvas options has no canvasEle");
  16.  
  17. this._cg = options.cg;
  18. this.pixelDensity = 1;
  19. this.canvasWidth = this.canvasEle.clientWidth;
  20. this.canvasHeight = this.canvasEle.clientHeight;
  21.  
  22. this._oldWidthRp = -1;
  23. this._oldHeightRp = -1;
  24.  
  25. this.setSize(this.canvasWidth, this.canvasHeight);
  26. }
  27.  
  28. get canvasEle() { return this._canvasEle; }
  29.  
  30.  
  31. setSize(w, h, ignorestyle)
  32. {
  33. if (this._oldWidthRp != w * this.pixelDensity || this._oldHeightRp != h * this.pixelDensity)
  34. {
  35. this._oldWidthRp = this.canvasEle.width = w * this.pixelDensity;
  36. this._oldHeightRp = this.canvasEle.height = h * this.pixelDensity;
  37.  
  38. if (!ignorestyle)
  39. {
  40. this.canvasEle.style.width = w + "px";
  41. this.canvasEle.style.height = h + "px";
  42. }
  43.  
  44. this.updateSize();
  45.  
  46. this._cg.emitEvent("resize");
  47. }
  48. }
  49.  
  50. updateSize()
  51. {
  52. this.canvasEle.width = this.canvasWidth = this.canvasEle.clientWidth * this.pixelDensity;
  53. this.canvasEle.height = this.canvasHeight = this.canvasEle.clientHeight * this.pixelDensity;
  54. }
  55.  
  56. dispose()
  57. {
  58. this._canvasEle.remove();
  59. this._canvasEle = null;
  60. }
  61. }
  62.  
  63. export { CgCanvas };