1+ import type { Chart , ChartArea , FontSpec , Point } from 'src/types.js' ;
2+ import type { RoundedRect } from 'src/types/geometric.js' ;
13import { isArray , isNullOrUndef } from './helpers.core.js' ;
24import { PI , TAU , HALF_PI , QUARTER_PI , TWO_THIRDS_PI , RAD_PER_DEG } from './helpers.math.js' ;
5+ import type { BackdropOptions , CanvasFontSpec , DrawPointOptions , RenderTextOpts , SplinePoint } from './types.js' ;
36
47/**
58 * Note: typedefs are auto-exported, so use a made-up `canvas` namespace where
@@ -19,7 +22,7 @@ import {PI, TAU, HALF_PI, QUARTER_PI, TWO_THIRDS_PI, RAD_PER_DEG} from './helper
1922 * @return {string|null } The CSS font string. See https://developer.mozilla.org/en-US/docs/Web/CSS/font
2023 * @private
2124 */
22- export function toFontString ( font ) {
25+ export function toFontString ( font : FontSpec ) : string | null {
2326 if ( ! font || isNullOrUndef ( font . size ) || isNullOrUndef ( font . family ) ) {
2427 return null ;
2528 }
@@ -33,7 +36,7 @@ export function toFontString(font) {
3336/**
3437 * @private
3538 */
36- export function _measureText ( ctx , data , gc , longest , string ) {
39+ export function _measureText ( ctx : CanvasRenderingContext2D , data , gc , longest : number , string ) {
3740 let textWidth = data [ string ] ;
3841 if ( ! textWidth ) {
3942 textWidth = data [ string ] = ctx . measureText ( string ) . width ;
@@ -48,7 +51,8 @@ export function _measureText(ctx, data, gc, longest, string) {
4851/**
4952 * @private
5053 */
51- export function _longestText ( ctx , font , arrayOfThings , cache ) {
54+ // eslint-disable-next-line complexity
55+ export function _longestText < T > ( ctx : CanvasRenderingContext2D , font , arrayOfThings : T [ ] , cache ) {
5256 cache = cache || { } ;
5357 let data = cache . data = cache . data || { } ;
5458 let gc = cache . garbageCollect = cache . garbageCollect || [ ] ;
@@ -64,7 +68,7 @@ export function _longestText(ctx, font, arrayOfThings, cache) {
6468 ctx . font = font ;
6569 let longest = 0 ;
6670 const ilen = arrayOfThings . length ;
67- let i , j , jlen , thing , nestedThing ;
71+ let i : number , j : number , jlen : number , thing : T , nestedThing ;
6872 for ( i = 0 ; i < ilen ; i ++ ) {
6973 thing = arrayOfThings [ i ] ;
7074
@@ -104,7 +108,7 @@ export function _longestText(ctx, font, arrayOfThings, cache) {
104108 * @returns {number } The aligned pixel value.
105109 * @private
106110 */
107- export function _alignPixel ( chart , pixel , width ) {
111+ export function _alignPixel ( chart : Chart , pixel : number , width : number ) {
108112 const devicePixelRatio = chart . currentDevicePixelRatio ;
109113 const halfWidth = width !== 0 ? Math . max ( width / 2 , 0.5 ) : 0 ;
110114 return Math . round ( ( pixel - halfWidth ) * devicePixelRatio ) / devicePixelRatio + halfWidth ;
@@ -115,7 +119,7 @@ export function _alignPixel(chart, pixel, width) {
115119 * @param {HTMLCanvasElement } canvas
116120 * @param {CanvasRenderingContext2D } [ctx]
117121 */
118- export function clearCanvas ( canvas , ctx ) {
122+ export function clearCanvas ( canvas : HTMLCanvasElement , ctx : CanvasRenderingContext2D ) {
119123 ctx = ctx || canvas . getContext ( '2d' ) ;
120124
121125 ctx . save ( ) ;
@@ -126,12 +130,13 @@ export function clearCanvas(canvas, ctx) {
126130 ctx . restore ( ) ;
127131}
128132
129- export function drawPoint ( ctx , options , x , y ) {
133+ export function drawPoint ( ctx : CanvasRenderingContext2D , options : DrawPointOptions , x : number , y : number ) {
130134 drawPointLegend ( ctx , options , x , y , null ) ;
131135}
132136
133- export function drawPointLegend ( ctx , options , x , y , w ) {
134- let type , xOffset , yOffset , size , cornerRadius , width , xOffsetW , yOffsetW ;
137+ // eslint-disable-next-line complexity
138+ export function drawPointLegend ( ctx : CanvasRenderingContext2D , options : DrawPointOptions , x : number , y : number , w : number ) {
139+ let type : string , xOffset : number , yOffset : number , size : number , cornerRadius : number , width : number , xOffsetW : number , yOffsetW : number ;
135140 const style = options . pointStyle ;
136141 const rotation = options . rotation ;
137142 const radius = options . radius ;
@@ -275,28 +280,28 @@ export function drawPointLegend(ctx, options, x, y, w) {
275280 * @returns {boolean }
276281 * @private
277282 */
278- export function _isPointInArea ( point , area , margin ) {
283+ export function _isPointInArea ( point : Point , area : ChartArea , margin : number ) {
279284 margin = margin || 0.5 ; // margin - default is to match rounded decimals
280285
281286 return ! area || ( point && point . x > area . left - margin && point . x < area . right + margin &&
282287 point . y > area . top - margin && point . y < area . bottom + margin ) ;
283288}
284289
285- export function clipArea ( ctx , area ) {
290+ export function clipArea ( ctx : CanvasRenderingContext2D , area : ChartArea ) {
286291 ctx . save ( ) ;
287292 ctx . beginPath ( ) ;
288293 ctx . rect ( area . left , area . top , area . right - area . left , area . bottom - area . top ) ;
289294 ctx . clip ( ) ;
290295}
291296
292- export function unclipArea ( ctx ) {
297+ export function unclipArea ( ctx : CanvasRenderingContext2D ) {
293298 ctx . restore ( ) ;
294299}
295300
296301/**
297302 * @private
298303 */
299- export function _steppedLineTo ( ctx , previous , target , flip , mode ) {
304+ export function _steppedLineTo ( ctx : CanvasRenderingContext2D , previous : Point , target : Point , flip , mode ) {
300305 if ( ! previous ) {
301306 return ctx . lineTo ( target . x , target . y ) ;
302307 }
@@ -315,7 +320,7 @@ export function _steppedLineTo(ctx, previous, target, flip, mode) {
315320/**
316321 * @private
317322 */
318- export function _bezierCurveTo ( ctx , previous , target , flip ) {
323+ export function _bezierCurveTo ( ctx : CanvasRenderingContext2D , previous : SplinePoint , target : SplinePoint , flip ) {
319324 if ( ! previous ) {
320325 return ctx . lineTo ( target . x , target . y ) ;
321326 }
@@ -331,10 +336,10 @@ export function _bezierCurveTo(ctx, previous, target, flip) {
331336/**
332337 * Render text onto the canvas
333338 */
334- export function renderText ( ctx , text , x , y , font , opts = { } ) {
339+ export function renderText ( ctx : CanvasRenderingContext2D , text : string | string [ ] , x : number , y : number , font : CanvasFontSpec , opts : RenderTextOpts = { } ) {
335340 const lines = isArray ( text ) ? text : [ text ] ;
336341 const stroke = opts . strokeWidth > 0 && opts . strokeColor !== '' ;
337- let i , line ;
342+ let i : number , line : string ;
338343
339344 ctx . save ( ) ;
340345 ctx . font = font . string ;
@@ -368,7 +373,7 @@ export function renderText(ctx, text, x, y, font, opts = {}) {
368373 ctx . restore ( ) ;
369374}
370375
371- function setRenderOpts ( ctx , opts ) {
376+ function setRenderOpts ( ctx : CanvasRenderingContext2D , opts : RenderTextOpts ) {
372377 if ( opts . translation ) {
373378 ctx . translate ( opts . translation [ 0 ] , opts . translation [ 1 ] ) ;
374379 }
@@ -390,7 +395,7 @@ function setRenderOpts(ctx, opts) {
390395 }
391396}
392397
393- function decorateText ( ctx , x , y , line , opts ) {
398+ function decorateText ( ctx : CanvasRenderingContext2D , x : number , y : number , line : string , opts : RenderTextOpts ) {
394399 if ( opts . strikethrough || opts . underline ) {
395400 /**
396401 * Now that IE11 support has been dropped, we can use more
@@ -415,7 +420,7 @@ function decorateText(ctx, x, y, line, opts) {
415420 }
416421}
417422
418- function drawBackdrop ( ctx , opts ) {
423+ function drawBackdrop ( ctx : CanvasRenderingContext2D , opts : BackdropOptions ) {
419424 const oldColor = ctx . fillStyle ;
420425
421426 ctx . fillStyle = opts . color ;
@@ -428,7 +433,7 @@ function drawBackdrop(ctx, opts) {
428433 * @param {CanvasRenderingContext2D } ctx Context
429434 * @param {* } rect Bounding rect
430435 */
431- export function addRoundedRectPath ( ctx , rect ) {
436+ export function addRoundedRectPath ( ctx : CanvasRenderingContext2D , rect : RoundedRect ) {
432437 const { x, y, w, h, radius} = rect ;
433438
434439 // top left arc
0 commit comments