1
1
import {
2
- DrawingCanvas ,
3
- RenderedImage ,
4
2
createCanvas ,
5
3
noteBlockImage ,
6
4
saveToImage ,
5
+ useFont ,
7
6
} from './canvasFactory' ;
8
- import { NoteQuadTree } from '../song/notes' ;
7
+ import { Canvas , DrawParams } from './types' ;
8
+ import { getKeyText , instrumentColors , isDarkColor , tintImage } from './utils' ;
9
9
10
10
export { bgColorsArray } from './colors' ;
11
+ useFont ( ) ;
11
12
12
- interface DrawParams {
13
- notes : NoteQuadTree ;
14
- startTick : number ;
15
- startLayer : number ;
16
- zoomLevel : number ;
17
- backgroundColor : string ;
18
- canvasWidth ?: number ;
19
- canvasHeight ?: number ;
20
- imgWidth : number ;
21
- imgHeight : number ;
22
- }
23
-
24
- type Canvas = typeof DrawingCanvas ;
25
- type Image = typeof RenderedImage ;
26
-
27
- const instrumentColors = [
28
- '#1964ac' ,
29
- '#3c8e48' ,
30
- '#be6b6b' ,
31
- '#bebe19' ,
32
- '#9d5a98' ,
33
- '#572b21' ,
34
- '#bec65c' ,
35
- '#be19be' ,
36
- '#52908d' ,
37
- '#bebebe' ,
38
- '#1991be' ,
39
- '#be2328' ,
40
- '#be5728' ,
41
- '#19be19' ,
42
- '#be1957' ,
43
- '#575757' ,
44
- ] ;
45
-
46
- const tintedImages : Record < string , Canvas > = { } ;
47
-
48
- // Function to apply tint to an image
49
- function tintImage ( image : Image , color : string ) : Canvas {
50
- if ( tintedImages [ color ] ) {
51
- return tintedImages [ color ] ;
52
- }
53
-
54
- const canvas = createCanvas ( image . width , image . height ) ;
55
- const ctx = canvas . getContext ( '2d' ) ;
56
-
57
- if ( ! ctx ) {
58
- throw new Error ( 'Could not get canvas context' ) ;
59
- }
60
-
61
- // Fill background with the color
62
- ctx . fillStyle = color ;
63
- ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
64
-
65
- // Apply the note block texture to the color
66
- ctx . globalCompositeOperation = 'hard-light' ;
67
- ctx . globalAlpha = 0.67 ;
68
- ctx . drawImage ( image , 0 , 0 ) ;
69
-
70
- // Reset canvas settings
71
- ctx . globalCompositeOperation = 'source-over' ;
72
- ctx . globalAlpha = 1 ;
73
-
74
- tintedImages [ color ] = canvas ;
75
-
76
- return canvas ;
77
- }
78
-
79
- // Function to convert key number to key text
80
- function getKeyText ( key : number ) : string {
81
- const octaves = Math . floor ( ( key + 9 ) / 12 ) ;
82
-
83
- const notes = [
84
- 'C' ,
85
- 'C#' ,
86
- 'D' ,
87
- 'D#' ,
88
- 'E' ,
89
- 'F' ,
90
- 'F#' ,
91
- 'G' ,
92
- 'G#' ,
93
- 'A' ,
94
- 'A#' ,
95
- 'B' ,
96
- ] ;
97
-
98
- const note = notes [ ( key + 9 ) % 12 ] ;
99
-
100
- return `${ note } ${ octaves } ` ;
101
- }
102
-
103
- function getLuma ( color : string ) : number {
104
- // source: https://stackoverflow.com/a/12043228/9045426
105
-
106
- const c = color ?. substring ( 1 ) || '' ; // strip #
107
- const rgb = parseInt ( c , 16 ) ; // convert rrggbb to decimal
108
- const r = ( rgb >> 16 ) & 0xff ; // extract red
109
- const g = ( rgb >> 8 ) & 0xff ; // extract green
110
- const b = ( rgb >> 0 ) & 0xff ; // extract blue
111
-
112
- const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b ; // per ITU-R BT.709
113
-
114
- return luma ;
115
- }
116
-
117
- function isDarkColor ( color : string , threshold = 40 ) : boolean {
118
- return getLuma ( color ) < threshold ;
119
- }
120
-
121
- export async function swap ( src : Canvas , dst : Canvas ) {
13
+ export const swap = async ( src : Canvas , dst : Canvas ) => {
122
14
/**
123
15
* Run a `drawFunction` that returns a canvas and draw it to the passed `canvas`.
124
16
*
@@ -127,7 +19,6 @@ export async function swap(src: Canvas, dst: Canvas) {
127
19
*
128
20
* @returns Nothing
129
21
*/
130
-
131
22
// Get canvas context
132
23
const ctx = dst . getContext ( '2d' ) ;
133
24
@@ -137,9 +28,9 @@ export async function swap(src: Canvas, dst: Canvas) {
137
28
138
29
// Swap the canvas
139
30
ctx . drawImage ( src , 0 , 0 ) ;
140
- }
31
+ } ;
141
32
142
- export async function drawNotesOffscreen ( {
33
+ export const drawNotesOffscreen = async ( {
143
34
notes,
144
35
startTick,
145
36
startLayer,
@@ -149,7 +40,7 @@ export async function drawNotesOffscreen({
149
40
//canvasHeight,
150
41
imgWidth = 1280 ,
151
42
imgHeight = 768 ,
152
- } : DrawParams ) {
43
+ } : DrawParams ) => {
153
44
// Create new offscreen canvas
154
45
const canvas = createCanvas ( imgWidth , imgHeight ) ;
155
46
const ctx = canvas . getContext ( '2d' ) ;
@@ -235,9 +126,9 @@ export async function drawNotesOffscreen({
235
126
} ) ;
236
127
237
128
return canvas ;
238
- }
129
+ } ;
239
130
240
- export async function drawToImage ( params : DrawParams ) : Promise < Buffer > {
131
+ export const drawToImage = async ( params : DrawParams ) : Promise < Buffer > = > {
241
132
let canvas ;
242
133
const { imgWidth, imgHeight } = params ;
243
134
@@ -251,4 +142,4 @@ export async function drawToImage(params: DrawParams): Promise<Buffer> {
251
142
// Convert to Buffer
252
143
const buffer = Buffer . from ( byteArray ) ;
253
144
return buffer ;
254
- }
145
+ } ;
0 commit comments