math.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * @Author: LiZhiWei
  3. * @Date: 2025-05-14 15:52:16
  4. * @LastEditors: LiZhiWei
  5. * @LastEditTime: 2025-05-14 15:52:19
  6. * @Description:
  7. */
  8. // 创建数学公式元素
  9. export function createMathElement (element) {
  10. const el = document.createElement('div')
  11. el.style.position = 'absolute'
  12. el.style.top = element.top + 'px'
  13. el.style.left = element.left + 'px'
  14. el.style.width = element.width + 'px'
  15. el.style.height = element.height + 'px'
  16. // 如果有公式图片,优先使用图片显示
  17. if (element.picBase64) {
  18. const img = document.createElement('img')
  19. img.src = element.picBase64
  20. img.style.width = '100%'
  21. img.style.height = '100%'
  22. img.style.objectFit = 'contain'
  23. el.appendChild(img)
  24. return el
  25. }
  26. // 如果有 LaTeX 表达式,使用 MathJax 渲染
  27. if (element.latex) {
  28. // 创建公式容器
  29. const mathContainer = document.createElement('div')
  30. mathContainer.style.width = '100%'
  31. mathContainer.style.height = '100%'
  32. mathContainer.style.display = 'flex'
  33. mathContainer.style.alignItems = 'center'
  34. mathContainer.style.justifyContent = 'center'
  35. // 添加 LaTeX 公式
  36. mathContainer.innerHTML = `\\[${element.latex}\\]`
  37. // 如果 MathJax 不可用,显示原始 LaTeX
  38. mathContainer.style.fontFamily = 'monospace'
  39. mathContainer.style.whiteSpace = 'pre-wrap'
  40. mathContainer.style.padding = '10px'
  41. mathContainer.textContent = element.latex
  42. el.appendChild(mathContainer)
  43. }
  44. return el
  45. }