umychart.uniapp.canvas.helper.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.MeasureText=function(text, canvas)
  10. {
  11. var font = canvas.font;
  12. var fontSize = 12;
  13. var pos=font.search('px');
  14. if (pos>0)
  15. {
  16. var strSize = font.substring(0,pos);
  17. fontSize = parseInt(strSize);
  18. }
  19. text = String(text);
  20. var text = text.split('');
  21. var width = 0;
  22. for (let i = 0; i < text.length; i++)
  23. {
  24. let item = text[i];
  25. if (/[a-zA-Z]/.test(item))
  26. {
  27. width += 7;
  28. } else if (/[0-9]/.test(item))
  29. {
  30. width += 5.5;
  31. } else if (/\./.test(item))
  32. {
  33. width += 2.7;
  34. } else if (/-/.test(item))
  35. {
  36. width += 3.25;
  37. } else if (/[\u4e00-\u9fa5]/.test(item))
  38. {
  39. width += 10;
  40. } else if (/\(|\)/.test(item))
  41. {
  42. width += 3.73;
  43. } else if (/\s/.test(item))
  44. {
  45. width += 2.5;
  46. } else if (/%/.test(item))
  47. {
  48. width += 8;
  49. } else
  50. {
  51. width += 10;
  52. }
  53. }
  54. return width * fontSize / 10;
  55. }
  56. //导出统一使用JSCommon命名空间名
  57. module.exports =
  58. {
  59. JSCommonUniApp:
  60. {
  61. JSUniAppCanvasHelper: JSUniAppCanvasHelper,
  62. }
  63. };