-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (84 loc) · 2.5 KB
/
index.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
const canvasSketch = require('canvas-sketch');
const { pathsToSVG } = require('canvas-sketch-util/penplot');
const math = require('canvas-sketch-util/math');
const { Midi } = require('@tonejs/midi')
const lines = [];
const settings = {
dimensions: 'A4',
orientation: 'landscape',
pixelsPerInch: 300,
scaleToView: true,
units: 'cm',
bleed: 0.25
};
// function to generate a random number between min and max
const random = (min, max) => Math.random() * (max - min) + min;
const sketch = async context => {
const midiFile = await Midi.fromUrl('./midi/PeacePiece.mid')
const {
durationTicks: trackDurationTicks,
duration: trackDuration,
tracks
} = midiFile;
return ({ bleed, context, width, height, units }) => {
context.fillStyle = 'white';
context.fillRect(0, 0, width, height);
const track = midiFile.tracks[0];
const notes = track.notes;
for (let i = 0; i < notes.length; i += 1) {
// for (let i = 0; i < 5; i += 1) {
const { name, durationTicks, midi, ticks } = notes[i]
const w = math.mapRange(durationTicks, 0, trackDurationTicks, 0, width - bleed);
const x = math.mapRange(ticks, 0, trackDurationTicks, bleed, width - bleed);
const y = math.mapRange(midi, 0, 127, height - bleed, bleed);
drawSquare({
context,
cx: x,
cy: y,
width: w,
height: 0.15,
})
}
return [
context.canvas,
{
data: pathsToSVG(lines, {
width,
height,
units
}),
extension: '.svg'
}
];
};
// rotate [x,y] around the center [cx, cy] with angle in degrees
// and y-axis moving downward
// draw a square in a single line
// and rotate it if needed
function drawSquare({context, cx, cy, width, height}) {
// calculate rotated coordinates
let xy1 = [cx, cy];
let xy2 = [cx + width, cy];
let xy3 = [cx + width, cy + height];
let xy4 = [cx, cy + height];
context.beginPath();
context.strokeStyle = 'black';
context.lineWidth = 0.02;
context.lineCap = 'square';
context.lineJoin = 'miter';
// draw square on context
context.moveTo(...xy1);
context.lineTo(...xy2);
context.lineTo(...xy3);
context.lineTo(...xy4);
context.lineTo(...xy1);
// context.rect(cx, cy, width, height)
context.stroke();
// draw square for svg polylines
lines.push([xy1, xy2]);
lines.push([xy2, xy3]);
lines.push([xy3, xy4]);
lines.push([xy4, xy1]);
}
};
canvasSketch(sketch, settings);