Skip to content

Recommended way to position drawing within canvas? #2

@jordan-loeser

Description

@jordan-loeser

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
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions