|
| 1 | +function bubbleCursor(options) { |
| 2 | + let hasWrapperEl = options && options.element |
| 3 | + let element = hasWrapperEl || document.body |
| 4 | + |
| 5 | + let width = window.innerWidth |
| 6 | + let height = window.innerHeight |
| 7 | + let cursor = { x: width / 2, y: width / 2 } |
| 8 | + let particles = [] |
| 9 | + let canvas, context |
| 10 | + |
| 11 | + let canvImages = [] |
| 12 | + |
| 13 | + function init(wrapperEl) { |
| 14 | + canvas = document.createElement("canvas") |
| 15 | + context = canvas.getContext("2d") |
| 16 | + |
| 17 | + canvas.style.top = "0px" |
| 18 | + canvas.style.left = "0px" |
| 19 | + canvas.style.pointerEvents = "none" |
| 20 | + |
| 21 | + if (hasWrapperEl) { |
| 22 | + canvas.style.position = "absolute" |
| 23 | + element.appendChild(canvas) |
| 24 | + canvas.width = element.clientWidth |
| 25 | + canvas.height = element.clientHeight |
| 26 | + } else { |
| 27 | + canvas.style.position = "fixed" |
| 28 | + document.body.appendChild(canvas) |
| 29 | + canvas.width = width |
| 30 | + canvas.height = height |
| 31 | + } |
| 32 | + |
| 33 | + bindEvents() |
| 34 | + loop() |
| 35 | + } |
| 36 | + |
| 37 | + // Bind events that are needed |
| 38 | + function bindEvents() { |
| 39 | + element.addEventListener("mousemove", onMouseMove) |
| 40 | + element.addEventListener("touchmove", onTouchMove) |
| 41 | + element.addEventListener("touchstart", onTouchMove) |
| 42 | + window.addEventListener("resize", onWindowResize) |
| 43 | + } |
| 44 | + |
| 45 | + function onWindowResize(e) { |
| 46 | + width = window.innerWidth |
| 47 | + height = window.innerHeight |
| 48 | + |
| 49 | + if (hasWrapperEl) { |
| 50 | + canvas.width = element.clientWidth |
| 51 | + canvas.height = element.clientHeight |
| 52 | + } else { |
| 53 | + canvas.width = width |
| 54 | + canvas.height = height |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function onTouchMove(e) { |
| 59 | + if (e.touches.length > 0) { |
| 60 | + for (let i = 0; i < e.touches.length; i++) { |
| 61 | + addParticle( |
| 62 | + e.touches[i].clientX, |
| 63 | + e.touches[i].clientY, |
| 64 | + canvImages[Math.floor(Math.random() * canvImages.length)] |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + function onMouseMove(e) { |
| 71 | + if (hasWrapperEl) { |
| 72 | + const boundingRect = element.getBoundingClientRect() |
| 73 | + cursor.x = e.clientX - boundingRect.left |
| 74 | + cursor.y = e.clientY - boundingRect.top |
| 75 | + } else { |
| 76 | + cursor.x = e.clientX |
| 77 | + cursor.y = e.clientY |
| 78 | + } |
| 79 | + |
| 80 | + addParticle(cursor.x, cursor.y) |
| 81 | + } |
| 82 | + |
| 83 | + function addParticle(x, y, img) { |
| 84 | + particles.push(new Particle(x, y, img)) |
| 85 | + } |
| 86 | + |
| 87 | + function updateParticles() { |
| 88 | + context.clearRect(0, 0, width, height) |
| 89 | + |
| 90 | + // Update |
| 91 | + for (let i = 0; i < particles.length; i++) { |
| 92 | + particles[i].update(context) |
| 93 | + } |
| 94 | + |
| 95 | + // Remove dead particles |
| 96 | + for (let i = particles.length - 1; i >= 0; i--) { |
| 97 | + if (particles[i].lifeSpan < 0) { |
| 98 | + particles.splice(i, 1) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + function loop() { |
| 104 | + updateParticles() |
| 105 | + requestAnimationFrame(loop) |
| 106 | + } |
| 107 | + |
| 108 | + function Particle(x, y, canvasItem) { |
| 109 | + const lifeSpan = Math.floor(Math.random() * 60 + 60) |
| 110 | + this.initialLifeSpan = lifeSpan // |
| 111 | + this.lifeSpan = lifeSpan //ms |
| 112 | + this.velocity = { |
| 113 | + x: (Math.random() < 0.5 ? -1 : 1) * (Math.random() / 10), |
| 114 | + y: -0.4 + Math.random() * -1, |
| 115 | + } |
| 116 | + this.position = { x: x, y: y } |
| 117 | + this.canv = canvasItem |
| 118 | + |
| 119 | + this.baseDimension = 4 |
| 120 | + |
| 121 | + this.update = function(context) { |
| 122 | + this.position.x += this.velocity.x |
| 123 | + this.position.y += this.velocity.y |
| 124 | + this.velocity.x += ((Math.random() < 0.5 ? -1 : 1) * 2) / 75 |
| 125 | + this.velocity.y -= Math.random() / 600 |
| 126 | + this.lifeSpan-- |
| 127 | + |
| 128 | + const scale = |
| 129 | + 0.2 + (this.initialLifeSpan - this.lifeSpan) / this.initialLifeSpan |
| 130 | + |
| 131 | + context.fillStyle = "#e6f1f7" |
| 132 | + context.strokeStyle = "#3a92c5" |
| 133 | + context.beginPath() |
| 134 | + context.arc( |
| 135 | + this.position.x - (this.baseDimension / 2) * scale, |
| 136 | + this.position.y - this.baseDimension / 2, |
| 137 | + this.baseDimension * scale, |
| 138 | + 0, |
| 139 | + 2 * Math.PI |
| 140 | + ) |
| 141 | + |
| 142 | + context.stroke() |
| 143 | + context.fill() |
| 144 | + |
| 145 | + context.closePath() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + init() |
| 150 | +} |
0 commit comments