From 936576e7d973751aa3f0fef4b56fa8d00db37888 Mon Sep 17 00:00:00 2001 From: meeki007 <5952964+meeki007@users.noreply.github.com> Date: Wed, 16 Dec 2020 08:56:45 -0500 Subject: [PATCH] Add labelLocation options to node --- utility/annotate-image/annotate.js | 45 ++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/utility/annotate-image/annotate.js b/utility/annotate-image/annotate.js index 2b1b35e6..99a288ac 100644 --- a/utility/annotate-image/annotate.js +++ b/utility/annotate-image/annotate.js @@ -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 => {