From a4980fccdc5bf6e02865348c6256cb984639bca5 Mon Sep 17 00:00:00 2001 From: tbo47 Date: Tue, 8 Apr 2025 14:46:18 +0000 Subject: [PATCH] add types --- src/Shape.ts | 12 +++++++++--- src/shapes/Line.ts | 10 +++++++++- src/shapes/Path.ts | 32 ++++++++++++++++++-------------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Shape.ts b/src/Shape.ts index e9ff2b25..f0ffdffa 100644 --- a/src/Shape.ts +++ b/src/Shape.ts @@ -213,10 +213,16 @@ export class Shape< shapes[key] = this; } + /** + * @deprecated + */ getContext() { Util.warn('shape.getContext() method is deprecated. Please do not use it.'); return this.getLayer()!.getContext(); } + /** + * @deprecated + */ getCanvas() { Util.warn('shape.getCanvas() method is deprecated. Please do not use it.'); return this.getLayer()!.getCanvas(); @@ -442,7 +448,7 @@ export class Shape< * @param {Number} point.y * @returns {Boolean} */ - intersects(point) { + intersects(point: Vector2d) { const stage = this.getStage(); if (!stage) { return false; @@ -599,7 +605,7 @@ export class Shape< cachedCanvas = this._getCanvasCache(), drawFunc = this.getSceneFunc(), hasShadow = this.hasShadow(); - let stage, bufferContext; + let stage; const skipBuffer = false; const cachingSelf = top === this; @@ -627,7 +633,7 @@ export class Shape< if (this._useBufferCanvas() && !skipBuffer) { stage = this.getStage(); const bc = bufferCanvas || stage.bufferCanvas; - bufferContext = bc.getContext(); + const bufferContext = bc.getContext(); bufferContext.clear(); bufferContext.save(); bufferContext._applyLineJoin(this); diff --git a/src/shapes/Line.ts b/src/shapes/Line.ts index f41e762d..fcd14626 100644 --- a/src/shapes/Line.ts +++ b/src/shapes/Line.ts @@ -6,7 +6,15 @@ import { getNumberArrayValidator, getNumberValidator } from '../Validators'; import { Context } from '../Context'; import { GetSet } from '../types'; -function getControlPoints(x0, y0, x1, y1, x2, y2, t) { +function getControlPoints( + x0: number, + y0: number, + x1: number, + y1: number, + x2: number, + y2: number, + t: number +) { const d01 = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)), d12 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)), fa = (t * d01) / (d01 + d12), diff --git a/src/shapes/Path.ts b/src/shapes/Path.ts index 6b19d952..32ee663f 100644 --- a/src/shapes/Path.ts +++ b/src/shapes/Path.ts @@ -217,7 +217,7 @@ export class Path extends Shape { * @example * var point = path.getPointAtLength(10); */ - getPointAtLength(length) { + getPointAtLength(length: number) { return Path.getPointAtLengthOfDataArray(length, this.dataArray); } @@ -370,26 +370,33 @@ export class Path extends Shape { return { x: ix + adjustedRun, y: iy + adjustedRise }; } - static getPointOnCubicBezier(pct, P1x, P1y, P2x, P2y, P3x, P3y, P4x, P4y) { - function CB1(t) { + static getPointOnCubicBezier( + pct: number, + P1x: number, + P1y: number, + P2x: number, + P2y: number, + P3x: number, + P3y: number, + P4x: number, + P4y: number + ) { + function CB1(t: number) { return t * t * t; } - function CB2(t) { + function CB2(t: number) { return 3 * t * t * (1 - t); } - function CB3(t) { + function CB3(t: number) { return 3 * t * (1 - t) * (1 - t); } - function CB4(t) { + function CB4(t: number) { return (1 - t) * (1 - t) * (1 - t); } const x = P4x * CB1(pct) + P3x * CB2(pct) + P2x * CB3(pct) + P1x * CB4(pct); const y = P4y * CB1(pct) + P3y * CB2(pct) + P2y * CB3(pct) + P1y * CB4(pct); - return { - x: x, - y: y, - }; + return { x, y }; } static getPointOnQuadraticBezier(pct, P1x, P1y, P2x, P2y, P3x, P3y) { function QB1(t) { @@ -404,10 +411,7 @@ export class Path extends Shape { const x = P3x * QB1(pct) + P2x * QB2(pct) + P1x * QB3(pct); const y = P3y * QB1(pct) + P2y * QB2(pct) + P1y * QB3(pct); - return { - x: x, - y: y, - }; + return { x, y }; } static getPointOnEllipticalArc( cx: number,