Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way to position drawing within canvas? #2

Open
jordan-loeser opened this issue Aug 13, 2023 · 1 comment
Open

Recommended way to position drawing within canvas? #2

jordan-loeser opened this issue Aug 13, 2023 · 1 comment

Comments

@jordan-loeser
Copy link

What is the recommended way to change the position where the bitmap is drawn on the canvas using draw2canvas()? I noticed that x and y are hardcoded to start at 0 in the function definition (below) and using context.moveTo() does not seem to have any effect.

bdfparser-js/src/bdfparser.ts

Lines 1796 to 1829 in 71a4ee3

/**
* Draw the bitmap to HTML canvas
*
* @param context - Canvas 2D context (`canvas.getContext("2d")`)
* @param pixelcolors - Object mapping `'0'`/`'1'`/`'2'` in the bitmap data to color
*
* @returns The `Bitmap` object itself
*
* @see online docs: {@link https://font.tomchen.org/bdfparser_js/bitmap#draw2canvas}
*/
draw2canvas(
context: CanvasContext,
pixelcolors?: Record<'0' | '1' | '2', string | null> | null
): this {
const _pixelcolors = pixelcolors ?? {
'0': null,
'1': 'black',
'2': 'red',
}
this.todata(2).forEach((line, y) => {
line.forEach((pixel, x) => {
const s = pixel.toString()
if (s === '0' || s === '1' || s === '2') {
const color = _pixelcolors[s]
if (color !== null && color !== undefined) {
context.fillStyle = color
context.fillRect(x, y, 1, 1)
}
}
})
})
return this
}
}

@jordan-loeser
Copy link
Author

I created a PR #3 that might be one way of doing this internally without using transformations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant