-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.js
132 lines (110 loc) · 4.13 KB
/
image.js
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
const Canvas = require('canvas');
const {MessageAttachment} = require('discord.js')
const noteUtils = require('./notes.js');
const Note = require('./note.js');
module.exports = async args => {
let width = 80*args.length + 85;
let normalHeight = 150;
let height = normalHeight
let staffTop = 0;
let heights = args.map(note => calculateHeight(note))
let top = Math.min(...heights.map(height => height.top));
let bot = Math.max(...heights.map(height => height.bot));
bot += 30
if(top < 0) {
height += -top;
staffTop += -top;
}
if(bot > normalHeight) {
height += bot-normalHeight;
}
let canvas = Canvas.createCanvas(width, height);
let ctx = canvas.getContext('2d');
ctx.fillStyle = "white";
ctx.fillRect(0,0, canvas.width, canvas.height);
let noteParts = await loadNoteParts();
ctx.drawImage(noteParts.trebleClef, 0, staffTop, 85, 170);
ctx.drawImage(noteParts.staff, 85, staffTop, width, 170)
for([i, height] of heights.entries()) {
let xPos = 100 + 80*i
createNote(ctx, noteParts, staffTop, xPos, args[i], height);
}
let attachment = new MessageAttachment(canvas.toBuffer(), 'notes.png');
return attachment;
}
let switchDown = new Note("Bb");
let ledgerDown = new Note("Db");
let ledgerUp = new Note("G#5");
let stemExtendDown = new Note("Bb5")
let stemExtendUp = new Note("B#3")
let stemEndPos = new Note("B");
function createNote(ctx, parts, staffTop, xPos, note, height) {
let notePos = 0;
let noteDirection = ""
if(switchDown.isAbove(note) || (switchDown.equals(note) && note.normalName === "A#")) {
notePos = height.top+staffTop+54
noteDirection = "down";
}
else {
notePos = height.top+54+staffTop
noteDirection = "up";
}
let xShift = 5;
let yShift = 15;
if(note.isAbove(ledgerUp) || (note.equals(ledgerUp) && note.normalName === "Ab")) {
let numOfLedgers = Math.ceil((Note.noAccidentalDistance(ledgerUp, note))/2)
for(let i = 1; i <= numOfLedgers; i++) {
ctx.drawImage(parts.ledger, xPos+xShift, staffTop+31-19*i, 38, 5)
}
}
else if(ledgerDown.isAbove(note) || (note.equals(ledgerDown) && note.normalName === "C#")) {
let numOfLedgers = Math.ceil((Note.noAccidentalDistance(note, ledgerDown))/2)
for(let i = 1; i <= numOfLedgers; i++) {
ctx.drawImage(parts.ledger, xPos+xShift, staffTop+31+19*(i+4), 38, 5)
}
}
if(noteDirection === "down") {
ctx.drawImage(parts.down, xPos+4, height.top+staffTop, 85/2, 170/2);
}
else {
ctx.drawImage(parts.up, xPos, height.top+54+staffTop, 85/2, 170/2);
}
if(note.accidental === "b") {
ctx.drawImage(parts.flat, xPos-25, notePos-28, 30, 60);
}
else if(note.accidental === "#") {
ctx.drawImage(parts.sharp, xPos-25, notePos-20, 30, 70);
}
if(note.isAbove(stemExtendDown) || (note.equals(stemExtendDown) && note.normalName === "Bb")) {
let length = 9.5*Note.noAccidentalDistance(stemEndPos, note)-10
ctx.drawImage(parts.stem, xPos+9, notePos+28, 5, length)
}
if(stemExtendUp.isAbove(note) || (note.equals(stemExtendUp) && note.standardName === "B#")) {
let length = 9.5*Note.noAccidentalDistance(note, stemExtendUp)+10
ctx.drawImage(parts.stemUp, xPos+32, height.top-length+15, 4, length)
}
}
let middleC = new Note(0);
function calculateHeight(note) {
let normalNote = new Note(note.normalName.slice(0, 1))
normalNote.octave = note.octave
if(note.normalName === "B#") {
normalNote.octave -= 1;
}
let distanceFromC = Note.noAccidentalDistance(middleC, note);
let isLineNote = distanceFromC%2 === 0 ? true : false;
let height = 9.5*(6-distanceFromC);
return {top: height, bot: height+170/2, isLineNote};
}
async function loadNoteParts() {
let staff = await Canvas.loadImage('./images/staff.png');
let trebleClef = await Canvas.loadImage('./images/treble.png');
let flat = await Canvas.loadImage('./images/flat.png');
let sharp = await Canvas.loadImage('./images/sharp.png');
let up = await Canvas.loadImage('./images/up.png');
let down = await Canvas.loadImage('./images/down.png');
let ledger = await Canvas.loadImage('./images/ledger.png');
let stem = await Canvas.loadImage('./images/stem.png');
let stemUp = await Canvas.loadImage('./images/stemUp.png');
return {staff, trebleClef, flat, sharp, up, down, ledger, stem, stemUp}
}