Skip to content

Commit f279e55

Browse files
committed
refactor(util): extract math functions and epsilons
1 parent 93d315f commit f279e55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+372
-338
lines changed

src/Handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Storage from './Storage';
1212
import Element, {ElementEvent} from './Element';
1313
import CanvasPainter from './canvas/Painter';
1414
import BoundingRect from './core/BoundingRect';
15+
import { PI, PI2, mathCos, mathSin } from './core/math';
1516

1617
/**
1718
* [The interface between `Handler` and `HandlerProxy`]:
@@ -382,12 +383,11 @@ class Handler extends Eventful {
382383
*/
383384
if (candidates.length) {
384385
const rStep = 4;
385-
const thetaStep = Math.PI / 12;
386-
const PI2 = Math.PI * 2;
386+
const thetaStep = PI / 12;
387387
for (let r = 0; r < targetSizeHalf; r += rStep) {
388388
for (let theta = 0; theta < PI2; theta += thetaStep) {
389-
const x1 = x + r * Math.cos(theta);
390-
const y1 = y + r * Math.sin(theta);
389+
const x1 = x + r * mathCos(theta);
390+
const y1 = y + r * mathSin(theta);
391391
setHoverTarget(candidates, out, x1, y1, exclude);
392392
if (out.target) {
393393
return out;

src/animation/Animator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import easingFuncs, { AnimationEasing } from './easing';
2121
import Animation from './Animation';
2222
import { createCubicEasingFunc } from './cubicEasing';
2323
import { isLinearGradient, isRadialGradient } from '../svg/helper';
24+
import { mathFloor, mathMax, mathMin } from '../core/math';
2425

2526
type NumberArray = ArrayLike<number>
2627
type InterpolatableType = string | number | NumberArray | NumberArray[];
@@ -120,9 +121,9 @@ function fillColorStops(val0: ParsedColorStop[], val1: ParsedColorStop[]) {
120121
const len1 = val1.length;
121122

122123
const shorterArr = len0 > len1 ? val1 : val0;
123-
const shorterLen = Math.min(len0, len1);
124+
const shorterLen = mathMin(len0, len1);
124125
const last = shorterArr[shorterLen - 1] || { color: [0, 0, 0, 0], offset: 0 };
125-
for (let i = shorterLen; i < Math.max(len0, len1); i++) {
126+
for (let i = shorterLen; i < mathMax(len0, len1); i++) {
126127
// Use last color stop to fill the shorter array
127128
shorterArr.push({
128129
offset: last.offset,
@@ -195,9 +196,9 @@ export function cloneValue(value: InterpolatableType) {
195196
}
196197

197198
function rgba2String(rgba: number[]): string {
198-
rgba[0] = Math.floor(rgba[0]) || 0;
199-
rgba[1] = Math.floor(rgba[1]) || 0;
200-
rgba[2] = Math.floor(rgba[2]) || 0;
199+
rgba[0] = mathFloor(rgba[0]) || 0;
200+
rgba[1] = mathFloor(rgba[1]) || 0;
201+
rgba[2] = mathFloor(rgba[2]) || 0;
201202
rgba[3] = rgba[3] == null ? 1 : rgba[3];
202203

203204
return 'rgba(' + rgba.join(',') + ')';
@@ -468,7 +469,6 @@ class Track {
468469
// find kf2 and kf3 and do interpolation
469470
let frameIdx;
470471
const lastFrame = this._lastFr;
471-
const mathMin = Math.min;
472472
let frame;
473473
let nextFrame;
474474
if (kfsNum === 1) {
@@ -787,7 +787,7 @@ export default class Animator<T> {
787787
}
788788
track.addKeyframe(time, cloneValue(props[propName]), easing);
789789
}
790-
this._maxTime = Math.max(this._maxTime, time);
790+
this._maxTime = mathMax(this._maxTime, time);
791791
return this;
792792
}
793793

src/animation/Clip.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import easingFuncs, {AnimationEasing} from './easing';
1616
import type Animation from './Animation';
1717
import { isFunction, noop } from '../core/util';
1818
import { createCubicEasingFunc } from './cubicEasing';
19+
import { mathMin } from '../core/math';
1920

2021
type OnframeCallback = (percent: number) => void;
2122
type ondestroyCallback = () => void
@@ -100,7 +101,7 @@ export default class Clip {
100101
percent = 0;
101102
}
102103

103-
percent = Math.min(percent, 1);
104+
percent = mathMin(percent, 1);
104105

105106
const easingFunc = this.easingFunc;
106107
const schedule = easingFunc ? easingFunc(percent) : percent;

src/animation/easing.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @exports zrender/animation/easing
55
*/
66

7+
import { PI_OVER_2, PI2, PI, mathCos, mathSin, mathPow, mathSqrt, mathASin } from '../core/math';
8+
79
type easingFunc = (percent: number) => number;
810

911
export type AnimationEasing = keyof typeof easingFuncs | easingFunc;
@@ -126,21 +128,21 @@ const easingFuncs = {
126128
* @return {number}
127129
*/
128130
sinusoidalIn(k: number) {
129-
return 1 - Math.cos(k * Math.PI / 2);
131+
return 1 - mathCos(k * PI_OVER_2);
130132
},
131133
/**
132134
* @param {number} k
133135
* @return {number}
134136
*/
135137
sinusoidalOut(k: number) {
136-
return Math.sin(k * Math.PI / 2);
138+
return mathSin(k * PI_OVER_2);
137139
},
138140
/**
139141
* @param {number} k
140142
* @return {number}
141143
*/
142144
sinusoidalInOut(k: number) {
143-
return 0.5 * (1 - Math.cos(Math.PI * k));
145+
return 0.5 * (1 - mathCos(PI * k));
144146
},
145147

146148
// 指数曲线的缓动(2^t)
@@ -149,14 +151,14 @@ const easingFuncs = {
149151
* @return {number}
150152
*/
151153
exponentialIn(k: number) {
152-
return k === 0 ? 0 : Math.pow(1024, k - 1);
154+
return k === 0 ? 0 : mathPow(1024, k - 1);
153155
},
154156
/**
155157
* @param {number} k
156158
* @return {number}
157159
*/
158160
exponentialOut(k: number) {
159-
return k === 1 ? 1 : 1 - Math.pow(2, -10 * k);
161+
return k === 1 ? 1 : 1 - mathPow(2, -10 * k);
160162
},
161163
/**
162164
* @param {number} k
@@ -170,9 +172,9 @@ const easingFuncs = {
170172
return 1;
171173
}
172174
if ((k *= 2) < 1) {
173-
return 0.5 * Math.pow(1024, k - 1);
175+
return 0.5 * mathPow(1024, k - 1);
174176
}
175-
return 0.5 * (-Math.pow(2, -10 * (k - 1)) + 2);
177+
return 0.5 * (-mathPow(2, -10 * (k - 1)) + 2);
176178
},
177179

178180
// 圆形曲线的缓动(sqrt(1-t^2))
@@ -181,24 +183,24 @@ const easingFuncs = {
181183
* @return {number}
182184
*/
183185
circularIn(k: number) {
184-
return 1 - Math.sqrt(1 - k * k);
186+
return 1 - mathSqrt(1 - k * k);
185187
},
186188
/**
187189
* @param {number} k
188190
* @return {number}
189191
*/
190192
circularOut(k: number) {
191-
return Math.sqrt(1 - (--k * k));
193+
return mathSqrt(1 - (--k * k));
192194
},
193195
/**
194196
* @param {number} k
195197
* @return {number}
196198
*/
197199
circularInOut(k: number) {
198200
if ((k *= 2) < 1) {
199-
return -0.5 * (Math.sqrt(1 - k * k) - 1);
201+
return -0.5 * (mathSqrt(1 - k * k) - 1);
200202
}
201-
return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1);
203+
return 0.5 * (mathSqrt(1 - (k -= 2) * k) + 1);
202204
},
203205

204206
// 创建类似于弹簧在停止前来回振荡的动画
@@ -221,10 +223,10 @@ const easingFuncs = {
221223
s = p / 4;
222224
}
223225
else {
224-
s = p * Math.asin(1 / a) / (2 * Math.PI);
226+
s = p * mathASin(1 / a) / PI2;
225227
}
226-
return -(a * Math.pow(2, 10 * (k -= 1))
227-
* Math.sin((k - s) * (2 * Math.PI) / p));
228+
return -(a * mathPow(2, 10 * (k -= 1))
229+
* mathSin((k - s) * PI2 / p));
228230
},
229231
/**
230232
* @param {number} k
@@ -245,38 +247,38 @@ const easingFuncs = {
245247
s = p / 4;
246248
}
247249
else {
248-
s = p * Math.asin(1 / a) / (2 * Math.PI);
250+
s = p * mathASin(1 / a) / PI2;
249251
}
250-
return (a * Math.pow(2, -10 * k)
251-
* Math.sin((k - s) * (2 * Math.PI) / p) + 1);
252+
return (a * mathPow(2, -10 * k)
253+
* mathSin((k - s) * PI2 / p) + 1);
252254
},
253255
/**
254256
* @param {number} k
255257
* @return {number}
256258
*/
257259
elasticInOut(k: number) {
258-
let s;
259-
let a = 0.1;
260-
let p = 0.4;
261260
if (k === 0) {
262261
return 0;
263262
}
264263
if (k === 1) {
265264
return 1;
266265
}
266+
let s;
267+
let a = 0.1;
268+
const p = 0.4;
267269
if (!a || a < 1) {
268270
a = 1;
269271
s = p / 4;
270272
}
271273
else {
272-
s = p * Math.asin(1 / a) / (2 * Math.PI);
274+
s = p * mathASin(1 / a) / PI2;
273275
}
274276
if ((k *= 2) < 1) {
275-
return -0.5 * (a * Math.pow(2, 10 * (k -= 1))
276-
* Math.sin((k - s) * (2 * Math.PI) / p));
277+
return -0.5 * (a * mathPow(2, 10 * (k -= 1))
278+
* mathSin((k - s) * PI2 / p));
277279
}
278-
return a * Math.pow(2, -10 * (k -= 1))
279-
* Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1;
280+
return a * mathPow(2, -10 * (k -= 1))
281+
* mathSin((k - s) * PI2 / p) * 0.5 + 1;
280282

281283
},
282284

@@ -286,23 +288,23 @@ const easingFuncs = {
286288
* @return {number}
287289
*/
288290
backIn(k: number) {
289-
let s = 1.70158;
291+
const s = 1.70158;
290292
return k * k * ((s + 1) * k - s);
291293
},
292294
/**
293295
* @param {number} k
294296
* @return {number}
295297
*/
296298
backOut(k: number) {
297-
let s = 1.70158;
299+
const s = 1.70158;
298300
return --k * k * ((s + 1) * k + s) + 1;
299301
},
300302
/**
301303
* @param {number} k
302304
* @return {number}
303305
*/
304306
backInOut(k: number) {
305-
let s = 1.70158 * 1.525;
307+
const s = 1.70158 * 1.525;
306308
if ((k *= 2) < 1) {
307309
return 0.5 * (k * k * ((s + 1) * k - s));
308310
}

src/canvas/Painter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import BoundingRect from '../core/BoundingRect';
1414
import { REDRAW_BIT } from '../graphic/constants';
1515
import { getSize } from './helper';
1616
import type IncrementalDisplayable from '../graphic/IncrementalDisplayable';
17+
import { mathRandom } from '../core/math';
1718

1819
const HOVER_LAYER_ZLEVEL = 1e5;
1920
const CANVAS_ZLEVEL = 314159;
@@ -236,7 +237,7 @@ export default class CanvasPainter implements PainterBase {
236237

237238
const zlevelList = this._zlevelList;
238239

239-
this._redrawId = Math.random();
240+
this._redrawId = mathRandom();
240241

241242
this._paintList(list, prevList, paintAll, this._redrawId);
242243

src/canvas/graphic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Path, { PathStyleProps } from '../graphic/Path';
1111
import ZRImage, { ImageStyleProps } from '../graphic/Image';
1212
import TSpan, {TSpanStyleProps} from '../graphic/TSpan';
1313
import { MatrixArray } from '../core/matrix';
14-
import { RADIAN_TO_DEGREE } from '../core/util';
14+
import { mathMax, mathMin, RADIAN_TO_DEGREE } from '../core/math';
1515
import { getLineDash } from './dashStyle';
1616
import { REDRAW_BIT, SHAPE_CHANGED_BIT } from '../graphic/constants';
1717
import type IncrementalDisplayable from '../graphic/IncrementalDisplayable';
@@ -383,7 +383,7 @@ function bindCommonProps(
383383
flushPathDrawn(ctx, scope);
384384
styleChanged = true;
385385
// Ensure opacity is between 0 ~ 1. Invalid opacity will lead to a failure set and use the leaked opacity from the previous.
386-
const opacity = Math.max(Math.min(style.opacity, 1), 0);
386+
const opacity = mathMax(mathMin(style.opacity, 1), 0);
387387
ctx.globalAlpha = isNaN(opacity) ? DEFAULT_COMMON_STYLE.opacity : opacity;
388388
}
389389

src/canvas/helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RadialGradientObject } from '../graphic/RadialGradient';
33
import { GradientObject } from '../graphic/Gradient';
44
import { RectLike } from '../core/BoundingRect';
55
import Path from '../graphic/Path';
6+
import { mathMin } from '../core/math';
67

78
function isSafeNum(num: number) {
89
// NaN、Infinity、undefined、'xx'
@@ -46,7 +47,7 @@ export function createRadialGradient(
4647
) {
4748
const width = rect.width;
4849
const height = rect.height;
49-
const min = Math.min(width, height);
50+
const min = mathMin(width, height);
5051

5152
let x = obj.x == null ? 0.5 : obj.x;
5253
let y = obj.y == null ? 0.5 : obj.y;

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import env from './core/env';
2+
import { mathMax } from './core/math';
23

34
let dpr = 1;
45

56
// If in browser environment
67
if (env.hasGlobalWindow) {
7-
dpr = Math.max(
8+
dpr = mathMax(
89
window.devicePixelRatio
910
|| (window.screen && (window.screen as any).deviceXDPI / (window.screen as any).logicalXDPI)
1011
|| 1, 1

src/contain/arc.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
import {normalizeRadian} from './util';
3-
4-
const PI2 = Math.PI * 2;
3+
import { PI2, EPSILON4, mathAbs, mathSqrt, mathATan2 } from '../core/math';
54

65
/**
76
* 圆弧描边包含判断
@@ -19,13 +18,13 @@ export function containStroke(
1918

2019
x -= cx;
2120
y -= cy;
22-
const d = Math.sqrt(x * x + y * y);
21+
const d = mathSqrt(x * x + y * y);
2322

2423
if ((d - _l > r) || (d + _l < r)) {
2524
return false;
2625
}
2726
// TODO
28-
if (Math.abs(startAngle - endAngle) % PI2 < 1e-4) {
27+
if (mathAbs(startAngle - endAngle) % PI2 < EPSILON4) {
2928
// Is a circle
3029
return true;
3130
}
@@ -42,7 +41,7 @@ export function containStroke(
4241
endAngle += PI2;
4342
}
4443

45-
let angle = Math.atan2(y, x);
44+
let angle = mathATan2(y, x);
4645
if (angle < 0) {
4746
angle += PI2;
4847
}

src/contain/line.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { mathAbs } from '../core/math';
12

23
/**
34
* 线段包含判断
@@ -35,7 +36,7 @@ export function containStroke(
3536
_b = (x0 * y1 - x1 * y0) / (x0 - x1);
3637
}
3738
else {
38-
return Math.abs(x - x0) <= _l / 2;
39+
return mathAbs(x - x0) <= _l / 2;
3940
}
4041
const tmp = _a * x - y + _b;
4142
const _s = tmp * tmp / (_a * _a + 1);

0 commit comments

Comments
 (0)