@remotion/studio
Version:
APIs for interacting with the Remotion Studio
62 lines (61 loc) • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.drawBars = void 0;
const parse_color_1 = require("./parse-color");
const CLIPPING_COLOR = '#FF7F50';
const drawBars = (canvas, peaks, color, volume, width) => {
const ctx = canvas.getContext('2d');
if (!ctx) {
throw new Error('Failed to get canvas context');
}
const { height } = canvas;
const w = canvas.width;
ctx.clearRect(0, 0, w, height);
if (volume === 0)
return;
const [r, g, b, a] = (0, parse_color_1.parseColor)(color);
const [cr, cg, cb, ca] = (0, parse_color_1.parseColor)(CLIPPING_COLOR);
const imageData = ctx.createImageData(w, height);
const { data } = imageData;
const numBars = width;
for (let barIndex = 0; barIndex < numBars; barIndex++) {
const x = barIndex;
if (x >= w)
break;
const peakIndex = Math.floor((barIndex / numBars) * peaks.length);
const peak = peaks[peakIndex] || 0;
const scaledPeak = peak * volume;
const halfBar = Math.max(0, Math.min(height / 2, (scaledPeak * height) / 2));
if (halfBar === 0)
continue;
const mid = height / 2;
const barY = Math.round(mid - halfBar);
const barEnd = Math.round(mid + halfBar);
const isClipping = scaledPeak > 1;
const clipTopEnd = isClipping ? Math.min(barY + 2, barEnd) : barY;
const clipBotStart = isClipping ? Math.max(barEnd - 2, barY) : barEnd;
for (let y = barY; y < clipTopEnd; y++) {
const idx = (y * w + x) * 4;
data[idx] = cr;
data[idx + 1] = cg;
data[idx + 2] = cb;
data[idx + 3] = ca;
}
for (let y = clipTopEnd; y < clipBotStart; y++) {
const idx = (y * w + x) * 4;
data[idx] = r;
data[idx + 1] = g;
data[idx + 2] = b;
data[idx + 3] = a;
}
for (let y = clipBotStart; y < barEnd; y++) {
const idx = (y * w + x) * 4;
data[idx] = cr;
data[idx + 1] = cg;
data[idx + 2] = cb;
data[idx + 3] = ca;
}
}
ctx.putImageData(imageData, 0, 0);
};
exports.drawBars = drawBars;