1
1
import canvas from 'canvas'
2
2
import fs from 'fs'
3
3
import path from 'path'
4
- // import articles from '../gen/articles.json'
4
+
5
+ const logColors = {
6
+ white : '\u001b[37m' ,
7
+ cyan : '\u001b[36m' ,
8
+ reset : '\u001b[0m' ,
9
+ }
5
10
6
11
function getLines ( title , maxWidth , ctx ) {
7
12
let lines = [ ]
@@ -24,24 +29,24 @@ function getLines(title, maxWidth, ctx) {
24
29
return lines
25
30
}
26
31
27
- function generateOgpImg ( slug , title ) {
32
+ async function generateOgpImg ( slug , title ) {
28
33
const WIDTH = 1200
29
34
const HEIGHT = 630
30
- const FONT_SIZE = 55
35
+ const FONT_SIZE = 60
31
36
const FONT_FAMILY = '"Noto Sans CJK JP"'
32
37
const FONT_TYPE = 'bold'
33
- const TEXT_WIDTH = 900
34
- const FONT_COLOR = '#474747 '
35
- const TEXT_MARGIN = 15
38
+ const TEXT_WIDTH = 950
39
+ const FONT_COLOR = '#262626 '
40
+ const TEXT_MARGIN = 20
36
41
const MAX_LINE = 4
37
- const BACKGROUND_COLOR = 'white'
42
+ const BACKGROUND_IMG_PATH = path . join ( process . cwd ( ) , 'img/ogp_bg/ogp_bg_4.png' )
38
43
39
44
const canv = canvas . createCanvas ( WIDTH , HEIGHT )
40
45
const ctx = canv . getContext ( '2d' )
41
46
42
- // Background color
43
- ctx . fillStyle = BACKGROUND_COLOR
44
- ctx . fillRect ( 0 , 0 , WIDTH , HEIGHT )
47
+ // Background image
48
+ const bgImage = await canvas . loadImage ( BACKGROUND_IMG_PATH )
49
+ ctx . drawImage ( bgImage , 0 , 0 , WIDTH , HEIGHT )
45
50
46
51
ctx . font = FONT_TYPE + ' ' + FONT_SIZE + 'px ' + FONT_FAMILY
47
52
ctx . fillStyle = FONT_COLOR
@@ -57,7 +62,9 @@ function generateOgpImg(slug, title) {
57
62
// console.log(i + ' - ' + titleLines[i])
58
63
const text = titleLines [ i ] . join ( '' )
59
64
const lineX = ( WIDTH - ctx . measureText ( text ) . width ) / 2
60
- const lineY = HEIGHT / 2 - FONT_SIZE * ( titleLines . length + 1 ) / 2 + ( FONT_SIZE + TEXT_MARGIN ) * i
65
+ // const lineY = HEIGHT / 2 - FONT_SIZE * (titleLines.length + 1) / 2 + (FONT_SIZE + TEXT_MARGIN) * i
66
+ // const lineY = (HEIGHT + 150) / 2 - FONT_SIZE * (titleLines.length + 1) / 2 + (FONT_SIZE + TEXT_MARGIN) * i
67
+ const lineY = ( HEIGHT + 50 ) / 2 - FONT_SIZE * ( titleLines . length + 1 ) / 2 + ( FONT_SIZE + TEXT_MARGIN ) * i
61
68
ctx . fillText ( text , lineX , lineY )
62
69
}
63
70
@@ -76,6 +83,7 @@ function main() {
76
83
}
77
84
generateOgpImg ( slug , title )
78
85
}
86
+ console . log ( logColors . cyan + '[script:ogpbuild]' + logColors . white + ' Successful!' + logColors . reset )
79
87
}
80
88
81
89
// run
0 commit comments