Created
August 31, 2020 01:26
-
-
Save danigb/cdb3964bb9d0a236e929e6f763518ca8 to your computer and use it in GitHub Desktop.
Generate 88keys sheet paper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const { PDFDocument, rgb } = require("pdf-lib"); | |
const KEY_COLORS = "WBWBWWBWBWBW"; | |
const KEYS = 88; | |
const MEASURES = 2 * 4; | |
const DIVISION = 3; | |
const SUBDIVISION = 3; | |
main().catch((e) => console.error(e)); | |
async function main() { | |
// Create a new PDFDocument | |
const pdfDoc = await PDFDocument.create(); | |
// Add a blank page to the document | |
const page = pdfDoc.addPage(); | |
// Get the width and height of the page | |
const size = page.getSize(); | |
const hMargin = size.width * 0.05; | |
const width = size.width - 2 * hMargin; | |
const vMargin = size.height * 0.05; | |
const height = size.height - 2 * vMargin; | |
const col = width / KEYS; | |
// black KEY_COLORS shades | |
for (let i = 0; i < KEYS; i++) { | |
const offset = hMargin + i * col; | |
const pos = (i - 3 + 12) % 12; | |
const isBlack = KEY_COLORS[pos] === "B"; | |
if (isBlack) { | |
page.drawRectangle({ | |
x: offset + col * 0.2, | |
y: vMargin, | |
width: col * 0.6, | |
height: height, | |
color: rgb(0, 0, 0), | |
borderColor: rgb(0, 0, 0), | |
opacity: 0.05, | |
borderOpacity: 0, | |
}); | |
} | |
} | |
// vertical lines | |
for (let i = 0; i <= KEYS; i++) { | |
const offset = hMargin + i * col; | |
const pos = (i - 3 + 12) % 12; | |
const isC = pos === 0; | |
const isBlack = KEY_COLORS[pos] === "B"; | |
const prevIsBlack = KEY_COLORS[(pos + 11) % 12] === "B"; | |
const isMiddleC = i === 39; | |
const x = isBlack | |
? offset + col * 0.1 | |
: prevIsBlack | |
? offset - col * 0.1 | |
: offset; | |
page.drawLine({ | |
start: { x, y: vMargin }, | |
end: { x, y: vMargin + height }, | |
thickness: isMiddleC ? 0.8 : isC ? 0.4 : 0.25, | |
color: rgb(0, 0, 0), | |
opacity: isC ? 0.8 : 0.5, | |
}); | |
} | |
// horizontal lines | |
const horizontalLines = MEASURES * DIVISION * SUBDIVISION; | |
const time = height / horizontalLines; | |
for (let i = 0; i <= horizontalLines; i++) { | |
const offset = vMargin + i * time; | |
const isMeasure = i % (DIVISION * SUBDIVISION) === 0; | |
const isDivision = !isMeasure && i % SUBDIVISION === 0; | |
page.drawLine({ | |
start: { x: hMargin, y: offset }, | |
end: { x: hMargin + width, y: offset }, | |
thickness: isMeasure ? 0.4 : 0.25, | |
color: rgb(0, 0, 0), | |
opacity: isMeasure ? 0.5 : isDivision ? 0.3 : 0.1, | |
}); | |
} | |
// Serialize the PDFDocument to bytes (a Uint8Array) | |
const pdfBytes = await pdfDoc.save(); | |
fs.writeFileSync("88keys.pdf", pdfBytes, "binary"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment