<!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 an HTML and JavaScript code for a 3D music sequencer. It allows users to create and play music by clicking on cells in a grid and choosing colors for each cell. The available colors are given in the COLORS array. The number of rows and columns in the grid are defined by ROWS and COLS variables, and the size of each cell is defined by CELLSIZE variable.
// The program uses a 3D array called drums to store the data of the grid. Each cell in the grid can have one of four values, which corresponds to the four beats in a music bar. If a cell is true for a particular beat, it means that it should be played for that beat. The colors of the cells can be changed by clicking on the color buttons below the canvas.
// The canvas is cleared and redrawn every time a cell is modified or the page is loaded. When the user clicks on a cell, the handleClick function is called, which updates the corresponding value in the drums array and redraws the canvas.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.