-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.js
More file actions
executable file
·133 lines (115 loc) · 4.29 KB
/
image.js
File metadata and controls
executable file
·133 lines (115 loc) · 4.29 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
const { createCanvas, GlobalFonts, loadImage } = require('@napi-rs/canvas');
const { join } = require('path');
// Load font
GlobalFonts.registerFromPath(join(__dirname, 'fonts', 'noto.ttf'), 'Noto');
async function generatePrayerCard(options) {
const {
prayerName,
prayerTime,
positiveHadith,
warningHadith,
} = options;
const canvas = createCanvas(800, 600);
const ctx = canvas.getContext('2d');
// Determine background image based on prayer name
let bgFile = 'default.png';
switch (prayerName.toLowerCase()) {
case 'fajr':
bgFile = 'fajr.jpg';
break;
case 'dhuhr':
case 'zuhr':
bgFile = 'dhuhr.webp';
break;
case 'asr':
bgFile = 'asr.jpg';
break;
case 'maghrib':
bgFile = 'maghrib.jpg';
break;
case 'isha':
bgFile = 'isha.jpg';
break;
default:
bgFile = 'default.jpg';
break;
}
// Load and draw background image
const bg = await loadImage(join(__dirname, 'assets', bgFile));
ctx.drawImage(bg, 0, 0, canvas.width, canvas.height);
// Header with underline
ctx.fillStyle = '#000';
ctx.font = 'bold 68px Noto';
const headerText = `حان وقت صلاة ${prayerName}`;
const headerWidth = ctx.measureText(headerText).width;
const headerX = (canvas.width - headerWidth) / 2;
const headerY = 115;
ctx.fillText(headerText, headerX, headerY);
ctx.beginPath();
ctx.moveTo(headerX, headerY + 17);
ctx.lineTo(headerX + headerWidth, headerY + 17);
ctx.strokeStyle = '#000';
ctx.lineWidth = 2;
ctx.stroke();
// Generic text renderer
const renderText = (text, x, startY, maxWidth, lineHeight, color, shadowColor = null, bold = false, align = 'center', fontSize = 28) => {
ctx.font = `${bold ? 'bold' : 'normal'} ${fontSize}px Noto`;
ctx.textAlign = align;
ctx.fillStyle = color;
// Set shadow if provided
if (shadowColor) {
ctx.shadowColor = shadowColor;
ctx.shadowBlur = 4;
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
} else {
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
}
const words = text.split(' ');
let line = '';
let y = startY;
for (let i = 0; i < words.length; i++) {
const testLine = line + words[i] + ' ';
const metrics = ctx.measureText(testLine);
if (metrics.width > maxWidth && i > 0) {
ctx.fillText(line, x, y);
line = words[i] + ' ';
y += lineHeight;
} else {
line = testLine;
}
}
ctx.fillText(line, x, y);
// Reset shadow
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
};
// Central Text
const centralText =
' اللهم صل على محمد، اللهم رب هذه الدعوة التامة، والصلاة القائمة، آت محمدًا الوسيلة والفضيلة، وابعثه مقامًا محمودًا الذي وعدته إنك لا تخلف الميعاد.';
renderText(centralText, canvas.width / 2, 220, 700, 44, '#000', null, true, 'center', 28);
// Hadiths with shadow
renderText(warningHadith, canvas.width / 4 + 15, 385, 350, 48, '#000', 'rgba(200, 0, 0, 0.65)', true, 'center', 32);
renderText(positiveHadith, (canvas.width * 3) / 4 - 15, 385, 350, 48, '#000', 'rgba(0, 200, 0, 0.65)', true, 'center', 32);
// Splitting line above footer
ctx.beginPath();
ctx.moveTo(30, canvas.height - 70);
ctx.lineTo(canvas.width - 30, canvas.height - 70);
ctx.strokeStyle = '#000';
ctx.lineWidth = 1.5;
ctx.stroke();
// Footer
ctx.fillStyle = '#000';
ctx.font = 'bold 24px Noto';
ctx.textAlign = 'left';
ctx.fillText(`Prayer Reminder • ${prayerName}`, 40, canvas.height - 40);
ctx.textAlign = 'right';
ctx.fillText(`Time: ${prayerTime}`, canvas.width - 40, canvas.height - 40);
return canvas.encode('png');
}
module.exports = generatePrayerCard;