Skip to content

Commit 27bfd7c

Browse files
authored
refactor: Cleaned up all code, hopefully fixed all filters (#342)
* refactor: Cleaned up all code, hopefully fixed all filters * fix: Resolved `darkness` and `convolute` bugs * fix: Change blur to do passes * fix: Added edge, fixed sharpen * feat: Make passes optional in `sharpen` and `blur` * fix: Make blur, sharpen, and edge, use opaque = true * build: Update typings
1 parent db85b53 commit 27bfd7c

File tree

10 files changed

+366
-289
lines changed

10 files changed

+366
-289
lines changed

.eslintrc.json

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
{
2-
"extends": "klasa",
2+
"extends": "bamboo",
33
"parserOptions": {
4-
"ecmaVersion": 2018
5-
},
6-
"globals": {
7-
"HTMLCanvasElement": false
8-
},
9-
"rules": {
10-
"max-len": "off",
11-
"operator-linebreak": ["error", "before", { "overrides": { "+": "after", "=": "after" } }],
12-
"curly": ["error", "multi-or-nest", "consistent"],
13-
"id-length": "off",
14-
"no-bitwise": ["error", { "int32Hint": true }],
15-
"no-cond-assign": "off"
4+
"project": "./tsconfig.eslint.json"
165
}
176
}

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"scripts": {
88
"prepublishOnly": "yarn run build:browser",
99
"build:browser": "webpack",
10-
"lint": "npx eslint --fix src && npx tslint --fix 'typings/*.ts'",
11-
"test": "npx eslint src && npx tslint 'typings/*.ts'",
12-
"docs": "npx dg --source src --custom guides/.docconfig.json --output docs/docs.json --logging"
10+
"lint": "eslint --ext js,ts src typings --fix",
11+
"test:lint": "eslint --ext js,ts src typings",
12+
"docs": "dg --source src --custom guides/.docconfig.json --output docs/docs.json --logging"
1313
},
1414
"repository": {
1515
"type": "git",
@@ -21,11 +21,12 @@
2121
},
2222
"devDependencies": {
2323
"@types/node": "^13.9.8",
24+
"@typescript-eslint/eslint-plugin": "^2.26.0",
25+
"@typescript-eslint/parser": "^2.26.0",
2426
"docgen": "github:dirigeants/docsgen",
2527
"eslint": "^6.8.0",
26-
"eslint-config-klasa": "github:dirigeants/klasa-lint",
28+
"eslint-config-bamboo": "^3.0.0",
2729
"terser-webpack-plugin": "^2.3.5",
28-
"tslint": "^6.1.0",
2930
"typescript": "^3.8.3",
3031
"webpack": "^4.42.1",
3132
"webpack-cli": "^3.3.11"

src/lib/canvas.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/restrict-plus-operands, @typescript-eslint/explicit-member-accessibility */
12
// @ts-nocheck
23
const { browser, getFontHeight, InternalCanvas, textWrap } = require('./util/util');
34

@@ -310,11 +311,11 @@ class Canvas {
310311
*/
311312
addResponsiveText(text, dx, dy, maxWidth) {
312313
const [, style = '', size, font] = /(\w+ )?(\d+)(.+)/.exec(this.context.font);
313-
const currentSize = parseInt(size);
314+
const currentSize = parseInt(size, 10);
314315
const { width } = this.measureText(text);
315316
const newLength = maxWidth > width ? currentSize : (maxWidth / width) * currentSize;
316317
return this
317-
.setTextFont(style + newLength + font)
318+
.setTextFont(`${style}${newLength}${font}`)
318319
.addText(text, dx, dy);
319320
}
320321

@@ -465,7 +466,7 @@ class Canvas {
465466
*/
466467
setTextSize(size) {
467468
const [, style = '', font] = /(\w+ )?(?:\d+)(.+)/.exec(this.context.font);
468-
return this.setTextFont(style + size + font);
469+
return this.setTextFont(`${style}${size}${font}`);
469470
}
470471

471472
/**
@@ -580,7 +581,7 @@ class Canvas {
580581
if (options.type === 'round') this.createRoundClip(dx + options.radius, dy + options.radius, options.radius);
581582
else if (options.type === 'bevel') this.createBeveledClip(dx, dy, width, height, options.radius);
582583
}
583-
this._resolveImage(imageOrBuffer, (image) => this.context.drawImage(image, ...args));
584+
this._resolveImage(imageOrBuffer, image => this.context.drawImage(image, ...args));
584585
if (options.restore) this.restore();
585586
return this;
586587
}
@@ -617,7 +618,7 @@ class Canvas {
617618
if (restore) this.save();
618619
const diameter = radius * 2;
619620
this.createRoundClip(dx, dy, radius);
620-
this._resolveImage(imageOrBuffer, (image) => this.context.drawImage(image, dx - radius, dy - radius, diameter, diameter));
621+
this._resolveImage(imageOrBuffer, image => this.context.drawImage(image, dx - radius, dy - radius, diameter, diameter));
621622
if (restore) this.restore();
622623
return this;
623624
}
@@ -647,7 +648,8 @@ class Canvas {
647648
* @chainable
648649
*/
649650
addCircle(dx, dy, radius) {
650-
return this.save().createRoundPath(dx, dy, radius).fill().restore();
651+
return this.save().createRoundPath(dx, dy, radius).fill()
652+
.restore();
651653
}
652654

653655
/**
@@ -703,7 +705,8 @@ class Canvas {
703705
* .toBuffer();
704706
*/
705707
addBeveledRect(...args) {
706-
return this.save().createBeveledPath(...args).fill().restore();
708+
return this.save().createBeveledPath(...args).fill()
709+
.restore();
707710
}
708711

709712
/**
@@ -926,7 +929,7 @@ class Canvas {
926929
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createPattern
927930
*/
928931
createPattern(imageOrBuffer, repetition, callback) {
929-
this._resolveImage(imageOrBuffer, (image) => callback(this.context.createPattern(image, repetition)));
932+
this._resolveImage(imageOrBuffer, image => callback(this.context.createPattern(image, repetition)));
930933
return this;
931934
}
932935

@@ -956,8 +959,9 @@ class Canvas {
956959
*/
957960
createLinearGradient(x0, y0, x1, y1, steps = []) {
958961
const gradient = this.context.createLinearGradient(x0, y0, x1, y1);
959-
for (let i = 0; i < steps.length; i++)
960-
gradient.addColorStop(steps[i].position, steps[i].color);
962+
for (const step of steps) {
963+
gradient.addColorStop(step.position, step.color);
964+
}
961965

962966
return gradient;
963967
}
@@ -1002,8 +1006,9 @@ class Canvas {
10021006
*/
10031007
createRadialGradient(x0, y0, r0, x1, y1, r1, steps = []) {
10041008
const gradient = this.context.createRadialGradient(x0, y0, r0, x1, y1, r1);
1005-
for (let i = 0; i < steps.length; i++)
1006-
gradient.addColorStop(steps[i].position, steps[i].color);
1009+
for (const step of steps) {
1010+
gradient.addColorStop(step.position, step.color);
1011+
}
10071012

10081013
return gradient;
10091014
}
@@ -1470,7 +1475,7 @@ class Canvas {
14701475
* @returns {Promise<Blob>}
14711476
*/
14721477
toBlobAsync(...args) {
1473-
return new Promise((resolve) => this.canvas.toBlob(resolve, ...args));
1478+
return new Promise(resolve => this.canvas.toBlob(resolve, ...args));
14741479
}
14751480

14761481
/**
@@ -1570,10 +1575,13 @@ class Canvas {
15701575
* @returns {Canvas}
15711576
*/
15721577
static registerFont(path, family) {
1573-
if (typeof InternalCanvas.registerFont !== 'function')
1578+
if (typeof InternalCanvas.registerFont !== 'function') {
15741579
throw new Error('registerFont is not supported in this version of node-canvas, please install node-canvas 2.x.');
1575-
if (!family)
1580+
}
1581+
1582+
if (!family) {
15761583
throw new TypeError('A family must be specified for registerFont.');
1584+
}
15771585

15781586
InternalCanvas.registerFont(path, family.constructor === Object ? family : { family });
15791587
return Canvas;

0 commit comments

Comments
 (0)