{"id":7395,"date":"2018-02-27T23:22:58","date_gmt":"2018-02-27T15:22:58","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7395"},"modified":"2018-02-27T23:22:58","modified_gmt":"2018-02-27T15:22:58","slug":"css-svg-canvas-text-pattern-overlay","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/02\/css-svg-canvas-text-pattern-overlay\/","title":{"rendered":"CSS, SVG\u548ccanvas\u5206\u522b\u5b9e\u73b0\u6587\u672c\u6587\u5b57\u7eb9\u7406\u53e0\u52a0\u6548\u679c"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7395\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7395<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u5f97\u5230\u539f\u4f5c\u8005\u4e66\u9762\u8bb8\u53ef\uff0c\u540c\u65f6\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\uff0c\u6458\u8981\u5f15\u6d41\u5219\u968f\u610f\u3002<\/p>\n<p>\u672c\u6587\u8fd9\u91cc\u6240\u8bf4\u7684\u53e0\u52a0\uff0c\u5c31\u662f\u6df7\u5408\u6a21\u5f0f\u4e2d\u7684\u53e0\u52a0\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u672c\u6587\u8981\u5b9e\u73b0\u7684\u6548\u679c\u662f\uff0c\u6587\u5b57\u672c\u8eab\u7684\u989c\u8272\u548c\u7eb9\u7406\u8fdb\u884c\u53e0\u52a0\uff0c\u800c\u975e\u76f4\u63a5\u586b\u5145\u7eb9\u7406\u3002<\/p>\n<p>CSS, SVG\u548ccanvas\u90fd\u80fd\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff0c\u6211\u4eec\u4e00\u4e2a\u4e00\u4e2a\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<h3>\u4e00\u3001CSS\/CSS3\u5b9e\u73b0\u6587\u672c\u7eb9\u7406\u53e0\u52a0<\/h3>\n<p>HTML\u548cCSS\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;h2 class=\"pattern-overlay\"&gt;\n    &lt;span data-text=\"CSS\u7eb9\u7406\u53e0\u52a0\"&gt;&lt;\/span&gt;\n    CSS\u7eb9\u7406\u53e0\u52a0\n&lt;\/h2&gt;<\/pre>\n<pre>.pattern-overlay {\n    font-size: 60px;\n    font-family: 'microsoft yahei';\n    background-image: url(.\/pattern01.jpg); \n    -webkit-text-fill-color: transparent;\n    -webkit-background-clip: text;\n}\n.pattern-overlay &gt; span {\n    position: absolute;\n    background-image: linear-gradient(to bottom, #f00, #f00);\n    mix-blend-mode: overlay;\n    -webkit-text-fill-color: transparent;\n    -webkit-background-clip: text;\n}\n.pattern-overlay &gt; span::before {\n    content: attr(data-text);    \n}<\/pre>\n<p>\u5c31\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u4e0b\u56fe\u7684\u6548\u679c\uff08\u7ea2\u8272\u6e10\u53d8\u548c\u7070\u8272\u77f3\u8d28\u7eb9\u7406\u53e0\u52a0\u6548\u679c\uff09\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/2018-02-27_215442.png\" width=\"361\" height=\"74\" alt=\"CSS\u7eb9\u7406\u53e0\u52a0\u6548\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u7684\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201802\/css-pattern-overlay.html\" rel=\"noopener\" target=\"_blank\">CSS\u5b9e\u73b0\u6587\u672c\u7684\u7eb9\u7406\u53e0\u52a0\u6548\u679cdemo<\/a><\/p>\n<p>\u5728demo\u9875\u9762\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574\u6e10\u53d8\u56fe\u7247\u7684\u8d77\u6b62\u989c\u8272\uff0c\u6216\u8005\u66f4\u6362\u6211\u4eec\u7684\u7eb9\u7406\u56fe\u7247\uff0c\u90fd\u6709\u5b9e\u65f6\u7684\u6e32\u67d3\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/2018-02-27_220753.png\" width=\"433\" height=\"481\" alt=\"\u4fee\u6539\u6e10\u53d8\u548c\u53e0\u52a0\u7eb9\u7406\u540e\u7684\u6548\u679c\" class=\"alignnone\" \/><\/p>\n<h4>\u5b9e\u73b0\u539f\u7406<\/h4>\n<p>\u800c\u5728webkit\u6d4f\u89c8\u5668\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762CSS\u7ec4\u5408\u5b9e\u73b0\u6587\u672c\u4ee5\u80cc\u666f\u663e\u793a\u6548\u679c\uff1a<\/p>\n<pre>.fill-bg {\n    -webkit-text-fill-color: transparent;\n    -webkit-background-clip: text;\n}<\/pre>\n<p>\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=1601\">\u6587\u5b57\u6e10\u53d8<\/a>\uff0c\u6216\u8005\u7c7b\u4f3c<a href=\"\/\">\u672c\u7ad9\u9996\u9875<\/a><a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=4016\">\u6587\u5b57\u6d41\u5149<\/a>\u7b49\u6548\u679c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/2018-02-27_220213.png\" width=\"410\" height=\"41\" alt=\"\u9996\u9875\u6587\u5b57\u6548\u679c\u622a\u56fe\" class=\"imgpad\" \/><\/p>\n<p>\u4e8e\u662f\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u5c42\u6807\u7b7e\uff0c\u5206\u660e\u586b\u5145\u6e10\u53d8\u80cc\u666f\u548c\u7eb9\u7406\u80cc\u666f\uff0c\u7136\u540e\u518d\u4f7f\u7528CSS3\u6df7\u5408\u6a21\u5f0f<code>mix-blend-mode:overly<\/code>\u5bf9\u4e24\u5c42\u6807\u7b7e\u8fdb\u884c\u53e0\u52a0\uff0c\u6548\u679c\u5373\u8fbe\u6210\uff01<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>webkit\u5185\u6838\u6d4f\u89c8\u5668\uff0cChrome\uff0cSafari\u7b49\u90fd\u652f\u6301\u3002<\/p>\n<h4>\u5173\u4e8e\u4e3a\u4f55\u4e0d\u4f7f\u7528background-blend-mode\u8bf4\u660e<\/h4>\n<p>\u7406\u8bba\u4e0a\uff0c\u4f7f\u7528<code>background-blend-mode<\/code>\u6700\u591a\u80cc\u666f\u56fe\u7247\u8fdb\u884c\u6a21\u5f0f\u6df7\u5408\u662f\u6700\u7b80\u5355\u7684\uff0c\u56e0\u4e3a\u53ea\u9700\u8981\u4e00\u5c42\u8868\u73b0\uff0c\u7406\u8bba\u652f\u6301\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;h2 class=\"pattern-overlay\"&gt;CSS\u7eb9\u7406\u53e0\u52a0&lt;\/h2&gt;<\/pre>\n<pre>.pattern-overlay {\n    font-size: 60px;\n    font-family: 'microsoft yahei';\n    background-image: linear-gradient(to bottom, #f00, #f00), url(.\/pattern01.jpg);\n    background-blend-mode: overlay;\n    -webkit-text-fill-color: transparent;\n    -webkit-background-clip: text;\n}<\/pre>\n<p>\u80cc\u666f\u6e10\u53d8\u548c\u7eb9\u7406\u53e0\u52a0\u672c\u8eab\u662f\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u7684\uff0c\u6548\u679c\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/2018-02-27_221656.png\" width=\"252\" height=\"90\" alt=\"\u6210\u529f\u7684\u7eb9\u7406\u53e0\u52a0\" class=\"alignnone\" \/><\/p>\n<p>\u4f46\u662f\u5f53\u5e94\u7528<code>background-clip:text<\/code>\u58f0\u660e\u7684\u65f6\u5019\uff0c\u6df7\u5408\u6a21\u5f0f\u88ab\u5ffd\u7565\uff0c\u4e8e\u662f\u6700\u7ec8\u7684\u6587\u672c\u5e76\u6ca1\u6709\u53e0\u52a0\u6548\u679c\u3002\u56e0\u6b64\u624d\u91c7\u7528\u4e24\u5c42\u72ec\u7acb\u7684\u6807\u7b7e\u5e94\u7528<code>mix-blend-mode<\/code>\u6765\u53e0\u52a0\u7684\u65b9\u6cd5\u3002<\/p>\n<p><span class=\"s\">\/\/zxx: CSS3\u5bf9\u6df7\u5408\u6a21\u5f0f\u5929\u7136\u652f\u6301\uff0c\u53ef\u4ee5\u53c2\u89c1\u8fd9\u7bc7\u6587\u7ae0\uff1a\u201c<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/2015\/05\/css3-mix-blend-mode-background-blend-mode\/\">CSS3\u6df7\u5408\u6a21\u5f0fmix-blend-mode\/background-blend-mode\u7b80\u4ecb<\/a>\u201d\uff0c\u5176\u4e2d<code>mix-blend-mode<\/code>\u662f\u5143\u7d20\u95f4\u7684\u6df7\u5408\uff0c<code>background-blend-mode<\/code>\u662f\u80cc\u666f\u56fe\u7247\u4e4b\u95f4\u7684\u6df7\u5408\u3002<\/span><\/p>\n<h3>\u4e8c\u3001\u4f7f\u7528SVG\u5b9e\u73b0\u66f4\u52a0\u517c\u5bb9\u7684\u6587\u672c\u7eb9\u7406\u53e0\u52a0\u6548\u679c<\/h3>\n<p>CSS3\u7684\u65b9\u6cd5\u6700\u5bb9\u6613\u7406\u89e3\u4e0a\u624b\u6700\u5feb\uff0c\u4f46\u662fFirefox\u548cIE\u6d4f\u89c8\u5668\u90fd\u4e0d\u652f\u6301\uff0c\u6240\u4ee5\u53ea\u80fd\u5728\u79fb\u52a8\u7aef\u4f7f\u7528\uff0c\u5982\u679c\u6211\u4eec\u60f3\u517c\u5bb9PC\u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u4f7f\u7528SVG\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;svg width=\"360\" height=\"120\"&gt;\n  &lt;defs&gt;\n    &lt;filter id=\"blend\"&gt;\n     &lt;feImage xlink:href=\".\/pattern01.jpg\" result=\"patternSource\" x=\"0\" y=\"0\" width=\"360\" height=\"120\" \/&gt;            \n     &lt;feBlend mode=\"overlay\" in=\"SourceGraphic\" in2=\"patternSource\" \/&gt;\n    &lt;\/filter&gt;\n    &lt;linearGradient id=\"gradient\" x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\"&gt;\n        &lt;stop offset=\"0%\"   stop-color=\"green\" \/&gt;\n        &lt;stop offset=\"100%\" stop-color=\"red\" \/&gt;\n    &lt;\/linearGradient&gt;\n    &lt;pattern id=\"pattern\" width=\"360\" height=\"120\" patternUnits=\"userSpaceOnUse\"&gt;\n        &lt;rect x=\"0\" y=\"0\" width=\"100%\" height=\"100%\" fill=\"url(#gradient)\" filter=\"url(#blend)\"&gt;&lt;\/rect&gt;\n    &lt;\/pattern&gt;\n  &lt;\/defs&gt;\n  &lt;text x=\"0\" y=\"60\" font-family=\"Microsoft Yahei\" font-size=\"60\" font-weight=\"900\" fill=\"url(#pattern)\"&gt;\n    SVG\u7eb9\u7406\u53e0\u52a0\n  &lt;\/text&gt;\n&lt;\/svg&gt;<\/pre>\n<p>\u7ea2\u7eff\u6e10\u53d8\u53e0\u52a0\u77f3\u5934\u8d28\u611f\u7eb9\u7406\uff0c\u6700\u7ec8\u5b9e\u73b0\u7684\u6548\u679c\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/2018-02-27_223346.png\" width=\"370\" height=\"74\" alt=\"SVG\u7eb9\u7406\u53e0\u52a0\u6548\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201802\/svg-pattern-overlay.html\" rel=\"noopener\" target=\"_blank\">SVG\u5b9e\u73b0\u6587\u672c\u7684\u7eb9\u7406\u53e0\u52a0\u6548\u679cdemo<\/a><\/p>\n<h4>\u5b9e\u73b0\u539f\u7406<\/h4>\n<p>SVG\u4e2d\u6709\u4e2a\u548c\u6df7\u5408\u6a21\u5f0f\u76f8\u5173\u7684\u6ee4\u955c\u5143\u7d20\u540d\u4e3a<code>&lt;feBlend&gt;<\/code>\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a<code>&lt;filter&gt;<\/code>\uff0c\u8ba9\u5f15\u7528\u8be5<code>&lt;filter&gt;<\/code>\u7684\u56fe\u5f62\uff08<code>in=\"SourceGraphic\"<\/code>\uff09\u548c<code>&lt;feImage&gt;<\/code>\u8fd9\u4e2a\u56fe\u7247\uff08<code>in2=\"patternSource\"<\/code>\uff09\u8fdb\u884c<code>overlay<\/code>\u6df7\u5408\uff08<code>mode=\"overlay\"<\/code>\uff09\u3002<\/p>\n<p>SVG\u4e2d\u6587\u672c\u9664\u4e86\u53ef\u4ee5\u586b\u5145\u989c\u8272\u5916\uff0c\u8fd8\u53ef\u4ee5\u586b\u5145\u7eb9\u7406\uff0c\u5143\u7d20\u662f<code>&lt;pattern&gt;<\/code>\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u6e10\u53d8\u548c\u7eb9\u7406\u7d20\u6750\u6df7\u5408\u7684<code>&lt;pattern&gt;<\/code>\u5143\u7d20\uff0c\u5f88\u7b80\u5355\uff0c\u4e00\u4e2a\u548cSVG\u5c3a\u5bf8\u4e00\u6837\u7684\u77e9\u5f62<code>&lt;rect&gt;<\/code>\u5143\u7d20\uff0c\u4f7f\u7528\u6e10\u53d8\u586b\u5145\uff0c\u5e94\u7528\u53e0\u52a0\u6ee4\u955c\uff0c\u4f5c\u4e3a<code>&lt;pattern&gt;<\/code>\u7eb9\u7406\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u6548\u679c\u8fbe\u6210\uff01<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>Chrome, Safari, Firefox\u6d4f\u89c8\u5668\u90fd\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u5728IE\u6d4f\u89c8\u5668\u4e0b\u8bbf\u95ee\u6211\u4eec\u7684demo\u9875\u9762\uff0c\u4f1a\u770b\u5230\u7eb9\u7406\u5e76\u6ca1\u6709\u53e0\u52a0\uff0c\u90a3\u662f\u56e0\u4e3a\uff0cIE\u6d4f\u89c8\u5668\u4e0b\u7684<code>&lt;feBlend&gt;<\/code>\u53ea\u652f\u6301\u89c4\u8303\u4e2d\u63d0\u5230\u7684\u51e0\u79cd\u6df7\u5408\u6a21\u5f0f\uff0c\u5305\u62ec\uff1a<code>normal<\/code>\uff0c<code>multiply<\/code>\uff0c<code>screen<\/code>\uff0c<code>darken<\/code>\uff0c<code>lighten<\/code>\u3002\u5e76\u6ca1\u6709\u53e0\u52a0<code>overlay<\/code>\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5e0c\u671bSVG\u7eb9\u7406\u53e0\u52a0\u6548\u679cIE9+\u5168\u517c\u5bb9\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u4f7f\u7528\u6b63\u7247\u53e0\u52a0\u6df7\u5408\u6a21\u5f0f-<code>multiply<\/code>\uff0c\u5bf9\u4e8e\u5927\u90e8\u5206\u7528\u6237\u800c\u8a00\uff0c\u662f\u770b\u4e0d\u51fa\u4ec0\u4e48\u5dee\u5f02\u7684\u3002<\/p>\n<h3>\u4e09\u3001\u4f7f\u7528canvas\u5b9e\u73b0\u7eb9\u7406\u53e0\u52a0\u6548\u679c<\/h3>\n<p>canvas\u5e76\u6ca1\u6709\u73b0\u6210\u7684\u6df7\u5408\u6a21\u5f0fapi\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u8981\u60f3\u5b9e\u73b0\u53e0\u52a0\u6548\u679c\uff0c\u9700\u8981\u901a\u8fc7\u7b97\u6cd5\u91cd\u65b0\u8ba1\u7b97\u65b9\u6cd5\u3002\u5173\u4e8e\u6df7\u5408\u6a21\u5f0f\u7684\u5404\u79cd\u7b97\u6cd5\uff0c\u5b9e\u9645\u4e0a\u90fd\u662f\u516c\u5f00\u7684\uff0c\u641c\u4e00\u641c\u5c31\u80fd\u627e\u5230\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0ccanvas\u7684\u6df7\u5408\u6a21\u5f0f\u6548\u679c\u4f7f\u7528\u4e86\u4e00\u4e2a\u5f00\u6e90\u7684JS\u65b9\u6cd5\uff0c\u9879\u76ee\u5730\u5740\u662f\uff1a<a href=\"https:\/\/github.com\/Phrogz\/context-blender\" rel=\"nofollow\" target=\"_blank\">https:\/\/github.com\/Phrogz\/context-blender<\/a><\/p>\n<p>JS\u672a\u538b\u7f29\u72b6\u6001\u4e5f\u5c319K\u4e0d\u5230\uff0c\u5404\u79cd\u66fe\u7ecf\u7684\u6df7\u5408\u6a21\u5f0f\u90fd\u652f\u6301\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u8981\u4f7f\u7528canvas\u5b9e\u73b0\u7eb9\u7406\u53e0\u52a0\u6548\u679c\u90a3\u5c31\u5bb9\u6613\u591a\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u5b9e\u73b0\u7684\u6548\u679c\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/2018-02-27_225424.png\" width=\"365\" height=\"71\" alt=\"canvas\u6587\u672c\u7eb9\u7406\u53e0\u52a0\u6548\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201802\/canvas-pattern-overlay.html\" rel=\"noopener\" target=\"_blank\">canvas\u5b9e\u73b0\u6587\u672c\u7684\u7eb9\u7406\u53e0\u52a0\u6548\u679cdemo<\/a><\/p>\n<p>\u540c\u6837\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6e10\u53d8\u989c\u8272\uff0c\u4fee\u6539\u7eb9\u7406\u56fe\u7247\u770b\u5230\u5176\u4ed6\u6e32\u67d3\u6548\u679c\uff0c\u4f8b\u5982\uff0c\u6211\u4eec\u9009\u62e9\u672c\u5730\u4e00\u5f20\u7eb8\u5f20\u56fe\u7247\u4f5c\u4e3a\u7eb9\u7406\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/2018-02-27_230109.jpg\" width=\"417\" height=\"834\" alt=\"\u672c\u5730\u9009\u62e9\u7eb8\u5f20\u6548\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<h4>\u5b9e\u73b0\u539f\u7406<\/h4>\n<p>\u7ed8\u5236JS\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u5148\u5f15\u5165context_blender.js\uff0c\u7136\u540e\u2026\u2026\n\/\/ canvas\u7ed8\u5236\u811a\u672c<\/span>\nvar canvas = document.querySelector('canvas');\nvar context = canvas.getContext('2d');\nvar width = canvas.width, height = canvas.height;\ncontext.textBaseline = 'middle';\ncontext.font = 'bold 60px \"Microsoft Yahei\"';\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;\">\/\/ \u6e10\u53d8\u548c\u7eb9\u7406<\/span>\n    var gradient, pattern;\n    <span style=\"color:green;\">\/\/ \u521b\u5efa\u6750\u8d28canvas<\/span>\n    var canvasPattern = document.createElement('canvas');\n    var contextUnder = canvasPattern.getContext('2d');\n    canvasPattern.width = width;\n    canvasPattern.height = height;\n\n    <span style=\"color:green;\">\/\/ \u521b\u5efa\u6e10\u53d8canvas<\/span>\n    var canvasGradient = document.createElement('canvas');\n    var contextOver = canvasGradient.getContext('2d');\n    canvasGradient.width = width;\n    canvasGradient.height = height;\n    <span style=\"color:green;\">\/\/ \u7ed8\u5236\u6e10\u53d8\u5bf9\u8c61<\/span>\n    gradient = contextOver.createLinearGradient(0, 0, 0, height);\n    gradient.addColorStop(0, red);\n    gradient.addColorStop(1, red);\n\n    <span style=\"color:green;\">\/\/ \u7eb9\u7406\u5bf9\u8c61\uff0cimg\u6307\u7eb9\u7406\u56fe\u7247\u5bf9\u8c61<\/span>\n    pattern = contextUnder.createPattern(img, 'no-repeat');\n    contextUnder.fillStyle = pattern;\n    contextUnder.fillRect(0, 0, width, height);\n    <span style=\"color:green;\">\/\/ \u5e94\u7528\u6e10\u53d8<\/span>\n    contextOver.fillStyle = gradient;\n    contextOver.fillRect(0, 0, width, height);\n    <span style=\"color:green;\">\/\/ \u53e0\u52a0canvas<\/span>\n    contextOver.blendOnto(contextUnder, 'overlay');\n\n    <span style=\"color:green;\">\/\/ \u7ed9\u5f53\u524dcontext\u521b\u5efapattern<\/span>\n    pattern = context.createPattern(canvasPattern, 'no-repeat');\n    \n    <span style=\"color:green;\">\/\/ \u7ed8\u5236\u6587\u672c<\/span>\n    context.fillStyle = pattern;\n    context.fillText('\u753b\u5e03\u7eb9\u7406\u53e0\u52a0', 0, 60);\n};<\/pre>\n<p><strong>\u539f\u7406\u63cf\u8ff0\uff1a<\/strong><br \/>\n\u4e34\u65f6\u521b\u5efa\u4e00\u4e2acanvas\u7ed8\u5236\u4e00\u4e2a\u6e10\u53d8\uff0c\u4e34\u65f6\u521b\u5efa\u4e00\u4e2acanvas\u4f7f\u7528\u7eb9\u7406\u56fe\u7247\u586b\u5145\uff0c\u4e24\u4e2acanvas\u53e0\u52a0\u6df7\u5408\u5f97\u5230\u65b0\u7684canvas\uff0c\u7136\u540e\u9875\u9762\u4e0a\u90a3\u4e2acanvas\u4e0a\u7684\u6587\u5b57\u5c31\u628a\u8fd9\u4e2a\u53e0\u52a0\u6df7\u5408\u540ecanvas\u4f5c\u4e3a\u7eb9\u7406\u8fdb\u884c\u586b\u5145\uff0c\u6548\u679c\u5373\u8fbe\u6210\u3002<\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>IE9+\uff0cChrome, Safari, Firefox\u6d4f\u89c8\u5668\u90fd\u652f\u6301\u3002<\/p>\n<h3>\u56db\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>\u672c\u6587\u6240\u6709\u6848\u4f8b\uff0c\u65e0\u8bba\u662fCSS3\uff0cSVG\u8fd8\u662fcanvas\u5b9e\u73b0\u90fd\u662f\u4ee5\u53e0\u52a0\u6df7\u5408\u6a21\u5f0f<code>overlay<\/code>\u793a\u610f\u7684\u3002\u5176\u4ed6\u5404\u79cd\u6df7\u5408\u6a21\u5f0f\u4e5f\u90fd\u53ef\u4ee5\u8f7b\u677e\u652f\u6301\uff0c\u53ea\u8981\u4fee\u6539<code>overlay<\/code>\u4e3a\u5176\u4ed6\u5173\u952e\u5b57\u5373\u53ef\uff0c\u5c31\u4e0d\u4e00\u4e00\u4e3e\u4f8b\u4e86\u3002<\/p>\n<p>\u4e00\u70b9\u629b\u7816\u5f15\u7389\uff0c\u4ee5\u4e0a\u5c31\u662f\u81ea\u5df1\u6839\u636e\u4e00\u4e9b\u7ecf\u9a8c\u6478\u7d22\u51fa\u6765\u7684\u65b9\u6cd5\uff0c\u5b9e\u9645\u4e0a\u5e94\u8be5\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u5b9e\u73b0\u7eb9\u7406\u53e0\u52a0\u6548\u679c\uff0c\u6b22\u8fce\u4e0d\u541d\u8d50\u6559\u3002<\/p>\n<p>\u611f\u8c22\u60a8\u82b1\u5b9d\u8d35\u7684\u65f6\u95f4\u9605\u8bfb\u5230\u8fd9\u91cc\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=7395\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7395<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8fd9\u91cc\u6240\u8bf4\u7684\u53e0\u52a0\uff0c\u5c31\u662f\u6df7\u5408\u6a21\u5f0f\u4e2d\u7684\u53e0\u52a0\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u672c\u6587\u8981\u5b9e\u73b0\u7684\u6548\u679c\u662f\uff0c\u6587\u5b57\u672c\u8eab\u7684\u989c\u8272\u548c\u7eb9\u7406\u8fdb\u884c\u53e0\u52a0\uff0c\u800c\u975e\u76f4\u63a5\u586b\u5145\u7eb9\u7406\u3002<\/p>\n<p>CSS, SVG\u548ccanvas\u90fd\u80fd\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff0c\u6211\u4eec\u4e00\u4e2a\u4e00\u4e2a\u6765\u770b\u4e00\u4e0b\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,3,895],"tags":[995,1219,1197,1079,1191,368,364,994,1080,590,993,206,1218],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7395"}],"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=7395"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7395\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=7395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=7395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=7395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}