const canvas
- User
const canvas
- Assistant
= document.getElementById("canvas"); const ctx = canvas.getContext("2d");
// Set canvas dimensions canvas.width = window.innerWidth; canvas.height = window.innerHeight;
// Draw a rectangle on the canvas ctx.fillStyle = "red"; ctx.fillRect(100, 100, 200, 200);
// Draw a circle on the canvas ctx.beginPath(); ctx.arc(400, 400, 50, 0, Math.PI * 2); ctx.fillStyle = "blue"; ctx.fill();
// Draw a line on the canvas ctx.beginPath(); ctx.moveTo(600, 300); ctx.lineTo(700, 400); ctx.strokeStyle = "green"; ctx.lineWidth = 5; ctx.stroke();
// Draw text on the canvas ctx.fillStyle = "black"; ctx.font = "30px Arial"; ctx.fillText("Hello World!", 800, 200);
- User
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const cols = canvas.width = window.innerWidth; const rows = canvas.height = window.innerHeight; const size = 10; const chars = "abcdefghijklmnopqrstuvwxyz0123456789"; const charArr = chars.split(""); const matrix = []; for (let i = 0; i < cols / size; i++) { matrix.push([]); for (let j = 0; j < rows / size; j++) { matrix[i].push(0); } }
function draw() { ctx.fillStyle = "rgba(0, 0, 0, 0.05)"; ctx.fillRect(0, 0, cols, rows); ctx.fillStyle = "#0F0"; ctx.font = size + "px arial"; for (let i = 0; i < matrix.length; i++) { for (let j = 0; j < matrix[i].length; j++) { const char = charArr[Math.floor(Math.random() * charArr.length)]; ctx.fillText(char, i * size, matrix[i][j] * size); if (matrix[i][j] * size > rows && Math.random() > 0.975) { matrix[i][j] = 0; } matrix[i][j]++; } } }
setInterval(draw, 50);
- Assistant
This script creates a matrix-style animation on a canvas element.
First, it sets up the canvas with the dimensions of the window and initializes some variables:
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const cols = canvas.width = window.innerWidth; const rows = canvas.height = window.innerHeight; const size = 10; const chars = "abcdefghijklmnopqrstuvwxyz0123456789"; const charArr = chars.split(""); const matrix = [];