59 lines
2.2 KiB
JavaScript
59 lines
2.2 KiB
JavaScript
// js/utils.js
|
|
import { appState } from './state.js';
|
|
import { PIXELS_PER_STEP, ZOOM_LEVELS } from './config.js';
|
|
|
|
/**
|
|
* Calcula a quantidade de pixels que representa um segundo na timeline,
|
|
* levando em conta o BPM e o nível de zoom atual.
|
|
* @returns {number} A quantidade de pixels por segundo.
|
|
*/
|
|
export function getPixelsPerSecond() {
|
|
const bpm = parseInt(document.getElementById("bpm-input").value, 10) || 120;
|
|
const stepsPerSecond = (bpm / 60) * 4;
|
|
const zoomFactor = ZOOM_LEVELS[appState.global.zoomLevelIndex];
|
|
return stepsPerSecond * PIXELS_PER_STEP * zoomFactor;
|
|
}
|
|
|
|
/**
|
|
* Calcula o número total de steps no sequenciador de patterns.
|
|
* @returns {number} O número total de steps.
|
|
*/
|
|
export function getTotalSteps() {
|
|
const barsInput = document.getElementById("bars-input");
|
|
const compassoAInput = document.getElementById("compasso-a-input");
|
|
const compassoBInput = document.getElementById("compasso-b-input");
|
|
|
|
const numberOfBars = parseInt(barsInput.value, 10) || 1;
|
|
const beatsPerBar = parseInt(compassoAInput.value, 10) || 4;
|
|
const noteValue = parseInt(compassoBInput.value, 10) || 4;
|
|
const subdivisions = Math.round(16 / noteValue);
|
|
|
|
return numberOfBars * beatsPerBar * subdivisions;
|
|
}
|
|
|
|
/**
|
|
* Garante que apenas números sejam inseridos em um campo de input.
|
|
* @param {Event} event - O evento de input.
|
|
*/
|
|
export function enforceNumericInput(event) {
|
|
event.target.value = event.target.value.replace(/[^0-9]/g, "");
|
|
}
|
|
|
|
/**
|
|
* Ajusta o valor de um elemento de input com base em um passo (step),
|
|
* respeitando os limites de min/max definidos no elemento.
|
|
* @param {HTMLInputElement} inputElement - O elemento de input a ser ajustado.
|
|
* @param {number} step - O valor a ser adicionado (pode ser negativo).
|
|
*/
|
|
export function adjustValue(inputElement, step) {
|
|
let currentValue = parseInt(inputElement.value, 10) || 0;
|
|
let min = parseInt(inputElement.dataset.min, 10);
|
|
let max = parseInt(inputElement.dataset.max, 10);
|
|
let newValue = currentValue + step;
|
|
|
|
if (!isNaN(min) && newValue < min) newValue = min;
|
|
if (!isNaN(max) && newValue > max) newValue = max;
|
|
|
|
inputElement.value = newValue;
|
|
inputElement.dispatchEvent(new Event("input", { bubbles: true }));
|
|
} |