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 🙏

© 2026 – Pkg Stats / Ryan Hefner

canvas-scrollbar

v1.0.0

Published

A lightweight, customizable scrollbar implementation for HTML Canvas elements. This library provides an efficient way to add scrolling capabilities to canvas-based applications.

Readme

canvas-scrollbar

version CI status codecov downloads size license

A lightweight, customizable scrollbar implementation for HTML Canvas elements. This library provides an efficient way to add scrolling capabilities to canvas-based applications.

应用于 Canvas 的自定义滚动条组件,支持水平和垂直方向滚动,可自定义样式和交互行为。

Features

  • 🚥 Scrolling horizontally and vertically
  • 💄 Customizable appearance
  • ⏰ Support mouse and wheel events
  • 🚀 No dependencies

Installation

npm

npm install canvas-scrollbar

browser

https://cdn.jsdelivr.net/npm/canvas-scrollbar/dist/index.min.js

Usage

const canvas = document.getElementById('myCanvas')
const scrollbar = new CanvasScrollbar(canvas, {
	direction: 'y', // 'y' for vertical, 'x' for horizontal
	contentSize: 1000, // Total content height/width
	viewportSize: canvas.height, // Visible area size
	onscroll: (position, maxScroll) => {
		// Handle scroll position changes
		console.log('Scrolled to:', position)
	},
})

[!TIP]

Vertical scrollbar defaults to right side.
Horizontal scrollbar defaults to bottom side.

API

Constructor

new CanvasScrollbar(canvas, options)

Parameters

  • canvas (HTMLCanvasElement): The canvas element to attach the scrollbar to
  • options (Object): Configuration options

Options

| Option | Type | Default | Description | | -------------- | -------- | ------------------ | -------------------------------------------------------------- | | x | number | - | Scrollbar's left position | | y | number | - | Scrollbar's top position | | width | number | - | Scrollbar's width | | height | number | - | Scrollbar's height | | direction | string | 'y' | Scroll direction: 'x' or 'y' | | contentSize | number | 0 | Total content size (width for horizontal, height for vertical) | | viewportSize | number | Canvas dimension | Visible area size | | style | object | See below | Scrollbar styling options | | onscroll | function | (pos, max) => {} | Callback when scroll position changes |

Style Options

| Option | Type | Default | Description | | ----------------- | ------ | ------- | ---------------------------------- | | color | string | '#000' | Scrollbar thumb color | | backgroundColor | string | '#fff' | Scrollbar track color | | radius | number | 0 | Scrollbar corner radius | | padding | number | 0 | Padding inside the scrollbar track |

Methods

draw()

Draws the scrollbar on the canvas.

scrollbar.draw()

scroll(delta)

Scrolls by the specified amount.

// Scroll down 50 pixels
scrollbar.scroll(50)

scrollTo(position)

Scrolls to a specific position.

// Scroll to position 200
scrollbar.scrollTo(200)

update(contentSize, viewportSize)

Updates the content and viewport dimensions.

// Update when content changes
scrollbar.update(newContentSize, newViewportSize)

destroy()

Removes event listeners and cleans up resources.

scrollbar.destroy()

Examples

const canvas = document.getElementById('myCanvas')
const ctx = canvas.getContext('2d')

// Content to be scrolled
const content = {
	height: 2000,
	render: function (pos) {
		// Clear canvas
		ctx.clearRect(0, 0, canvas.width, canvas.height)

		ctx.save()
		ctx.translate(0, -pos)

		ctx.fillStyle = '#007bff'
		for (let y = 0; y < content.height; y += 100) {
			for (let x = 0; x < canvas.width; x += 100) {
				ctx.fillRect(x + 10, y + 10, 80, 80)
			}
		}

		ctx.restore()
	},
}

// Create scrollbar
const scrollbar = new CanvasScrollbar(canvas, {
	direction: 'y',
	contentSize: content.height,
	style: {
		color: '#555',
		backgroundColor: '#eee',
		radius: 5,
		padding: 2,
	},
	onscroll: (position) => {
		// Redraw content at new scroll position
		content.render(position)
	},
})

// Initial render
content.render(0)
scrollbar.draw()

License

MIT © Mariner

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.