mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 08:22:44 +08:00
Merge pull request #1848 from tbo47/eslint-recommended
Eslint recommended
This commit is contained in:
commit
be24e9bb8b
3
eslint.config.mjs
Normal file
3
eslint.config.mjs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import tseslint from 'typescript-eslint';
|
||||||
|
|
||||||
|
export default tseslint.config(...tseslint.configs.recommended);
|
18
src/Stage.ts
18
src/Stage.ts
@ -249,11 +249,10 @@ export class Stage extends Container<Layer> {
|
|||||||
* @name Konva.Stage#clear
|
* @name Konva.Stage#clear
|
||||||
*/
|
*/
|
||||||
clear() {
|
clear() {
|
||||||
let layers = this.children,
|
const layers = this.children,
|
||||||
len = layers.length,
|
len = layers.length;
|
||||||
n;
|
|
||||||
|
|
||||||
for (n = 0; n < len; n++) {
|
for (let n = 0; n < len; n++) {
|
||||||
layers[n].clear();
|
layers[n].clear();
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -367,12 +366,11 @@ export class Stage extends Container<Layer> {
|
|||||||
if (!pos) {
|
if (!pos) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
let layers = this.children,
|
const layers = this.children,
|
||||||
len = layers.length,
|
len = layers.length,
|
||||||
end = len - 1,
|
end = len - 1;
|
||||||
n;
|
|
||||||
|
|
||||||
for (n = end; n >= 0; n--) {
|
for (let n = end; n >= 0; n--) {
|
||||||
const shape = layers[n].getIntersection(pos);
|
const shape = layers[n].getIntersection(pos);
|
||||||
if (shape) {
|
if (shape) {
|
||||||
return shape;
|
return shape;
|
||||||
@ -820,8 +818,8 @@ export class Stage extends Container<Layer> {
|
|||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
setPointersPositions(evt) {
|
setPointersPositions(evt) {
|
||||||
let contentPosition = this._getContentPosition(),
|
const contentPosition = this._getContentPosition();
|
||||||
x: number | null = null,
|
let x: number | null = null,
|
||||||
y: number | null = null;
|
y: number | null = null;
|
||||||
evt = evt ? evt : window.event;
|
evt = evt ? evt : window.event;
|
||||||
|
|
||||||
|
40
src/Tween.ts
40
src/Tween.ts
@ -4,7 +4,7 @@ import { Node, NodeConfig } from './Node';
|
|||||||
import { Konva } from './Global';
|
import { Konva } from './Global';
|
||||||
import { Line } from './shapes/Line';
|
import { Line } from './shapes/Line';
|
||||||
|
|
||||||
let blacklist = {
|
const blacklist = {
|
||||||
node: 1,
|
node: 1,
|
||||||
duration: 1,
|
duration: 1,
|
||||||
easing: 1,
|
easing: 1,
|
||||||
@ -14,8 +14,8 @@ let blacklist = {
|
|||||||
PAUSED = 1,
|
PAUSED = 1,
|
||||||
PLAYING = 2,
|
PLAYING = 2,
|
||||||
REVERSING = 3,
|
REVERSING = 3,
|
||||||
idCounter = 0,
|
|
||||||
colorAttrs = ['fill', 'stroke', 'shadowColor'];
|
colorAttrs = ['fill', 'stroke', 'shadowColor'];
|
||||||
|
let idCounter = 0;
|
||||||
|
|
||||||
class TweenEngine {
|
class TweenEngine {
|
||||||
prop: string;
|
prop: string;
|
||||||
@ -195,12 +195,12 @@ export class Tween {
|
|||||||
onUpdate: Function | undefined;
|
onUpdate: Function | undefined;
|
||||||
|
|
||||||
constructor(config: TweenConfig) {
|
constructor(config: TweenConfig) {
|
||||||
let that = this,
|
const that = this,
|
||||||
node = config.node as any,
|
node = config.node as any,
|
||||||
nodeId = node._id,
|
nodeId = node._id,
|
||||||
duration,
|
|
||||||
easing = config.easing || Easings.Linear,
|
easing = config.easing || Easings.Linear,
|
||||||
yoyo = !!config.yoyo,
|
yoyo = !!config.yoyo;
|
||||||
|
let duration,
|
||||||
key;
|
key;
|
||||||
|
|
||||||
if (typeof config.duration === 'undefined') {
|
if (typeof config.duration === 'undefined') {
|
||||||
@ -266,26 +266,23 @@ export class Tween {
|
|||||||
this.onUpdate = config.onUpdate;
|
this.onUpdate = config.onUpdate;
|
||||||
}
|
}
|
||||||
_addAttr(key, end) {
|
_addAttr(key, end) {
|
||||||
let node = this.node,
|
const node = this.node,
|
||||||
nodeId = node._id,
|
nodeId = node._id;
|
||||||
start,
|
let diff,
|
||||||
diff,
|
|
||||||
tweenId,
|
|
||||||
n,
|
|
||||||
len,
|
len,
|
||||||
trueEnd,
|
trueEnd,
|
||||||
trueStart,
|
trueStart,
|
||||||
endRGBA;
|
endRGBA;
|
||||||
|
|
||||||
// remove conflict from tween map if it exists
|
// remove conflict from tween map if it exists
|
||||||
tweenId = Tween.tweens[nodeId][key];
|
const tweenId = Tween.tweens[nodeId][key];
|
||||||
|
|
||||||
if (tweenId) {
|
if (tweenId) {
|
||||||
delete Tween.attrs[nodeId][tweenId][key];
|
delete Tween.attrs[nodeId][tweenId][key];
|
||||||
}
|
}
|
||||||
|
|
||||||
// add to tween map
|
// add to tween map
|
||||||
start = node.getAttr(key);
|
let start = node.getAttr(key);
|
||||||
|
|
||||||
if (Util._isArray(end)) {
|
if (Util._isArray(end)) {
|
||||||
diff = [];
|
diff = [];
|
||||||
@ -310,7 +307,7 @@ export class Tween {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (key.indexOf('fill') === 0) {
|
if (key.indexOf('fill') === 0) {
|
||||||
for (n = 0; n < len; n++) {
|
for (let n = 0; n < len; n++) {
|
||||||
if (n % 2 === 0) {
|
if (n % 2 === 0) {
|
||||||
diff.push(end[n] - start[n]);
|
diff.push(end[n] - start[n]);
|
||||||
} else {
|
} else {
|
||||||
@ -326,7 +323,7 @@ export class Tween {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (n = 0; n < len; n++) {
|
for (let n = 0; n < len; n++) {
|
||||||
diff.push(end[n] - start[n]);
|
diff.push(end[n] - start[n]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -353,9 +350,9 @@ export class Tween {
|
|||||||
Tween.tweens[nodeId][key] = this._id;
|
Tween.tweens[nodeId][key] = this._id;
|
||||||
}
|
}
|
||||||
_tweenFunc(i) {
|
_tweenFunc(i) {
|
||||||
let node = this.node,
|
const node = this.node,
|
||||||
attrs = Tween.attrs[node._id][this._id],
|
attrs = Tween.attrs[node._id][this._id];
|
||||||
key,
|
let key,
|
||||||
attr,
|
attr,
|
||||||
start,
|
start,
|
||||||
diff,
|
diff,
|
||||||
@ -525,14 +522,13 @@ export class Tween {
|
|||||||
* @name Konva.Tween#destroy
|
* @name Konva.Tween#destroy
|
||||||
*/
|
*/
|
||||||
destroy() {
|
destroy() {
|
||||||
let nodeId = this.node._id,
|
const nodeId = this.node._id,
|
||||||
thisId = this._id,
|
thisId = this._id,
|
||||||
attrs = Tween.tweens[nodeId],
|
attrs = Tween.tweens[nodeId];
|
||||||
key;
|
|
||||||
|
|
||||||
this.pause();
|
this.pause();
|
||||||
|
|
||||||
for (key in attrs) {
|
for (const key in attrs) {
|
||||||
delete Tween.tweens[nodeId][key];
|
delete Tween.tweens[nodeId][key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
src/Util.ts
13
src/Util.ts
@ -260,7 +260,7 @@ export class Transform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CONSTANTS
|
// CONSTANTS
|
||||||
let OBJECT_ARRAY = '[object Array]',
|
const OBJECT_ARRAY = '[object Array]',
|
||||||
OBJECT_NUMBER = '[object Number]',
|
OBJECT_NUMBER = '[object Number]',
|
||||||
OBJECT_STRING = '[object String]',
|
OBJECT_STRING = '[object String]',
|
||||||
OBJECT_BOOLEAN = '[object Boolean]',
|
OBJECT_BOOLEAN = '[object Boolean]',
|
||||||
@ -423,8 +423,8 @@ let OBJECT_ARRAY = '[object Array]',
|
|||||||
yellow: [255, 255, 0],
|
yellow: [255, 255, 0],
|
||||||
yellowgreen: [154, 205, 5],
|
yellowgreen: [154, 205, 5],
|
||||||
},
|
},
|
||||||
RGB_REGEX = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/,
|
RGB_REGEX = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
|
||||||
animQueue: Array<Function> = [];
|
let animQueue: Array<Function> = [];
|
||||||
|
|
||||||
const req =
|
const req =
|
||||||
(typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame) ||
|
(typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame) ||
|
||||||
@ -904,21 +904,20 @@ export const Util = {
|
|||||||
return pc;
|
return pc;
|
||||||
},
|
},
|
||||||
_prepareArrayForTween(startArray, endArray, isClosed) {
|
_prepareArrayForTween(startArray, endArray, isClosed) {
|
||||||
let n,
|
const start: Vector2d[] = [],
|
||||||
start: Vector2d[] = [],
|
|
||||||
end: Vector2d[] = [];
|
end: Vector2d[] = [];
|
||||||
if (startArray.length > endArray.length) {
|
if (startArray.length > endArray.length) {
|
||||||
const temp = endArray;
|
const temp = endArray;
|
||||||
endArray = startArray;
|
endArray = startArray;
|
||||||
startArray = temp;
|
startArray = temp;
|
||||||
}
|
}
|
||||||
for (n = 0; n < startArray.length; n += 2) {
|
for (let n = 0; n < startArray.length; n += 2) {
|
||||||
start.push({
|
start.push({
|
||||||
x: startArray[n],
|
x: startArray[n],
|
||||||
y: startArray[n + 1],
|
y: startArray[n + 1],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
for (n = 0; n < endArray.length; n += 2) {
|
for (let n = 0; n < endArray.length; n += 2) {
|
||||||
end.push({
|
end.push({
|
||||||
x: endArray[n],
|
x: endArray[n],
|
||||||
y: endArray[n + 1],
|
y: endArray[n + 1],
|
||||||
|
@ -59,13 +59,12 @@ Factory.addGetterSetter(
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export const HSL: Filter = function (imageData) {
|
export const HSL: Filter = function (imageData) {
|
||||||
let data = imageData.data,
|
const data = imageData.data,
|
||||||
nPixels = data.length,
|
nPixels = data.length,
|
||||||
v = 1,
|
v = 1,
|
||||||
s = Math.pow(2, this.saturation()),
|
s = Math.pow(2, this.saturation()),
|
||||||
h = Math.abs(this.hue() + 360) % 360,
|
h = Math.abs(this.hue() + 360) % 360,
|
||||||
l = this.luminance() * 127,
|
l = this.luminance() * 127;
|
||||||
i;
|
|
||||||
|
|
||||||
// Basis for the technique used:
|
// Basis for the technique used:
|
||||||
// http://beesbuzz.biz/code/hsv_color_transforms.php
|
// http://beesbuzz.biz/code/hsv_color_transforms.php
|
||||||
@ -94,7 +93,7 @@ export const HSL: Filter = function (imageData) {
|
|||||||
|
|
||||||
let r: number, g: number, b: number, a: number;
|
let r: number, g: number, b: number, a: number;
|
||||||
|
|
||||||
for (i = 0; i < nPixels; i += 4) {
|
for (let i = 0; i < nPixels; i += 4) {
|
||||||
r = data[i + 0];
|
r = data[i + 0];
|
||||||
g = data[i + 1];
|
g = data[i + 1];
|
||||||
b = data[i + 2];
|
b = data[i + 2];
|
||||||
|
@ -9,11 +9,10 @@ import { Filter } from '../Node';
|
|||||||
* node.filters([Konva.Filters.Invert]);
|
* node.filters([Konva.Filters.Invert]);
|
||||||
*/
|
*/
|
||||||
export const Invert: Filter = function (imageData) {
|
export const Invert: Filter = function (imageData) {
|
||||||
let data = imageData.data,
|
const data = imageData.data,
|
||||||
len = data.length,
|
len = data.length;
|
||||||
i;
|
|
||||||
|
|
||||||
for (i = 0; i < len; i += 4) {
|
for (let i = 0; i < len; i += 4) {
|
||||||
// red
|
// red
|
||||||
data[i] = 255 - data[i];
|
data[i] = 255 - data[i];
|
||||||
// green
|
// green
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Factory } from '../Factory';
|
import { Factory } from '../Factory';
|
||||||
import { Node, Filter } from '../Node';
|
import { Filter, Node } from '../Node';
|
||||||
import { Util } from '../Util';
|
import { Util } from '../Util';
|
||||||
import { getNumberValidator } from '../Validators';
|
import { getNumberValidator } from '../Validators';
|
||||||
|
|
||||||
@ -20,53 +20,41 @@ import { getNumberValidator } from '../Validators';
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const ToPolar = function (src, dst, opt) {
|
const ToPolar = function (src, dst, opt) {
|
||||||
let srcPixels = src.data,
|
const srcPixels = src.data,
|
||||||
dstPixels = dst.data,
|
dstPixels = dst.data,
|
||||||
xSize = src.width,
|
xSize = src.width,
|
||||||
ySize = src.height,
|
ySize = src.height,
|
||||||
xMid = opt.polarCenterX || xSize / 2,
|
xMid = opt.polarCenterX || xSize / 2,
|
||||||
yMid = opt.polarCenterY || ySize / 2,
|
yMid = opt.polarCenterY || ySize / 2;
|
||||||
i,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
r = 0,
|
|
||||||
g = 0,
|
|
||||||
b = 0,
|
|
||||||
a = 0;
|
|
||||||
|
|
||||||
// Find the largest radius
|
// Find the largest radius
|
||||||
let rad,
|
let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
|
||||||
rMax = Math.sqrt(xMid * xMid + yMid * yMid);
|
let x = xSize - xMid;
|
||||||
x = xSize - xMid;
|
let y = ySize - yMid;
|
||||||
y = ySize - yMid;
|
const rad = Math.sqrt(x * x + y * y);
|
||||||
rad = Math.sqrt(x * x + y * y);
|
|
||||||
rMax = rad > rMax ? rad : rMax;
|
rMax = rad > rMax ? rad : rMax;
|
||||||
|
|
||||||
// We'll be uisng y as the radius, and x as the angle (theta=t)
|
// We'll be uisng y as the radius, and x as the angle (theta=t)
|
||||||
let rSize = ySize,
|
const rSize = ySize,
|
||||||
tSize = xSize,
|
tSize = xSize;
|
||||||
radius,
|
|
||||||
theta;
|
|
||||||
|
|
||||||
// We want to cover all angles (0-360) and we need to convert to
|
// We want to cover all angles (0-360) and we need to convert to
|
||||||
// radians (*PI/180)
|
// radians (*PI/180)
|
||||||
let conversion = ((360 / tSize) * Math.PI) / 180,
|
const conversion = ((360 / tSize) * Math.PI) / 180;
|
||||||
sin,
|
|
||||||
cos;
|
|
||||||
|
|
||||||
// var x1, x2, x1i, x2i, y1, y2, y1i, y2i, scale;
|
// var x1, x2, x1i, x2i, y1, y2, y1i, y2i, scale;
|
||||||
|
|
||||||
for (theta = 0; theta < tSize; theta += 1) {
|
for (let theta = 0; theta < tSize; theta += 1) {
|
||||||
sin = Math.sin(theta * conversion);
|
const sin = Math.sin(theta * conversion);
|
||||||
cos = Math.cos(theta * conversion);
|
const cos = Math.cos(theta * conversion);
|
||||||
for (radius = 0; radius < rSize; radius += 1) {
|
for (let radius = 0; radius < rSize; radius += 1) {
|
||||||
x = Math.floor(xMid + ((rMax * radius) / rSize) * cos);
|
x = Math.floor(xMid + ((rMax * radius) / rSize) * cos);
|
||||||
y = Math.floor(yMid + ((rMax * radius) / rSize) * sin);
|
y = Math.floor(yMid + ((rMax * radius) / rSize) * sin);
|
||||||
i = (y * xSize + x) * 4;
|
let i = (y * xSize + x) * 4;
|
||||||
r = srcPixels[i + 0];
|
const r = srcPixels[i + 0];
|
||||||
g = srcPixels[i + 1];
|
const g = srcPixels[i + 1];
|
||||||
b = srcPixels[i + 2];
|
const b = srcPixels[i + 2];
|
||||||
a = srcPixels[i + 3];
|
const a = srcPixels[i + 3];
|
||||||
|
|
||||||
// Store it
|
// Store it
|
||||||
//i = (theta * xSize + radius) * 4;
|
//i = (theta * xSize + radius) * 4;
|
||||||
@ -97,35 +85,23 @@ const ToPolar = function (src, dst, opt) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const FromPolar = function (src, dst, opt) {
|
const FromPolar = function (src, dst, opt) {
|
||||||
let srcPixels = src.data,
|
const srcPixels = src.data,
|
||||||
dstPixels = dst.data,
|
dstPixels = dst.data,
|
||||||
xSize = src.width,
|
xSize = src.width,
|
||||||
ySize = src.height,
|
ySize = src.height,
|
||||||
xMid = opt.polarCenterX || xSize / 2,
|
xMid = opt.polarCenterX || xSize / 2,
|
||||||
yMid = opt.polarCenterY || ySize / 2,
|
yMid = opt.polarCenterY || ySize / 2;
|
||||||
i,
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
dx,
|
|
||||||
dy,
|
|
||||||
r = 0,
|
|
||||||
g = 0,
|
|
||||||
b = 0,
|
|
||||||
a = 0;
|
|
||||||
|
|
||||||
// Find the largest radius
|
// Find the largest radius
|
||||||
let rad,
|
let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
|
||||||
rMax = Math.sqrt(xMid * xMid + yMid * yMid);
|
let x = xSize - xMid;
|
||||||
x = xSize - xMid;
|
let y = ySize - yMid;
|
||||||
y = ySize - yMid;
|
const rad = Math.sqrt(x * x + y * y);
|
||||||
rad = Math.sqrt(x * x + y * y);
|
|
||||||
rMax = rad > rMax ? rad : rMax;
|
rMax = rad > rMax ? rad : rMax;
|
||||||
|
|
||||||
// We'll be uisng x as the radius, and y as the angle (theta=t)
|
// We'll be uisng x as the radius, and y as the angle (theta=t)
|
||||||
let rSize = ySize,
|
const rSize = ySize,
|
||||||
tSize = xSize,
|
tSize = xSize,
|
||||||
radius,
|
|
||||||
theta,
|
|
||||||
phaseShift = opt.polarRotation || 0;
|
phaseShift = opt.polarRotation || 0;
|
||||||
|
|
||||||
// We need to convert to degrees and we need to make sure
|
// We need to convert to degrees and we need to make sure
|
||||||
@ -137,18 +113,18 @@ const FromPolar = function (src, dst, opt) {
|
|||||||
|
|
||||||
for (x = 0; x < xSize; x += 1) {
|
for (x = 0; x < xSize; x += 1) {
|
||||||
for (y = 0; y < ySize; y += 1) {
|
for (y = 0; y < ySize; y += 1) {
|
||||||
dx = x - xMid;
|
const dx = x - xMid;
|
||||||
dy = y - yMid;
|
const dy = y - yMid;
|
||||||
radius = (Math.sqrt(dx * dx + dy * dy) * rSize) / rMax;
|
const radius = (Math.sqrt(dx * dx + dy * dy) * rSize) / rMax;
|
||||||
theta = ((Math.atan2(dy, dx) * 180) / Math.PI + 360 + phaseShift) % 360;
|
let theta = ((Math.atan2(dy, dx) * 180) / Math.PI + 360 + phaseShift) % 360;
|
||||||
theta = (theta * tSize) / 360;
|
theta = (theta * tSize) / 360;
|
||||||
x1 = Math.floor(theta);
|
x1 = Math.floor(theta);
|
||||||
y1 = Math.floor(radius);
|
y1 = Math.floor(radius);
|
||||||
i = (y1 * xSize + x1) * 4;
|
let i = (y1 * xSize + x1) * 4;
|
||||||
r = srcPixels[i + 0];
|
const r = srcPixels[i + 0];
|
||||||
g = srcPixels[i + 1];
|
const g = srcPixels[i + 1];
|
||||||
b = srcPixels[i + 2];
|
const b = srcPixels[i + 2];
|
||||||
a = srcPixels[i + 3];
|
const a = srcPixels[i + 3];
|
||||||
|
|
||||||
// Store it
|
// Store it
|
||||||
i = (y * xSize + x) * 4;
|
i = (y * xSize + x) * 4;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Factory } from '../Factory';
|
import { Factory } from '../Factory';
|
||||||
import { Node, Filter } from '../Node';
|
import { Filter, Node } from '../Node';
|
||||||
import { getNumberValidator } from '../Validators';
|
import { getNumberValidator } from '../Validators';
|
||||||
|
|
||||||
function pixelAt(idata, x, y) {
|
function pixelAt(idata, x, y) {
|
||||||
@ -181,8 +181,8 @@ function smoothEdgeMask(mask, sw, sh) {
|
|||||||
*/
|
*/
|
||||||
export const Mask: Filter = function (imageData) {
|
export const Mask: Filter = function (imageData) {
|
||||||
// Detect pixels close to the background color
|
// Detect pixels close to the background color
|
||||||
let threshold = this.threshold(),
|
const threshold = this.threshold();
|
||||||
mask = backgroundMask(imageData, threshold);
|
let mask = backgroundMask(imageData, threshold);
|
||||||
if (mask) {
|
if (mask) {
|
||||||
// Erode
|
// Erode
|
||||||
mask = erodeMask(mask, imageData.width, imageData.height);
|
mask = erodeMask(mask, imageData.width, imageData.height);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Factory } from '../Factory';
|
import { Factory } from '../Factory';
|
||||||
import { Node, Filter } from '../Node';
|
import { Filter, Node } from '../Node';
|
||||||
import { getNumberValidator } from '../Validators';
|
import { getNumberValidator } from '../Validators';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Factory } from '../Factory';
|
import { Factory } from '../Factory';
|
||||||
import { Node, Filter } from '../Node';
|
import { Filter, Node } from '../Node';
|
||||||
import { getNumberValidator } from '../Validators';
|
import { getNumberValidator } from '../Validators';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Posterize Filter. Adjusts the channels so that there are no more
|
* Posterize Filter. Adjusts the channels so that there are no more
|
||||||
* than n different values for that channel. This is also applied
|
* than n different values for that channel. This is also applied
|
||||||
@ -15,16 +16,14 @@ import { getNumberValidator } from '../Validators';
|
|||||||
* node.filters([Konva.Filters.Posterize]);
|
* node.filters([Konva.Filters.Posterize]);
|
||||||
* node.levels(0.8); // between 0 and 1
|
* node.levels(0.8); // between 0 and 1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const Posterize: Filter = function (imageData) {
|
export const Posterize: Filter = function (imageData) {
|
||||||
// level must be between 1 and 255
|
// level must be between 1 and 255
|
||||||
let levels = Math.round(this.levels() * 254) + 1,
|
const levels = Math.round(this.levels() * 254) + 1,
|
||||||
data = imageData.data,
|
data = imageData.data,
|
||||||
len = data.length,
|
len = data.length,
|
||||||
scale = 255 / levels,
|
scale = 255 / levels;
|
||||||
i;
|
|
||||||
|
|
||||||
for (i = 0; i < len; i += 1) {
|
for (let i = 0; i < len; i += 1) {
|
||||||
data[i] = Math.floor(data[i] / scale) * scale;
|
data[i] = Math.floor(data[i] / scale) * scale;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -15,18 +15,15 @@ import { RGBComponent } from '../Validators';
|
|||||||
* node.blue(120);
|
* node.blue(120);
|
||||||
* node.green(200);
|
* node.green(200);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const RGB: Filter = function (imageData) {
|
export const RGB: Filter = function (imageData) {
|
||||||
let data = imageData.data,
|
const data = imageData.data,
|
||||||
nPixels = data.length,
|
nPixels = data.length,
|
||||||
red = this.red(),
|
red = this.red(),
|
||||||
green = this.green(),
|
green = this.green(),
|
||||||
blue = this.blue(),
|
blue = this.blue();
|
||||||
i,
|
|
||||||
brightness;
|
|
||||||
|
|
||||||
for (i = 0; i < nPixels; i += 4) {
|
for (let i = 0; i < nPixels; i += 4) {
|
||||||
brightness =
|
const brightness =
|
||||||
(0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2]) / 255;
|
(0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2]) / 255;
|
||||||
data[i] = brightness * red; // r
|
data[i] = brightness * red; // r
|
||||||
data[i + 1] = brightness * green; // g
|
data[i + 1] = brightness * green; // g
|
||||||
|
@ -12,17 +12,13 @@ import { Filter } from '../Node';
|
|||||||
* node.filters([Konva.Filters.Sepia]);
|
* node.filters([Konva.Filters.Sepia]);
|
||||||
*/
|
*/
|
||||||
export const Sepia: Filter = function (imageData) {
|
export const Sepia: Filter = function (imageData) {
|
||||||
let data = imageData.data,
|
const data = imageData.data,
|
||||||
nPixels = data.length,
|
nPixels = data.length;
|
||||||
i,
|
|
||||||
r,
|
|
||||||
g,
|
|
||||||
b;
|
|
||||||
|
|
||||||
for (i = 0; i < nPixels; i += 4) {
|
for (let i = 0; i < nPixels; i += 4) {
|
||||||
r = data[i + 0];
|
const r = data[i + 0];
|
||||||
g = data[i + 1];
|
const g = data[i + 1];
|
||||||
b = data[i + 2];
|
const b = data[i + 2];
|
||||||
|
|
||||||
data[i + 0] = Math.min(255, r * 0.393 + g * 0.769 + b * 0.189);
|
data[i + 0] = Math.min(255, r * 0.393 + g * 0.769 + b * 0.189);
|
||||||
data[i + 1] = Math.min(255, r * 0.349 + g * 0.686 + b * 0.168);
|
data[i + 1] = Math.min(255, r * 0.349 + g * 0.686 + b * 0.168);
|
||||||
|
Loading…
Reference in New Issue
Block a user