Tip of the day
Want to find something particular about the game and don't mind spoilers? Check the Wiki!

MediaWiki:Gadget-AnimatedCursorFollower.js

From Walkscape Walkthrough
Revision as of 01:17, 23 May 2026 by Bonez565 (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * Animated Cursor Follower
 */

(function () {
	'use strict';

	const CONFIG = {
		scale: 1,
		speed: 0.5,
		offsetX: 30,
		offsetY: 30
	};

	const state = {
		targetX: window.innerWidth / 2,
		targetY: window.innerHeight / 2,
		currentX: window.innerWidth / 2,
		currentY: window.innerHeight / 2,
		facingRight: true,
		reqId: null,
		mounted: false
	};

	function getFrameSize(sprite) {
		const styles = window.getComputedStyle(sprite);
		return parseInt(styles.getPropertyValue('--frame'), 10) || 48;
	}

	function mountFollower(sourceNode) {
		if (state.mounted) return;
		state.mounted = true;

		const pet = sourceNode.cloneNode(true);
		const wrapper = document.createElement('span');

		const frameSize = getFrameSize(sourceNode);
		const centerOffset = (frameSize * CONFIG.scale) / 2;

		wrapper.className = 'animated-cursor-follower-wrapper';

		Object.assign(wrapper.style, {
			position: 'fixed',
			top: '0',
			left: '0',
			pointerEvents: 'none',
			zIndex: '99999',
			margin: '0',
			padding: '0',
			width: `${frameSize * CONFIG.scale}px`,
			height: `${frameSize * CONFIG.scale}px`,
			willChange: 'transform'
		});

		pet.classList.add('animated-cursor-follower-pet');
		pet.style.setProperty('--scale', String(CONFIG.scale));

		// Keep the already-applied sprite data/style from the original clone.
		// Do not transform the sprite itself; transform only the wrapper.
		Object.assign(pet.style, {
			position: 'static',
			margin: '0',
			pointerEvents: 'none'
		});

		wrapper.appendChild(pet);
		document.body.appendChild(wrapper);

		document.addEventListener('mousemove', function (e) {
			state.targetX = e.clientX + CONFIG.offsetX;
			state.targetY = e.clientY + CONFIG.offsetY;
		}, { passive: true });

		function render() {
			state.currentX += (state.targetX - state.currentX) * CONFIG.speed;
			state.currentY += (state.targetY - state.currentY) * CONFIG.speed;

			if (state.targetX > state.currentX + 1) {
				state.facingRight = true;
			} else if (state.targetX < state.currentX - 1) {
				state.facingRight = false;
			}

			const scaleX = state.facingRight ? 1 : -1;

			wrapper.style.transform =
				`translate3d(${state.currentX - centerOffset}px, ` +
				`${state.currentY - centerOffset}px, 0) scaleX(${scaleX})`;

			state.reqId = requestAnimationFrame(render);
		}

		render();
	}

	function init() {
		const sprite = document.querySelector('span.ws-sprite');

		if (!sprite || !sprite.dataset.spriteApplied) {
			setTimeout(init, 250);
			return;
		}

		mountFollower(sprite);
	}

	if (document.readyState === 'complete') {
		init();
	} else {
		window.addEventListener('load', init);
	}

}());