-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcard.js
More file actions
200 lines (153 loc) · 6.59 KB
/
card.js
File metadata and controls
200 lines (153 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const {registerFont,loadImage,createCanvas} = require("canvas");
const { resolve } = require("path");
const StackBlur = require('stackblur-canvas');
var OpenType = require('opentype.js');
async function CreateBackground(bg,boderblur=0,bgblur=0){
const oimage = await loadImage(bg)
const canvas = createCanvas(700, 250);
const ctx = canvas.getContext('2d');
ctx.drawImage(oimage, 0, 0, canvas.width, canvas.height);
StackBlur.canvasRGBA(canvas, 0, 0, canvas.width, canvas.height, boderblur);
// 绘制圆角矩形
const radius = 35; // 圆角半径
ctx.globalCompositeOperation = 'destination-in';
ctx.beginPath();
ctx.moveTo(radius, 0);
ctx.lineTo(canvas.width - radius, 0);
ctx.quadraticCurveTo(canvas.width, 0, canvas.width, radius);
ctx.lineTo(canvas.width, canvas.height - radius);
ctx.quadraticCurveTo(canvas.width, canvas.height, canvas.width - radius, canvas.height);
ctx.lineTo(radius, canvas.height);
ctx.quadraticCurveTo(0, canvas.height, 0, canvas.height - radius);
ctx.lineTo(0, radius);
ctx.quadraticCurveTo(0, 0, radius, 0);
ctx.closePath();
ctx.fill();
ctx.clip();
// 添加邊框
const borderWidth = 15; // 邊框寬度
ctx.lineWidth = borderWidth;
ctx.strokeStyle = '#000';
ctx.stroke();
ctx.globalCompositeOperation = 'destination-over';
const canvas1 = createCanvas(canvas.width, canvas.height);
const ctx1 = canvas1.getContext('2d');
ctx1.drawImage(oimage, 0, 0, canvas1.width, canvas1.height);
StackBlur.canvasRGBA(canvas1, 0, 0, canvas1.width, canvas1.height, bgblur);
ctx.drawImage(canvas1, 0, 0, canvas.width, canvas.height);
return canvas
}
async function addAvatar(bgcanvas,avatars){
const circleX = bgcanvas.width/5
const circleY = bgcanvas.height/2
const circleRadius = 100
const canvas = createCanvas(bgcanvas.width, bgcanvas.height);
const ctx = canvas.getContext('2d');
const gradient = ctx.createLinearGradient(0, circleY+circleRadius/2,circleRadius+circleRadius/2);
gradient.addColorStop(0, '#33f');
gradient.addColorStop(1, '#f33');
ctx.beginPath();
ctx.arc(circleX, circleY, circleRadius, 0, 2 * Math.PI);
ctx.closePath();
ctx.fillStyle = '#fff';
ctx.lineWidth = 10;
ctx.strokeStyle = '#fff';//gradient;
ctx.stroke();
ctx.fill();
ctx.clip();
const avatar = await loadImage(avatars)
ctx.globalCompositeOperation = 'source-over';
ctx.drawImage(avatar, circleX - circleRadius, circleY - circleRadius, circleRadius*2, circleRadius*2);
const ctx2 = bgcanvas.getContext('2d');
ctx2.globalCompositeOperation = 'source-over';
ctx2.drawImage(canvas,0,0)
return bgcanvas
}
async function addTitle(c,welcomefont,welcomeText,welcomeTextFontSize=34,Titlefont,text,fontSize = 52,subTitleFont,subTitle,subTitleFontSize){
const TextX = (c.width/5) * 2
const TextY = (c.height/2) + (fontSize/2)
const canvas = createCanvas(c.width, c.height);
const ctx = canvas.getContext('2d');
var path = Titlefont.getPath(text,TextX,TextY,fontSize);
path.draw(ctx);
ctx.fillStyle = '#fff';
ctx.strokeStyle = '#F3F3F3';
ctx.lineWidth = 1.5;
ctx.stroke();
ctx.fill();
welcomefont.getPath(welcomeText,TextX, TextY - fontSize - 5, welcomeTextFontSize).draw(ctx)
ctx.fillStyle = '#fff';
ctx.lineWidth = 1.5;
ctx.strokeStyle = '#000';
ctx.fill();
subTitleFont.getPath(subTitle,TextX,(c.height/4)*3.2,subTitleFontSize).draw(ctx)
ctx.fillStyle = '#fff';
ctx.fill();
const ctx2 = c.getContext('2d');
ctx2.globalCompositeOperation = 'source-over';
ctx2.drawImage(canvas,0,0)
return c
}
/**
* @typedef {Object} CardOptions
* @property {string} [WelcomeTextFont] - The blur of boder.
* @property {string} [Titlefont] - The blur of boder.
* @property {string} [SubTitleFont] - The blur of boder.
*
* @typedef {Object} BackgroundOptions
* @property {number} [boderblur] - The blur of boder.
* @property {number} [bgblur] - The blur of the background.
*
* @typedef {Object} TextSizeOptions
* @property {number} [WelcomeTextSize] -
* @property {number} [TitleSize] -
* @property {number} [SubtitleSize] -
*/
class WelcomeCard{
/**
* @param {CardOptions} [options] - The options for the person.
*/
constructor(options = {}){
this.WelcomeTextFontPath = options.WelcomeTextFont || "./fonts/NaikaiFont-Bold.ttf"
this.TitlefontPath = options.Titlefont || "./fonts/NotoSansTC-Black.otf"
this.SubTitleFontPath = options.SubTitleFont || "./fonts/NaikaiFont-Bold.ttf"
}
async LoadFonts(){
this.WelcomeTextFont = await OpenType.load(this.WelcomeTextFontPath)
this.Titlefont = await OpenType.load(this.TitlefontPath)
this.subTitleFont = await OpenType.load(this.SubTitleFontPath)
}
/**
* Create a Image.
* @param {BackgroundOptions} [bgOptions] - The options for the person.
* @param {TextSizeOptions} [textOptions] - The options for the person.
*/
async CreateImageCard(BackgroundImg, Avatar, WelcomeText, Title, Subtitle, bgOptions={},textOptions={}){
const BackgroundOptions1 = {
boderblur: bgOptions.boderblur || 90,
bgblur: bgOptions.bgblur || 8
}
const TextSize ={
WelcomeTextSize: textOptions.WelcomeTextSize || 34,
TitleSize: textOptions.TitleSize || 62,
SubtitleSize:textOptions.SubtitleSize || 24
}
const welcomefont = this.WelcomeTextFont
const Titlefont = this.Titlefont
const SubTitleFont = this.subTitleFont
const c_bg = await CreateBackground(BackgroundImg, BackgroundOptions1.boderblur, BackgroundOptions1.bgblur)
const c_avatar = await addAvatar(c_bg, Avatar);
const c_Title = await addTitle(c_avatar, welcomefont, WelcomeText,TextSize.WelcomeTextSize, Titlefont, Title, TextSize.TitleSize, SubTitleFont, Subtitle, TextSize.SubtitleSize)
return c_Title.toBuffer('image/png')
}
}
async function CreateImageCard(bg,avatar,WelcomeText,Title,Subtitle){
const welcomefont = await OpenType.load('./fonts/NaikaiFont-Bold.ttf')
const Titlefont = await OpenType.load('./fonts/NotoSansTC-Black.otf')
const SubTitleFont = await OpenType.load('./fonts/NaikaiFont-Bold.ttf')
const c_bg = await CreateBackground(bg, 90, 8)
const c_avatar = await addAvatar(c_bg, avatar);
const c_Title = await addTitle(c_avatar, welcomefont, WelcomeText, Titlefont, Title, 62, SubTitleFont, Subtitle, 24)
return c_Title.toBuffer('image/png')
}
module.exports = { WelcomeCard,CreateImageCard }