<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>3D Music sequencer</title>…
- User
const COLORS = [“#ff0000”, “#00ff00”, “#0000ff”, “#ffff00”, “#ff00ff”, “#00ffff”, “#cccccc”, “#ffffff”]; // available colors for cells
let drums = []; // array to hold 3D drum sequencer data let selectedColor = “#ff0000”; // default selected color
// initialize the 3D drum sequencer with empty cells function init() { for (let i = 0; i < ROWS; i++) { let row = []; for (let j = 0; j < COLS; j++) { let col = []; for (let k = 0; k <= 3; k++) { col.push(false); } row.push(col); } drums.push(row); } }
// draw the 3D drum sequencer on the canvas function draw() { let canvas = document.getElementById(“canvas”); let ctx = canvas.getContext(“2d”);
// clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height);
// draw the cells for (let i = 0; i < ROWS; i++) { for (let j = 0; j < COLS; j++) { for (let k = 0; k <= 3; k++) { if (drums[i][j][k]) { ctx.fillStyle = COLORS[k]; ctx.fillRect(j * CELLSIZE, k * CELLSIZE + i * CELLSIZE, CELLSIZE, CELLSIZE); } } } }
// draw grid lines ctx.strokeStyle = “black”; for (let i = 0; i <= ROWS; i++) { ctx.beginPath(); ctx.moveTo(0, i * CELLSIZE); ctx.lineTo(COLS * CELLSIZE, i * CELLSIZE); ctx.stroke(); } for (let j = 0; j <= COLS; j++) { ctx.beginPath(); ctx.moveTo(j * CELLSIZE, 0); ctx.lineTo(j * CELLSIZE, ROWS * CELLSIZE); ctx.stroke(); } }
// update the selected color function setColor(color) { selectedColor = color; }
// handle cell clicks function handleClick(event) { let cellX = Math.floor(event.offsetX / CELLSIZE); let cellY = Math.floor(event.offsetY / CELLSIZE); for (let k = 0; k <= 3; k++) { if (drums[cellY][cellX][k] && COLORS[k] == selectedColor) { drums[cellY][cellX][k] = false; // remove cell } else if (!drums[cellY][cellX][k] && COLORS[k] == selectedColor) { drums[cellY][cellX][k] = true; // add cell } } draw(); // redraw canvas }
// add event listeners function addListeners() { // handle color clicks let colorButtons = document.getElementsByClassName(“color-button”); for (let i = 0; i < colorButtons.length; i++) { colorButtons[i].addEventListener(“click”, function() { setColor(COLORS[i]); }); }
// handle cell clicks let canvas = document.getElementById(“canvas”); canvas.addEventListener(“click”, handleClick); }
// start the sequencer function start() { let beat = 0; setInterval(function() { // play the drums for the current beat for (let i = 0; i < ROWS; i++) { for (let j = 0; j < COLS; j++) { for (let k = 0; k <= 3; k++) { if (drums[i][j][k] && k == beat) { let audio = new Audio(“drum-” + k + “.mp3”); audio.play(); } } } } // update the beat counter beat++; if (beat > 3) { beat = 0; } }, 400); // 400 milliseconds per beat }
// initialize, draw, and add event listeners to the sequencer function main() { init(); draw(); addListeners(); }
// start the sequencer when the page is loaded window.onload = function() { main(); start(); };
- Assistant
// This is a JavaScript code for a 3D Music Sequencer web application, which allows the user to create and play music using an interactive 3D grid of cells. The user can select a color and add or remove cells by clicking on them. The sequencer plays the music for a fixed beat cycle and uses audio samples for each drum sound.
// The code begins by defining some constants, including the number of rows and columns in the grid, the size of each cell in pixels, and an array of available colors. It also initializes an empty array to hold the data for the 3D drum sequencer and sets the default selected color.
// The init() function populates the drums array with empty cells for each row, column, and drum sound. The draw() function renders the current state of the sequencer on the canvas by looping through each cell of the drums array and drawing a rectangle in the corresponding color if it is set.
// The setColor() function updates the selected color when a color button is clicked, and the handleClick() function adds or removes a cell in the drums array when a cell in the canvas is clicked. The addListeners() function sets up event listeners for color and cell clicks on the canvas. The start() function plays the music for each beat by looping through each cell of the drums array and playing the corresponding audio sample if the cell is set.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.