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.

143 lines
4.4 KiB

  1. /*
  2. copyright (c) 2018 jones
  3. http://www.apache.org/licenses/LICENSE-2.0
  4. 开源项目 https://github.com/jones2000/HQChart
  5. jones_2000@163.com
  6. 画布节点元素
  7. */
  8. //日志
  9. import { JSConsole } from "./umychart.console.wechat.js"
  10. import { JSCommonUniApp } from './umychart.uniapp.canvas.helper.js'
  11. function JSCanvasElement()
  12. {
  13. this.Height;
  14. this.Width;
  15. this.ID;
  16. this.WebGLCanvas;
  17. this.IsUniApp=false; //uniapp app程序
  18. this.IsUniAppX=false;
  19. this.IsDingTalk=false; //钉钉小程序
  20. this.IsDouYin=false; //抖音小程序
  21. this.CanvasNode=null;
  22. this.ComponentObject=null; //在自定义组件下,当前组件实例的this,表示在这个自定义组件下查找拥有 canvas-id 的 canvas ,如果省略则不在任何自定义组件内查找
  23. this.PixelRatio=null;
  24. //获取画布
  25. this.GetContext = function ()
  26. {
  27. var canvas;
  28. if (this.IsDingTalk)
  29. {
  30. canvas = dd.createCanvasContext(this.ID);
  31. if (this.PixelRatio==null) this.PixelRatio=dd.getSystemInfoSync().pixelRatio;
  32. canvas.restore();
  33. canvas.save();
  34. canvas.scale(this.PixelRatio,this.PixelRatio);
  35. JSConsole.Chart.Log('[JSCanvasElement::GetContext] measureText() => JSUniAppCanvasHelper.MeasureText()');
  36. JSCommonUniApp.JSUniAppCanvasHelper.GetCanvasFont=function(canvas)
  37. {
  38. return canvas.state.font;
  39. }
  40. canvas.measureText = function (text) //uniapp 计算宽度需要自己计算
  41. {
  42. var width = JSCommonUniApp.JSUniAppCanvasHelper.MeasureText(text, canvas);
  43. return { width: width };
  44. }
  45. return canvas;
  46. }
  47. if (this.CanvasNode && this.CanvasNode.node)
  48. {
  49. const width = this.CanvasNode.width;
  50. const height = this.CanvasNode.height;
  51. var node = this.CanvasNode.node;
  52. node._height = height;
  53. node._width = width;
  54. JSConsole.Chart.Log("[JSCanvasElement::GetContext] create by getContext('2d')");
  55. canvas = node.getContext('2d');
  56. const dpr = wx.getSystemInfoSync().pixelRatio;
  57. this.PixelRatio=dpr;
  58. node.width = width * dpr;
  59. node.height = height * dpr;
  60. canvas.restore();
  61. canvas.save();
  62. canvas.scale(dpr, dpr);
  63. canvas.draw = (bDraw, callback) => { if (callback) callback(); };
  64. canvas.DomNode = node;
  65. }
  66. else
  67. {
  68. if (this.ComponentObject) //小程序自定义组件
  69. canvas=wx.createCanvasContext(this.ID,this.ComponentObject);
  70. else
  71. canvas=wx.createCanvasContext(this.ID);
  72. }
  73. if (this.IsUniApp)
  74. {
  75. JSConsole.Chart.Log('[JSCanvasElement::GetContext] measureText() => JSUniAppCanvasHelper.MeasureText()');
  76. canvas.measureText = function (text) //uniapp 计算宽度需要自己计算
  77. {
  78. var width = JSCommonUniApp.JSUniAppCanvasHelper.MeasureText(text, canvas);
  79. return { width: width };
  80. }
  81. canvas.fillText_backup=canvas.fillText; //uniapp fillText 填了最大长度就会失真, 所以去掉
  82. canvas.fillText=function(text,x,y,maxWidth)
  83. {
  84. canvas.fillText_backup(text,x,y);
  85. }
  86. }
  87. if (this.IsUniAppX)
  88. {
  89. var element = uni.getElementById(this.ID);
  90. canvas = element.getContext("2d");
  91. const dpr = uni.getSystemInfoSync().pixelRatio;
  92. element.width = element.offsetWidth * dpr;
  93. element.height = element.offsetHeight * dpr;
  94. canvas.scale(dpr, dpr);
  95. canvas.draw = () => { };
  96. }
  97. return canvas;
  98. }
  99. this.GetWebGLCanvas=function(id)
  100. {
  101. var self=this;
  102. const query = wx.createSelectorQuery();
  103. query.select(id).node().exec((res) => {
  104. JSConsole.Chart.Log('[JSCanvasElement::GetWebGLCanvas] res ', res)
  105. self.WebGLCanvas = res[0].node;
  106. })
  107. }
  108. }
  109. //导出统一使用JSCommon命名空间名
  110. export
  111. {
  112. JSCanvasElement
  113. };
  114. /*
  115. module.exports =
  116. {
  117. JSCommonElement:
  118. {
  119. JSCanvasElement: JSCanvasElement,
  120. },
  121. //单个类导出
  122. JSCommonElement_JSCanvasElement: JSCanvasElement,
  123. };
  124. */