Add labelLocation options to node

This commit is contained in:
meeki007 2020-12-16 08:56:45 -05:00 committed by GitHub
parent b11c8c5b15
commit 936576e7d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,7 @@ module.exports = function(RED) {
const defaultStroke = n.stroke || "#ffC000";
const defaultLineWidth = parseInt(n.lineWidth) || 5;
const defaultFontSize = n.fontSize || 24;
const defaultFontColor = n.fontColor || "#ffC000"
const defaultFontColor = n.fontColor || "#ffC000";
loadFont();
this.on("input", function(msg) {
@ -83,7 +83,46 @@ module.exports = function(RED) {
ctx.fillStyle = annotation.fontColor || defaultFontColor;
ctx.textBaseline = "top";
ctx.textAlign = "left";
ctx.fillText(annotation.label, x+2,y)
//set offset value so txt is above or below image
if (annotation.labelLocation)
{
if (annotation.labelLocation === "top")
{
y = y - (20+((defaultLineWidth*0.5)+(Number(defaultFontSize))));
if (y < 0)
{
y = 0;
}
}
else if (annotation.labelLocation === "bottom")
{
y = y + (10+h+(((defaultLineWidth*0.5)+(Number(defaultFontSize)))));
ctx.textBaseline = "bottom";
}
}
//if not user defined make best guess for top or bottom based on location
else
{
//not enought room above imagebox, put label on the bottom
if (y < 0 + (20+((defaultLineWidth*0.5)+(Number(defaultFontSize)))))
{
y = y + (10+h+(((defaultLineWidth*0.5)+(Number(defaultFontSize)))));
ctx.textBaseline = "bottom";
}
//else put the label on the top
else
{
y = y - (20+((defaultLineWidth*0.5)+(Number(defaultFontSize))));
if (y < 0)
{
y = 0;
}
}
}
ctx.fillText(annotation.label, x,y);
}
break;
case 'circle':
@ -114,6 +153,8 @@ module.exports = function(RED) {
const bufferOutput = getWritableBuffer();
pureimage.encodeJPEGToStream(c,bufferOutput.stream,90).then(() => {
msg.payload = bufferOutput.getBuffer();
msg.defaultFontSize = defaultFontSize;
node.send(msg);
})
}).catch(err => {