Skip to content

Commit

Permalink
Temporarily use manually defined types
Browse files Browse the repository at this point in the history
* rename setTextBlock method to setText
* Prepare 1.0.3 release
  • Loading branch information
blakewilson committed Apr 7, 2021
1 parent 70ddde6 commit 449ffd7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ canvas.width = 1000;
canvas.height = 1000;
// Init the package.
const canvasTextBlock = new CanvasTextBlock(
const textBlock = new CanvasTextBlock(
canvas, // An HTML Canvas Element
100, // The x position of the canvas where the text block should start
100, // The y position of the canvas where the text block should start
Expand All @@ -64,7 +64,7 @@ const canvasTextBlock = new CanvasTextBlock(
);
// Set the text
canvasTextBlock.setTextBlock(
textBlock.setText(
"This text block will be rendered in the region specified in the constructor above"
);
```
Expand Down
2 changes: 1 addition & 1 deletion __tests__/nodeCanvasTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ test("it should work with Node Canvas", () => {
const canvas = createCanvas(1000, 1000);

const instance = new CreateTextBlock(canvas, 100, 100, 800, 800);
instance.setTextBlock("This is the text block message");
instance.setText("This is the text block message");
});
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "canvas-text-block",
"version": "1.0.2",
"version": "1.0.3",
"description": "Easily render a wrapping block of text in an HTML Canvas Element.",
"main": "dist/CanvasTextBlock.common.js",
"module": "dist/CanvasTextBlock.esm.js",
"browser": "dist/CanvasTextBlock.js",
"types": "dist/CanvasTextBlock.d.ts",
"types": "types/CanvasTextBlock.d.ts",
"homepage": "https://github.com/blakewilson/canvas-text-block",
"files": [
"src",
"dist",
"LICENSE",
"README.md"
"README.md",
"types"
],
"scripts": {
"test": "jest",
Expand Down
18 changes: 3 additions & 15 deletions src/CanvasTextBlock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CanvasTextBlockOptions } from "./types";
import type { CanvasTextBlockOptions } from "./types";
import type {
Canvas as NodeCanvas,
CanvasRenderingContext2D as NodeCanvasRenderingContext2D,
Expand All @@ -23,7 +23,7 @@ class CanvasTextBlock {
private options: Required<CanvasTextBlockOptions>;

constructor(
canvas: any,
canvas: HTMLCanvasElement | NodeCanvas,
x: number,
y: number,
width: number,
Expand Down Expand Up @@ -75,23 +75,14 @@ class CanvasTextBlock {
}
}

/**
* Get the max width of the text block.
*/
private getTextBlockMaxWidth = () => {
return this.width;
};

/**
* Get the options used for the canvas text block
*/
private getOptions = () => {
return this.options;
};

/**
* Get the max amount of lines possible in the text block.
*/
private getMaxLineCount = (): number => {
return Math.floor(this.height / this.options.lineHeight);
};
Expand All @@ -103,10 +94,7 @@ class CanvasTextBlock {
}
};

/**
* Set the text in the text block region.
*/
setTextBlock = (text: string) => {
setText = (text: string) => {
const lines = this.getLinesFromText(text);

// Loop through each line and render then to the canvas.
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"moduleResolution": "node",
"strictNullChecks": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true
"declaration": false
},
"include": ["src"],
"exclude": ["__tests__"]
}
44 changes: 44 additions & 0 deletions types/CanvasTextBlock.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Canvas as NodeCanvas } from "canvas";

export = CanvasTextBlock;

declare class CanvasTextBlock {
/**
* Create a text block instance
*
* @param canvas The canvas element
* @param x The x-axis position in pixels where the text block should start
* @param y The y-axis position in pixels where the text block should start
* @param width The width of the text block in pixels
* @param height The height of the text block in pixels
* @param options
*/
constructor(
canvas: HTMLCanvasElement | NodeCanvas,
x: number,
y: number,
width: number,
height: number,
options: CanvasTextBlock.CanvasTextBlockOptions
);

/**
* Set the text of the text block instance.
*
* @param text
*/
setText(text: string): void;
}

declare namespace CanvasTextBlock {
export interface CanvasTextBlockOptions {
color?: string;
backgroundColor?: string;
fontFamily?: string;
fontSize?: number;
lineHeight?: number;
weight?: string;
ellipsis?: boolean;
overflow?: boolean;
}
}

0 comments on commit 449ffd7

Please sign in to comment.