Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Color Gradient Animation</title>
- <style>
- body {
- margin: 0;
- overflow: hidden;
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- background-color: #f0f0f0;
- }
- canvas {
- border: 1px solid #000;
- }
- </style>
- </head>
- <body>
- <canvas id="colorCanvas"></canvas>
- <script>
- const canvas = document.getElementById('colorCanvas');
- const ctx = canvas.getContext('2d');
- const ww = 1200;
- const hh = 600;
- const zz = 10;
- const w0 = Math.floor(ww / zz);
- const h0 = Math.floor(hh / zz);
- canvas.width = ww;
- canvas.height = hh;
- const colors = [
- [0, 0, 0], [255, 0, 0], [255, 165, 0], [255, 255, 0],
- [75, 0, 130], [0, 0, 255], [0, 128, 0], [255, 255, 255]
- ];
- const rx = (4 - 1) / ww;
- const ry = (2 - 1) / hh;
- function bicubic(c00, c10, c01, c11, x, y) {
- const w00 = (1 - x) * (1 - y);
- const w10 = x * (1 - y);
- const w01 = (1 - x) * y;
- const w11 = x * y;
- const r = Math.round(w00 * c00[0] + w10 * c10[0] + w01 * c01[0] + w11 * c11[0]);
- const g = Math.round(w00 * c00[1] + w10 * c10[1] + w01 * c01[1] + w11 * c11[1]);
- const b = Math.round(w00 * c00[2] + w10 * c10[2] + w01 * c01[2] + w11 * c11[2]);
- return [r, g, b];
- }
- function transitionColor(x, y) {
- x *= zz;
- y *= zz;
- const xRatio = x * rx;
- const yRatio = y * ry;
- const xIndex = Math.floor(xRatio);
- const yIndex = Math.floor(yRatio) * 4;
- const c00 = colors[xIndex + yIndex];
- const c10 = colors[(xIndex + 1) + yIndex];
- const c01 = colors[xIndex + (yIndex + 4)];
- const c11 = colors[(xIndex + 1) + (yIndex + 4)];
- const xFraction = xRatio - xIndex;
- const yFraction = yRatio - yIndex;
- return bicubic(c00, c10, c01, c11, xFraction, yFraction);
- }
- let rgb = new Array(w0 * h0).fill([128, 128, 128]);
- const xy2rgb = new Map();
- const XYo = [];
- for (let y = 0; y < h0; y++) {
- for (let x = 0; x < w0; x++) {
- rgb[x + y * w0] = transitionColor(x, y);
- xy2rgb.set(`${x},${y}`, x + y * w0);
- XYo.push([x, y]);
- }
- }
- function makeAlive(x, y) {
- const nn0 = [[-1, 0], [0, -1], [0, 1], [1, 0]];
- const n0 = rgb[xy2rgb.get(`${x},${y}`)];
- const nn = [];
- for (const [i, j] of nn0) {
- const x0 = x + i;
- const y0 = y + j;
- if (xy2rgb.has(`${x0},${y0}`)) {
- const nx = rgb[xy2rgb.get(`${x0},${y0}`)];
- const colorDiff = Math.sqrt(
- (nx[0] - n0[0]) ** 2 + (nx[1] - n0[1]) ** 2 + (nx[2] - n0[2]) ** 2
- );
- nn.push([colorDiff, [x0, y0], [i, j]]);
- }
- }
- nn.sort((a, b) => a[0] - b[0]);
- const [, [x0, y0]] = nn[0];
- const index1 = xy2rgb.get(`${x},${y}`);
- const index2 = xy2rgb.get(`${x0},${y0}`);
- [rgb[index1], rgb[index2]] = [rgb[index2], rgb[index1]];
- }
- function shuffleArray(array) {
- for (let i = array.length - 1; i > 0; i--) {
- const j = Math.floor(Math.random() * (i + 1));
- [array[i], array[j]] = [array[j], array[i]];
- }
- }
- function animate() {
- shuffleArray(XYo);
- for (const [x, y] of XYo) {
- makeAlive(x, y);
- }
- const imageData = ctx.createImageData(w0, h0);
- for (let i = 0; i < rgb.length; i++) {
- const [r, g, b] = rgb[i];
- imageData.data[i * 4] = r;
- imageData.data[i * 4 + 1] = g;
- imageData.data[i * 4 + 2] = b;
- imageData.data[i * 4 + 3] = 255;
- }
- ctx.putImageData(imageData, 0, 0);
- ctx.drawImage(canvas, 0, 0, w0, h0, 0, 0, ww, hh);
- requestAnimationFrame(animate);
- }
- animate();
- document.addEventListener('keydown', (event) => {
- if (event.code === 'Space') {
- shuffleArray(rgb);
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement