|
| 1 | +<html> |
| 2 | + <head> |
| 3 | + <title>Expand demo</title> |
| 4 | + <script src="https://unpkg.com/blobs/v2/animate"></script> |
| 5 | + <style> |
| 6 | + body { |
| 7 | + background-color: #ffffff; |
| 8 | + height: 100%; |
| 9 | + margin: 0; |
| 10 | + overflow: hidden; |
| 11 | + width: 100%; |
| 12 | + } |
| 13 | + |
| 14 | + #container { |
| 15 | + background-color: #ffffff; |
| 16 | + border-radius: 5px; |
| 17 | + box-shadow: 0 2px 10px 0 rgb(0 0 0 / 20%); |
| 18 | + cursor: pointer; |
| 19 | + display: flex; |
| 20 | + flex-direction: column; |
| 21 | + position: absolute; |
| 22 | + top: 35vh; |
| 23 | + left: 15vw; |
| 24 | + min-height: 20%; |
| 25 | + width: 400px; |
| 26 | + } |
| 27 | + |
| 28 | + canvas { |
| 29 | + border: 1px solid #ec576b; |
| 30 | + box-sizing: border-box; |
| 31 | + height: 100%; |
| 32 | + left: 0; |
| 33 | + position: absolute; |
| 34 | + top: 0; |
| 35 | + width: 100%; |
| 36 | + z-index: -1; |
| 37 | + } |
| 38 | + </style> |
| 39 | + </head> |
| 40 | + <body> |
| 41 | + <div id="container"></div> |
| 42 | + <canvas></canvas> |
| 43 | + |
| 44 | + <script> |
| 45 | + const canvas = document.querySelector("canvas"); |
| 46 | + const container = document.getElementById("container"); |
| 47 | + |
| 48 | + const ctx = canvas.getContext("2d"); |
| 49 | + const animation = blobs2Animate.canvasPath(); |
| 50 | + |
| 51 | + const width = canvas.clientWidth; |
| 52 | + const height = canvas.clientHeight; |
| 53 | + canvas.width = width; |
| 54 | + canvas.height = height; |
| 55 | + |
| 56 | + const renderAnimation = () => { |
| 57 | + ctx.clearRect(0, 0, width, height); |
| 58 | + ctx.fillStyle = "#ec576b"; |
| 59 | + ctx.fill(animation.renderFrame()); |
| 60 | + requestAnimationFrame(renderAnimation); |
| 61 | + }; |
| 62 | + requestAnimationFrame(renderAnimation); |
| 63 | + |
| 64 | + const size = Math.min(width, height) * 1.2; |
| 65 | + const defaultOptions = () => ({ |
| 66 | + blobOptions: { |
| 67 | + seed: Math.random(), |
| 68 | + extraPoints: 36, |
| 69 | + randomness: 0.7, |
| 70 | + size, |
| 71 | + }, |
| 72 | + canvasOptions: { |
| 73 | + offsetX: -size / 2.2, |
| 74 | + offsetY: -size / 2.2, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + // Keyframe loop. |
| 79 | + const loopAnimation = () => { |
| 80 | + animation.transition({ |
| 81 | + duration: 4000, |
| 82 | + timingFunction: "ease", |
| 83 | + callback: loopAnimation, |
| 84 | + ...defaultOptions(), |
| 85 | + }); |
| 86 | + }; |
| 87 | + |
| 88 | + // Initial frame. |
| 89 | + animation.transition({ |
| 90 | + duration: 0, // Render immediately. |
| 91 | + callback: loopAnimation, |
| 92 | + ...defaultOptions(), |
| 93 | + }); |
| 94 | + |
| 95 | + container.onclick = () => { |
| 96 | + const options = defaultOptions(); |
| 97 | + options.blobOptions.size = Math.max(width, height) * 1.6; |
| 98 | + options.blobOptions.randomness = 1.4; |
| 99 | + options.canvasOptions.offsetX = -size / 2; |
| 100 | + options.canvasOptions.offsetY = -size / 2; |
| 101 | + animation.transition({ |
| 102 | + duration: 2000, |
| 103 | + timingFunction: "elasticEnd0", |
| 104 | + ...options, |
| 105 | + }); |
| 106 | + }; |
| 107 | + </script> |
| 108 | + </body> |
| 109 | +</html> |
0 commit comments