
export default function drawText(ctx, text, x, y, align='left top', font_size = 28, color = '#000') {
    return new Promise(function(resolve, reject) {
        let r_x
        let r_y
        let { width } = ctx.measureText(text)

        if (align.indexOf('center') > -1) {
            ctx.textAlign = 'center';
            r_x = x - width / 2
        } else if (align.indexOf('right') > -1) {
            ctx.textAlign = 'end';
            r_x = x - width
        } else {
            ctx.textAlign = 'start';
            r_x = x
        }
        if (align.indexOf('middle') > -1) {
            ctx.textBaseline = 'middle';
            r_y = y - font_size / 2
        } else if (align.indexOf('bottom') > -1) {
            ctx.textBaseline = 'bottom';
            r_y = y - font_size
        } else {
            ctx.textBaseline = 'top';
            r_y = y
        } 
        ctx.font = font_size + "px Arial"
        ctx.fillStyle = color
        ctx.fillText(text, x, y)
        resolve({
            startX: r_x,
            startY: r_y,
            endX: r_x + width,
            endY: r_y + font_size,
            width: width,
            height: font_size
        })
    })
}