|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import d3 from 'd3'; |
| 20 | + |
| 21 | +const height = 700; |
| 22 | +const width = document.getElementById('div_svg').offsetWidth; |
| 23 | +const points = 20; |
| 24 | +const matrix = []; |
| 25 | +const duration = 2000; |
| 26 | +let i = 0; |
| 27 | +let flip = 0; |
| 28 | +const colors = [ |
| 29 | + '#FF5A5F', '#007A87', '#7B0051', '#00D1C1', '#8CE071', '#FFB400', |
| 30 | + '#FFAA91', '#B4A76C', '#9CA299', '#565A5C', |
| 31 | +]; |
| 32 | + |
| 33 | +function choose(choices) { |
| 34 | + const index = Math.floor(Math.random() * choices.length); |
| 35 | + return choices[index]; |
| 36 | +} |
| 37 | +// Making a matrix |
| 38 | +for (let x = 0; x < points; x += 1) { |
| 39 | + for (let y = 0; y < points; y += 1) { |
| 40 | + matrix[i] = { |
| 41 | + x, |
| 42 | + y, |
| 43 | + }; |
| 44 | + i += 1; |
| 45 | + } |
| 46 | +} |
| 47 | +const sclx = d3.scale.linear().domain([-1, points]).range([0, width]); |
| 48 | +const scly = d3.scale.linear().domain([-1, points]).range([0, height]); |
| 49 | + |
| 50 | +const circles = d3.select('#circles-svg') |
| 51 | + .attr('width', '100%') |
| 52 | + |
| 53 | + .attr('height', height) |
| 54 | + .selectAll('circle') |
| 55 | + .data(matrix) |
| 56 | + .enter() |
| 57 | + .append('circle') |
| 58 | + .attr('stroke', 'black') |
| 59 | + .attr('fill', 'none') |
| 60 | + .attr('cx', (d) => sclx(d.x)) |
| 61 | + .attr('cy', (d) => scly(d.y)) |
| 62 | + .attr('r', () => 0); |
| 63 | + |
| 64 | +function toggle() { |
| 65 | + const size = 50 + (200 * Math.random()); |
| 66 | + let yDelay = 0; |
| 67 | + let randomDelay = 0; |
| 68 | + if (Math.random() > 0.7) { |
| 69 | + yDelay = 50 + (Math.random() * 50); |
| 70 | + } |
| 71 | + let xDelay = 0; |
| 72 | + if (Math.random() > 0.7) { |
| 73 | + xDelay = 50 + (Math.random() * 50); |
| 74 | + } |
| 75 | + if (Math.random() > 0.7) { |
| 76 | + randomDelay = 1; |
| 77 | + } |
| 78 | + |
| 79 | + const randomX = Math.random() * width; |
| 80 | + const randomY = Math.random() * height; |
| 81 | + let col; |
| 82 | + if (Math.random() > 0.5) { |
| 83 | + col = choose(colors); |
| 84 | + } else { |
| 85 | + col = 'black'; |
| 86 | + } |
| 87 | + if (Math.random() > 0.8) { |
| 88 | + col = choose(colors); |
| 89 | + } |
| 90 | + |
| 91 | + if (flip === 0) { |
| 92 | + flip = 1; |
| 93 | + |
| 94 | + circles.transition() |
| 95 | + .duration(duration) |
| 96 | + .attr('cx', (d) => sclx(d.x)) |
| 97 | + .attr('cy', (d) => scly(d.y)) |
| 98 | + .attr('stroke', col) |
| 99 | + .delay((d) => (randomDelay * Math.random() * 1000) + (d.x * xDelay) + (d.y * yDelay)) |
| 100 | + .attr('r', () => size); |
| 101 | + } else { |
| 102 | + flip = 0; |
| 103 | + |
| 104 | + if (Math.random() > 0.6) { |
| 105 | + circles.transition() |
| 106 | + .duration(duration) |
| 107 | + .attr('r', () => 0) |
| 108 | + .attr('cx', () => randomX) |
| 109 | + .attr('stroke', col) |
| 110 | + .delay((d, j) => (j / 2) * Math.random() * 5) |
| 111 | + .attr('cy', () => randomY); |
| 112 | + } else { |
| 113 | + circles.transition() |
| 114 | + .duration(duration) |
| 115 | + .attr('cx', (d) => sclx(d.x)) |
| 116 | + .attr('cy', (d) => scly(d.y)) |
| 117 | + .attr('stroke', col) |
| 118 | + .delay((d) => (randomDelay * Math.random() * 1000) + (d.x * xDelay) + (d.y * yDelay)) |
| 119 | + .attr('r', () => 0); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +document.addEventListener('DOMContentLoaded', () => { |
| 125 | + setInterval(toggle, duration * 3); |
| 126 | + toggle(); |
| 127 | +}); |
0 commit comments