|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head lang="en"> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> |
| 6 | + <title>blobs</title> |
| 7 | + <script src="./v2/animate/index.js"></script> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + <style> |
| 11 | + html, |
| 12 | + body, |
| 13 | + main { |
| 14 | + height: 100%; |
| 15 | + width: 100%; |
| 16 | + margin: 0; |
| 17 | + } |
| 18 | + |
| 19 | + header { |
| 20 | + padding-top: 2rem; |
| 21 | + position: fixed; |
| 22 | + top: 0; |
| 23 | + text-align: center; |
| 24 | + width: 100%; |
| 25 | + } |
| 26 | + |
| 27 | + a { |
| 28 | + color: #aaa; |
| 29 | + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, |
| 30 | + Cantarell, "Open Sans", "Helvetica Neue", sans-serif; |
| 31 | + font-weight: 600; |
| 32 | + padding: 1rem; |
| 33 | + text-decoration: none; |
| 34 | + user-select: none; |
| 35 | + } |
| 36 | + |
| 37 | + main { |
| 38 | + align-items: center; |
| 39 | + display: flex; |
| 40 | + justify-content: center; |
| 41 | + } |
| 42 | + |
| 43 | + main canvas { |
| 44 | + cursor: pointer; |
| 45 | + } |
| 46 | + </style> |
| 47 | + |
| 48 | + <header> |
| 49 | + <a href="mailto:gabrielj.harel@gmail.com">contact</a> |
| 50 | + <a href="https://github.com/g-harel/blobs">github</a> |
| 51 | + <a href="https://npmjs.com/package/blobs">npm</a> |
| 52 | + </header> |
| 53 | + |
| 54 | + <main> |
| 55 | + <canvas id="canvas"></canvas> |
| 56 | + </main> |
| 57 | + |
| 58 | + <script> |
| 59 | + // Set blob size relative to window, but limit to 600. |
| 60 | + const size = Math.min(600, window.innerWidth - 64); |
| 61 | + |
| 62 | + // Fetch reference to canvas and resize dynamically. |
| 63 | + const canvas = document.getElementById("canvas"); |
| 64 | + canvas.width = size; |
| 65 | + canvas.height = size; |
| 66 | + |
| 67 | + // Set blob color and set context to erase intersection of content. |
| 68 | + const ctx = canvas.getContext("2d"); |
| 69 | + ctx.fillStyle = "#ec576b"; |
| 70 | + ctx.globalCompositeOperation = "xor"; |
| 71 | + |
| 72 | + // Use logo word asset to cut out from the generated blob. |
| 73 | + const logoWord = new Image(); |
| 74 | + logoWord.src = "./assets/logo-word-white.svg"; |
| 75 | + |
| 76 | + // Create animation and draw its frames in `requestAnimationFrame` callbacks. |
| 77 | + const animation = blobs2Animate.canvasPath(); |
| 78 | + const renderFrame = () => { |
| 79 | + ctx.clearRect(0, 0, size, size); |
| 80 | + ctx.drawImage(logoWord, size / 10, size / 10, (size * 4) / 5, (size * 4) / 5); |
| 81 | + ctx.fill(animation.renderFrame()); |
| 82 | + requestAnimationFrame(renderFrame); |
| 83 | + }; |
| 84 | + requestAnimationFrame(renderFrame); |
| 85 | + |
| 86 | + // Generate a keyframe with overridable default values. |
| 87 | + const genFrame = (overrides) => ({ |
| 88 | + duration: 4000, |
| 89 | + timingFunction: "easeInOut", |
| 90 | + callback: loopAnimation, |
| 91 | + blobOptions: { |
| 92 | + extraPoints: 3, |
| 93 | + randomness: 4, |
| 94 | + seed: Math.random(), |
| 95 | + size: size, |
| 96 | + }, |
| 97 | + ...overrides, |
| 98 | + }); |
| 99 | + |
| 100 | + // Callback for every frame which starts transition to a new frame. |
| 101 | + const loopAnimation = () => animation.transition(genFrame()); |
| 102 | + |
| 103 | + // Immediately show a shape. |
| 104 | + animation.transition(genFrame({duration: 0})); |
| 105 | + |
| 106 | + // Quickly animate to a new frame when canvas is clicked. |
| 107 | + canvas.onclick = () => { |
| 108 | + animation.transition(genFrame({duration: 400, timingFunction: "elasticIn0"})); |
| 109 | + }; |
| 110 | + </script> |
| 111 | + </body> |
| 112 | +</html> |
0 commit comments