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.

107 lines
1.7 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. uniapp canvas 兼容方法
  7. */
  8. function JSUniAppCanvasHelper() { }
  9. JSUniAppCanvasHelper.GetCanvasFont=function(canvas)
  10. {
  11. return canvas.font;
  12. }
  13. JSUniAppCanvasHelper.MeasureText=function(text, canvas)
  14. {
  15. var font= JSUniAppCanvasHelper.GetCanvasFont(canvas);
  16. var fontSize = 12;
  17. var pos=font.search('px');
  18. if (pos>0)
  19. {
  20. var strSize = font.substring(0,pos);
  21. fontSize = parseInt(strSize);
  22. }
  23. text = String(text);
  24. var text = text.split('');
  25. var width = 0;
  26. for (let i = 0; i < text.length; i++)
  27. {
  28. let item = text[i];
  29. if (/[a-zA-Z]/.test(item))
  30. {
  31. width += 7;
  32. }
  33. else if (/[0-9]/.test(item))
  34. {
  35. width += 5.5;
  36. }
  37. else if (/\./.test(item))
  38. {
  39. width += 2.7;
  40. }
  41. else if (/,/.test(item))
  42. {
  43. width += 2.7;
  44. }
  45. else if (/-/.test(item))
  46. {
  47. width += 3.25;
  48. }
  49. else if (/_/.test(item))
  50. {
  51. width += 4.5;
  52. }
  53. else if (/[\u4e00-\u9fa5]/.test(item))
  54. {
  55. width += 10;
  56. }
  57. else if (/\(|\)/.test(item))
  58. {
  59. width += 3.73;
  60. }
  61. else if (/\s/.test(item)) //空格
  62. {
  63. width += 3.25;
  64. }
  65. else if (/%/.test(item))
  66. {
  67. width += 8;
  68. }
  69. else if (/:/.test(item))
  70. {
  71. width += 3.25;
  72. }
  73. else
  74. {
  75. width += 10;
  76. }
  77. }
  78. return width * fontSize / 10;
  79. }
  80. //导出统一使用JSCommon命名空间名
  81. var JSCommonUniApp=
  82. {
  83. JSUniAppCanvasHelper: JSUniAppCanvasHelper,
  84. };
  85. export
  86. {
  87. JSCommonUniApp
  88. }
  89. /*
  90. module.exports =
  91. {
  92. JSCommonUniApp:
  93. {
  94. JSUniAppCanvasHelper: JSUniAppCanvasHelper,
  95. }
  96. };
  97. */