{"id":46,"date":"2025-03-25T21:19:03","date_gmt":"2025-03-25T13:19:03","guid":{"rendered":"https:\/\/private.blueju.cn\/?p=46"},"modified":"2026-05-14T16:20:41","modified_gmt":"2026-05-14T08:20:41","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%9c%a8ios%e4%b8%8b%ef%bc%8chtml2canvas%e7%94%9f%e6%88%90%e6%b5%b7%e6%8a%a5%e6%96%87%e5%ad%97%e8%87%aa%e5%8a%a8%e6%8d%a2%e8%a1%8c%e5%bc%82%e5%b8%b8%e7%bc%a9%e8%bf%9b","status":"publish","type":"post","link":"https:\/\/private.blueju.cn\/?p=46","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u5728iOS\u4e0b\uff0chtml2canvas\u751f\u6210\u6d77\u62a5\u6587\u5b57\u81ea\u52a8\u6362\u884c\u5f02\u5e38\u7f29\u8fdb\u7684\u95ee\u9898"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u6700\u8fd1\u5728\u505a\u5c0f\u7a0b\u5e8f\uff0c\u6709\u4e00\u4e2a\u751f\u6210\u6d77\u62a5\u7684\u9700\u6c42\uff0c\u5728\u5c0f\u7a0b\u5e8f\u4e0b\u662f\u6ca1\u6709DOM\u7684\uff0c\u6240\u4ee5\u7528 Webview H\n5 + <code>html2canvas<\/code> \u6765\u505a\uff0c\u505a\u51fa\u6765\u4ee5\u540e\u5728 Andriod \u4e0a\u8fd8\u597d\uff0c\u4f46\u662f\u5728 iOS \u91cc\u6362\u884c\u6709\u95ee\u9898\uff0c\u6362\u884c\u5f02\u5e38\u7f29\u8fdb\u3002<\/p>\n<p>\u6211\u4f7f\u7528\u7684\u662f <code>html2canvas<\/code> \uff0c\u76f4\u63a5\u4f20\u5165\u9700\u8981\u6e32\u67d3\u7684\u6d77\u62a5\u6587\u672c\uff0c\u8ba9 <code>html2canvas<\/code> \u81ea\u52a8\u5904\u7406\u6362\u884c\uff0c\u4f30\u8ba1\u8fd9\u4e2a <code>html2canvas<\/code> \u5728\u6a21\u62df\u6d4f\u89c8\u5668\u6587\u672c\u5e03\u5c40\u65f6\uff0c\u5728 iOS \u4e0b\u81ea\u52a8\u6362\u884c\u8ba1\u7b97\u7ed3\u679c\u6709 bug\u3002<\/p>\n<p>\u89e3\u51b3\u601d\u8def\u662f\uff1a<strong>\u5728\u8c03\u7528 <code>html2canvas<\/code> \u4e4b\u524d\uff0c\u4e0d\u518d\u8ba9\u76ee\u6807\u6587\u672c\u4f9d\u8d56 <code>html2canvas<\/code> \u81ea\u52a8\u6362\u884c\uff0c\u800c\u662f\u5728 DOM \u9636\u6bb5\u5148\u628a\u6587\u672c\u62c6\u6210\u591a\u4e2a\u663e\u5f0f\u884c\u8282\u70b9\u3002<\/strong><\/p>\n<h2>\u65b9\u6848\u601d\u8def<\/h2>\n<p>\u6838\u5fc3\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u9690\u85cf\u7684\u6d4b\u91cf <code>span<\/code><\/li>\n<li>\u5c06\u5f85\u5224\u65ad\u7684\u5019\u9009\u5b57\u7b26\u4e32\u585e\u5165 <code>span<\/code><\/li>\n<li>\u901a\u8fc7 <code>span.getBoundingClientRect().width<\/code> \u83b7\u53d6\u771f\u5b9e DOM \u6e32\u67d3\u5bbd\u5ea6<\/li>\n<li>\u5c06\u8be5\u5bbd\u5ea6\u4e0e\u76ee\u6807\u5bb9\u5668\u5185\u5bb9\u5bbd\u5ea6\u6bd4\u8f83<\/li>\n<li>\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\uff0c\u627e\u5230\u5f53\u524d\u884c\u80fd\u5bb9\u7eb3\u7684\u6700\u5927\u5b57\u7b26\u4e32\u957f\u5ea6<\/li>\n<li>\u4e0d\u65ad\u5904\u7406\u5269\u4f59\u6587\u672c\uff0c\u76f4\u5230\u5168\u6587\u62c6\u5206\u5b8c\u6210<\/li>\n<li>\u5c06\u62c6\u5206\u540e\u7684\u6bcf\u4e00\u884c\u6e32\u67d3\u6210\u72ec\u7acb DOM \u8282\u70b9<\/li>\n<li>\u6700\u540e\u518d\u8c03\u7528 <code>html2canvas<\/code> \u622a\u56fe<\/li>\n<\/ol>\n<p>\u5904\u7406\u524d\u7684 DOM \u53ef\u80fd\u662f\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"lang-html language-html html\">&lt;div class=&quot;text&quot;&gt;\n  \u8fd9\u662f\u4e00\u6bb5\u5f88\u957f\u7684\u6587\u672c\uff0c\u7531\u6d4f\u89c8\u5668\u81ea\u52a8\u6362\u884c\u3002\n&lt;\/div&gt;<\/code><\/pre>\n<p>\u5904\u7406\u540e\u53d8\u6210\uff1a<\/p>\n<pre><code class=\"lang-html language-html html\">&lt;div class=&quot;line&quot;&gt;\u8fd9\u662f\u4e00\u6bb5\u5f88\u957f\u7684&lt;\/div&gt;\n&lt;div class=&quot;line&quot;&gt;\u6587\u672c\uff0c\u7531\u6d4f\u89c8\u5668&lt;\/div&gt;\n&lt;div class=&quot;line&quot;&gt;\u81ea\u52a8\u6362\u884c\u3002&lt;\/div&gt;<\/code><\/pre>\n<p>\u8fd9\u6837 <code>html2canvas<\/code> \u4e0d\u518d\u9700\u8981\u81ea\u5df1\u7406\u89e3\u957f\u6587\u672c\u5982\u4f55\u81ea\u52a8\u6362\u884c\uff0c\u53ea\u9700\u8981\u622a\u56fe\u5df2\u7ecf\u6392\u597d\u7248\u7684 DOM\u3002<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u7528\u4e8c\u5206<\/h2>\n<p>\u5982\u679c\u4ece\u7b2c 1 \u4e2a\u5b57\u7b26\u5f00\u59cb\u4e00\u4e2a\u4e2a\u8ffd\u52a0\uff0c\u518d\u5224\u65ad\u5bbd\u5ea6\u662f\u5426\u8d85\u51fa\u5bb9\u5668\uff0c\u590d\u6742\u5ea6\u4f1a\u504f\u9ad8\u3002\n\u4f8b\u5982\u4e00\u6bb5\u6587\u672c\u6709 1000 \u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u9010\u5b57\u6d4b\u91cf\uff0c\u6bcf\u4e00\u884c\u90fd\u53ef\u80fd\u89e6\u53d1\u5f88\u591a\u6b21 DOM \u5e03\u5c40\u8ba1\u7b97\u3002<\/p>\n<p>\u4e0d\u8fc7\u5176\u5b9e\u4e0d\u7528\u4e8c\u5206\u4e5f\u53ef\u4ee5\uff0c\u6bd5\u7adf\u5c0f\u7a0b\u5e8f\u91cc\u7684\u6d77\u62a5\uff0c\u80fd\u585e\u4e0b\u51e0\u4e2a\u5b57\uff0c\u4e00\u4e2a\u4e2a\u52a0\u7136\u540e\u518d\u8ba1\u7b97\u6bd4\u8f83\u5c31\u884c\uff0c\u4e0d\u8fc7\u5e0c\u671b\u81ea\u5df1\u80fd\u8003\u8651\u591a\u4e00\u4e9b\u3001\u9002\u5e94\u6027\u66f4\u5e7f\u7684\u573a\u666f\uff0c\u6240\u4ee5\u9009\u62e9\u4e86\u4e8c\u5206\u3002<\/p>\n<p>\u4e8c\u5206\u7b56\u7565\u7684\u4f18\u52bf\u662f\uff0c\u6bcf\u4e00\u884c\u53ea\u9700\u8981\u5927\u7ea6 <code>log2(n)<\/code> \u6b21\u6d4b\u91cf\u3002<\/p>\n<p>\u4f8b\u5982\u5f53\u524d\u5269\u4f59\u6587\u672c\u957f\u5ea6\u662f 1000\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u6b21\u6d4b 500 \u4e2a\u5b57\u7b26<\/li>\n<li>\u7b2c\u4e8c\u6b21\u6d4b 250 \u6216 750 \u4e2a\u5b57\u7b26<\/li>\n<li>\u7b2c\u4e09\u6b21\u7ee7\u7eed\u7f29\u5c0f\u8303\u56f4<\/li>\n<li>\u5927\u7ea6 10 \u6b21\u4ee5\u5185\u5c31\u80fd\u627e\u5230\u6700\u5408\u9002\u7684\u957f\u5ea6<\/li>\n<\/ul>\n<p>\u8fd9\u6bd4\u9010\u5b57\u8ffd\u52a0\u66f4\u9002\u5408\u957f\u6587\u672c\u573a\u666f\u3002<\/p>\n<h2>\u6838\u5fc3\u5b9e\u73b0<\/h2>\n<h3>1. \u51c6\u5907\u6837\u5f0f<\/h3>\n<p>\u4e3a\u4e86\u8ba9\u6d4b\u91cf\u7ed3\u679c\u548c\u6700\u7ec8\u6e32\u67d3\u7ed3\u679c\u4e00\u81f4\uff0c\u9690\u85cf <code>span<\/code> \u5fc5\u987b\u590d\u5236\u771f\u5b9e\u6587\u672c\u8282\u70b9\u7684\u5173\u952e\u5b57\u4f53\u6837\u5f0f\u3002<\/p>\n<pre><code class=\"lang-js language-js js\">function copyLineStyle(source, target) {\n  const style = getComputedStyle(source);\n  const props = [\n    &quot;fontFamily&quot;,\n    &quot;fontSize&quot;,\n    &quot;fontWeight&quot;,\n    &quot;fontStyle&quot;,\n    &quot;fontVariant&quot;,\n    &quot;letterSpacing&quot;,\n    &quot;lineHeight&quot;,\n    &quot;textTransform&quot;\n  ];\n\n  props.forEach((prop) =&gt; {\n    target.style[prop] = style[prop];\n  });\n}<\/code><\/pre>\n<p>\u5982\u679c\u6d4b\u91cf\u8282\u70b9\u548c\u771f\u5b9e\u8282\u70b9\u7684\u5b57\u4f53\u3001\u5b57\u53f7\u3001\u5b57\u91cd\u3001\u5b57\u95f4\u8ddd\u4e0d\u4e00\u81f4\uff0c\u8ba1\u7b97\u51fa\u6765\u7684\u884c\u5bbd\u5c31\u4f1a\u6709\u504f\u5dee\u3002<\/p>\n<h3>2. \u521b\u5efa\u9690\u85cf\u6d4b\u91cf\u8282\u70b9<\/h3>\n<pre><code class=\"lang-js language-js js\">let measureSpan = null;\n\nfunction getMeasureSpan(styleSource) {\n  if (!measureSpan) {\n    measureSpan = document.createElement(&quot;span&quot;);\n    measureSpan.className = &quot;measure-span&quot;;\n    document.body.appendChild(measureSpan);\n  }\n\n  copyLineStyle(styleSource, measureSpan);\n  return measureSpan;\n}<\/code><\/pre>\n<p>\u5bf9\u5e94 CSS\uff1a<\/p>\n<pre><code class=\"lang-css language-css css\">.measure-span {\n  position: fixed;\n  left: -99999px;\n  top: -99999px;\n  display: inline-block;\n  width: auto;\n  max-width: none;\n  padding: 0;\n  margin: 0;\n  white-space: pre;\n  word-break: keep-all;\n  overflow-wrap: normal;\n  visibility: hidden;\n  pointer-events: none;\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6709\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n<ul>\n<li><code>position: fixed<\/code> \u52a0 <code>left\/top: -99999px<\/code>\uff1a\u907f\u514d\u5f71\u54cd\u9875\u9762\u5e03\u5c40<\/li>\n<li><code>display: inline-block<\/code>\uff1a\u4fbf\u4e8e\u8bfb\u53d6\u6587\u672c\u5b9e\u9645\u5bbd\u5ea6<\/li>\n<li><code>white-space: pre<\/code>\uff1a\u4fdd\u7559\u7a7a\u683c\uff0c\u4e0d\u8ba9\u6d4b\u91cf\u6587\u672c\u81ea\u52a8\u6298\u884c<\/li>\n<li><code>word-break: keep-all<\/code> \u548c <code>overflow-wrap: normal<\/code>\uff1a\u907f\u514d\u6d4b\u91cf\u65f6\u88ab\u6d4f\u89c8\u5668\u63d0\u524d\u65ad\u5f00<\/li>\n<\/ul>\n<h3>3. \u6d4b\u91cf\u6587\u672c\u5bbd\u5ea6<\/h3>\n<pre><code class=\"lang-js language-js js\">const widthCache = new Map();\n\nfunction measureTextWidth(text, styleSource) {\n  if (!text) return 0;\n\n  const style = getComputedStyle(styleSource);\n  const cacheKey = [\n    text,\n    style.fontFamily,\n    style.fontSize,\n    style.fontWeight,\n    style.letterSpacing\n  ].join(&quot;::&quot;);\n\n  if (widthCache.has(cacheKey)) {\n    return widthCache.get(cacheKey);\n  }\n\n  const span = getMeasureSpan(styleSource);\n  span.textContent = text;\n\n  const width = span.getBoundingClientRect().width;\n  widthCache.set(cacheKey, width);\n  return width;\n}<\/code><\/pre>\n<p>\u8fd9\u4e00\u6b65\u662f\u6574\u4e2a\u65b9\u6848\u7684\u57fa\u7840\u3002<\/p>\n<p>\u6bcf\u6b21\u5c06\u5019\u9009\u5b57\u7b26\u4e32\u5199\u5165\u9690\u85cf <code>span<\/code>\uff0c\u7136\u540e\u8bfb\u53d6\u5b83\u7684\u5b9e\u9645 DOM \u5bbd\u5ea6\u3002\u8fd9\u91cc\u6ca1\u6709\u8c03\u7528 <code>html2canvas<\/code>\uff0c\u56e0\u6b64\u6027\u80fd\u6bd4\u201c\u6bcf\u6b21\u5019\u9009\u90fd\u622a\u56fe\u518d\u6d4b\u91cf\u201d\u8981\u597d\u5f88\u591a\u3002<\/p>\n<p><code>widthCache<\/code> \u7528\u6765\u7f13\u5b58\u5df2\u7ecf\u6d4b\u91cf\u8fc7\u7684\u5b57\u7b26\u4e32\uff0c\u907f\u514d\u91cd\u590d\u89e6\u53d1\u5e03\u5c40\u8bfb\u53d6\u3002<\/p>\n<h3>4. \u4e8c\u5206\u67e5\u627e\u6bcf\u884c\u6700\u5927\u957f\u5ea6<\/h3>\n<pre><code class=\"lang-js language-js js\">function findMaxFitLength(text, maxWidth, styleSource) {\n  let left = 1;\n  let right = text.length;\n  let answer = 1;\n\n  while (left &lt;= right) {\n    const mid = Math.floor((left + right) \/ 2);\n    const candidate = text.slice(0, mid);\n    const width = measureTextWidth(candidate, styleSource);\n\n    if (width &lt;= maxWidth) {\n      answer = mid;\n      left = mid + 1;\n    } else {\n      right = mid - 1;\n    }\n  }\n\n  return answer;\n}<\/code><\/pre>\n<p>\u5224\u65ad\u903b\u8f91\u5f88\u7b80\u5355\uff1a<\/p>\n<ul>\n<li>\u5019\u9009\u5b57\u7b26\u4e32\u5bbd\u5ea6 <code>&lt;= maxWidth<\/code>\uff1a\u8bf4\u660e\u8fd8\u80fd\u7ee7\u7eed\u653e\u66f4\u591a\u5b57\u7b26<\/li>\n<li>\u5019\u9009\u5b57\u7b26\u4e32\u5bbd\u5ea6 <code>&gt; maxWidth<\/code>\uff1a\u8bf4\u660e\u5f53\u524d\u5b57\u7b26\u4e32\u592a\u957f\uff0c\u9700\u8981\u7f29\u77ed<\/li>\n<\/ul>\n<p>\u6700\u7ec8\u8fd4\u56de\u7684 <code>answer<\/code> \u5c31\u662f\u5f53\u524d\u884c\u80fd\u653e\u4e0b\u7684\u6700\u5927\u5b57\u7b26\u6570\u3002<\/p>\n<h3>5. \u5c06\u6574\u6bb5\u6587\u672c\u62c6\u6210\u591a\u884c<\/h3>\n<pre><code class=\"lang-js language-js js\">function splitTextIntoLines(text, maxWidth, styleSource) {\n  const normalized = text.replace(\/\\r\\n\/g, &quot;\\n&quot;);\n  const lines = [];\n\n  normalized.split(&quot;\\n&quot;).forEach((paragraph) =&gt; {\n    let rest = paragraph;\n\n    if (!rest) {\n      lines.push(&quot;&quot;);\n      return;\n    }\n\n    while (rest.length &gt; 0) {\n      let length = findMaxFitLength(rest, maxWidth, styleSource);\n      length = Math.max(1, length);\n\n      let line = rest.slice(0, length);\n      line = adjustBreakPoint(line, rest.length);\n\n      lines.push(line.trimEnd());\n      rest = rest.slice(line.length).trimStart();\n    }\n  });\n\n  return lines;\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5faa\u73af\u5904\u7406\u5269\u4f59\u6587\u672c\uff1a<\/p>\n<ol>\n<li>\u5bf9\u5f53\u524d\u5269\u4f59\u6587\u672c\u6267\u884c\u4e8c\u5206<\/li>\n<li>\u627e\u5230\u4e00\u884c\u80fd\u653e\u4e0b\u7684\u6700\u5927\u957f\u5ea6<\/li>\n<li>\u622a\u53d6\u8fd9\u4e00\u884c<\/li>\n<li>\u5c06\u5269\u4f59\u5185\u5bb9\u7ee7\u7eed\u8fdb\u5165\u4e0b\u4e00\u8f6e<\/li>\n<\/ol>\n<p><code>Math.max(1, length)<\/code> \u662f\u4e3a\u4e86\u515c\u5e95\u3002\u5982\u679c\u67d0\u4e2a\u5355\u5b57\u7b26\u672c\u8eab\u5c31\u8d85\u8fc7\u5bb9\u5668\u5bbd\u5ea6\uff0c\u4e5f\u5fc5\u987b\u81f3\u5c11\u63a8\u8fdb 1 \u4e2a\u5b57\u7b26\uff0c\u907f\u514d\u6b7b\u5faa\u73af\u3002<\/p>\n<h3>6. \u4f18\u5316\u65ad\u884c\u4f4d\u7f6e<\/h3>\n<p>\u4e3a\u4e86\u8ba9\u4e2d\u82f1\u6587\u6df7\u6392\u7684\u65ad\u884c\u66f4\u81ea\u7136\uff0c\u53ef\u4ee5\u4f18\u5148\u5728\u6807\u70b9\u6216\u7a7a\u683c\u5904\u65ad\u5f00\u3002<\/p>\n<pre><code class=\"lang-js language-js js\">function adjustBreakPoint(line, restLength) {\n  if (line.length &gt;= restLength) {\n    return line;\n  }\n\n  const breakChars = &quot;\uff0c\u3002\uff1b\uff1a\u3001,.!?;: &quot;;\n  for (let i = line.length - 1; i &gt; 0; i -= 1) {\n    if (breakChars.includes(line[i])) {\n      return line.slice(0, i + 1);\n    }\n  }\n\n  return line;\n}<\/code><\/pre>\n<p>\u8fd9\u4e0d\u662f\u5fc5\u9700\u903b\u8f91\uff0c\u4f46\u5bf9\u53ef\u8bfb\u6027\u6709\u5e2e\u52a9\u3002<\/p>\n<p>\u4f8b\u5982\u539f\u672c\u53ef\u80fd\u62c6\u6210\uff1a<\/p>\n<pre><code class=\"lang-txt language-txt txt\">\u8fd9\u662f\u4e00\u4e2a html2can\nvas \u622a\u56fe\u95ee\u9898<\/code><\/pre>\n<p>\u7ecf\u8fc7\u65ad\u70b9\u4f18\u5316\u540e\uff0c\u53ef\u80fd\u53d8\u6210\uff1a<\/p>\n<pre><code class=\"lang-txt language-txt txt\">\u8fd9\u662f\u4e00\u4e2a\nhtml2canvas \u622a\u56fe\u95ee\u9898<\/code><\/pre>\n<p>\u5177\u4f53\u662f\u5426\u4fdd\u7559\uff0c\u8981\u770b\u4e1a\u52a1\u662f\u5426\u66f4\u91cd\u89c6\u6392\u7248\u81ea\u7136\u5ea6\uff0c\u8fd8\u662f\u66f4\u91cd\u89c6\u6bcf\u884c\u5c3d\u91cf\u586b\u6ee1\u3002<\/p>\n<h3>7. \u6e32\u67d3\u4e3a\u663e\u5f0f\u884c\u8282\u70b9<\/h3>\n<pre><code class=\"lang-js language-js js\">function renderLines(lines) {\n  lineRoot.replaceChildren();\n\n  lines.forEach((line) =&gt; {\n    const div = document.createElement(&quot;div&quot;);\n    div.className = &quot;line&quot;;\n    div.textContent = line || &quot; &quot;;\n    lineRoot.appendChild(div);\n  });\n}<\/code><\/pre>\n<p>\u5bf9\u5e94 CSS\uff1a<\/p>\n<pre><code class=\"lang-css language-css css\">.line {\n  min-height: 25.6px;\n  font-size: 16px;\n  line-height: 1.6;\n  color: #101828;\n  white-space: pre;\n  word-break: keep-all;\n  overflow-wrap: normal;\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u5173\u952e\u662f\uff1a<\/p>\n<ul>\n<li>\u6bcf\u4e00\u884c\u90fd\u662f\u72ec\u7acb\u8282\u70b9<\/li>\n<li>\u4e0d\u518d\u4f9d\u8d56 <code>html2canvas<\/code> \u81ea\u52a8\u6362\u884c<\/li>\n<li><code>white-space: pre<\/code> \u4fdd\u8bc1\u5355\u884c\u5185\u5bb9\u6309\u539f\u6837\u5c55\u793a<\/li>\n<\/ul>\n<h3>8. \u6700\u540e\u6267\u884c html2canvas<\/h3>\n<pre><code class=\"lang-js language-js js\">async function captureImage() {\n  const canvas = await html2canvas(captureTarget, {\n    backgroundColor: &quot;#ffffff&quot;,\n    scale: window.devicePixelRatio || 1,\n    logging: false\n  });\n\n  result.replaceChildren(canvas);\n}<\/code><\/pre>\n<p>\u5230\u8fd9\u4e00\u6b65\u65f6\uff0c\u76ee\u6807 DOM \u5df2\u7ecf\u5b8c\u6210\u9884\u6362\u884c\u5904\u7406\u3002<\/p>\n<p><code>html2canvas<\/code> \u53ea\u9700\u8981\u8d1f\u8d23\u622a\u56fe\uff0c\u4e0d\u518d\u53c2\u4e0e\u6362\u884c\u8ba1\u7b97\u3002<\/p>\n<h2>\u5b8c\u6574\u8c03\u7528\u6d41\u7a0b<\/h2>\n<pre><code class=\"lang-js language-js js\">async function wrapByDomBinarySearch() {\n  widthCache.clear();\n\n  const maxTextWidth = getContentWidth(captureTarget);\n  const lines = splitTextIntoLines(inputText.value, maxTextWidth, styleSource);\n\n  renderLines(lines);\n}<\/code><\/pre>\n<h2>\u65b9\u6848\u4f18\u70b9<\/h2>\n<p>\u76f8\u6bd4\u76f4\u63a5\u8ba9 <code>html2canvas<\/code> \u5904\u7406\u81ea\u52a8\u6362\u884c\uff0c\u8fd9\u4e2a\u65b9\u6848\u6709\u51e0\u4e2a\u660e\u663e\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li>\u622a\u56fe\u524d\u5df2\u7ecf\u5b8c\u6210\u6392\u7248\uff0c\u7ed3\u679c\u66f4\u7a33\u5b9a<\/li>\n<li>\u4e0d\u4f9d\u8d56 <code>html2canvas<\/code> \u5bf9\u81ea\u52a8\u6362\u884c\u7684\u8fd8\u539f\u80fd\u529b<\/li>\n<li>DOM \u6d4b\u91cf\u6027\u80fd\u597d\u4e8e\u53cd\u590d\u8c03\u7528 <code>html2canvas<\/code><\/li>\n<li>\u4e8c\u5206\u67e5\u627e\u6bd4\u9010\u5b57\u8ffd\u52a0\u66f4\u9002\u5408\u957f\u6587\u672c<\/li>\n<li>\u5bf9 iOS WebView\u3001Safari \u7b49\u517c\u5bb9\u6027\u95ee\u9898\u66f4\u53ef\u63a7<\/li>\n<\/ul>\n<h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<h3>1. \u6d4b\u91cf\u6837\u5f0f\u5fc5\u987b\u4e00\u81f4<\/h3>\n<p>\u9690\u85cf <code>span<\/code> \u7684\u6837\u5f0f\u8981\u548c\u6700\u7ec8 <code>.line<\/code> \u7684\u6837\u5f0f\u4fdd\u6301\u4e00\u81f4\uff0c\u81f3\u5c11\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>font-family<\/code><\/li>\n<li><code>font-size<\/code><\/li>\n<li><code>font-weight<\/code><\/li>\n<li><code>font-style<\/code><\/li>\n<li><code>letter-spacing<\/code><\/li>\n<li><code>line-height<\/code><\/li>\n<li><code>text-transform<\/code><\/li>\n<li><code>white-space<\/code><\/li>\n<li><code>word-break<\/code><\/li>\n<li><code>overflow-wrap<\/code><\/li>\n<\/ul>\n<p>\u5426\u5219\u6d4b\u91cf\u5bbd\u5ea6\u548c\u6700\u7ec8\u6e32\u67d3\u5bbd\u5ea6\u4f1a\u4e0d\u4e00\u81f4\u3002<\/p>\n<h3>2. \u5bb9\u5668\u5bbd\u5ea6\u8981\u7528\u5185\u5bb9\u5bbd\u5ea6<\/h3>\n<p>\u5982\u679c\u76ee\u6807\u5bb9\u5668\u6709 <code>padding<\/code>\uff0c\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528 <code>clientWidth<\/code> \u4f5c\u4e3a\u6587\u672c\u53ef\u7528\u5bbd\u5ea6\u3002<\/p>\n<p>\u5e94\u8be5\u51cf\u53bb\u5de6\u53f3 padding\uff1a<\/p>\n<pre><code class=\"lang-js language-js js\">function getContentWidth(element) {\n  const style = getComputedStyle(element);\n  return element.clientWidth\n    - parseFloat(style.paddingLeft)\n    - parseFloat(style.paddingRight);\n}<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u8fd9\u4e2a\u65b9\u6848\u7684\u672c\u8d28\u662f\uff1a<\/p>\n<p><strong>\u628a\u4e0d\u53ef\u63a7\u7684\u81ea\u52a8\u6362\u884c\uff0c\u63d0\u524d\u8f6c\u6362\u6210\u53ef\u63a7\u7684\u663e\u5f0f DOM \u884c\u3002<\/strong><\/p>\n<p>\u5728 iOS \u4e0b\u9047\u5230 <code>html2canvas<\/code> \u81ea\u52a8\u6362\u884c\u622a\u56fe\u5f02\u5e38\u65f6\uff0c\u4e0d\u8981\u8ba9 <code>html2canvas<\/code> \u7ee7\u7eed\u731c\u6d4b\u6587\u672c\u5e94\u8be5\u5982\u4f55\u6362\u884c\uff0c\u800c\u662f\u5728\u622a\u56fe\u4e4b\u524d\u5b8c\u6210\u6362\u884c\u8ba1\u7b97\u3002<\/p>\n<p>DOM \u9636\u6bb5\u4f7f\u7528\u9690\u85cf <code>span<\/code> \u6d4b\u91cf\u5bbd\u5ea6\uff0c\u914d\u5408\u4e8c\u5206\u67e5\u627e\u8ba1\u7b97\u6bcf\u884c\u6700\u5927\u957f\u5ea6\uff0c\u65e2\u80fd\u4fdd\u8bc1\u6027\u80fd\uff0c\u4e5f\u80fd\u63d0\u9ad8\u622a\u56fe\u7ed3\u679c\u7684\u4e00\u81f4\u6027\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u6700\u8fd1\u5728\u505a\u5c0f\u7a0b\u5e8f\uff0c\u6709\u4e00\u4e2a\u751f\u6210\u6d77\u62a5\u7684\u9700\u6c42\uff0c\u5728\u5c0f\u7a0b\u5e8f\u4e0b\u662f\u6ca1\u6709DOM\u7684\uff0c\u6240\u4ee5\u7528 Webview H 5 + ht [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/private.blueju.cn\/index.php?rest_route=\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/private.blueju.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/private.blueju.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/private.blueju.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/private.blueju.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=46"}],"version-history":[{"count":1,"href":"https:\/\/private.blueju.cn\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/private.blueju.cn\/index.php?rest_route=\/wp\/v2\/posts\/46\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/private.blueju.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/private.blueju.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/private.blueju.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}