-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcanvas.js
More file actions
107 lines (86 loc) · 2.84 KB
/
canvas.js
File metadata and controls
107 lines (86 loc) · 2.84 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
// Custom Canvas function
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line.trim(), x, y);
return y
}
function drawCenteredImage(ctx, img, x, y, width, height) {
ctx.drawImage(img, x - 0.5 * width, y - 0.5 * height, width, height)
}
var colorOne = "blue"
var colorTwo = "green"
function saveImage(imageID) {
download = document.getElementById(imageID).src
}
function loadSocialCanvasBackground(colorOne, colorTwo, backgroundImgId) {
if (colorTwo !== undefined) {
console.log(backgroundImgId)
loadSocialCanvas(colorOne, colorTwo, backgroundImgId);
}
else {
loadSocialCanvas(colorOne, colorOne, backgroundImgId)
}
}
function loadSocialCanvas(colorOne, colorTwo, backgroundImgId) {
c = document.getElementById("socialCanvas");
ctx = c.getContext("2d");
width = c.width;
height = c.height;
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 1;
ctx.shadowOffsetY = 1;
ctx.shadowColor = "rgba(0,0,0,0.3)"
midpointX = width * 0.5;
midpointY = height * 0.5;
ctx.textAlign = "center";
// Create gradient
var grd = ctx.createLinearGradient(0, 0, width, height);
grd.addColorStop(0, colorOne);
grd.addColorStop(1, colorTwo);
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(0, 0, width, height);
if (backgroundImgId !== undefined) {
backgroundImg = document.getElementById(backgroundImgId);
ctx.drawImage(backgroundImg, 0, 0, width, height);
}
nowPlaying = $(".joan-now-playing h3").html()
time = $(".joan-now-playing").children().eq(1).html();
nowPlayingImg = document.getElementsByClassName("joan-image-thumbnail")[0]
if (nowPlaying == undefined) {
nowPlaying = "Dartmouth Radio"
time = "live 24-7";
}
if (nowPlayingImg == undefined) {
nowPlayingImg = document.getElementById("default");
}
ctx.shadowBlur = 6;
ctx.fillStyle = "#ffffff";
textY = midpointY + 420;
ctx.font = "bold 55px Roboto Mono";
playingY = wrapText(ctx, nowPlaying.trim(), midpointX, textY, width * 0.9, 60);
ctx.font = "40px Roboto Mono";
ctx.fillText(time, midpointX, playingY + 60);
ctx.shadowColor = "rgba(0,0,0,0.3)"
ctx.shadowBlur = 20;
drawCenteredImage(ctx, nowPlayingImg, midpointX, midpointY - 100, width * 0.8, width * 0.8);
ctx.shadowBlur = 10;
logoImg = document.getElementById("logo");
drawCenteredImage(ctx, logoImg, midpointX, 300, 200, 200)
var dataURL = c.toDataURL();
document.getElementById('socialCanvasImg').src = dataURL;
}