Merge pull request #1838 from tbo47/refactor-cont-let
Some checks failed
Test Browser / build (16.x) (push) Has been cancelled
Test NodeJS / build (16.x) (push) Has been cancelled

refactor const let var
This commit is contained in:
Anton Lavrenov 2024-10-28 13:41:22 -05:00 committed by GitHub
commit e47d54889c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 49 additions and 65 deletions

View File

@ -6,14 +6,12 @@ import { IRect } from './types';
import type { Node } from './Node'; import type { Node } from './Node';
function simplifyArray(arr: Array<any>) { function simplifyArray(arr: Array<any>) {
let retArr: Array<any> = [], const retArr: Array<any> = [],
len = arr.length, len = arr.length,
util = Util, util = Util;
n,
val;
for (n = 0; n < len; n++) { for (let n = 0; n < len; n++) {
val = arr[n]; let val = arr[n];
if (util._isNumber(val)) { if (util._isNumber(val)) {
val = Math.round(val * 1000) / 1000; val = Math.round(val * 1000) / 1000;
} else if (!util._isString(val)) { } else if (!util._isString(val)) {

View File

@ -52,19 +52,17 @@ export const Factory = {
validator?: Function, validator?: Function,
after?: Function after?: Function
) { ) {
let len = components.length, const len = components.length,
capitalize = Util._capitalize, capitalize = Util._capitalize,
getter = GET + capitalize(attr), getter = GET + capitalize(attr),
setter = SET + capitalize(attr), setter = SET + capitalize(attr);
n,
component;
// getter // getter
constructor.prototype[getter] = function () { constructor.prototype[getter] = function () {
const ret = {}; const ret = {};
for (n = 0; n < len; n++) { for (let n = 0; n < len; n++) {
component = components[n]; const component = components[n];
ret[component] = this.getAttr(attr + capitalize(component)); ret[component] = this.getAttr(attr + capitalize(component));
} }
@ -75,8 +73,7 @@ export const Factory = {
// setter // setter
constructor.prototype[setter] = function (val) { constructor.prototype[setter] = function (val) {
let oldVal = this.attrs[attr], const oldVal = this.attrs[attr];
key;
if (validator) { if (validator) {
val = validator.call(this, val); val = validator.call(this, val);
@ -86,7 +83,7 @@ export const Factory = {
basicValidator.call(this, val, attr); basicValidator.call(this, val, attr);
} }
for (key in val) { for (const key in val) {
if (!val.hasOwnProperty(key)) { if (!val.hasOwnProperty(key)) {
continue; continue;
} }

View File

@ -722,31 +722,25 @@ export class Shape<
* shape.drawHitFromCache(); * shape.drawHitFromCache();
*/ */
drawHitFromCache(alphaThreshold = 0) { drawHitFromCache(alphaThreshold = 0) {
let cachedCanvas = this._getCanvasCache(), const cachedCanvas = this._getCanvasCache(),
sceneCanvas = this._getCachedSceneCanvas(), sceneCanvas = this._getCachedSceneCanvas(),
hitCanvas = cachedCanvas.hit, hitCanvas = cachedCanvas.hit,
hitContext = hitCanvas.getContext(), hitContext = hitCanvas.getContext(),
hitWidth = hitCanvas.getWidth(), hitWidth = hitCanvas.getWidth(),
hitHeight = hitCanvas.getHeight(), hitHeight = hitCanvas.getHeight();
hitImageData,
hitData,
len,
rgbColorKey,
i,
alpha;
hitContext.clear(); hitContext.clear();
hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight); hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);
try { try {
hitImageData = hitContext.getImageData(0, 0, hitWidth, hitHeight); const hitImageData = hitContext.getImageData(0, 0, hitWidth, hitHeight);
hitData = hitImageData.data; const hitData = hitImageData.data;
len = hitData.length; const len = hitData.length;
rgbColorKey = Util._hexToRgb(this.colorKey); const rgbColorKey = Util._hexToRgb(this.colorKey);
// replace non transparent pixels with color key // replace non transparent pixels with color key
for (i = 0; i < len; i += 4) { for (let i = 0; i < len; i += 4) {
alpha = hitData[i + 3]; const alpha = hitData[i + 3];
if (alpha > alphaThreshold) { if (alpha > alphaThreshold) {
hitData[i] = rgbColorKey.r; hitData[i] = rgbColorKey.r;
hitData[i + 1] = rgbColorKey.g; hitData[i + 1] = rgbColorKey.g;

View File

@ -121,19 +121,20 @@ function filterGaussBlurRGBA(imageData, radius) {
pa, pa,
rbs; rbs;
let div = radius + radius + 1, const div = radius + radius + 1,
widthMinus1 = width - 1, widthMinus1 = width - 1,
heightMinus1 = height - 1, heightMinus1 = height - 1,
radiusPlus1 = radius + 1, radiusPlus1 = radius + 1,
sumFactor = (radiusPlus1 * (radiusPlus1 + 1)) / 2, sumFactor = (radiusPlus1 * (radiusPlus1 + 1)) / 2,
stackStart = new BlurStack(), stackStart = new BlurStack(),
stackEnd = null,
stack = stackStart,
stackIn: any = null,
stackOut: any = null,
mul_sum = mul_table[radius], mul_sum = mul_table[radius],
shg_sum = shg_table[radius]; shg_sum = shg_table[radius];
let stackEnd = null,
stack = stackStart,
stackIn: any = null,
stackOut: any = null;
for (i = 1; i < div; i++) { for (i = 1; i < div; i++) {
stack = stack.next = new BlurStack(); stack = stack.next = new BlurStack();
if (i === radiusPlus1) { if (i === radiusPlus1) {

View File

@ -13,12 +13,11 @@ import { getNumberValidator } from '../Validators';
* node.brightness(0.8); * node.brightness(0.8);
*/ */
export const Brighten: Filter = function (imageData) { export const Brighten: Filter = function (imageData) {
let brightness = this.brightness() * 255, const brightness = this.brightness() * 255,
data = imageData.data, 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] += brightness; data[i] += brightness;
// green // green

View File

@ -15,14 +15,13 @@ import { getNumberValidator } from '../Validators';
export const Contrast: Filter = function (imageData) { export const Contrast: Filter = function (imageData) {
const adjust = Math.pow((this.contrast() + 100) / 100, 2); const adjust = Math.pow((this.contrast() + 100) / 100, 2);
let data = imageData.data, const data = imageData.data,
nPixels = data.length, nPixels = data.length;
red = 150, let red = 150,
green = 150, green = 150,
blue = 150, blue = 150;
i;
for (i = 0; i < nPixels; i += 4) { for (let i = 0; i < nPixels; i += 4) {
red = data[i]; red = data[i];
green = data[i + 1]; green = data[i + 1];
blue = data[i + 2]; blue = data[i + 2];

View File

@ -23,16 +23,16 @@ export const Emboss: Filter = function (imageData) {
// pixastic greyLevel is between 0 and 255. I want it between 0 and 1. Also, // pixastic greyLevel is between 0 and 255. I want it between 0 and 1. Also,
// a max value of greyLevel yields a white emboss, and the min value yields a black // a max value of greyLevel yields a white emboss, and the min value yields a black
// emboss. Therefore, I changed greyLevel to whiteLevel // emboss. Therefore, I changed greyLevel to whiteLevel
let strength = this.embossStrength() * 10, const strength = this.embossStrength() * 10,
greyLevel = this.embossWhiteLevel() * 255, greyLevel = this.embossWhiteLevel() * 255,
direction = this.embossDirection(), direction = this.embossDirection(),
blend = this.embossBlend(), blend = this.embossBlend(),
dirY = 0,
dirX = 0,
data = imageData.data, data = imageData.data,
w = imageData.width, w = imageData.width,
h = imageData.height, h = imageData.height,
w4 = w * 4, w4 = w * 4;
let dirY = 0,
dirX = 0,
y = h; y = h;
switch (direction) { switch (direction) {

View File

@ -4,9 +4,8 @@ import { getNumberValidator } from '../Validators';
function remap(fromValue: number, fromMin: number, fromMax: number, toMin: number, toMax: number) { function remap(fromValue: number, fromMin: number, fromMax: number, toMin: number, toMax: number) {
// Compute the range of the data // Compute the range of the data
let fromRange = fromMax - fromMin, const fromRange = fromMax - fromMin,
toRange = toMax - toMin, toRange = toMax - toMin;
toValue;
// If either range is 0, then the value can only be mapped to 1 value // If either range is 0, then the value can only be mapped to 1 value
if (fromRange === 0) { if (fromRange === 0) {
@ -17,7 +16,7 @@ function remap(fromValue: number, fromMin: number, fromMax: number, toMin: numbe
} }
// (1) untranslate, (2) unscale, (3) rescale, (4) retranslate // (1) untranslate, (2) unscale, (3) rescale, (4) retranslate
toValue = (fromValue - fromMin) / fromRange; let toValue = (fromValue - fromMin) / fromRange;
toValue = toRange * toValue + toMin; toValue = toRange * toValue + toMin;
return toValue; return toValue;
@ -38,9 +37,9 @@ function remap(fromValue: number, fromMin: number, fromMax: number, toMin: numbe
* node.enhance(0.4); * node.enhance(0.4);
*/ */
export const Enhance: Filter = function (imageData) { export const Enhance: Filter = function (imageData) {
let data = imageData.data, const data = imageData.data,
nSubPixels = data.length, nSubPixels = data.length;
rMin = data[0], let rMin = data[0],
rMax = rMin, rMax = rMin,
r, r,
gMin = data[1], gMin = data[1],
@ -48,8 +47,7 @@ export const Enhance: Filter = function (imageData) {
g, g,
bMin = data[2], bMin = data[2],
bMax = bMin, bMax = bMin,
b, b;
i;
// If we are not enhancing anything - don't do any computation // If we are not enhancing anything - don't do any computation
const enhanceAmount = this.enhance(); const enhanceAmount = this.enhance();
@ -58,7 +56,7 @@ export const Enhance: Filter = function (imageData) {
} }
// 1st Pass - find the min and max for each channel: // 1st Pass - find the min and max for each channel:
for (i = 0; i < nSubPixels; i += 4) { for (let i = 0; i < nSubPixels; i += 4) {
r = data[i + 0]; r = data[i + 0];
if (r < rMin) { if (r < rMin) {
rMin = r; rMin = r;
@ -128,7 +126,7 @@ export const Enhance: Filter = function (imageData) {
} }
// Pass 2 - remap everything, except the alpha // Pass 2 - remap everything, except the alpha
for (i = 0; i < nSubPixels; i += 4) { for (let i = 0; i < nSubPixels; i += 4) {
data[i + 0] = remap(data[i + 0], rMin, rMax, rGoalMin, rGoalMax); data[i + 0] = remap(data[i + 0], rMin, rMax, rGoalMin, rGoalMax);
data[i + 1] = remap(data[i + 1], gMin, gMax, gGoalMin, gGoalMax); data[i + 1] = remap(data[i + 1], gMin, gMax, gGoalMin, gGoalMax);
data[i + 2] = remap(data[i + 2], bMin, bMax, bGoalMin, bGoalMax); data[i + 2] = remap(data[i + 2], bMin, bMax, bGoalMin, bGoalMax);

View File

@ -10,13 +10,11 @@ import { Filter } from '../Node';
* node.filters([Konva.Filters.Grayscale]); * node.filters([Konva.Filters.Grayscale]);
*/ */
export const Grayscale: Filter = function (imageData) { export const Grayscale: Filter = function (imageData) {
let data = imageData.data, const data = imageData.data,
len = data.length, len = data.length;
i,
brightness;
for (i = 0; i < len; i += 4) { for (let i = 0; i < len; i += 4) {
brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2]; const brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
// red // red
data[i] = brightness; data[i] = brightness;
// green // green