{"id":275943,"date":"2024-05-16T14:39:34","date_gmt":"2024-05-16T06:39:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/275943.html"},"modified":"2024-05-16T14:39:42","modified_gmt":"2024-05-16T06:39:42","slug":"vue3-%e9%a1%b9%e7%9b%ae%e7%a8%8b%e5%ba%8f%e6%80%8e%e4%b9%88%e5%88%a9%e7%94%a8-canvas-%e5%ae%9e%e7%8e%b0%e7%a7%bb%e5%8a%a8%e7%ab%af%e6%89%8b%e5%86%99%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/275943.html","title":{"rendered":"Vue3 \u9879\u76ee\u7a0b\u5e8f\u600e\u4e48\u5229\u7528 canvas \u5b9e\u73b0\u79fb\u52a8\u7aef\u624b\u5199\u677f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200650\/1fb0465e-5219-46f6-9f3e-2b94ecdc4bc7.webp\" alt=\"Vue3 \u9879\u76ee\u7a0b\u5e8f\u600e\u4e48\u5229\u7528 canvas \u5b9e\u73b0\u79fb\u52a8\u7aef\u624b\u5199\u677f\" \/><\/p>\n<p><p>Vue3\u9879\u76ee\u4e2d\u5229\u7528canvas\u5b9e\u73b0\u79fb\u52a8\u7aef\u624b\u5199\u677f\u662f\u4e00\u4e2a\u7ed3\u5408\u524d\u7aef\u6846\u67b6\u548cHtml5\u65b0\u7279\u6027\u7684\u5b9e\u7528\u529f\u80fd\uff0c\u5b83<strong>\u63d0\u4f9b\u7528\u6237\u624b\u5199\u8f93\u5165\u548c\u7ed8\u753b<\/strong>\u7684\u754c\u9762\u3002\u901a\u8fc7Vue3\u7684\u54cd\u5e94\u5f0f\u548c\u7ec4\u4ef6\u5316\u7279\u6027\u3001\u7ed3\u5408canvas\u7684\u7ed8\u56feAPI\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u9ad8\u6548\u4e14\u6d41\u7545\u7684\u79fb\u52a8\u7aef\u624b\u5199\u677f\u3002<strong>\u9996\u5148\uff0c\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2acanvas\u5143\u7d20\u5e76\u901a\u8fc7Vue3\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u521d\u59cb\u5316\u7ed8\u56fe\u4e0a\u4e0b\u6587<\/strong>\uff0c\u518d\u76d1\u542c\u7528\u6237\u7684\u89e6\u6478\u4e8b\u4ef6\u6765\u7ed8\u5236\u7528\u6237\u7684\u624b\u5199\u8def\u5f84\uff0c\u6700\u540e\u53ef\u4ee5\u6dfb\u52a0\u529f\u80fd\u5982\u64a4\u9500\u3001\u91cd\u505a\u6216\u6e05\u7a7a\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efaCanvas\u7ed8\u56fe\u533a\u57df<\/p>\n<\/p>\n<p><p>\u5728Vue3\u7ec4\u4ef6\u7684template\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49canvas\u5143\u7d20\uff0c\u4e3a\u4e4b\u540e\u7684\u7ed8\u5236\u5de5\u4f5c\u505a\u51c6\u5907\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;template&gt;<\/p>\n<p>  &lt;div class=&quot;handwriting-board&quot;&gt;<\/p>\n<p>    &lt;canvas ref=&quot;handwritingCanvas&quot;&gt;&lt;\/canvas&gt;<\/p>\n<p>  &lt;\/div&gt;<\/p>\n<p>&lt;\/template&gt;<\/p>\n<p>&lt;style&gt;<\/p>\n<p>.handwriting-board {<\/p>\n<p>  \/* \u6837\u5f0f\u8bbe\u7f6e\uff0c\u786e\u4fddcanvas\u5143\u7d20\u586b\u5145\u7236\u5bb9\u5668 *\/<\/p>\n<p>  width: 100%;<\/p>\n<p>  height: 100%;<\/p>\n<p>}<\/p>\n<p>&lt;\/style&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u521d\u59cb\u5316Canvas<\/p>\n<\/p>\n<p><p>\u5728\u7ec4\u4ef6\u7684<code>setup<\/code>\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u4f7f\u7528Vue3\u63d0\u4f9b\u7684<code>onMounted<\/code>\u751f\u547d\u5468\u671f\u94a9\u5b50\u6765\u521d\u59cb\u5316canvas\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">&lt;script&gt;<\/p>\n<p>import { ref, onMounted } from &#039;vue&#039;;<\/p>\n<p>export default {<\/p>\n<p>  setup() {<\/p>\n<p>    const handwritingCanvas = ref(null);<\/p>\n<p>    onMounted(() =&gt; {<\/p>\n<p>      const canvas = handwritingCanvas.value;<\/p>\n<p>      \/\/ \u8bbe\u7f6ecanvas\u7684\u5c3a\u5bf8\uff0c\u786e\u4fdd\u5176\u5728\u4e0d\u540c\u8bbe\u5907\u4e0a\u7684\u4e00\u81f4\u6027<\/p>\n<p>      canvas.width = window.innerWidth;<\/p>\n<p>      canvas.height = window.innerHeight;<\/p>\n<p>      \/\/ \u83b7\u53d62D\u7ed8\u56fe\u4e0a\u4e0b\u6587\u5e76\u914d\u7f6e<\/p>\n<p>      const ctx = canvas.getContext(&#039;2d&#039;);<\/p>\n<p>      ctx.strokeStyle = &#039;#000000&#039;; \/\/ \u8bbe\u7f6e\u753b\u7b14\u989c\u8272<\/p>\n<p>      ctx.lineWidth = 2; \/\/ \u8bbe\u7f6e\u753b\u7b14\u7c97\u7ec6<\/p>\n<p>    });<\/p>\n<p>    return { handwritingCanvas };<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5b9e\u73b0\u624b\u5199\u903b\u8f91<\/p>\n<\/p>\n<p><p>\u5b83\u8981\u76d1\u542c\u7528\u6237\u7684\u624b\u6307\u89e6\u6478\u4e8b\u4ef6\uff0c\u5e76\u5728canvas\u4e0a\u7ed8\u56fe\u3002\u6b64\u8fc7\u7a0b\u6d89\u53ca<code>touchstart<\/code>\u3001<code>touchmove<\/code>\u548c<code>touchend<\/code>\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u89e6\u6478\u4e8b\u4ef6\u76d1\u542c\u4e0e\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5c06\u89e6\u6478\u4e8b\u4ef6\u7ed1\u5b9a\u5230canvas\u5143\u7d20\uff0c\u5e76\u5b9a\u4e49\u5176\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">&lt;script&gt;<\/p>\n<p>\/\/ ...<\/p>\n<p>function handleTouchStart(event) {<\/p>\n<p>  \/\/ \u9632\u6b62\u5c4f\u5e55\u6eda\u52a8<\/p>\n<p>  event.preventDefault();<\/p>\n<p>  \/\/ \u5b9e\u73b0\u7ed8\u5236\u5f00\u59cb\u7684\u903b\u8f91<\/p>\n<p>}<\/p>\n<p>function handleTouchMove(event) {<\/p>\n<p>  \/\/ \u9632\u6b62\u5c4f\u5e55\u6eda\u52a8<\/p>\n<p>  event.preventDefault();<\/p>\n<p>  \/\/ \u5b9e\u73b0\u7ed8\u5236\u8fc7\u7a0b\u7684\u903b\u8f91<\/p>\n<p>}<\/p>\n<p>function handleTouchEnd(event) {<\/p>\n<p>  \/\/ \u5b9e\u73b0\u7ed8\u5236\u7ed3\u675f\u7684\u903b\u8f91<\/p>\n<p>}<\/p>\n<p>\/\/ ...<\/p>\n<p>export default {<\/p>\n<p>  \/\/ ...<\/p>\n<p>  setup() {<\/p>\n<p>    \/\/ ...<\/p>\n<p>    onMounted(() =&gt; {<\/p>\n<p>      \/\/ ...<\/p>\n<p>      \/\/ \u7ed1\u5b9a\u4e8b\u4ef6\u76d1\u542c<\/p>\n<p>      canvas.addEventListener(&#039;touchstart&#039;, handleTouchStart);<\/p>\n<p>      canvas.addEventListener(&#039;touchmove&#039;, handleTouchMove);<\/p>\n<p>      canvas.addEventListener(&#039;touchend&#039;, handleTouchEnd);<\/p>\n<p>    });<\/p>\n<p>    return {<\/p>\n<p>      handwritingCanvas<\/p>\n<p>      \/\/ ...<\/p>\n<p>    };<\/p>\n<p>  }<\/p>\n<p>};<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u7ed8\u5236\u8def\u5f84\u903b\u8f91\u5b9e\u73b0<\/p>\n<\/p>\n<p><p>\u5728\u89e6\u6478\u4e8b\u4ef6\u7684\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6309\u7167\u624b\u6307\u7684\u79fb\u52a8\u7ed8\u5236\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ ...<\/p>\n<p>let lastTouch = null;<\/p>\n<p>function drawLine(x1, y1, x2, y2) {<\/p>\n<p>  \/\/ \u4f7f\u7528ctx\u7ed8\u5236\u8def\u5f84<\/p>\n<p>}<\/p>\n<p>function handleTouchStart(event) {<\/p>\n<p>  \/\/ ...<\/p>\n<p>  const touch = event.touches[0];<\/p>\n<p>  lastTouch = { x: touch.clientX, y: touch.clientY };<\/p>\n<p>  \/\/ \u5f00\u59cb\u65b0\u7684\u8def\u5f84\u7ed8\u5236<\/p>\n<p>  ctx.beginPath();<\/p>\n<p>  ctx.moveTo(lastTouch.x, lastTouch.y);<\/p>\n<p>}<\/p>\n<p>function handleTouchMove(event) {<\/p>\n<p>  \/\/ ...<\/p>\n<p>  const touch = event.touches[0];<\/p>\n<p>  const currentTouch = { x: touch.clientX, y: touch.clientY };<\/p>\n<p>  drawLine(lastTouch.x, lastTouch.y, currentTouch.x, currentTouch.y);<\/p>\n<p>  \/\/ \u66f4\u65b0\u6700\u540e\u4e00\u6b21\u89e6\u6478\u4f4d\u7f6e<\/p>\n<p>  lastTouch = currentTouch;<\/p>\n<p>}<\/p>\n<p>function handleTouchEnd(event) {<\/p>\n<p>  \/\/ \u7ed3\u675f\u8def\u5f84\u7ed8\u5236<\/p>\n<p>  ctx.closePath();<\/p>\n<p>}<\/p>\n<p>\/\/ ...<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u589e\u52a0\u624b\u5199\u677f\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u624b\u5199\u677f\u66f4\u52a0\u5b9e\u7528\uff0c\u6dfb\u52a0\u5982\u64a4\u9500\u3001\u91cd\u505a\u3001\u4fdd\u5b58\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u624b\u5199\u677f\u529f\u80fd\u5b9e\u73b0<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ ...<\/p>\n<p>const paths = []; \/\/ \u4fdd\u5b58\u7ed8\u5236\u7684\u8def\u5f84\u7528\u4e8e\u64a4\u9500\u529f\u80fd<\/p>\n<p>let redoStack = []; \/\/ \u7528\u4e8e\u91cd\u505a\u529f\u80fd\u7684\u5806\u6808<\/p>\n<p>function undo() {<\/p>\n<p>  \/\/ \u64a4\u9500\u64cd\u4f5c<\/p>\n<p>  if (paths.length &gt; 0) {<\/p>\n<p>    redoStack.push(paths.pop());<\/p>\n<p>    redrawCanvas();<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>function redo() {<\/p>\n<p>  \/\/ \u91cd\u505a\u64cd\u4f5c<\/p>\n<p>  if (redoStack.length &gt; 0) {<\/p>\n<p>    paths.push(redoStack.pop());<\/p>\n<p>    redrawCanvas();<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>function save() {<\/p>\n<p>  \/\/ \u4fdd\u5b58\u624b\u5199\u5185\u5bb9<\/p>\n<p>  const dataURL = handwritingCanvas.value.toDataURL(&#039;image\/png&#039;);<\/p>\n<p>  \/\/ \u5c06dataURL\u53d1\u9001\u5230\u670d\u52a1\u5668\u6216\u5728\u65b0\u7a97\u53e3\u6253\u5f00<\/p>\n<p>}<\/p>\n<p>function redrawCanvas() {<\/p>\n<p>  \/\/ \u6e05\u9664canvas\u5e76\u91cd\u7ed8\u6240\u6709\u8def\u5f84<\/p>\n<p>  ctx.clearRect(0, 0, canvas.width, canvas.height);<\/p>\n<p>  paths.forEach(path =&gt; {<\/p>\n<p>    path.forEach((point, index) =&gt; {<\/p>\n<p>      if (index === 0) {<\/p>\n<p>        ctx.moveTo(point.x, point.y);<\/p>\n<p>      } else {<\/p>\n<p>        drawLine(paths[index - 1].x, paths[index - 1].y, point.x, point.y);<\/p>\n<p>      }<\/p>\n<p>    });<\/p>\n<p>  });<\/p>\n<p>}<\/p>\n<p>\/\/ ...<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u89e6\u6478\u9002\u914d\u53ca\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u8981\u786e\u4fdd\u624b\u5199\u677f\u5728\u4e0d\u540c\u8bbe\u5907\u4e0a\u8868\u73b0\u826f\u597d\uff0c\u53ef\u4ee5\u8fdb\u884c\u9002\u914d\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u9002\u914d\u79fb\u52a8\u7aef\u8bbe\u5907<\/p>\n<\/p>\n<p><p>\u9488\u5bf9\u79fb\u52a8\u7aef\u8bbe\u5907\uff0c\u786e\u4fddcanvas\u7684\u89e6\u6478\u4e8b\u4ef6\u6b63\u786e\u5904\u7406\uff0c\u5e76\u4f18\u5316\u7ed8\u56fe\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ ...<\/p>\n<p>function handleTouchMove(event) {<\/p>\n<p>  \/\/ ...<\/p>\n<p>  \/\/ \u901a\u8fc7requestAnimationFrame\u4f18\u5316\u7ed8\u5236\u6027\u80fd<\/p>\n<p>  if (!isDrawing) {<\/p>\n<p>    requestAnimationFrame(() =&gt; {<\/p>\n<p>      drawLine(lastTouch.x, lastTouch.y, currentTouch.x, currentTouch.y);<\/p>\n<p>      lastTouch = currentTouch;<\/p>\n<p>      isDrawing = false;<\/p>\n<p>    });<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>\/\/ ...<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6240\u6709\u6838\u5fc3\u529f\u80fd\u5b9e\u73b0\u4e4b\u540e\uff0cVue3\u79fb\u52a8\u7aef\u624b\u5199\u677f\u5e94\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u7528\u6237\u7684\u624b\u5199\u8f93\u5165\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u6d41\u7545\u7684\u7528\u6237\u4f53\u9a8c\u3002\u901a\u8fc7\u4e0d\u65ad\u8c03\u8bd5\u548c\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u624b\u5199\u677f\u7684\u54cd\u5e94\u6027\u548c\u53ef\u7528\u6027\u6ee1\u8db3\u73b0\u4ee3\u79fb\u52a8\u5e94\u7528\u7684\u8981\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 Vue3 \u9879\u76ee\u4e2d\u4f7f\u7528 canvas \u5b9e\u73b0\u79fb\u52a8\u7aef\u624b\u5199\u677f\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p>\u4e3a\u4e86\u5728 Vue3 \u9879\u76ee\u4e2d\u5b9e\u73b0\u79fb\u52a8\u7aef\u624b\u5199\u677f\uff0c\u9996\u5148\u9700\u8981\u5728\u7ec4\u4ef6\u4e2d\u5f15\u5165 canvas \u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528 <code>&lt;canvas&gt;<\/code> \u6807\u7b7e\u6216\u8005\u52a8\u6001\u521b\u5efa canvas \u5143\u7d20\u3002<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\uff0c\u9700\u8981\u5728 Vue3 \u7ec4\u4ef6\u7684 <code>mounted<\/code> \u94a9\u5b50\u51fd\u6570\u4e2d\u83b7\u53d6\u5230 canvas \u5143\u7d20\uff0c\u5e76\u8bbe\u7f6e\u5176\u5bbd\u9ad8\u7b49\u5c5e\u6027\u3002\u4e5f\u53ef\u4ee5\u5728\u6837\u5f0f\u4e2d\u8bbe\u7f6e canvas \u7684\u5bbd\u9ad8\uff0c\u5e76\u4f7f\u7528 <code>window.devicePixelRatio<\/code> \u83b7\u53d6\u9ad8\u6e05\u5c4f\u7684\u8bbe\u5907\u50cf\u7d20\u6bd4\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u5728 Vue3 \u7ec4\u4ef6\u4e2d\u76d1\u542c <code>touchstart<\/code>\u3001<code>touchmove<\/code> \u548c <code>touchend<\/code> \u4e8b\u4ef6\uff0c\u6765\u83b7\u53d6\u624b\u6307\u5728 canvas \u4e0a\u7684\u79fb\u52a8\u8f68\u8ff9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728 <code>touchstart<\/code> \u4e8b\u4ef6\u4e2d\uff0c\u83b7\u53d6\u624b\u6307\u6309\u4e0b\u65f6\u7684\u5750\u6807\uff0c\u5e76\u8c03\u7528 canvas \u7684 <code>beginPath<\/code> \u65b9\u6cd5\uff0c\u5f00\u59cb\u8def\u5f84\u8bb0\u5f55\u3002\u5728 <code>touchmove<\/code> \u4e8b\u4ef6\u4e2d\uff0c\u6839\u636e\u624b\u6307\u7684\u79fb\u52a8\u8f68\u8ff9\u8c03\u7528 canvas \u7684 <code>lineTo<\/code> \u65b9\u6cd5\uff0c\u7ed8\u5236\u8def\u5f84\u3002\u6700\u540e\uff0c\u5728 <code>touchend<\/code> \u4e8b\u4ef6\u4e2d\uff0c\u8c03\u7528 canvas \u7684 <code>stroke<\/code> \u65b9\u6cd5\uff0c\u5c06\u8def\u5f84\u7ed8\u5236\u5230\u753b\u5e03\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>\u901a\u8fc7\u76d1\u542c\u624b\u5199\u677f\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u624b\u5199\u5185\u5bb9\u7684\u7ed8\u5236\u3002\u53ef\u4ee5\u4f7f\u7528 <code>context.lineWidth<\/code> \u8bbe\u7f6e\u7ebf\u5bbd\uff0c<code>context.strokeStyle<\/code> \u8bbe\u7f6e\u7ebf\u6761\u989c\u8272\u7b49\u3002<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u5728 Vue3 \u9879\u76ee\u4e2d\u5b9e\u73b0\u624b\u5199\u5185\u5bb9\u7684\u4fdd\u5b58\u548c\u6e05\u9664\u529f\u80fd\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p>\u8981\u5b9e\u73b0\u624b\u5199\u5185\u5bb9\u7684\u4fdd\u5b58\u529f\u80fd\uff0c\u53ef\u4ee5\u5728 Vue3 \u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a data \u5c5e\u6027\uff0c\u5982 <code>handwritingContent<\/code>\uff0c\u7528\u6765\u5b58\u50a8\u624b\u5199\u7684\u5185\u5bb9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u624b\u5199\u677f\u4e0a\uff0c\u6bcf\u6b21\u7ed8\u5236\u8def\u5f84\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u76d1\u542c <code>touchend<\/code> \u4e8b\u4ef6\u83b7\u53d6\u5230\u6700\u540e\u7ed8\u5236\u7684\u56fe\u7247\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528 <code>canvas.toDataURL<\/code> \u65b9\u6cd5\u5c06 canvas \u7684\u5185\u5bb9\u8f6c\u6362\u4e3a base64 \u7f16\u7801\u7684\u56fe\u7247\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c06\u8fd9\u4e9b\u56fe\u7247\u6570\u636e\u4fdd\u5b58\u5230 <code>handwritingContent<\/code> \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u591a\u5f20\u56fe\u7247\u3002\u5728\u4fdd\u5b58\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>push<\/code> \u65b9\u6cd5\u5c06\u56fe\u7247\u6570\u636e\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>\u8981\u5b9e\u73b0\u624b\u5199\u5185\u5bb9\u7684\u6e05\u9664\u529f\u80fd\uff0c\u53ef\u4ee5\u5728 Vue3 \u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5982 <code>clearHandwriting<\/code>\u3002\u5f53\u7528\u6237\u70b9\u51fb\u6e05\u9664\u6309\u94ae\u65f6\uff0c\u8c03\u7528\u8be5\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728 <code>clearHandwriting<\/code> \u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u6e05\u7a7a <code>handwritingContent<\/code> \u6570\u7ec4\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u5728 Vue3 \u9879\u76ee\u4e2d\u5b9e\u73b0\u624b\u5199\u5185\u5bb9\u7684\u64a4\u9500\u529f\u80fd\uff1f<\/strong><\/p>\n<ol>\n<li>\n<p>\u8981\u5b9e\u73b0\u624b\u5199\u5185\u5bb9\u7684\u64a4\u9500\u529f\u80fd\uff0c\u53ef\u4ee5\u5728 Vue3 \u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5982 <code>undoHandwriting<\/code>\u3002\u5f53\u7528\u6237\u70b9\u51fb\u64a4\u9500\u6309\u94ae\u65f6\uff0c\u8c03\u7528\u8be5\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728 <code>undoHandwriting<\/code> \u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7684 <code>pop<\/code> \u65b9\u6cd5\uff0c\u5c06 <code>handwritingContent<\/code> \u4e2d\u6700\u540e\u4e00\u4e2a\u56fe\u7247\u6570\u636e\u5220\u9664\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e3a\u4e86\u5b9e\u65f6\u5c55\u793a\u64a4\u9500\u64cd\u4f5c\u7684\u6548\u679c\uff0c\u53ef\u4ee5\u5728 Vue3 \u7ec4\u4ef6\u7684\u6a21\u677f\u4e2d\u4f7f\u7528 <code>v-for<\/code> \u6307\u4ee4\u904d\u5386 <code>handwritingContent<\/code> \u6570\u7ec4\uff0c\u5c06\u6bcf\u4e2a\u56fe\u7247\u6570\u636e\u90fd\u7ed8\u5236\u5230 canvas \u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53 <code>handwritingContent<\/code> \u6570\u7ec4\u53d1\u751f\u53d8\u5316\u65f6\uff0cVue3 \u4f1a\u81ea\u52a8\u66f4\u65b0\u89c6\u56fe\uff0c\u5b9e\u73b0\u64a4\u9500\u64cd\u4f5c\u7684\u6548\u679c\u3002<\/p>\n<\/li>\n<li>\n<p>\u53e6\u5916\uff0c\u53ef\u4ee5\u7ed9\u64a4\u9500\u6309\u94ae\u6dfb\u52a0\u4e00\u4e2a\u6761\u4ef6\u5224\u65ad\uff0c\u5f53 <code>handwritingContent<\/code> \u6570\u7ec4\u4e3a\u7a7a\u65f6\uff0c\u7981\u7528\u64a4\u9500\u6309\u94ae\uff0c\u907f\u514d\u64a4\u9500\u64cd\u4f5c\u8fc7\u5ea6\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"Vue3\u9879\u76ee\u4e2d\u5229\u7528canvas\u5b9e\u73b0\u79fb\u52a8\u7aef\u624b\u5199\u677f\u662f\u4e00\u4e2a\u7ed3\u5408\u524d\u7aef\u6846\u67b6\u548cHtml5\u65b0\u7279\u6027\u7684\u5b9e\u7528\u529f\u80fd\uff0c\u5b83\u63d0\u4f9b\u7528\u6237\u624b\u5199\u8f93 [&hellip;]","protected":false},"author":3,"featured_media":275956,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275943"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=275943"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275943\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/275956"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=275943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=275943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=275943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}