{"id":7411,"date":"2018-03-18T00:08:40","date_gmt":"2018-03-17T16:08:40","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7411"},"modified":"2018-06-16T00:38:54","modified_gmt":"2018-06-15T16:38:54","slug":"canvas-image-element-mirror-animation","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/03\/canvas-image-element-mirror-animation\/","title":{"rendered":"\u5c0ftips: \u5728canvas\u4e0a\u5b9e\u73b0\u5143\u7d20\u56fe\u7247\u955c\u50cf\u7ffb\u8f6c\u52a8\u753b\u6548\u679c"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7411\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7411<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u5f97\u5230\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/p>\n<h3>\u4e00\u3001Canvas\u56fe\u7247\u6c34\u5e73\u955c\u50cf\u7ffb\u8f6c\u6548\u679c\u9884\u89c8<\/h3>\n<p>\u5982\u4e0bMP4\u89c6\u9891\uff1a<\/p>\n<p><video src=\"\/\/image.zhangxinxu.com\/video\/blog\/201803\/canvas-image-flipx.mp4\" preload=\"auto\" autoplay=\"autoplay\" autobuffer loop=\"loop\" width=\"288\" height=\"218\"><\/video><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u7684\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201803\/canvas-image-mirror.html\">canvas\u56fe\u7247\u6c34\u5e73\u955c\u50cf\u7ffb\u8f6c\u52a8\u753bdemo<\/a><\/p>\n<p>demo\u9875\u9762\u4e2d\u70b9\u51fb\u56fe\u7247\u52a8\u753b\u6548\u679c\u53ef\u89c1\u3002<\/p>\n<h3>\u4e8c\u3001Canvas\u4e0a\u5b9e\u73b0\u56fe\u7247\u955c\u50cf\u7ffb\u8f6c\u7684\u5b9e\u73b0<\/h3>\n<p>CSS\u4e2d\u8981\u60f3\u5b9e\u73b0\u5143\u7d20\u7684\u7ffb\u8f6c\u6548\u679c\uff0c\u6bd4\u8f83\u7b80\u5355\uff0c\u4f8b\u5982\u6211\u4eec\u5e0c\u671b\u67d0\u4e00\u5f20\u56fe\u7247\u6c34\u5e73\u955c\u50cf\u7ffb\u8f6c\uff0c\u53ea\u9700\u8981\u4e00\u884cCSS\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<pre>img {\n    transform: scaleX(-1);\n}<\/pre>\n<p>\u6216\u8005\uff1a<\/p>\n<pre>img {\n    transform: scale(-1, 1);\n}<\/pre>\n<p>\u4f46\u662f\u5728canvas\u4e2d\uff0c\u5219\u8981\u9ebb\u70e6\u4e00\u4e9b\uff0c\u9ebb\u70e6\u7684\u5e76\u4e0d\u662f\u65e0\u6cd5\u7ffb\u8f6c\uff0c\u800c\u662f\u5750\u6807\u7cfb\u7684\u5b9a\u4f4d\u3002<\/p>\n<p>\u5728Canvas\u4e2d\uff0c\u5982\u4e0b\u4ee3\u7801\u53ef\u4ee5\u5b9e\u73b0\u8d44\u6e90\u7684\u6c34\u5e73\u955c\u50cf\u7ffb\u8f6c\uff08\u5047\u8bbe<code>context<\/code>\u662fCanvas\u7684<code>2d<\/code>\u4e0a\u4e0b\u6587\uff09\uff1a<\/p>\n<pre>context.scale(-1, 1);<\/pre>\n<p>\u6216\u8005\u4f7f\u7528<code>setTransform<\/code> API\u76f4\u63a5\u77e9\u9635\u53d8\u6362\uff1a<\/p>\n<pre>context.setTransform(-1, 0, 0, 1, 0, 0);<\/pre>\n<p>\u7136\u800c\uff0c\u7ffb\u8f6c\u867d\u7136\u5b9e\u73b0\u4e86\uff0c\u4f46\u662fCanvas\u4e2d\u5143\u7d20\u5b9a\u4f4d\u5c31\u51fa\u4e86\u5f88\u5927\u7684\u95ee\u9898\u3002\u8fd9\u662f\u56e0\u4e3aCanvas\u7684\u5750\u6807\u53d8\u6362\u7cfb\u548cCSS\u4e0d\u4e00\u6837\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5b9e\u73b0\u5c45\u4e2d\u7ffb\u8f6c\u6548\u679c\uff0c\u9700\u8981\u5728\u7ffb\u8f6c\u4e4b\u524d\u5c06\u76ee\u6807\u5143\u7d20\u7684\u4e2d\u5fc3\u70b9\u79fb\u52a8\u5230\u53d8\u6362\u8f74\u4e0a\u3002<\/p>\n<p>\u62ff\u6c34\u5e73\u7ffb\u8f6c\u8ddd\u79bb\uff0c\u5728<code>scale<\/code>\u4e4b\u524d\u5148<code>translate<\/code>\u4f4d\u79fb\u53d8\u6362\u540e\u7684\u6c34\u5e73\u504f\u79fb\uff0c\u7136\u540e\u5c31\u80fd\u770b\u5230\u4e00\u76f4\u5c45\u4e2d\u7ffb\u8f6c\u7684\u6548\u679c\u4e86\u3002<\/p>\n<p>\u8bed\u8a00\u82cd\u767d\uff0c\u62ff\u56fe\u793a\u610f\u4e00\u4e0b\u3002<\/p>\n<p>canvas\u9ed8\u8ba4\u7684\u53d8\u5316\u5750\u6807\u7cfb\u662f\u5de6\u4e0a\u89d2\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u6c34\u5e73<code>scale<\/code>\u4e3a<code>1<\/code>, <code>0.5<\/code>, <code>0<\/code>, <code>-0.5<\/code>, <code>-1<\/code>\u65f6\u5019\u7684\u6700\u7ec8\u4f4d\u7f6e\u5982\u4e0b\u56fe\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201803\/scale-canavs.jpg\" width=\"385\" height=\"713\" alt=\"\u7f29\u653e\u548c\u5c45\u4e2d\u9700\u8981\u7684\u504f\u79fb\" class=\"alignnone\" \/><\/p>\n<p>\u4e8e\u662f\u53ef\u4ee5\u5f97\u5230\u5e94\u5f53\u504f\u79fb\u7684\u6c34\u5e73\u8ddd\u79bb\u516c\u5f0f\uff1a<\/p>\n<p>distance = (canvas.width &#8211; image.width * scale) \/ 2;<\/p>\n<p>\u4e8e\u662f\uff0c\u6700\u7ec8\u955c\u50cf\u7ed8\u5236\u56fe\u7247\u7684\u5173\u952e\u4ee3\u7801\u53d8\u6210\u8fd9\u6837\uff08\u5047\u8bbe\u6c34\u5e73\u7f29\u653e\u5927\u5c0f\u662f<code>scale<\/code>\uff09\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u5750\u6807\u53c2\u8003\u8c03\u6574<\/span>\ncontext.translate((canvas.width - image.width * scale) \/ 2, 0);\ncontext.scale(scale, 1);\ncontext.drawImage(image, 0, 0);\n<span style=\"color:green;\">\/\/ \u5750\u6807\u53c2\u8003\u8fd8\u539f<\/span>\ncontext.setTransform(1, 0, 0, 1, 0, 0);\n<\/pre>\n<h4>\u5982\u4f55\u589e\u52a0\u52a8\u753b\u6548\u679c\u5462\uff1f<\/h4>\n<p>\u6211\u4eec\u53ef\u4ee5\u501f\u52a9<a href=\"https:\/\/github.com\/zhangxinxu\/tween\" rel=\"noopener\" target=\"_blank\">Tween.js<\/a>\uff0c<a href=\"https:\/\/github.com\/zhangxinxu\/tween\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/zhangxinxu\/tween<\/a><\/p>\n<p>\u91cc\u9762\u6709\u5404\u79cd\u7f13\u52a8\u7b97\u6cd5\uff0c\u501f\u52a9\u65b9\u4fbf\u8c03\u7528\u7684<code>Math.animation()<\/code>\u65b9\u6cd5\uff0c\u5c31\u80fd\u8f7b\u677e\u5b9e\u73b0\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u5566\uff01<\/p>\n<pre>Math.animation(form, to, duration, easing, callback);<\/pre>\n<p>\u52a8\u753bJS\u5982\u4e0b\uff1a<\/p>\n<pre>var canvas = document.querySelector('canvas');\nvar context = canvas.getContext('2d');\n<span style=\"color:green;\">\/\/ \u52a8\u753b\u8fdb\u884c<\/span>\nMath.animation(1, -1, 600, 'Quad.easeInOut', function (value, isEnding) {\n    <span style=\"color:green;\">\/\/ \u6e05\u9664\u753b\u5e03\u5185\u5bb9<\/span>\n    context.clearRect(0, 0, canvas.width, canvas.height);\n    <span style=\"color:green;\">\/\/ \u8c03\u6574\u5750\u6807<\/span>\n    context.translate((canvas.width - canvas.width * value) \/ 2, 0);\n    <span style=\"color:green;\">\/\/ \u8c03\u6574\u7f29\u653e<\/span>\n    context.scale(value, 1);\n    <span style=\"color:green;\">\/\/ \u7ed8\u5236\u6b64\u65f6\u56fe\u7247<\/span>\n    context.drawImage(eleImg, 0, 0);\n    <span style=\"color:green;\">\/\/ \u5750\u6807\u53c2\u8003\u8fd8\u539f<\/span>\n    context.setTransform(1, 0, 0, 1, 0, 0);\n});<\/pre>\n<h3>\u4e09\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>\u53c8\u662f\u4e00\u7bc7\u51b7\u6587\uff0ccanvas\u8fd9\u4e1c\u897f\uff0c\u73a9\u7684\u524d\u7aef\u5e76\u4e0d\u591a\uff0c\u53d7\u4f17\u6709\u9650\uff0c\u4e0d\u6bd4\u6d41\u884c\u6280\u672f\u3002\u7136\uff0c\u53e4\u8bed\u6709\u4e91\uff0c\u4e0d\u4ee5\u5584\u5c0f\u800c\u4e0d\u4e3a\uff0c\u5e0c\u671b\u4ee5\u540e\u6709\u5c0f\u4f19\u4f34\u641c\u7d22\u5230\u76f8\u5173\u95ee\u9898\u7684\u65f6\u5019\u53ef\u4ee5\u63d0\u4f9b\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\uff0c\u611f\u8c22\u9605\u8bfb\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/14.png\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7411\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7411<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSS\u4e2d\u8981\u60f3\u5b9e\u73b0\u5143\u7d20\u7684\u7ffb\u8f6c\u6548\u679c\uff0c\u6bd4\u8f83\u7b80\u5355\uff0c\u4f8b\u5982\u6211\u4eec\u5e0c\u671b\u67d0\u4e00\u5f20\u56fe\u7247\u6c34\u5e73\u955c\u50cf\u7ffb\u8f6c\uff0c\u53ea\u9700\u8981\u4e00\u884cCSS\u5c31\u53ef\u4ee5\u4e86\u3002\u4f46\u662f\u5728canvas\u4e2d\uff0c\u5219\u8981\u9ebb\u70e6\u4e00\u4e9b\uff0c\u9ebb\u70e6\u7684\u5e76\u4e0d\u662f\u65e0\u6cd5\u7ffb\u8f6c\uff0c\u800c\u662f\u5750\u6807\u7cfb\u7684\u5b9a\u4f4d\u2026\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[896],"tags":[53,1064,1019,1221,287,599,724],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7411"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=7411"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7411\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=7411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=7411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=7411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}