{"id":1713,"date":"2024-11-10T17:16:12","date_gmt":"2024-11-10T09:16:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1713\/"},"modified":"2024-11-10T17:16:12","modified_gmt":"2024-11-10T09:16:12","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-canvas-%e7%94%9f%e6%88%90%e9%ab%98%e6%b8%85%e8%a7%86%e9%a2%91%e5%92%8c-gif-%e5%9b%be%e5%83%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1713\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 Canvas \u751f\u6210\u9ad8\u6e05\u89c6\u9891\u548c GIF \u56fe\u50cf\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173054106492121.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528 Canvas \u751f\u6210\u9ad8\u6e05\u89c6\u9891\u548c GIF \u56fe\u50cf\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528 Canvas \u751f\u6210\u9ad8\u6e05\u89c6\u9891\u548c GIF \u56fe\u50cf\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5229\u7528 canvas \u751f\u6210\u9ad8\u6e05\u89c6\u9891\uff1f<\/strong><\/p>\n<p>\u5f53\u9700\u8981\u5c06\u57fa\u4e8e echarts \u7684\u56fe\u8868\u8f6c\u6362\u4e3a\u89c6\u9891\u65f6\uff0c\u4f7f\u7528\u5c4f\u5e55\u5f55\u5236\u5de5\u5177\u5f80\u5f80\u4f1a\u751f\u6210\u6e05\u6670\u5ea6\u8f83\u4f4e\u7684\u89c6\u9891\u3002\u8fd9\u662f\u56e0\u4e3aecharts \u6e32\u67d3\u7684\u662f canvas\uff0c\u5176\u9ed8\u8ba4\u6e05\u6670\u5ea6\u8f83\u4f4e\u3002<\/p>\n<p><strong>\u4f7f\u7528 recordrtc.js \u751f\u6210\u89c6\u9891<\/strong><\/p>\n<p>\u4e3a\u4e86\u63d0\u9ad8\u6e05\u6670\u5ea6\uff0c\u53ef\u4ee5\u501f\u52a9 recordrtc.js \u63d2\u4ef6\uff0c\u5b83\u80fd\u591f\u4f7f\u7528 canvas \u6765\u751f\u6210\u89c6\u9891\u3002\u5728 html \u4e2d\u5f15\u5165\u6240\u9700\u63d2\u4ef6\u540e\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u51fa\u89c6\u9891\uff1a<\/p>\n<ol>\n<li>\u521d\u59cb\u5316 recordrtc\uff0c\u6307\u5b9a canvas \u4f5c\u4e3a\u5f55\u5236\u5bf9\u8c61\uff1a<\/li>\n<\/ol>\n<pre>var recordrtc = recordrtc($canvas, {\n    type: 'canvas'\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5f00\u59cb\u5f55\u5236\uff1a<\/li>\n<\/ol>\n<pre>recordrtc.startrecording();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5f55\u5236\u7ed3\u675f\u540e\uff0c\u505c\u6b62\u5f55\u5236\u5e76\u83b7\u53d6\u89c6\u9891 blob\uff1a<\/li>\n<\/ol>\n<pre>recordrtc.stoprecording(function(videourl) {\n    var recordedblob = recordrtc.getblob();\n    saveas(recordedblob, \"test.mp4\");\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 gif.js \u751f\u6210 gif<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u5bfc\u51fa\u4e3a gif\uff0c\u53ef\u4ee5\u4f7f\u7528 gif.js \u63d2\u4ef6\u3002\u540c\u6837\u5730\uff0c\u5f15\u5165\u63d2\u4ef6\u540e\uff0c\u53ef\u4ee5\u6309\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u51fa gif\uff1a<\/p>\n<ol>\n<li>\u521d\u59cb\u5316 gif \u5b9e\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>var gif = new gif({\n    workers: 2,\n    quality: 10,\n    workerscript: '.\/js\/gif.worker.js'\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u9010\u5e27\u6dfb\u52a0 canvas \u5230 gif \u5b9e\u4f8b\u4e2d\uff1a<\/li>\n<\/ol>\n<pre>gif.addframe($canvas, {delay: 100});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5f53\u8fbe\u5230\u6307\u5b9a\u5e27\u6570\u6216\u5f55\u5236\u65f6\u95f4\u65f6\uff0c\u751f\u6210 gif\uff1a<\/li>\n<\/ol>\n<pre>gif.render();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5b8c\u6210\u540e\uff0c\u5bfc\u51fa gif blob\uff1a<\/li>\n<\/ol>\n<pre>gif.on('finished', function(blob) {\n    saveAs(blob, \"test.gif\");\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 canvas \u751f\u6210\u9ad8\u6e05\u7684\u89c6\u9891\u6216 gif \u56fe\u50cf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528 Canvas \u751f\u6210\u9ad8\u6e05\u89c6\u9891\u548c GIF \u56fe\u50cf\uff1f\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>\u5982\u4f55\u5229\u7528 canvas \u751f\u6210\u9ad8\u6e05\u89c6\u9891\uff1f \u5f53\u9700\u8981\u5c06\u57fa\u4e8e echarts \u7684\u56fe\u8868\u8f6c\u6362\u4e3a\u89c6\u9891\u65f6\uff0c\u4f7f\u7528\u5c4f\u5e55\u5f55\u5236\u5de5\u5177\u5f80\u5f80\u4f1a\u751f\u6210\u6e05\u6670\u5ea6\u8f83\u4f4e\u7684\u89c6\u9891\u3002\u8fd9\u662f\u56e0\u4e3aecharts \u6e32\u67d3\u7684\u662f canvas\uff0c\u5176\u9ed8\u8ba4\u6e05\u6670\u5ea6\u8f83\u4f4e\u3002 \u4f7f\u7528 recordrtc.js \u751f\u6210\u89c6\u9891 \u4e3a\u4e86\u63d0\u9ad8\u6e05\u6670\u5ea6\uff0c\u53ef\u4ee5\u501f\u52a9 recordrtc.js \u63d2\u4ef6\uff0c\u5b83\u80fd\u591f\u4f7f\u7528 canvas \u6765\u751f\u6210\u89c6\u9891\u3002\u5728 html \u4e2d\u5f15\u5165\u6240\u9700\u63d2\u4ef6\u540e\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u51fa\u89c6\u9891\uff1a \u521d\u59cb\u5316 recordrtc\uff0c\u6307\u5b9a canvas \u4f5c\u4e3a\u5f55\u5236\u5bf9\u8c61\uff1a var recordrtc = recordrtc($canvas, { type: &#8216;canvas&#8217; }); \u767b\u5f55\u540e\u590d\u5236 \u5f00\u59cb\u5f55\u5236\uff1a recordrtc.startrecording(); \u767b\u5f55\u540e\u590d\u5236 \u5f55\u5236\u7ed3\u675f\u540e\uff0c\u505c\u6b62\u5f55\u5236\u5e76\u83b7\u53d6\u89c6\u9891 blob\uff1a recordrtc.stoprecording(function(videourl) { var recordedblob = recordrtc.getblob(); saveas(recordedblob, &#8220;test.mp4&#8221;); }); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 gif.js \u751f\u6210 gif \u5982\u679c\u9700\u8981\u5bfc\u51fa\u4e3a gif\uff0c\u53ef\u4ee5\u4f7f\u7528 gif.js \u63d2\u4ef6\u3002\u540c\u6837\u5730\uff0c\u5f15\u5165\u63d2\u4ef6\u540e\uff0c\u53ef\u4ee5\u6309\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u51fa gif\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1713","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1713","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=1713"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1713\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}