npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cursor-engine

v1.0.2

Published

User cursor engine for creating lightweight custom and interactive event-driven cursors

Downloads

9

Readme

Current stable version: 1.0.0 Licence: Apache 2.0

User cursor engine

This package provides an ability to create custom cursors and attach custom events handling logic to it. It's just an 'engine', so it's pretty lightweight and fast. It may not provide you an out-of-box ability to define and use it raw, but an ability to respond to varriaty of 'missing events' in couple with dynamic cursor's UI container and so create anything accordingly to your needs.

Usage

Creating custom cursor


import { UCECursorEngine } from "./cursor-engine";

export class TestCursor extends UCECursorEngine {
    // This tag is used to defined custom attribute name,
    // which will be gathered by the cursor
    protected attributeTag: string = 'cursor-attr';

    public constructor() {
        super();
        // Insert UI to the cursor, because cursor itself node
        // is just an empty container
        this.insertElements(customHTMLElement);
    };
};

Running cursor

const userCursor = new TestCursor();

// starts cursor rendering and turns on event responding
userCursor.Render();

// makes visible on screen
userCursor.Display();

Cursor does not have any visible styling, so to customize it's UI you need to insert element to it using method:

userCursor.insertElements()

First uppercase of a cursor property/method means it's public, while lowercase - protected.

Hide() method removes it from screen, and Freeze() - stops its render cycle, which means the cursor will be freezed according to it's during state on a screen.

Cursor gives you ability to manage custom events, linked with its life cycle, like pointer target or targeted element cursor sattribute changed.

Events fired not only when cursor moves, but something changed under the pointer. This means that it's not neccessary to move cursor to lookup for changes, it will do it under the hood, meaning more proper events handling of a moving elements for example.

Features

  • [x] Position serialisation

Cursor will stay in the same place even after reloading

Interface

import { UserCursorController } from 'CursorController'
interface UserCursorController

Show cursor on a screen

Display(customMountPoint?: HTMLElement): void

@param customMountPoint change default (document.body) node to mount cursor on @returns void

Hide cursor

Hide(): void

@returns void

Start cursor inner rendering loop

Render(): void

Render loop is responsible for cursor inner logic processing, which changes its state and UI. @returns void

Stop cursor inner render loop

Freeze(): void

Render loop is responsible for cursor inner logic processing, which changes its state and UI. @returns void

Docs

Set shift between real user pointer and UserCursor UI positions

setShift(xDifferencePX?: number, yDifferencePX?: number): void

How it works - for example user is holding his mouse pointer on a x: 10 and y: 20 coordinates. So, by default, cursor position wound be changed to x: 10 and y: 20, but cursor width and height are, for example, 5px and 5px respectively. On a screen it wound look like cursor triggers events when it's only his left upper corner hits a html node.

So to 'fix' that, you can set xDifferencePX and yDifferencePX. In this situatuation, after you set this values to 5 and 5, cursor will look proper and trigger events when its center hits a node. @param xDifferencePX difference between user real pointer on a screen and a cursor ui position for X coordinate @param yDifferencePX difference between user real pointer on a screen and a cursor ui position for Y coordinate @returns void

Add elements

protected insertElements(...elements: HTMLElement[]): void

Insert any elements to cursor.

Mounted screen

Hidden layer element, which acts like a container screen for cursor element and holds it within.

Cursor is mounted on Display() to this element, so it can be changed statically or dynamically.

protected cursorScreen: HTMLElement = UCEDefaultScreen

Custom cursor attribute name

HTMLElement's custom attribute name, which must be used to make cursor interactive and customise its behaviour.

This tag name can be used whatever you want, engine gives you access to it and determines its changes, so, for example, can be used to set custom styling to cursor, like css cursor do.

This approach gives to ability to hide cursor interface at all to interact with it, just set custom attribute on HTMLElement, like classname or style attribute works.

protected abstract attributeTag: string

Get current cursor state

Used to retrive cursor's state.

Method is exposed because of overhead of attaching additional methods to handle all variaty of possible events, more like when they don't event used, so feel free to use it as: window.addEventListener('event-type', (e) => { ev = cursor.dumpState(); })

Subscribing to events within cursor interface will require additional struct to contain created UCEEventHandlers and deleting unsused handlers.

Maybe will be solved with WeakMap in future releases, but, due to possibility to use window eventlistening, which will be more efficient, it's not the feature which is being worked on.

protected readonly dumpState: () => UCEEvent

Events

/**
 * Fired when attribute value of a target is changed
 */
protected onTagChange:     UCEEventHandler = () => undefined;
/**
 * Fired when target which cursor is currently
 * pointing to changed.
 */
protected onTargetChange:  UCEEventHandler = () => undefined;
/**
 * Fired when cursor node is being mouted to the screen.
 * Event fired after node was mounted.
 */
protected onCursorDisplay: UCEEventHandler = () => undefined;
/**
 * Fired when cursor is being removed from the screen.
 * Event is fired before node is removed.
 */
protected onCursorRemove:  UCEEventHandler = () => undefined;