cherry-box
Advanced tools
+15
| ISC License | ||
| Copyright (c) 2022, CheryX | ||
| Permission to use, copy, modify, and/or distribute this software for any | ||
| purpose with or without fee is hereby granted, provided that the above | ||
| copyright notice and this permission notice appear in all copies. | ||
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
+26
-29
@@ -1,37 +0,34 @@ | ||
| class coloredText { | ||
| /** | ||
| * | ||
| * @param {ctx} ctx Canvas context | ||
| * @param {string} text Text to be displayed | ||
| * @param {int} x X coordinate of the text box | ||
| * @param {int} y Y coordinate of the text box | ||
| * @param {string} font Font name of the text | ||
| */ | ||
| constructor(ctx, text, x, y, fontSize) { | ||
| /** | ||
| * | ||
| * @param {ctx} ctx Canvas context | ||
| * @param {string} text Text to be displayed | ||
| * @param {int} x X coordinate of the text box | ||
| * @param {int} y Y coordinate of the text box | ||
| * @param {string} font Font name of the text | ||
| */ | ||
| function paintText(ctx, text, x, y, fontSize) { | ||
| ctx.save(); | ||
| ctx.save(); | ||
| text.forEach(({ text, color, shadow, font, modifier }) => { | ||
| text.forEach(({ text, color, shadow, font, modifier }) => { | ||
| ctx.fillStyle = color; | ||
| ctx.font = `${modifier} ${fontSize}px ${font}`; | ||
| ctx.fillStyle = color; | ||
| ctx.font = `${modifier} ${fontSize}px ${font}`; | ||
| if (shadow != undefined) { | ||
| ctx.shadowColor = shadow.color; | ||
| ctx.shadowOffsetX = shadow.offset[0] * fontSize / 100; | ||
| ctx.shadowOffsetY = shadow.offset[1] * fontSize / 100; | ||
| ctx.shadowBlur = shadow.blur; | ||
| } else { | ||
| ctx.shadowColor = "transparent"; | ||
| } | ||
| ctx.fillText(text, x, y); | ||
| x += ctx.measureText(text).width; | ||
| }); | ||
| if (shadow) { | ||
| ctx.restore(); | ||
| } | ||
| ctx.shadowColor = shadow.color; | ||
| ctx.shadowOffsetX = shadow.offset[0] * fontSize / 100; | ||
| ctx.shadowOffsetY = shadow.offset[1] * fontSize / 100; | ||
| ctx.shadowBlur = shadow.blur; | ||
| } else ctx.shadowColor = "transparent"; | ||
| ctx.fillText(text, x, y); | ||
| x += ctx.measureText(text).width; | ||
| }); | ||
| ctx.restore(); | ||
| } | ||
| export default coloredText; | ||
| export default paintText; |
+2
-4
| import paintText from './paintText.js'; | ||
| import Canvas from 'canvas'; | ||
@@ -23,3 +22,3 @@ function getTotalWidth(ctx, text, fontSize) { | ||
| let metrics = ctx.measureText(wordsTotal); | ||
| const metrics = ctx.measureText(wordsTotal); | ||
| return metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent; | ||
@@ -60,4 +59,3 @@ | ||
| new paintText(ctx, text, x, y, fontSize); | ||
| paintText(ctx, text, x, y, fontSize); | ||
| ctx.restore() | ||
@@ -64,0 +62,0 @@ |
+1
-1
| { | ||
| "name": "cherry-box", | ||
| "version": "1.1.0", | ||
| "version": "1.1.1", | ||
| "description": "A node-canvas package filled with utilities, manage text boxes easily and more", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+6
-6
@@ -5,13 +5,13 @@ import { textBox } from "./lib/textBox.js"; | ||
| let canvas = new Canvas.createCanvas(800, 600); | ||
| let ctx = canvas.getContext("2d"); | ||
| const canvas = new Canvas.createCanvas(800, 600); | ||
| const ctx = canvas.getContext("2d"); | ||
| let upperText = [ | ||
| { | ||
| text: "test", | ||
| text: "This text is in the center of an image", | ||
| color: "white", | ||
| font: "Impact", | ||
| modifier: "bold", | ||
| font: "ubuntu", | ||
| shadow: { | ||
| color: "black", | ||
| color: "red", | ||
| offset: [0, 0], | ||
@@ -22,3 +22,3 @@ blur: 10 | ||
| ]; | ||
| textBox(ctx, 5, 10, canvas.width-10, 100, upperText, 200, ["top", "center"]); | ||
| textBox(ctx, 0, 0, canvas.width, canvas.height, upperText, 100, ["middle", "center"]) | ||
@@ -25,0 +25,0 @@ // Save canvas to file |
7685
8.3%8
14.29%94
-5.05%