{"id":7809,"date":"2018-07-22T21:14:57","date_gmt":"2018-07-22T13:14:57","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=7809"},"modified":"2018-08-02T01:17:27","modified_gmt":"2018-08-01T17:17:27","slug":"canvas-color-transition","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/07\/canvas-color-transition\/","title":{"rendered":"Canvas\u4e2d\u989c\u8272\u8fc7\u6e21\u52a8\u753b\u6548\u679c\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=7809\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=7809<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/p>\n<h3>\u4e00\u3001\u5173\u4e8e\u989c\u8272\u52a8\u753b\u7684\u524d\u8a00<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/color-canvas-cover-deal.jpg\" width=\"242\" height=\"150\" alt=\"canvas\u989c\u8272\u8fc7\u6e21\u5904\u7406\u7f29\u7565\u56fe\" class=\"alignright thumb size-medium\" \/><\/p>\n<p>\u5728CSS3\u4e2d\uff0c\u6211\u4eec\u8981\u60f3\u5b9e\u73b0\u4eceA\u989c\u8272\u5230B\u989c\u8272\u7684\u8fc7\u6e21\u6548\u679c\uff0c\u662f\u76f8\u5f53\u5bb9\u6613\u7684\uff0c\u53ea\u8981\u6307\u5b9a\u8d77\u6b62\u989c\u8272\uff0c\u914d\u5408<code>transition<\/code>\u8fc7\u6e21\u6216\u8005<code>animation<\/code>\u52a8\u753b\u90fd\u53ef\u4ee5\u65f6\u95f4\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5728<code>&lt;canvas&gt;<\/code>\u4e2d\u5374\u6ca1\u6709\u8fd9\u4e48\u7b80\u5355\uff0c\u56e0\u4e3a<code>&lt;canvas&gt;<\/code>\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u9759\u6001\u753b\u5e03\uff0c\u8981\u60f3\u5b9e\u73b0\u989c\u8272\u53d8\u5316\uff0c\u9700\u8981JS\u53bb\u4e0d\u65ad\u7ed8\u5236\uff0c\u5b9e\u73b0\u8d77\u6765\u8981\u6bd4CSS\u5b9e\u73b0\u9ebb\u70e6\u5f88\u591a\u3002\u518d\u52a0\u4e0a\u989c\u8272\u503c\u672c\u8eab\u5c31\u4e0d\u4e00\u5b9a\u662f\u7eaf\u7cb9\u7684\u6570\u503c\uff0c\u66f4\u589e\u52a0\u4e86\u6211\u4eec\u5b9e\u73b0\u7684\u96be\u5ea6\u3002<\/p>\n<p>\u672c\u6587\u5c31\u5c06\u901a\u8fc7\u591a\u4e2a\u6848\u4f8b\uff0c\u9010\u6b65\u6df1\u5165\uff0c\u4ecb\u7ecd\u4e00\u4e9b\u5728Canvas\u4e2d\u7684\u989c\u8272\u5904\u7406\u6280\u5de7\uff0c\u6709\u4e9b\u6280\u5de7\u8bf4\u4e0d\u5b9a\u4f1a\u8ba9\u4f60\u5927\u5f00\u773c\u754c\u3002<\/p>\n<h3>\u4e8c\u3001Canvas\u989c\u8272\u52a8\u753b\u786c\u5b9e\u73b0<\/h3>\n<p>\u6240\u8c13\u201c\u786c\u5b9e\u73b0\u201d\uff0c\u5c31\u662f\u4e0d\u901a\u8fc7\u201c\u5947\u5de7\u6deb\u6280\u201d\uff0c\u6309\u7167\u6b63\u5e38\u903b\u8f91\uff0c\u4e00\u6761\u8def\u8d70\u5230\u5e95\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u8981\u5728<code>&lt;canvas&gt;<\/code>\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u84dd\u8272\u5230\u7ea2\u8272\u7684\u52a8\u753b\u6548\u679c\uff0c\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u83b7\u53d6\u84dd\u8272\u548c\u7ea2\u8272\u7684RGB\u8272\u503c\uff1b<\/li>\n<li>\u4f7f\u7528\u5b9a\u65f6\u5668\uff0c\u914d\u5408\u8fd0\u52a8\u7b97\u6cd5\uff0cR\uff0cG\uff0cB\u6570\u503c\u540c\u65f6\u53d8\u5316\u4ea7\u751f\u53d8\u8272\u3002<\/li>\n<\/ol>\n<p>\u5177\u4f53\u5982\u4f55\u5b9e\u73b0\uff0c\u53ef\u4ee5\u53c2\u89c1\u8fd9\u4e2a\u6848\u4f8b\u3002\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201807\/color-gradient-js-by-number-tween.php\" rel=\"noopener\" target=\"_blank\">\u989c\u8272\u6570\u503c\u53d8\u5316\u4e0b\u7684\u989c\u8272\u52a8\u753bdemo<\/a><\/p>\n<p>\u5b9e\u73b0\u7684\u6548\u679c\u5982\u4e0bGIF\u622a\u56fe\uff08\u4e0a\u9762\u662fCanvas\u5b9e\u73b0\uff0c\u4e0b\u9762\u662fCSS3\u5b9e\u73b0\uff0c\u65b9\u4fbf\u6548\u679c\u5bf9\u6bd4\uff09\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/canvas-rgb-ani-s.gif\" width=\"283\" height=\"443\" alt=\"\u786c\u5b9e\u73b0\u7684canvas\u989c\u8272\u53d8\u5316\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5b8c\u6574JS\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>var canvas = document.getElementById('canvas');\nvar context = canvas.getContext('2d');\nvar width = canvas.width, height = canvas.height;\n<span style=\"color:green;\">\/\/ \u52a8\u753b\u6267\u884c\u7684\u5e27\u6570<\/span>\nvar start = 0, frames = 200;\n<span style=\"color:green;\">\/\/ \u8fc7\u6e21\u989c\u8272 \u84dd\u8272 \u5230 \u7ea2\u8272<\/span>\nvar from = [0, 0, 255];\nvar to = [255, 0, 0];\n<span style=\"color:green;\">\/\/ \u52a8\u753b\u7b97\u6cd5\uff0c\u8fd9\u91cc\u4f7f\u7528Cubic.easeOut\u7b97\u6cd5<\/span>\nvar cubicEaseOut = function(t, b, c, d) {\n    return c * ((t = t\/d - 1) * t * t + 1) + b;\n};\n<span style=\"color:green;\">\/\/ \u7ed8\u5236\u65b9\u6cd5<\/span>\nvar draw = function () {\n    context.clearRect(0, 0, width, height);\n    <span style=\"color:green;\">\/\/ \u8ba1\u7b97\u6b64\u65f6r, g, b\u6570\u503c<\/span>\n    var r = cubicEaseOut(start, from[0], to[0] - from[0], frames);\n    var g = cubicEaseOut(start, from[1], to[1] - from[1], frames);\n    var b = cubicEaseOut(start, from[2], to[2] - from[2], frames);\n    <span style=\"color:green;\">\/\/ \u53ef\u4ee5\u786e\u5b9a\u8272\u503c<\/span>\n    context.fillStyle = 'rgb('+ [r, g, b].join() +')';\n    context.arc(width \/ 2, height \/ 2, height \/ 2, 0, 2 * Math.PI);\n    context.fill();\n    <span style=\"color:green;\">\/\/ \u6301\u7eed\u53d8\u5316<\/span>\n    start++;\n    if (start &lt;= frames) {\n        requestAnimationFrame(draw);    \n    }\n};\ndraw();<\/pre>\n<h4>\u201c\u786c\u5b9e\u73b0\u201d\u7684\u5c40\u9650\u6240\u5728<\/h4>\n<p>\u201c\u786c\u5b9e\u73b0\u201d\u867d\u7136\u57fa\u672c\u6548\u679c\u6709\u4e86\uff0c\u4f46\u662f\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\uff0c\u4e3b\u8981\u5c31\u662f\u6211\u4eec\u524d\u540e\u53d8\u5316\u7684\u8272\u503c\u4e0d\u4e00\u5b9a\u6b63\u597d\u5c31\u662fRGB\u989c\u8272\uff0c\u53ef\u80fd\u662f<code>#RRGGBB<\/code>\u8fd9\u79cd\u5341\u516d\u8fdb\u5236\u8272\u503c\uff0c\u6216\u8005<code>hsl<\/code>\u989c\u8272\uff0c\u8fd9\u4e2a\u8fd8\u597d\uff0c\u6211\u4eec\u53ef\u4ee5\u8272\u503c\u8f6c\u6362\u4e0b\uff08\u8f6c\u6362\u65b9\u6cd5\u53c2\u89c1\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2010\/03\/javascript-hex-rgb-hsl-color-convert\/\">HEX\u5341\u516d\u8fdb\u5236\u4e0eRGB, HSL\u989c\u8272\u7684\u76f8\u4e92\u8f6c\u6362<\/a>\u201d\u8fd9\u7bc7\u6587\u7ae0\uff09\uff1b\u8fd8\u6709\u53ef\u80fd\u989c\u8272\u662f\u534a\u900f\u660e\u7684RGBA\u6216\u8005HSLA\u989c\u8272\uff0c\u8fd9\u4e48\u4e5f\u8fd8\u884c\uff0c\u54ac\u54ac\u7259\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4e0d\u5c31\u662f\u591a\u4e00\u4e2a\u900f\u660e\u5ea6\u53d8\u5316\u7684\u6570\u503c\u53c2\u6570\u561b\uff1b\u7136\u800c\uff0c\u6700\u6015\u7684\u5c31\u662f\u8272\u503c\u662f<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1064\">147\u4e2a\u989c\u8272\u5173\u952e\u5b57\u4e4b\u4e00<\/a>\uff0c\u4f8b\u5982\u8bf7\u5b9e\u73b0<code>blue<\/code>\u5230<code>red<\/code>\u7684\u6548\u679c\u3002\u662f\u4e0d\u662f\u53ea\u80fd\u671b\u7801\u5174\u53f9\u4e86\u3002<\/p>\n<p>\u8272\u503c\u8f6c\u6362\u9ebb\u70e6\uff0c\u5173\u952e\u5b57\u53d8\u5316\u65e0\u80fd\u4e3a\u529b\uff0c\u6709\u6ca1\u6709\u4ec0\u4e48\u6280\u5de7\u53ef\u4ee5\u4e00\u6b21\u6027\u641e\u5b9a\u5462\uff1f\u6709\uff01<\/p>\n<h3>\u4e09\u3001\u4efb\u610f\u989c\u8272\u8f6cRGB\u8272\u503c\u6280\u5de7<\/h3>\n<p>\u6240\u6709web\u6d4f\u89c8\u5668\u6709\u8fd9\u4e48\u4e00\u4e2a\u7279\u6027\uff0c\u5f53\u6211\u4eec\u4f7f\u7528<code>getComputedStyle<\/code>\u539f\u751f\u63a5\u53e3\u53bb\u83b7\u53d6DOM\u5143\u7d20\u7684\u8272\u503c\u7684\u65f6\u5019\uff0c\u8fd4\u56de\u7684\u5168\u90e8\u90fd\u662fRGB\u8272\u503c\u6216\u8005RGBA\u8272\u503c\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u4e0bJavaScript\u4ee3\u7801\uff1a<\/p>\n<pre>var div = document.createElement('div');\ndiv.style.color = 'red';\ndocument.body.appendChild(div);\nconsole.log(window.getComputedStyle(div).color);<\/pre>\n<p>\u7ed3\u679c\u662f\uff1a<code>rgb(255, 0, 0)<\/code><\/p>\n<p>\u5e76\u4e0d\u662f<code>'red'<\/code>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/2018-07-22_185635.png\" width=\"372\" height=\"105\" alt=\"\u8272\u503c\u8f6cRGB\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4e8e\u662f\uff0c\u501f\u52a9\u6d4f\u89c8\u5668\u7279\u6027\uff0c\u65e0\u8bba\u4ec0\u4e48\u989c\u8272\u503c\uff0c\u60f3\u8981\u5728canvas\u4e2d\u6709\u52a8\u753b\u6548\u679c\uff0c\u90fd\u4e0d\u6210\u95ee\u9898\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u5c01\u88c5\u4e00\u4e2a\u4efb\u610f\u8272\u503c\u8f6cRGBA\u989c\u8272\u7684\u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><span style=\"color:green;\">\/**\n* \u4efb\u610f\u989c\u8272\u8f6cRGBA\u65b9\u6cd5\n*\/<\/span>\nvar toRGBA = function (color) {\n    <span style=\"color:green;\">\/\/ \u521b\u5efadiv\u5143\u7d20\u5e76\u8bbe\u7f6e\u989c\u8272<\/span>\n    var div = document.createElement('div');\n    div.style.color = color;\n    document.body.appendChild(div);\n    <span style=\"color:green;\">\/\/ \u8fd4\u56de\u8ba1\u7b97\u540e\u7684\u989c\u8272\u503c<\/span>\n    var cssColor = window.getComputedStyle(div).color;\n    <span style=\"color:green;\">\/\/ div\u5143\u7d20\u79fb\u9664<\/span>\n    document.body.removeChild(div);\n    <span style=\"color:green;\">\/\/ \u5982\u679c\u662fRGB\u989c\u8272\uff0c\u5219\u8f6c\u6362\u6210RGBA\u8868\u793a<\/span>\n    var arrRGBA = cssColor.match(\/\\d+\/g);\n    if (arrRGBA.length == 3) {\n        arrRGBA.push(1);\n    }\n    <span style=\"color:green;\">\/\/ \u6bcf\u4e00\u9879\u8f6c\u6362\u6210\u6570\u503c\u7c7b\u578b<\/span>\n    return arrRGBA.map(function (value) {\n        return value * 1;\n    });\n};<\/pre>\n<p>\u4e8e\u662f\u4e4e\uff0c\u5173\u952e\u5b57\u8272\u503c\u7684\u8f6c\u6362\u6548\u679c\u5b9e\u73b0\u4e5f\u4e0d\u5728\u8bdd\u4e0b\u4e86\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201807\/canvas-any-color-transition.php\" rel=\"noopener\" target=\"_blank\">canvas\u4efb\u610f\u8272\u503c\u4e0b\u7684\u8fc7\u6e21\u52a8\u753bdemo<\/a><\/p>\n<p>\u5b9e\u73b0\u7684\u662f\u6df1\u5929\u7a7a\u84dd<code>'deepskyblue'<\/code>\u5230\u4f69\u5947\u7c89<code>'deeppink'<\/code>\u989c\u8272\u52a8\u753b\u6548\u679c\uff0c\u5982\u4e0bGIF\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/canvas-keycolor-s.gif\" width=\"283\" height=\"443\" alt=\"canvas\u5173\u952e\u5b57\u989c\u8272\u8fc7\u6e21\u6548\u679c\" class=\"alignnone size-medium\" \/><\/p>\n<p>JavaScript\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>var canvas = document.getElementById('canvas');\nvar context = canvas.getContext('2d');\nvar width = canvas.width, height = canvas.height;\n<span style=\"color:green;\">\/\/ \u52a8\u753b\u6267\u884c\u7684\u5e27\u6570<\/span>\nvar start = 0, frames = 200;\n<span style=\"color:green;\">\/\/ \u8fc7\u6e21\u989c\u8272 \u5929\u7a7a\u84dd \u5230 \u4f69\u5947\u7c89<\/span>\n<span style=\"color:#cd0000;\">var from = toRGBA('deepskyblue');\nvar to = toRGBA('deeppink');<\/span>\n<span style=\"color:green;\">\/\/ \u52a8\u753b\u7b97\u6cd5\uff0c\u8fd9\u91cc\u4f7f\u7528Cubic.easeOut\u7b97\u6cd5<\/span>\nvar cubicEaseOut = function(t, b, c, d) {\n    return c * ((t = t\/d - 1) * t * t + 1) + b;\n};\n<span style=\"color:green;\">\/\/ \u7ed8\u5236\u65b9\u6cd5<\/span>\nvar draw = function () {\n    context.clearRect(0, 0, width, height);\n    <span style=\"color:green;\">\/\/ \u8ba1\u7b97\u6b64\u65f6r, g, b, a\u6570\u503c<\/span>\n    var r = cubicEaseOut(start, from[0], to[0] - from[0], frames);\n    var g = cubicEaseOut(start, from[1], to[1] - from[1], frames);\n    var b = cubicEaseOut(start, from[2], to[2] - from[2], frames);\n    var a = cubicEaseOut(start, from[3], to[3] - from[3], frames);\n    <span style=\"color:green;\">\/\/ \u53ef\u4ee5\u786e\u5b9a\u8272\u503c<\/span>\n    context.fillStyle = 'rgba('+ [r, g, b, a].join() +')';\n    context.arc(width \/ 2, height \/ 2, height \/ 2, 0, 2 * Math.PI);\n    context.fill();\n    <span style=\"color:green;\">\/\/ \u6301\u7eed\u53d8\u5316<\/span>\n    start++;\n    if (start &lt;= frames) {\n        requestAnimationFrame(draw);    \n    }\n};\ndraw();<\/pre>\n<h4>\u4f9d\u7136\u5b58\u5728\u7684\u5c40\u9650<\/h4>\n<p>\u76ee\u524dCanvas\u4e2d\u4eceA\u8272\u5230B\u8272\u7684\u8fc7\u6e21\u52a8\u753b\u6548\u679c\u5df2\u7ecf\u53ef\u4ee5\u5b9e\u73b0\u4e86\uff0c\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u7684\u989c\u8272\u53d8\u5316\u662f\u4e30\u5bcc\u7684\u6709\u5c42\u6b21\u7684\uff0c\u5219\u6211\u4eec\u7684\u5b9e\u73b0\u53c8\u53d8\u5f97\u9ebb\u70e6\u4e86\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u5b9e\u73b0\u7ea2\u6a59\u9ec4\u7eff\u9752\u84dd\u7d2b7\u8272\u6e10\u53d8\uff0c\u4e14\u6bcf\u79cd\u8fc7\u6e21\u6240\u5360\u65f6\u95f4\u90fd\u4e0d\u4e00\u6837\uff0c\u8be5\u600e\u4e48\u529e\uff1f<\/p>\n<p>\u5982\u679c\u662fCSS3\u5b9e\u73b0\uff0c\u5219\u5f88\u7b80\u5355\uff0c\u5b9a\u4e49\u4e00\u4e2a<code>animation<\/code>\u52a8\u753b\u5c31\u53ef\u4ee5\uff0c\u7c7b\u4f3c\u8fd9\u6837\uff1a<\/p>\n<pre>@keyframes color {\n    0% { background-color: red; }\n    7% { background-color: orange; }\n   17% { background-color: yellow; }\n   22% { background-color: green; }\n   42% { background-color: cyan; }\n   82% { background-color: blue; }\n   90% { background-color: purple; }\t\n}<\/pre>\n<p>\u5982\u679c\u8981\u5728Canvas\u4e2d\u5b9e\u73b0\uff0c\u6211\u53bb\uff0c\u4e0d\u6562\u60f3\u8c61\uff0c\u4ee3\u7801\u4f30\u8ba1\u4f1a\u5f88\u5570\u55e6\u3002\u6709\u6ca1\u6709\u4ec0\u4e48\u7b80\u5355\u53d6\u5de7\u7684\u65b9\u6cd5\u5b9e\u73b0\u590d\u6742\u989c\u8272\u52a8\u753b\u6548\u679c\u5462\uff1f\u6709\uff01<\/p>\n<h3>\u56db\u3001\u501f\u52a9CSS3\u5b9e\u73b0\u590d\u6742canvas\u52a8\u753b<\/h3>\n<p>\u65e0\u8bba\u662fCSS3 <code>transition<\/code>\u8fc7\u6e21\u6216\u8005<code>animation<\/code>\u52a8\u753b\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u4f7f\u7528JS\u5b9e\u65f6\u8fd4\u56de\u5f53\u524d\u52a8\u753b\u4e2d\u7684\u989c\u8272\u503c\u7b49CSS\u5c5e\u6027\u503c\uff0c\u4e8e\u662f\uff0c\u6211\u4eec\u6839\u672c\u5c31\u4e0d\u9700\u8981\u5728Canvas\u4e2d\u201c\u786c\u5b9e\u73b0\u201d\u52a8\u753b\u6548\u679c\uff0c\u76f4\u63a5\u628aDOM\u4e2d\u7684\u53d8\u5316\u91cf\u5b9e\u65f6\u8d4b\u503c\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201807\/canvas-animation-by-css3.php\" rel=\"noopener\" target=\"_blank\">\u501f\u52a9CSS3 animation\u5b9e\u73b0canvas\u989c\u8272\u52a8\u753bdemo<\/a><\/p>\n<p>\u6548\u679c\u5982\u4e0bGIF\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/canvas-colorful-s.gif\" width=\"283\" height=\"443\" alt=\"canvas\u4e03\u5f69\u989c\u8272\u52a8\u753b\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6211\u4eec\u7684DOM\u5143\u7d20\uff08\u4e0bJS\u4ee3\u7801\u4e2d\u7684<code>eleShape<\/code>\uff09\u5e94\u7528CSS3\u52a8\u753b\uff1a<\/p>\n<pre>.shape.active {\n    animation: color 3.33s both;\t\n}<\/pre>\n<p>JS\u4ee3\u7801\u5c31\u7b80\u5355\u591a\u4e86\uff0c\u5b9e\u65f6\u8272\u503c\u4e00\u81f4\u5373\u53ef\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ CSS3\u52a8\u753b\u5143\u7d20<\/span>\nvar eleShape = document.getElementById('shape');\n<span style=\"color:green;\">\/\/ Canvas\u5143\u7d20<\/span>\nvar canvas = document.getElementById('canvas');\nvar context = canvas.getContext('2d');\nvar width = canvas.width, height = canvas.height;\n<span style=\"color:green;\">\/\/ \u7ed8\u5236\u65b9\u6cd5<\/span>\nvar draw = function () {\n    context.clearRect(0, 0, width, height);\n    <span style=\"color:green;\">\/\/ \u53ef\u4ee5\u786e\u5b9a\u8272\u503c<\/span>\n    <span style=\"color:#cd0000;\">context.fillStyle = window.getComputedStyle(eleShape).backgroundColor;<\/span>\n    context.arc(width \/ 2, height \/ 2, height \/ 2, 0, 2 * Math.PI);\n    context.fill();\n    <span style=\"color:green;\">\/\/ \u6301\u7eed\u53d8\u5316<\/span>\n    requestAnimationFrame(draw);\n};\ndraw();<\/pre>\n<p>\u6700\u5173\u952e\u7684\u5176\u5b9e\u5c31\u662f\u4e0a\u9762\u7ea2\u8272\u9ad8\u4eae\u7684\u90a3\u4e00\u884cJS\u4ee3\u7801\uff0c\u4e5f\u5c31\u662fCanvas\u4e2d\u7684\u5706\u5708\u7684\u586b\u5145\u8272\u7b49\u4e8e\u6b64\u523bDOM\u5143\u7d20\u80cc\u666f\u8272\u5373\u53ef\uff01<\/p>\n<p>\u540c\u7406\uff0c\u672c\u6587\u5c55\u793a\u7684\u5934\u4e24\u4e2a\u6848\u4f8b\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u5b9e\u73b0\u6211\u4eec\u60f3\u8981\u7684Canvas\u52a8\u753b\u6548\u679c\uff0c\u7701\u53bb\u4e86\u8ba1\u7b97\u3001\u8fd8\u6709\u8f6c\u6362\u8fd9\u4e9b\u7d2f\u5fc3\u70e6\u4eba\u7684\u64cd\u4f5c\uff0c\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u4eba\u4eba\u90fd\u53ef\u4ee5\u4e0a\u624b\uff0c\u53ea\u8981\u4f60\u4f1a\u4e00\u70b9CSS\u52a8\u753b\u5373\u53ef\u3002<\/p>\n<h3>\u4e94\u3001\u8fdb\u4e00\u6b65\u6269\u5c55<\/h3>\n<p>\u5176\u5b9e\u4e0d\u4ec5\u4ec5\u662f\u989c\u8272\u53d8\u5316\uff0c\u5176\u4ed6\u53d8\u5316\uff0c\u4f8b\u5982\u4f4d\u79fb\uff0c\u7f29\u653e\u7b49\u90fd\u53ef\u4ee5\u901a\u8fc7DOM+CSS3\u52a8\u753b\u201c\u79fb\u82b1\u63a5\u6728\u201d\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u65b0\u5efa\u4e00\u4e2a\u548c<code>&lt;canvas&gt;<\/code>\u5c3a\u5bf8\u4e00\u6837\u5927\u7684\u5bb9\u5668\uff0c\u91cc\u9762DOM\u8fdb\u884cCSS3\u8fc7\u6e21\u6216\u52a8\u753b\uff0cCanvas\u4e2d\u7684\u56fe\u5f62\u4e0e\u4e4b\u5b9e\u65f6\u5339\u914d\uff0c\u4f8b\u5982<code>x<\/code>, <code>y<\/code>\u5339\u914dDOM\u5143\u7d20\u7684<code>left<\/code>, <code>top<\/code>\u5b9a\u4f4d\u503c\uff1b\u81f3\u4e8e\u7f29\u653e\u6548\u679c\uff0cCanvas\u4e2d\u7684\u56fe\u5f62\u7684\u5bbd\u9ad8\u5c3a\u5bf8\u5b9e\u65f6\u7b49\u540c\u4e8eDOM\u5143\u7d20\u7684\u53ef\u89c6\u5c3a\u5bf8\u5373\u53ef\u3002<\/p>\n<p>\u53ea\u4e0d\u8fc7\u4ece\u6027\u4ef7\u6bd4\u4e0a\u800c\u8a00\uff0c\u989c\u8272\u662f\u662f\u6700\u9ad8\uff0c\u56e0\u4e3a\u989c\u8272\u503c\u6709N\u591a\u79cd\u8868\u793a\u65b9\u6cd5\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u5355\u7eaf\u7684\u6570\u503c\uff0c\u501f\u52a9DOM\u7279\u6027\u5b9e\u73b0\u76f8\u5f53\u4e8e\u8e29\u5728\u4e86\u6d4f\u89c8\u5668\u7684\u80a9\u8180\u4e0a\uff0c\u5f88\u591a\u5de5\u4f5c\u6d4f\u89c8\u5668\u5df2\u7ecf\u5e2e\u6211\u4eec\u5b8c\u6210\u4e86\uff0c\u6211\u4eec\u5de5\u4f5c\u4f1a\u8f7b\u677e\u5f88\u591a\u3002<\/p>\n<p><strong>\u518d\u626f\u70b9\u8fdc\u7684<\/strong><\/p>\n<p>\u77e5\u9053\u6280\u672f\u7ec6\u8282\u6709\u4ec0\u4e48\u7528\uff1f<\/p>\n<p>\u65e0\u8bba\u662fjQuery\u7684<code>$().css()<\/code>\u65b9\u6cd5\u8fd8\u662f\u8fd9\u91cc\u7684<code>getComputedStyle<\/code>\u8fd4\u56de\u7684\u8272\u503c\u90fd\u662fRGB\u6216RGBA\uff0c\u60f3\u5fc5\u8fd9\u4e2a\u5f88\u591a\u4eba\u90fd\u6ca1\u6ce8\u610f\u5230\uff0c\u66f4\u4e0d\u77e5\u9053\u517c\u5bb9\u6027\u662f\u600e\u6837\u7684\u3002\u5b9e\u9645\u4e0a\u517c\u5bb9\u6027\u975e\u5e38\u597d\uff0c\u6240\u6709\u6d4f\u89c8\u5668\u884c\u4e3a\u90fd\u4e00\u81f4\u3002\u5927\u591a\u6570\u65f6\u5019\uff0c99.9%\u7684\u65f6\u5019\uff0c\u77e5\u9053\u8fd9\u4e2a\u6280\u672f\u7ec6\u8282\u662f\u6ca1\u4ec0\u4e48\u7528\u7684\uff0c\u56e0\u4e3a\u83b7\u53d6\u989c\u8272\u518d\u8d4b\u503c\u989c\u8272\uff0c\u7ba1\u4ed6HEX\uff0cHSL\u8fd8\u662fRGB\uff0c\u6548\u679c\u90fd\u80fd\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5e94\u7528\u573a\u666f\u5343\u5343\u4e07\uff0c\u7c7b\u4f3c\u672c\u6587Canvas\u8981\u5b9e\u73b0\u989c\u8272\u5173\u952e\u5b57\u7684\u52a8\u753b\u6548\u679c\uff0c\u5982\u679c\u4e0d\u6e05\u695a\u6d4f\u89c8\u5668\u6709API\u5929\u7136\u8f6c\u6362\uff0c\u90a3\u53ef\u771f\u662f\u5bf8\u6b65\u96be\u884c\u4e86\uff0c\u4f30\u8ba1\u8981\u5efa\u4e00\u4e2a\u504c\u5927\u7684color\u5173\u952e\u5b57rgb\u8272\u503c\u6620\u5c04\u8868\uff0c\u5475\u5475\uff0c\u90a3\u65b9\u5411\u5c31\u8d70\u9519\u4e86\uff01<\/p>\n<p>\u6240\u4ee5\uff0c\u638c\u63e1\u6280\u672f\u7ec6\u8282\uff0c\u5c24\u5176\u662f\u5927\u91cf\u7684\u4e8b\u65e0\u5de8\u7ec6\u7684\u6280\u672f\u7ec6\u8282\uff0c\u53ef\u4ee5\u5728\u5173\u952e\u65f6\u523b\u7ed9\u4f60\u63d0\u4f9b\u66f4\u591a\u7684\u6280\u672f\u5b9e\u73b0\u601d\u8def\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u4f60\u7684\u6280\u672f\u5b9e\u73b0\u51e0\u4e4e\u6700\u4f73\uff0c\u53ef\u4ee5\u5c11\u8d70\u5f2f\u8def\uff0c\u6709\u4e0d\u4e00\u6837\u7684\u6280\u672f\u521b\u9020\u529b\uff0c\u8fd9\u4e2a\u53ef\u662f\u522b\u4eba\u65e0\u6cd5\u8d85\u8d8a\u7684\u6280\u672f\u7ade\u4e89\u529b\u6240\u5728\u3002<\/p>\n<p><strong>\u8865\u5145\u4e8e1\u5c0f\u65f6\u540e<\/strong><br \/>\n\u5fae\u535a\u8fbe\u4eba<a href=\"https:\/\/weibo.com\/zswang\" rel=\"noopener\" target=\"_blank\">@\u738b\u96c6\u9e44<\/a>\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cdCanvas\u5173\u952e\u5b57\u8272\u503c\u52a8\u753b\u5b9e\u73b0\u601d\u8def\uff0c\u633a\u597d\u7684\uff0c\u5927\u5bb6\u53ef\u4ee5\u5b66\u4e60\u4e0b\u3002<\/p>\n<p>\u5c31\u662f\u5229\u7528 Canvas \u7684\u7ebf\u6027\u6e10\u53d8\u751f\u6210\u6e10\u53d8\u8272\u7684\u8c03\u8272\u677f\uff0c\u7136\u540e\u5c31\u80fd\u517c\u987e\u6027\u80fd\u548c\u989c\u8272\u5b57\u9762\u91cf\u7684\u89e3\u6790\u3002<\/p>\n<p>\u4ee3\u7801\u5b9e\u73b0\u4e3b\u8981\u90e8\u5206\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/code-whj.png\" width=\"500\" height=\"524\" alt=\"\u8c03\u8272\u677f\u6838\u5fc3\u4ee3\u7801\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5728\u7ebfdemo<a href=\"http:\/\/jsbin.com\/piwihur\/edit?html,js,output\" rel=\"noopener\" target=\"_blank\">\u70b9\u51fb\u8fd9\u91cc<\/a>\u3002<\/p>\n<hr>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u91cd\u8981\u7684\u662f\u5b9e\u73b0\u601d\u8def\uff0c\u6b22\u8fce\u4ea4\u6d41\uff01<\/p>\n<p><img decoding=\"async\" src=\"\/\/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=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=7809\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=7809<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201807\/color-canvas-cover-deal.jpg\" width=\"161\" height=\"100\" alt=\"canvas\u989c\u8272\u8fc7\u6e21\u5904\u7406\u7f29\u7565\u56fe\" class=\"alignright size-medium\" \/><\/p>\n<p>\u5728CSS3\u4e2d\uff0c\u6211\u4eec\u8981\u60f3\u5b9e\u73b0\u4eceA\u989c\u8272\u5230B\u989c\u8272\u7684\u8fc7\u6e21\u6548\u679c\uff0c\u662f\u76f8\u5f53\u5bb9\u6613\u7684\uff0c\u53ea\u8981\u6307\u5b9a\u8d77\u6b62\u989c\u8272\uff0c\u914d\u5408<code>transition<\/code>\u8fc7\u6e21\u6216\u8005<code>animation<\/code>\u52a8\u753b\u90fd\u53ef\u4ee5\u65f6\u95f4\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5728<code>&lt;canvas&gt;<\/code>\u4e2d\u5374\u6ca1\u6709\u8fd9\u4e48\u7b80\u5355\uff0c\u56e0\u4e3a<code>&lt;canvas&gt;<\/code>\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u9759\u6001\u753b\u5e03\uff0c\u8981\u60f3\u5b9e\u73b0\u989c\u8272\u53d8\u5316\uff0c\u9700\u8981JS\u53bb\u4e0d\u65ad\u7ed8\u5236\uff0c\u5b9e\u73b0\u8d77\u6765\u8981\u6bd4CSS\u5b9e\u73b0\u9ebb\u70e6\u5f88\u591a\u3002\u518d\u52a0\u4e0a\u989c\u8272\u503c\u672c\u8eab\u5c31\u4e0d\u4e00\u5b9a\u662f\u7eaf\u7cb9\u7684\u6570\u503c\uff0c\u66f4\u589e\u52a0\u4e86\u6211\u4eec\u5b9e\u73b0\u7684\u96be\u5ea6\u3002<\/p>\n<p>\u672c\u6587\u5c31\u5c06\u901a\u8fc7\u591a\u4e2a\u6848\u4f8b\uff0c\u9010\u6b65\u6df1\u5165\uff0c\u4ecb\u7ecd\u4e00\u4e9b\u5728Canvas\u4e2d\u7684\u989c\u8272\u5904\u7406\u6280\u5de7\uff0c\u6709\u4e9b\u6280\u5de7\u8bf4\u4e0d\u5b9a\u4f1a\u8ba9\u4f60\u5927\u5f00\u773c\u754c\u3002<\/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":[531,714,1177,337,497,204,251],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7809"}],"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=7809"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7809\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=7809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=7809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=7809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}