11import {
2- DrawingCanvas ,
3- RenderedImage ,
42 createCanvas ,
53 noteBlockImage ,
64 saveToImage ,
5+ useFont ,
76} from './canvasFactory' ;
8- import { NoteQuadTree } from '../song/notes' ;
7+ import { Canvas , DrawParams } from './types' ;
8+ import { getKeyText , instrumentColors , isDarkColor , tintImage } from './utils' ;
99
1010export { bgColorsArray } from './colors' ;
11+ useFont ( ) ;
1112
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 ) => {
12214 /**
12315 * Run a `drawFunction` that returns a canvas and draw it to the passed `canvas`.
12416 *
@@ -127,7 +19,6 @@ export async function swap(src: Canvas, dst: Canvas) {
12719 *
12820 * @returns Nothing
12921 */
130-
13122 // Get canvas context
13223 const ctx = dst . getContext ( '2d' ) ;
13324
@@ -137,9 +28,9 @@ export async function swap(src: Canvas, dst: Canvas) {
13728
13829 // Swap the canvas
13930 ctx . drawImage ( src , 0 , 0 ) ;
140- }
31+ } ;
14132
142- export async function drawNotesOffscreen ( {
33+ export const drawNotesOffscreen = async ( {
14334 notes,
14435 startTick,
14536 startLayer,
@@ -149,7 +40,7 @@ export async function drawNotesOffscreen({
14940 //canvasHeight,
15041 imgWidth = 1280 ,
15142 imgHeight = 768 ,
152- } : DrawParams ) {
43+ } : DrawParams ) => {
15344 // Create new offscreen canvas
15445 const canvas = createCanvas ( imgWidth , imgHeight ) ;
15546 const ctx = canvas . getContext ( '2d' ) ;
@@ -235,9 +126,9 @@ export async function drawNotesOffscreen({
235126 } ) ;
236127
237128 return canvas ;
238- }
129+ } ;
239130
240- export async function drawToImage ( params : DrawParams ) : Promise < Buffer > {
131+ export const drawToImage = async ( params : DrawParams ) : Promise < Buffer > = > {
241132 let canvas ;
242133 const { imgWidth, imgHeight } = params ;
243134
@@ -251,4 +142,4 @@ export async function drawToImage(params: DrawParams): Promise<Buffer> {
251142 // Convert to Buffer
252143 const buffer = Buffer . from ( byteArray ) ;
253144 return buffer ;
254- }
145+ } ;
0 commit comments