{"id":29018,"date":"2024-11-25T09:58:38","date_gmt":"2024-11-25T01:58:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/29018\/"},"modified":"2024-11-25T09:58:38","modified_gmt":"2024-11-25T01:58:38","slug":"%e5%9b%be%e6%96%87%e8%af%a6%e8%a7%a3%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8fcanvas-api%e5%ae%9e%e4%be%8b%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/29018\/","title":{"rendered":"\u56fe\u6587\u8be6\u89e3\u5fae\u4fe1\u5c0f\u7a0b\u5e8fcanvas API\u5b9e\u4f8b\u4ee3\u7801"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f  api\u8be6\u89e3\u53ca\u5b9e\u4f8b\u4ee3\u7801\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/013\/8a3c098466babb62988596a210a64d3f-0.png\" style=\"max-width:90%\" class=\"aligncenter\" title=\"\u56fe\u6587\u8be6\u89e3\u5fae\u4fe1\u5c0f\u7a0b\u5e8fcanvas API\u5b9e\u4f8b\u4ee3\u7801\u63d2\u56fe\" alt=\"\u56fe\u6587\u8be6\u89e3\u5fae\u4fe1\u5c0f\u7a0b\u5e8fcanvas API\u5b9e\u4f8b\u4ee3\u7801\u63d2\u56fe\" \/><\/p>\n<p>\u7ed8\u56fe\u662f\u6bcf\u4e2a\u79fb\u52a8\u5e94\u7528\u5fc5\u5907\u7684\u6280\u672f\uff0c\u57fa\u672c\u4e0a\u548c\uff0cIOS\uff0c\u7b49\u79fb\u52a8\u5f00\u53d1\u90fd\u662f\u76f8\u540c\u7684\uff0c\u521b\u5efa\u4e2a\u4e0a\u4e0b\u6587\uff0c\u7ed9\u4f60\u4e2a\u753b\u5e03\u518d\u4e0a\u753b\uff0c\u5b98\u7f51\u7ed9\u7684\u5c0f\u4f8b\u5b50\u90fd\u6bd4\u8f83\u5168\u4e86\u81ea\u5df1\u53bb\u770b\u5427\uff0cdrawImage\u65f6\u6ca1\u6709\u53cd\u5e94\u4e0d\u77e5\u9053\u662fBUG\u8fd8\u662f\u7535\u8111\u4e0d\u80fd\u6d4b\u8bd5\u5f85\u5b9a\uff0chttp:\/\/www.php.cn\/course\/342.html<\/p>\n<p>\u5c4f\u5e55\u5c31\u50cf\u662f\u6570\u5b66\u4e0a\u7684\u5750\u6807\u8f74\uff0c\u4e14\u5728\u7b2c\u56db\u8c61\u9650\uff0c\u4ee5\u5c4f\u5e55\u5de6\u4e0a\u89d2\u4e3a\u5706\u70b9\uff0cX\u8f74\u5411\u53f3\u4e3a\u6b63\u5411\u5de6\u4e3a\u8d1f\uff0cY\u8f74\u5411\u4e0b\u4e3a\u6b63\u5411\u4e0a\u4e3a\u8d1f\uff08\u8fd9\u70b9\u548c\u6570\u5b66\u4e0a\u76f8\u53cd\u7684\uff09\u4ee5\u5706\u70b9\u4e3a\u57fa\u70b9\u753b\u4e2a\u8ddd\u79bb\u5706\u70b9\u4e0a\u4e0b50\u5bbd\u9ad8100\u7684\u77e9\u5f62\u6765\u6f14\u793acanvas\u57fa\u672c\u7528\u6cd5<\/p>\n<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8fd9\u91cc\u63d0\u4f9b\u4e86\u4e24\u4e2aAPI<\/p>\n<p>wx.createContext() \u521b\u5efa\u5e76\u8fd4\u56de\u7ed8\u56fe\u4e0a\u4e0b\u6587context\u5bf9\u8c61<\/p>\n<p>getActions \u83b7\u53d6\u5f53\u524dcontext\u4e0a\u5b58\u50a8\u7684\u7ed8\u56fe\u52a8\u4f5c\uff0c\u5bf9\u5e94wx.drawCanvas(object)\u4e2d\u7684actions<br \/>clearActions \u6e05\u7a7a\u5f53\u524d\u7684\u5b58\u50a8\u7ed8\u56fe\u52a8\u4f5c<\/p>\n<p>wx.drawCanvas(object) \u7ed8\u5236<\/p>\n<p>canvasId \u753b\u5e03\u6807\u8bc6\uff0c\u4f20\u5165\u7684cavas-id\uff0c\u8fd9\u91cc\u7684\u6807\u8bc6\u53ef\u4ee5\u4e3aNumber\uff0c\u4e5f\u53ef\u4ee5\u662f<br \/>actions \u7ed8\u56fe\u52a8\u4f5c\uff0c\u7531wx.createContext\u521b\u5efa\u7684context\uff0c\u8c03\u7528getActions\u65b9\u6cd5\u5bfc\u51fa\u7ed8\u56fe\u52a8\u4f5c\u6570\u7ec4\u3002<\/p>\n<p>\u7ed8\u56fe\u4e2d\u53ef\u4ee5\u8fdb\u884c\u53d8\u5f62\uff0c\u7ed8\u5236\uff0c\u8def\u5f84\uff0c\u6837\u5f0f\uff0c\u8fd9\u4e9b\u4e2a\u4e1c\u897f\u6709\u70b9\u591a\u5b98\u7f51\u6709\u4f8b\u5b50\uff0c\u8fd9\u91cc\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u5f15\u5165<\/p>\n<p><span style=\"color: #0000ff\">wxml<\/span><\/p>\n<pre>&lt;!--\u753b\u5e03\r\n canvas-id \u4e3a\u753b\u5e03\u6807\u8bc6\uff0c\u5f53\u7ed8\u5236\u65f6\u901a\u8fc7canvas-id\u627e\u5230\u753b\u5e03\r\n--&gt;\r\n&lt;canvas&gt;&lt;\/canvas&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><span style=\"color: #0000ff\">js<\/span><\/p>\n<pre>Page({\r\n&nbsp;data:{\r\n&nbsp;&nbsp;text:\"Page&nbsp;canvas\"\r\n&nbsp;},\r\n&nbsp;onLoad:function(options){\r\n&nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u521d\u59cb\u5316&nbsp;options\u4e3a\u9875\u9762\u8df3\u8f6c\u6240\u5e26\u6765\u7684\u53c2\u6570\r\n&nbsp;},\r\n&nbsp;onReady:function(){\r\n&nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u6e32\u67d3\u5b8c\u6210\r\n&nbsp;&nbsp;\/\/\u7b2c\u4e00\u6b65\u521b\u5efa\u4e2a\u4e0a\u4e0b\u6587\u5bb9\u5668\r\n&nbsp;&nbsp;var&nbsp;context&nbsp;=&nbsp;wx.createContext();\r\n\r\n&nbsp;&nbsp;\/\/\u7b2c\u4e8c\u6b65\u7ed8\u5236\u8fd9\u91cc\u6211\u4eec\u7ed8\u5236\u4e2a\u77e9\u5f62&nbsp;\r\n&nbsp;&nbsp;\/\/x,&nbsp;y,&nbsp;widht,&nbsp;height\r\n&nbsp;&nbsp;context.rect(50,&nbsp;50,&nbsp;100,&nbsp;100);\r\n&nbsp;&nbsp;\/\/\u7ed8\u5236\u7684\u6837\u5f0f\u8fdb\u884c\u63cf\u8fb9\u7ed8\u5236\uff0cfill\u4e3a\u586b\u5145\u4f4d\u7f6e\r\n&nbsp;&nbsp;context.stroke();\r\n&nbsp;&nbsp;\/**\r\n&nbsp;&nbsp;&nbsp;*&nbsp;\u8c03\u7528wx.drawCanvas\uff0c\u901a\u8fc7canvasId\u6307\u5b9a\u5728\u54ea\u5f20\u753b\u5e03\u4e0a\u7ed8\u5236\uff0c\u901a\u8fc7actions\u6307\u5b9a\u7ed8\u5236\u884c\u4e3a\r\n&nbsp;&nbsp;&nbsp;*&nbsp;\r\n&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;\u6ce8\u610fconvasId\u53ef\u4ee5\u4e3a\u6570\u5b57\u8868\u793a\u4e5f\u53ef\u4ee5\u7528\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5c31\u662f\u4e00\u4e2a\u7ed8\u5236\u5bf9\u8c61\u7684\u6807\u8bc6\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\r\n&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;actions&nbsp;\u662f\u4ececontext\u4e0a\u4e0b\u6587\u4e2d\u83b7\u53d6\u7684\u7ed8\u5236\u884c\u4e3a\uff0c\u5373\u4e3a\u7b2c\u4e8c\u6b65\u64cd\u4f5c\r\n&nbsp;&nbsp;&nbsp;*\/\r\n\r\n&nbsp;&nbsp;wx.drawCanvas({\r\n&nbsp;&nbsp;&nbsp;\/\/\u753b\u5e03\u6807\u8bc6\uff0c\u4f20\u5165&lt;canvas&gt;&lt;\/canvas&gt;\u7684cavas-id\r\n&nbsp;&nbsp;&nbsp;canvasId:&nbsp;'identify',\r\n&nbsp;&nbsp;&nbsp;\/\/\u83b7\u53d6\u7ed8\u5236\u884c\u4e3a\uff0c&nbsp;\u5c31\u76f8\u5f53\u4e8e\u4f60\u60f3\u505a\u5230\u83dccontext.getActions()\u5c31\u662f\u98df\u6750\r\n&nbsp;&nbsp;&nbsp;actions:&nbsp;context.getActions(),\r\n&nbsp;&nbsp;})\r\n\r\n\r\n&nbsp;},\r\n&nbsp;onShow:function(){\r\n&nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u663e\u793a\r\n&nbsp;},\r\n&nbsp;onHide:function(){\r\n&nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u9690\u85cf\r\n&nbsp;},\r\n&nbsp;onUnload:function(){\r\n&nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u5173\u95ed\r\n&nbsp;}\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u8c22\u8c22\u5927\u5bb6\u5bf9\u672c\u7ad9\u7684\u652f\u6301\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u56fe\u6587\u8be6\u89e3\u5fae\u4fe1\u5c0f\u7a0b\u5e8fcanvas API\u5b9e\u4f8b\u4ee3\u7801\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f api\u8be6\u89e3\u53ca\u5b9e\u4f8b\u4ee3\u7801\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b \u7ed8\u56fe\u662f\u6bcf\u4e2a\u79fb\u52a8\u5e94\u7528\u5fc5\u5907\u7684\u6280\u672f\uff0c\u57fa\u672c\u4e0a\u548c\uff0cIOS\uff0c\u7b49\u79fb\u52a8\u5f00\u53d1\u90fd\u662f\u76f8\u540c\u7684\uff0c\u521b\u5efa\u4e2a\u4e0a\u4e0b\u6587\uff0c\u7ed9\u4f60\u4e2a\u753b\u5e03\u518d\u4e0a\u753b\uff0c\u5b98\u7f51\u7ed9\u7684\u5c0f\u4f8b\u5b50\u90fd\u6bd4\u8f83\u5168\u4e86\u81ea\u5df1\u53bb\u770b\u5427\uff0cdrawImage\u65f6\u6ca1\u6709\u53cd\u5e94\u4e0d\u77e5\u9053\u662fBUG\u8fd8\u662f\u7535\u8111\u4e0d\u80fd\u6d4b\u8bd5\u5f85\u5b9a\uff0chttp:\/\/www.php.cn\/course\/342.html \u5c4f\u5e55\u5c31\u50cf\u662f\u6570\u5b66\u4e0a\u7684\u5750\u6807\u8f74\uff0c\u4e14\u5728\u7b2c\u56db\u8c61\u9650\uff0c\u4ee5\u5c4f\u5e55\u5de6\u4e0a\u89d2\u4e3a\u5706\u70b9\uff0cX\u8f74\u5411\u53f3\u4e3a\u6b63\u5411\u5de6\u4e3a\u8d1f\uff0cY\u8f74\u5411\u4e0b\u4e3a\u6b63\u5411\u4e0a\u4e3a\u8d1f\uff08\u8fd9\u70b9\u548c\u6570\u5b66\u4e0a\u76f8\u53cd\u7684\uff09\u4ee5\u5706\u70b9\u4e3a\u57fa\u70b9\u753b\u4e2a\u8ddd\u79bb\u5706\u70b9\u4e0a\u4e0b50\u5bbd\u9ad8100\u7684\u77e9\u5f62\u6765\u6f14\u793acanvas\u57fa\u672c\u7528\u6cd5 \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u8fd9\u91cc\u63d0\u4f9b\u4e86\u4e24\u4e2aAPI wx.createContext() \u521b\u5efa\u5e76\u8fd4\u56de\u7ed8\u56fe\u4e0a\u4e0b\u6587context\u5bf9\u8c61 getActions \u83b7\u53d6\u5f53\u524dcontext\u4e0a\u5b58\u50a8\u7684\u7ed8\u56fe\u52a8\u4f5c\uff0c\u5bf9\u5e94wx.drawCanvas(object)\u4e2d\u7684actionsclearActions \u6e05\u7a7a\u5f53\u524d\u7684\u5b58\u50a8\u7ed8\u56fe\u52a8\u4f5c wx.drawCanvas(object) \u7ed8\u5236 canvasId \u753b\u5e03\u6807\u8bc6\uff0c\u4f20\u5165\u7684cavas-id\uff0c\u8fd9\u91cc\u7684\u6807\u8bc6\u53ef\u4ee5\u4e3aNumber\uff0c\u4e5f\u53ef\u4ee5\u662factions \u7ed8\u56fe\u52a8\u4f5c\uff0c\u7531wx.createContext\u521b\u5efa\u7684context\uff0c\u8c03\u7528getActions\u65b9\u6cd5\u5bfc\u51fa\u7ed8\u56fe\u52a8\u4f5c\u6570\u7ec4\u3002 \u7ed8\u56fe\u4e2d\u53ef\u4ee5\u8fdb\u884c\u53d8\u5f62\uff0c\u7ed8\u5236\uff0c\u8def\u5f84\uff0c\u6837\u5f0f\uff0c\u8fd9\u4e9b\u4e2a\u4e1c\u897f\u6709\u70b9\u591a\u5b98\u7f51\u6709\u4f8b\u5b50\uff0c\u8fd9\u91cc\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u5f15\u5165 wxml &lt;!&#8211;\u753b\u5e03 canvas-id \u4e3a\u753b\u5e03\u6807\u8bc6\uff0c\u5f53\u7ed8\u5236\u65f6\u901a\u8fc7canvas-id\u627e\u5230\u753b\u5e03 &#8211;&gt; &lt;canvas&gt;&lt;\/canvas&gt; \u767b\u5f55\u540e\u590d\u5236 js Page({ &nbsp;data:{ &nbsp;&nbsp;text:&#8221;Page&nbsp;canvas&#8221; &nbsp;}, &nbsp;onLoad:function(options){ &nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u521d\u59cb\u5316&nbsp;options\u4e3a\u9875\u9762\u8df3\u8f6c\u6240\u5e26\u6765\u7684\u53c2\u6570 &nbsp;}, &nbsp;onReady:function(){ &nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u6e32\u67d3\u5b8c\u6210 &nbsp;&nbsp;\/\/\u7b2c\u4e00\u6b65\u521b\u5efa\u4e2a\u4e0a\u4e0b\u6587\u5bb9\u5668 &nbsp;&nbsp;var&nbsp;context&nbsp;=&nbsp;wx.createContext(); &nbsp;&nbsp;\/\/\u7b2c\u4e8c\u6b65\u7ed8\u5236\u8fd9\u91cc\u6211\u4eec\u7ed8\u5236\u4e2a\u77e9\u5f62&nbsp; &nbsp;&nbsp;\/\/x,&nbsp;y,&nbsp;widht,&nbsp;height &nbsp;&nbsp;context.rect(50,&nbsp;50,&nbsp;100,&nbsp;100); &nbsp;&nbsp;\/\/\u7ed8\u5236\u7684\u6837\u5f0f\u8fdb\u884c\u63cf\u8fb9\u7ed8\u5236\uff0cfill\u4e3a\u586b\u5145\u4f4d\u7f6e &nbsp;&nbsp;context.stroke(); &nbsp;&nbsp;\/** &nbsp;&nbsp;&nbsp;*&nbsp;\u8c03\u7528wx.drawCanvas\uff0c\u901a\u8fc7canvasId\u6307\u5b9a\u5728\u54ea\u5f20\u753b\u5e03\u4e0a\u7ed8\u5236\uff0c\u901a\u8fc7actions\u6307\u5b9a\u7ed8\u5236\u884c\u4e3a &nbsp;&nbsp;&nbsp;*&nbsp; &nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;\u6ce8\u610fconvasId\u53ef\u4ee5\u4e3a\u6570\u5b57\u8868\u793a\u4e5f\u53ef\u4ee5\u7528\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5c31\u662f\u4e00\u4e2a\u7ed8\u5236\u5bf9\u8c61\u7684\u6807\u8bc6\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a &nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;actions&nbsp;\u662f\u4ececontext\u4e0a\u4e0b\u6587\u4e2d\u83b7\u53d6\u7684\u7ed8\u5236\u884c\u4e3a\uff0c\u5373\u4e3a\u7b2c\u4e8c\u6b65\u64cd\u4f5c &nbsp;&nbsp;&nbsp;*\/ &nbsp;&nbsp;wx.drawCanvas({ &nbsp;&nbsp;&nbsp;\/\/\u753b\u5e03\u6807\u8bc6\uff0c\u4f20\u5165&lt;canvas&gt;&lt;\/canvas&gt;\u7684cavas-id &nbsp;&nbsp;&nbsp;canvasId:&nbsp;&#8216;identify&#8217;, &nbsp;&nbsp;&nbsp;\/\/\u83b7\u53d6\u7ed8\u5236\u884c\u4e3a\uff0c&nbsp;\u5c31\u76f8\u5f53\u4e8e\u4f60\u60f3\u505a\u5230\u83dccontext.getActions()\u5c31\u662f\u98df\u6750 &nbsp;&nbsp;&nbsp;actions:&nbsp;context.getActions(), &nbsp;&nbsp;}) &nbsp;}, &nbsp;onShow:function(){ &nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u663e\u793a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-29018","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=29018"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29018\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=29018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=29018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=29018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}