|
| 1 | +const json = { |
| 2 | + "moduleId": 10000, |
| 3 | + "name": "effectText", |
| 4 | + "canvas": { |
| 5 | + "width": 700, |
| 6 | + "height": 1000 |
| 7 | + }, |
| 8 | + "position": { |
| 9 | + // "left": "0", |
| 10 | + // "top": "50" |
| 11 | + }, |
| 12 | + "config": { |
| 13 | + "font": { |
| 14 | + "fontStyle": "normal", |
| 15 | + "fontWeight": 900, |
| 16 | + "fontSize": "60px", |
| 17 | + "fontFamily": "SimSun, Songti SC", |
| 18 | + }, |
| 19 | + "contents": [ |
| 20 | + { |
| 21 | + "text": "通勤风长袖条纹", |
| 22 | + "fillStyle": '#BEFE1F', |
| 23 | + "textAlign": 'center', |
| 24 | + "shadowOffsetX": 3, |
| 25 | + "shadowOffsetY": 2, |
| 26 | + "shadowColor": '#000000', |
| 27 | + "position": { |
| 28 | + "top": "50", |
| 29 | + "right": "0", |
| 30 | + "left": "0" |
| 31 | + }, |
| 32 | + "effects": [ |
| 33 | + { |
| 34 | + "strokeStyle": '#000000', |
| 35 | + "lineWidth": 4 |
| 36 | + } |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "text": "水洗超高腰", |
| 41 | + "fillStyle": '#FCE370', |
| 42 | + "textAlign": 'left', |
| 43 | + "position": { |
| 44 | + "top": "50", |
| 45 | + "right": "0", |
| 46 | + "left": "0" |
| 47 | + }, |
| 48 | + "effects": [ |
| 49 | + { |
| 50 | + "strokeStyle": '#000000', |
| 51 | + "lineWidth": 4 |
| 52 | + } |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "text": "直筒长裤", |
| 57 | + "fillStyle": '#FCE370', |
| 58 | + "effects": [ |
| 59 | + { |
| 60 | + "strokeStyle": '#000000', |
| 61 | + "lineWidth": 4 |
| 62 | + } |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "text": "迷你自动白色", |
| 67 | + "fillStyle": '#FFFFFF', |
| 68 | + "textAlign": 'left', |
| 69 | + "lineHeight": 10, |
| 70 | + "position": { |
| 71 | + "left": "60" |
| 72 | + }, |
| 73 | + "effects": [ |
| 74 | + { |
| 75 | + "strokeStyle": '#000000', |
| 76 | + "lineWidth": 6 |
| 77 | + } |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "text": "生活电器", |
| 82 | + "fillStyle": '#000000', |
| 83 | + "textAlign": 'left', |
| 84 | + "position": { |
| 85 | + "left": "60", |
| 86 | + }, |
| 87 | + "effects": [ |
| 88 | + { |
| 89 | + "strokeStyle": '#ffffff', |
| 90 | + "lineWidth": 6 |
| 91 | + } |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "text": "OL风两件套", |
| 96 | + "fillStyle": '#FFFFFF', |
| 97 | + "textAlign": 'right', |
| 98 | + "lineHeight": 10, |
| 99 | + "shadowOffsetX": 5, |
| 100 | + "shadowOffsetY": 4, |
| 101 | + "shadowColor": '#6D92FF', |
| 102 | + "shadowBlur":0, |
| 103 | + "position": { |
| 104 | + "right": "10" |
| 105 | + }, |
| 106 | + "effects": [ |
| 107 | + { |
| 108 | + "strokeStyle": '#0546FF', |
| 109 | + "lineWidth": 4 |
| 110 | + } |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "text": "职业装", |
| 115 | + "fillStyle": '#FFFFFF', |
| 116 | + "textAlign": 'right', |
| 117 | + "effects": [ |
| 118 | + { |
| 119 | + "strokeStyle": '#0546FF', |
| 120 | + "lineWidth": 4 |
| 121 | + } |
| 122 | + ] |
| 123 | + }, |
| 124 | + ], |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +function parseJsonToCanvas(json) { |
| 129 | + const canvas = document.querySelector('canvas'); |
| 130 | + const ctx = canvas.getContext('2d'); |
| 131 | + canvas.width = json.canvas.width; |
| 132 | + canvas.height = json.canvas.height; |
| 133 | + |
| 134 | + ctx.x = json.position.left || canvas.width/2; |
| 135 | + ctx.y = json.position.top || canvas.height/2; |
| 136 | + |
| 137 | + // 渲染默认字体大小 |
| 138 | + ctx.font = json.config.font && Object.values(json.config.font).join(" "); |
| 139 | + ctx.fontSize = json.config.font && json.config.font.fontSize && parseInt(json.config.font.fontSize, 10); |
| 140 | + |
| 141 | + json.config.contents.forEach((item, index) => { |
| 142 | + Object.keys(item).forEach(key => { |
| 143 | + item[key] ? ctx[key] = item[key] : ''; |
| 144 | + }); |
| 145 | + const text = item.text; |
| 146 | + const _textZh = ctx.measureText(item.text); |
| 147 | + ctx.textBaseline = item.textBaseline || 'top'; |
| 148 | + ctx.lineHeight = item.lineHeight ? parseInt(item.lineHeight, 10) : 10; |
| 149 | + if(item.textAlign) { |
| 150 | + ctx.textAlign = item.textAlign |
| 151 | + } |
| 152 | + |
| 153 | + //x 轴设置, left,right 同时存在为居中,不设置position: left/right 则默认居中。优先级:right > left > center |
| 154 | + if(item.position && item.position.left && item.position.right) { |
| 155 | + if (item.textAlign === 'left') { |
| 156 | + ctx.x = canvas.width/2 - _textZh.width/2; |
| 157 | + } |
| 158 | + if (item.textAlign === 'right') { |
| 159 | + ctx.x = canvas.width/2 + _textZh.width/2; |
| 160 | + } |
| 161 | + if (item.textAlign === 'center') { |
| 162 | + ctx.x = canvas.width/2 |
| 163 | + } |
| 164 | + } else if (item.position && item.position.right) { |
| 165 | + ctx.x = canvas.width - parseInt(item.position.right, 10) |
| 166 | + } else if(item.position && item.position.left) { |
| 167 | + ctx.x = parseInt(item.position.left, 10); |
| 168 | + } |
| 169 | + |
| 170 | + // y 轴设置 |
| 171 | + if(item.position && item.position.top) { |
| 172 | + ctx.y = item.position.top |
| 173 | + } |
| 174 | + |
| 175 | + // 渲染描边 |
| 176 | + item.effects && item.effects.forEach((effect) => { |
| 177 | + if (effect.strokeStyle) { |
| 178 | + ctx.strokeStyle = effect.strokeStyle; |
| 179 | + } |
| 180 | + if (effect.gradient && effect.gradient.length > 0) { |
| 181 | + const _strokeStyle = renderLinearGradient(text, effect.gradient, ctx); |
| 182 | + ctx.strokeStyle = _strokeStyle; |
| 183 | + } |
| 184 | + |
| 185 | + ctx.lineWidth = effect.lineWidth; |
| 186 | + ctx.strokeText(text, ctx.x, Number(ctx.y)+ index*(ctx.fontSize + ctx.lineHeight)); |
| 187 | + }); |
| 188 | + |
| 189 | + // 渲染文字 |
| 190 | + ctx.fillText(item.text, ctx.x, Number(ctx.y)+ index*(ctx.fontSize + ctx.lineHeight)); |
| 191 | + }); |
| 192 | +} |
| 193 | + |
| 194 | +function renderLinearGradient(text, gradient, ctx) { |
| 195 | + const text_width = ctx.measureText(text); |
| 196 | + const _gradient = ctx.createLinearGradient(0,0,text_width.width,0); |
| 197 | + gradient.forEach(y => { |
| 198 | + Object.entries(y).forEach(u => { |
| 199 | + _gradient.addColorStop(u[0],u[1]); |
| 200 | + }); |
| 201 | + }); |
| 202 | + return _gradient; |
| 203 | +} |
| 204 | +parseJsonToCanvas(json); |
| 205 | + |
0 commit comments