UNPKG

@elastic/charts

Version:

Elastic-Charts data visualization library

59 lines 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withContext = withContext; exports.clearCanvas = clearCanvas; exports.renderLayers = renderLayers; exports.withClip = withClip; exports.withClipRanges = withClipRanges; function withContext(ctx, fun) { ctx.save(); const r = fun(ctx); ctx.restore(); return r; } function clearCanvas(ctx, bgColor) { withContext(ctx, () => { ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.fillStyle = bgColor; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); }); } function renderLayers(ctx, layers) { layers.forEach((renderLayer) => renderLayer(ctx)); } function withClip(ctx, clippings, fun, shouldClip = true) { withContext(ctx, () => { if (shouldClip) { const { x, y, width, height } = clippings; ctx.beginPath(); ctx.rect(x, y, width, height); ctx.clip(); } withContext(ctx, () => fun(ctx)); }); } function withClipRanges(ctx, clippedRanges, { width, height, y }, negate, fun) { withContext(ctx, () => { if (clippedRanges.length > 0) { ctx.beginPath(); if (negate) { clippedRanges.forEach(([x0, x1]) => ctx.rect(x0, y, x1 - x0, height)); } else { const firstX = clippedRanges[0]?.[0] ?? NaN; const lastX = clippedRanges.at(-1)?.[1] ?? NaN; ctx.rect(0, -0.5, firstX, height); ctx.rect(lastX, y, width - lastX, height); clippedRanges.forEach(([, x0], i) => { if (i < clippedRanges.length - 1) { ctx.rect(x0, y, (clippedRanges[i + 1]?.[0] ?? NaN) - x0, height); } }); } ctx.clip(); } fun(ctx); }); } //# sourceMappingURL=index.js.map