Skip to content

Commit

Permalink
const and let
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaut(Teebo) committed Oct 11, 2024
1 parent e9b8d84 commit 28659ee
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 65 deletions.
10 changes: 4 additions & 6 deletions src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import { IRect } from './types';
import type { Node } from './Node';

function simplifyArray(arr: Array<any>) {
let retArr: Array<any> = [],
const retArr: Array<any> = [],
len = arr.length,
util = Util,
n,
val;
util = Util;

for (n = 0; n < len; n++) {
val = arr[n];
for (let n = 0; n < len; n++) {
let val = arr[n];
if (util._isNumber(val)) {
val = Math.round(val * 1000) / 1000;
} else if (!util._isString(val)) {
Expand Down
15 changes: 6 additions & 9 deletions src/Factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ export const Factory = {
validator?: Function,
after?: Function
) {
let len = components.length,
const len = components.length,
capitalize = Util._capitalize,
getter = GET + capitalize(attr),
setter = SET + capitalize(attr),
n,
component;
setter = SET + capitalize(attr);

// getter
constructor.prototype[getter] = function () {
const ret = {};

for (n = 0; n < len; n++) {
component = components[n];
for (let n = 0; n < len; n++) {
const component = components[n];
ret[component] = this.getAttr(attr + capitalize(component));
}

Expand All @@ -75,8 +73,7 @@ export const Factory = {

// setter
constructor.prototype[setter] = function (val) {
let oldVal = this.attrs[attr],
key;
const oldVal = this.attrs[attr];

if (validator) {
val = validator.call(this, val);
Expand All @@ -86,7 +83,7 @@ export const Factory = {
basicValidator.call(this, val, attr);
}

for (key in val) {
for (const key in val) {
if (!val.hasOwnProperty(key)) {
continue;
}
Expand Down
22 changes: 8 additions & 14 deletions src/Shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,31 +722,25 @@ export class Shape<
* shape.drawHitFromCache();
*/
drawHitFromCache(alphaThreshold = 0) {
let cachedCanvas = this._getCanvasCache(),
const cachedCanvas = this._getCanvasCache(),
sceneCanvas = this._getCachedSceneCanvas(),
hitCanvas = cachedCanvas.hit,
hitContext = hitCanvas.getContext(),
hitWidth = hitCanvas.getWidth(),
hitHeight = hitCanvas.getHeight(),
hitImageData,
hitData,
len,
rgbColorKey,
i,
alpha;
hitHeight = hitCanvas.getHeight();

hitContext.clear();
hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);

try {
hitImageData = hitContext.getImageData(0, 0, hitWidth, hitHeight);
hitData = hitImageData.data;
len = hitData.length;
rgbColorKey = Util._hexToRgb(this.colorKey);
const hitImageData = hitContext.getImageData(0, 0, hitWidth, hitHeight);
const hitData = hitImageData.data;
const len = hitData.length;
const rgbColorKey = Util._hexToRgb(this.colorKey);

// replace non transparent pixels with color key
for (i = 0; i < len; i += 4) {
alpha = hitData[i + 3];
for (let i = 0; i < len; i += 4) {
const alpha = hitData[i + 3];
if (alpha > alphaThreshold) {
hitData[i] = rgbColorKey.r;
hitData[i + 1] = rgbColorKey.g;
Expand Down
11 changes: 6 additions & 5 deletions src/filters/Blur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,20 @@ function filterGaussBlurRGBA(imageData, radius) {
pa,
rbs;

let div = radius + radius + 1,
const div = radius + radius + 1,
widthMinus1 = width - 1,
heightMinus1 = height - 1,
radiusPlus1 = radius + 1,
sumFactor = (radiusPlus1 * (radiusPlus1 + 1)) / 2,
stackStart = new BlurStack(),
stackEnd = null,
stack = stackStart,
stackIn: any = null,
stackOut: any = null,
mul_sum = mul_table[radius],
shg_sum = shg_table[radius];

let stackEnd = null,
stack = stackStart,
stackIn: any = null,
stackOut: any = null;

for (i = 1; i < div; i++) {
stack = stack.next = new BlurStack();
if (i === radiusPlus1) {
Expand Down
7 changes: 3 additions & 4 deletions src/filters/Brighten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import { getNumberValidator } from '../Validators';
* node.brightness(0.8);
*/
export const Brighten: Filter = function (imageData) {
let brightness = this.brightness() * 255,
const brightness = this.brightness() * 255,
data = imageData.data,
len = data.length,
i;
len = data.length;

for (i = 0; i < len; i += 4) {
for (let i = 0; i < len; i += 4) {
// red
data[i] += brightness;
// green
Expand Down
11 changes: 5 additions & 6 deletions src/filters/Contrast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import { getNumberValidator } from '../Validators';
export const Contrast: Filter = function (imageData) {
const adjust = Math.pow((this.contrast() + 100) / 100, 2);

let data = imageData.data,
nPixels = data.length,
red = 150,
const data = imageData.data,
nPixels = data.length;
let red = 150,
green = 150,
blue = 150,
i;
blue = 150;

for (i = 0; i < nPixels; i += 4) {
for (let i = 0; i < nPixels; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
Expand Down
8 changes: 4 additions & 4 deletions src/filters/Emboss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// a max value of greyLevel yields a white emboss, and the min value yields a black
// emboss. Therefore, I changed greyLevel to whiteLevel
let strength = this.embossStrength() * 10,
const strength = this.embossStrength() * 10,
greyLevel = this.embossWhiteLevel() * 255,
direction = this.embossDirection(),
blend = this.embossBlend(),
dirY = 0,
dirX = 0,
data = imageData.data,
w = imageData.width,
h = imageData.height,
w4 = w * 4,
w4 = w * 4;
let dirY = 0,
dirX = 0,
y = h;

switch (direction) {
Expand Down
20 changes: 9 additions & 11 deletions src/filters/Enhance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { getNumberValidator } from '../Validators';

function remap(fromValue: number, fromMin: number, fromMax: number, toMin: number, toMax: number) {
// Compute the range of the data
let fromRange = fromMax - fromMin,
toRange = toMax - toMin,
toValue;
const fromRange = fromMax - fromMin,
toRange = toMax - toMin;

// If either range is 0, then the value can only be mapped to 1 value
if (fromRange === 0) {
Expand All @@ -17,7 +16,7 @@ function remap(fromValue: number, fromMin: number, fromMax: number, toMin: numbe
}

// (1) untranslate, (2) unscale, (3) rescale, (4) retranslate
toValue = (fromValue - fromMin) / fromRange;
let toValue = (fromValue - fromMin) / fromRange;
toValue = toRange * toValue + toMin;

return toValue;
Expand All @@ -38,18 +37,17 @@ function remap(fromValue: number, fromMin: number, fromMax: number, toMin: numbe
* node.enhance(0.4);
*/
export const Enhance: Filter = function (imageData) {
let data = imageData.data,
nSubPixels = data.length,
rMin = data[0],
const data = imageData.data,
nSubPixels = data.length;
let rMin = data[0],
rMax = rMin,
r,
gMin = data[1],
gMax = gMin,
g,
bMin = data[2],
bMax = bMin,
b,
i;
b;

// If we are not enhancing anything - don't do any computation
const enhanceAmount = this.enhance();
Expand All @@ -58,7 +56,7 @@ export const Enhance: Filter = function (imageData) {
}

// 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];
if (r < rMin) {
rMin = r;
Expand Down Expand Up @@ -128,7 +126,7 @@ export const Enhance: Filter = function (imageData) {
}

// 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 + 1] = remap(data[i + 1], gMin, gMax, gGoalMin, gGoalMax);
data[i + 2] = remap(data[i + 2], bMin, bMax, bGoalMin, bGoalMax);
Expand Down
10 changes: 4 additions & 6 deletions src/filters/Grayscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import { Filter } from '../Node';
* node.filters([Konva.Filters.Grayscale]);
*/
export const Grayscale: Filter = function (imageData) {
let data = imageData.data,
len = data.length,
i,
brightness;
const data = imageData.data,
len = data.length;

for (i = 0; i < len; i += 4) {
brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
for (let i = 0; i < len; i += 4) {
const brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
// red
data[i] = brightness;
// green
Expand Down

0 comments on commit 28659ee

Please sign in to comment.