Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphics-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphics-wrapper - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

test/testTextBox.js

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

2

package.json
{
"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);
})();
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc