graphics-wrapper
Advanced tools
Comparing version 1.1.1 to 1.2.0
76
index.js
@@ -214,2 +214,3 @@ const Canvas = require("canvas"); | ||
* @param {string} font | ||
* @param {string} emphasis | ||
* @param {Number} size | ||
@@ -224,6 +225,6 @@ * @param {string} color | ||
*/ | ||
module.exports.drawTextFill = function(text, font, size, color, x, y, align, baseline, w, h) { | ||
module.exports.drawTextFill = function(text, font, emphasis, size, color, x, y, align, baseline, w, h) { | ||
if (!align) align = "left"; | ||
if (!baseline) baseline = "middle"; | ||
ctx.font = `${size}px ${font}`; | ||
ctx.font = `${emphasis}${emphasis.length > 0 ? " " : ""}${size}px ${font}`; | ||
ctx.fillStyle = color; | ||
@@ -234,3 +235,3 @@ ctx.textAlign = align; | ||
while (ctx.measureText(text).width >= w || ctx.measureText(text).actualBoundingBoxAscent + ctx.measureText(text).actualBoundingBoxDescent >= h) | ||
ctx.font = `${size-=1}px ${font}`; | ||
ctx.font = `${emphasis}${emphasis.length > 0 ? " " : ""}${size-=1}px ${font}`; | ||
ctx.fillText(text, x, y); | ||
@@ -243,2 +244,3 @@ }; | ||
* @param {string} font | ||
* @param {string} emphasis | ||
* @param {Number} size | ||
@@ -254,6 +256,6 @@ * @param {string} color | ||
*/ | ||
module.exports.drawTextStroke = function(text, font, size, color, x, y, stroke, align, baseline, w, h) { | ||
module.exports.drawTextStroke = function(text, font, emphasis, size, color, x, y, stroke, align, baseline, w, h) { | ||
if (!align) align = "left"; | ||
if (!baseline) baseline = "middle"; | ||
ctx.font = `${size}px ${font}`; | ||
ctx.font = `${emphasis}${emphasis.length > 0 ? " " : ""}${size}px ${font}`; | ||
ctx.fillStyle = color; | ||
@@ -264,3 +266,3 @@ ctx.textAlign = align; | ||
while (ctx.measureText(text).width >= w || ctx.measureText(text).actualBoundingBoxAscent + ctx.measureText(text).actualBoundingBoxDescent >= h) | ||
ctx.font = `${size-=1}px ${font}`; | ||
ctx.font = `${emphasis}${emphasis.length > 0 ? " " : ""}${size-=1}px ${font}`; | ||
ctx.strokeWidth = w; | ||
@@ -270,2 +272,64 @@ ctx.strokeText(text, x, y); | ||
/** | ||
* Returns text with newlines to fit specified width. | ||
* @param {string} text | ||
* @param {string} font | ||
* @param {string} emphasis | ||
* @param {Number} size | ||
* @param {Number} w | ||
*/ | ||
module.exports.measureTextBox = function(text, font, emphasis, size, w) { | ||
ctx.font = `${emphasis}${emphasis.length > 0 ? " " : ""}${size}px ${font}`; | ||
const words = text.split(" "); | ||
let count = 1; | ||
text = ""; | ||
while (words.length > 0) { | ||
if (count === words.length) break; | ||
while (ctx.measureText(words.slice(0,count+1)).width < w && count+1 <= words.length) { | ||
count++; | ||
} | ||
text += words.slice(0,count).join(" ") + "\n"; | ||
while (count > 0) { | ||
words.shift(); | ||
count--; | ||
} | ||
count++; | ||
} | ||
text += words.slice(0,count).join(" "); | ||
return text; | ||
}; | ||
/** | ||
* Draws filled text fit to specified width. | ||
* @param {string} text | ||
* @param {string} font | ||
* @param {string} emphasis | ||
* @param {Number} size | ||
* @param {Number} w | ||
* @param {string} color | ||
* @param {Number} x | ||
* @param {Number} y | ||
*/ | ||
module.exports.drawTextBoxFill = function(text, font, emphasis, size, w, color, x, y) { | ||
text = module.exports.measureTextBox(text, font, emphasis, size, w); | ||
module.exports.drawTextFill(text, font, emphasis, size, color, x, y); | ||
}; | ||
/** | ||
* Draws text outline fit to specified width. | ||
* @param {string} text | ||
* @param {string} font | ||
* @param {string} emphasis | ||
* @param {Number} size | ||
* @param {Number} w | ||
* @param {string} color | ||
* @param {Number} x | ||
* @param {Number} y | ||
* @param {Number} stroke | ||
*/ | ||
module.exports.drawTextBoxStroke = function(text, font, emphasis, size, w, color, x, y, stroke) { | ||
text = module.exports.measureTextBox(text, font, emphasis, size, w); | ||
module.exports.drawTextStroke(text, font, emphasis, size, color, x, y, stroke); | ||
}; | ||
// IMAGE | ||
@@ -272,0 +336,0 @@ |
{ | ||
"name": "graphics-wrapper", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A wrapper for node-canvas", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,13 +8,13 @@ const fs = require('fs'); | ||
const ctx = canvas.getContext('2d'); | ||
Draw.setContext(ctx); | ||
Draw.drawRectFill('black', 0, 0, canvas.width, canvas.height); | ||
Draw.drawRoundedRectFill('white', 20, 20, canvas.width - 40, canvas.height - 40, 50); | ||
Draw.setShadow('black',7,6, 4); | ||
Draw.drawTextFill("graphics", "sans-serif", 80, 'blue', canvas.width/2, canvas.height/2-100, "center", "middle"); | ||
Draw.drawTextFill("wrapper", "sans-serif", 80, 'blue', canvas.width/2, canvas.height/2, "center", "middle"); | ||
Draw.drawTextFill("test", "sans-serif", 80, 'blue', canvas.width/2, canvas.height/2+100, "center", "middle"); | ||
Draw.drawTextFill("graphics", "sans-serif", "bold", 80, 'blue', canvas.width/2, canvas.height/2-100, "center", "middle"); | ||
Draw.drawTextFill("wrapper", "sans-serif", "bold", 80, 'blue', canvas.width/2, canvas.height/2, "center", "middle"); | ||
Draw.drawTextFill("test", "sans-serif", "bold", 80, 'blue', canvas.width/2, canvas.height/2+100, "center", "middle"); | ||
let buffer = canvas.toBuffer(); | ||
fs.writeFileSync("test.png", buffer); | ||
})(); |
@@ -8,3 +8,2 @@ const fs = require('fs'); | ||
const ctx = canvas.getContext('2d'); | ||
Draw.setContext(ctx); | ||
@@ -14,5 +13,5 @@ | ||
Draw.drawNineSlice(image, 50, 50, canvas.width - 100, canvas.height - 100, 150); | ||
let buffer = canvas.toBuffer(); | ||
fs.writeFileSync("testNineSlice.png", buffer); | ||
})(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13200
7
372
3