Skip to content

Commit e0c7de8

Browse files
committed
0.4.0 release
1 parent 96057e0 commit e0c7de8

File tree

7 files changed

+137
-24
lines changed

7 files changed

+137
-24
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"consistent-this": "error",
8181
"func-names": "error",
8282
"func-style": ["error", "declaration", { "allowArrowFunctions": true }],
83-
"id-length": ["error", { "exceptions": ["i", "j", "a", "b", "c", "d", "e", "f","x", "y", "$"] }],
83+
"id-length": ["error", { "exceptions": ["i", "j", "a", "b", "c", "d", "e", "f","x", "y", "$", "n"] }],
8484
"indent": ["error", 4, { "SwitchCase": 1 }],
8585
"key-spacing": "error",
8686
"keyword-spacing": ["error", {

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## 0.4.0
8+
### Added
9+
- `addMultilineText()` to print texts that are too long to be displayed.
10+
711
## 0.3.1
812
### Added
913
- Both `printLinearGradient()` and `printRadialGradient()` to have chainable methods.

METHODS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| addText | `text`, `x`, `y`, `maxWidth`
1919
| addCircle | `x`, `y`, `radius`
2020
| addResponsiveText | `text`, `x`, `y`, `maxWidth`, `options`
21+
| addMultilineText | `text`, `x`, `y`, `maxWidth`, `lineHeight`
2122
| stroke | `path`
2223
| addStrokeRect | `x`, `y`, `width`, `height`
2324
| addStrokeText | `text`, `x`, `y`

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Please check [node-canvas readme](https://github.com/Automattic/node-canvas/blob
1313

1414
---
1515

16+
> **IMPORTANT** If you use `canvas` 2.0, you must install the 2.0 version of Canvas Constructor: `npm i kyranet/canvasConstructor`. Otherwise install the NPM version: `npm i canvas-constructor`.
17+
18+
---
19+
1620
How to use it:
1721

1822
```js

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "canvas-constructor",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "A ES6 class for node-canvas with built-in functions and chained methods.",
55
"main": "index.js",
66
"types": "typings/index.d.ts",
@@ -15,9 +15,9 @@
1515
"canvas": "github:Automattic/node-canvas"
1616
},
1717
"devDependencies": {
18-
"eslint": "^4.7.2",
19-
"tslint": "^5.7.0",
20-
"typescript": "^2.5.2"
18+
"eslint": "^4.13.1",
19+
"tslint": "^5.8.0",
20+
"typescript": "^2.6.2"
2121
},
2222
"keywords": [
2323
"node-canvas",

src/canvas.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,40 @@ class CanvasConstructor {
212212
.addText(text, x, y);
213213
}
214214

215+
/**
216+
* Add responsive text
217+
* @param {string} text The text to write.
218+
* @param {number} x The position x to start drawing the element.
219+
* @param {number} y The position y to start drawing the element.
220+
* @param {number} maxWidth The max length in pixels for the text.
221+
* @param {number} lineHeight The line's height.
222+
* @returns {CanvasConstructor}
223+
* @chainable
224+
* @example
225+
* new Canvas(400, 300)
226+
* .setTextFont('25px Tahoma')
227+
* .addMultilineText('This is a really long text!', 139, 360, 156, 28)
228+
* .toBuffer();
229+
*/
230+
addMultilineText(text, x, y, maxWidth, lineHeight) {
231+
const words = text.split(' ');
232+
let line = '';
233+
234+
for (let n = 0; n < words.length; n++) {
235+
const testLine = `${line + words[n]} `;
236+
const metrics = this.measureText(testLine);
237+
const testWidth = metrics.width;
238+
if (testWidth > maxWidth && n > 0) {
239+
this.addText(line, x, y);
240+
line = `${words[n]} `;
241+
y += lineHeight;
242+
} else {
243+
line = testLine;
244+
}
245+
}
246+
return this.addText(line, x, y);
247+
}
248+
215249
/**
216250
* Strokes the current or given path with the current stroke style using the non-zero winding rule.
217251
* @param {any} path A Path2D path to stroke.
@@ -396,18 +430,21 @@ class CanvasConstructor {
396430
* @param {Object} options Options.
397431
* @param {number} options.radius The radius for the new image.
398432
* @param {'round'|'bevel'} options.type The type for the new image.
433+
* @param {boolean} options.restore Whether this method should restore the drawing state. Use this when you use options.type
399434
* @returns {CanvasConstructor}
400435
* @chainable
401436
*/
402437
addImage(buffer, x, y, width, height, options = {}) {
438+
if (options.restore) this.save();
403439
if (options.type) {
404440
if (isNaN(options.radius)) options.radius = 10;
405441
if (options.type === 'round') this.createRoundClip(x + options.radius, y + options.radius, options.radius);
406-
if (options.type === 'bevel') this.createBeveledClip(x, y, width, height, options.radius);
442+
else if (options.type === 'bevel') this.createBeveledClip(x, y, width, height, options.radius);
407443
}
408444
const image = new Canvas.Image();
409445
image.onload = () => this.context.drawImage(image, x, y, width, height);
410446
image.src = buffer;
447+
if (options.restore) this.restore();
411448
return this;
412449
}
413450

typings/index.d.ts

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare module 'canvas-constructor' {
1010
public changeCanvasWidth(width: number): this;
1111
public changeCanvasHeigth(heigth: number): this;
1212
public save(): this;
13+
public restore(): this;
1314
public rotate(angle: number): this;
1415
public scale(x: number, y: number): this;
1516
public traslate(x: number, y: number): this;
@@ -20,6 +21,7 @@ declare module 'canvas-constructor' {
2021
public fill(path: any, fillRule?: fillRuleType): this;
2122
public addText(text: string, x: number, y: number, maxWidth?: number): this;
2223
public addResponsiveText(text: string, x: number, y: number, maxWidth: number): this;
24+
public addMultilineText(text: string, x: number, y: number, maxWidth: number, lineHeight: number): this;
2325
public stroke(path?: any): this;
2426
public addStrokeRect(x: number, y: number, width: number, height: number): this;
2527
public addStrokeText(text: string, x: number, y: number): this;
@@ -80,32 +82,97 @@ declare module 'canvas-constructor' {
8082
public toBufferAsync(): Promise<Buffer>;
8183

8284
public static getCanvas(): NodeCanvas;
83-
public static registerFont(path: string, family: string);
84-
public static registerFont(path: string, family: fontFaceType);
85+
public static registerFont(path: string, family: string | fontFaceType);
8586

8687
}
8788

88-
export type CanvasType = 'pdf'|'svg';
89-
export type fillRuleType = 'nonzero'|'evenodd';
90-
export type lineJoinValue = 'bevel'|'round'|'miter';
91-
export type lineCapValue = 'butt'|'round'|'square';
89+
export type CanvasType = 'pdf'
90+
| 'svg';
91+
export type fillRuleType = 'nonzero'
92+
| 'evenodd';
93+
export type lineCapValue = 'butt'
94+
| 'round'
95+
| 'square';
96+
export type lineJoinValue = 'bevel'
97+
| 'round'
98+
| 'miter';
9299
export type addImageOptions = {
93-
radius: number;
94-
type: roundType;
100+
radius?: number;
101+
type?: roundType;
102+
restore?: boolean;
95103
};
96-
export type roundType = 'round'|'bevel';
97-
export type textAlignType = 'left'|'center'|'right'|'start'|'end';
98-
export type textBaselineType = 'top'|'hanging'|'middle'|'alphabetic'|'ideographic'|'bottom';
99-
export type patternRepetition = 'repeat'|'repeat-x'|'repeat-y'|'no-repeat';
100-
export type patternQuality = 'fast'|'good'|'best'|'nearest'|'bilinear';
101-
export type textDrawingMode = 'path'|'glyph';
102-
export type antialiasType = 'default'|'none'|'gray'|'subpixel';
103-
export type globalCompositeOperation = 'source-over'|'source-in'|'source-out'|'source-atop'|'destination-over'|'destination-in'|'destination-out'|'destination-atop'|'lighter'|'copy'|'xor'|'darken'|'lighten'|'color-dodge'|'color-burn'|'difference'|'exclusion'|'hue'|'saturation'|'color'|'luminosity'|'multiply'|'screen'|'overlay'|'hard-light'|'soft-light'|'hsl-hue'|'hsl-saturation'|'hsl-color'|'hsl-luminosity';
104+
105+
export type antialiasType = 'default'
106+
| 'none'
107+
| 'gray'
108+
| 'subpixel';
109+
110+
export type patternQuality = 'fast'
111+
| 'good'
112+
| 'best'
113+
| 'nearest'
114+
| 'bilinear';
115+
116+
export type patternRepetition = 'repeat'
117+
| 'repeat-x'
118+
| 'repeat-y'
119+
| 'no-repeat';
120+
121+
export type roundType = 'round'
122+
| 'bevel';
123+
124+
export type textAlignType = 'left'
125+
| 'center'
126+
| 'right'
127+
| 'start'
128+
| 'end';
129+
130+
export type textBaselineType = 'alphabetic'
131+
| 'bottom'
132+
| 'hanging'
133+
| 'ideographic'
134+
| 'middle'
135+
| 'top';
136+
137+
export type textDrawingMode = 'path'
138+
| 'glyph';
139+
140+
export type globalCompositeOperation = 'color-burn'
141+
| 'color-dodge'
142+
| 'color'
143+
| 'copy'
144+
| 'darken'
145+
| 'destination-atop'
146+
| 'destination-in'
147+
| 'destination-out'
148+
| 'destination-over'
149+
| 'difference'
150+
| 'exclusion'
151+
| 'hard-light'
152+
| 'hsl-color'
153+
| 'hsl-hue'
154+
| 'hsl-luminosity'
155+
| 'hsl-saturation'
156+
| 'hue'
157+
| 'lighten'
158+
| 'lighter'
159+
| 'luminosity'
160+
| 'multiply'
161+
| 'overlay'
162+
| 'saturation'
163+
| 'screen'
164+
| 'soft-light'
165+
| 'source-atop'
166+
| 'source-in'
167+
| 'source-out'
168+
| 'source-over'
169+
| 'xor';
170+
104171
export type fontFaceType = {
105172
family: string;
106-
weight?: string;
107173
style?: string;
108-
}
174+
weight?: string;
175+
};
109176

110177
export type GradientStep = {
111178
position: number;

0 commit comments

Comments
 (0)