New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

canvas-scrollbar

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

canvas-scrollbar

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

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

OptionTypeDefaultDescription
xnumber-Scrollbar's left position
ynumber-Scrollbar's top position
widthnumber-Scrollbar's width
heightnumber-Scrollbar's height
directionstring'y'Scroll direction: 'x' or 'y'
contentSizenumber0Total content size (width for horizontal, height for vertical)
viewportSizenumberCanvas dimensionVisible area size
styleobjectSee belowScrollbar styling options
onscrollfunction(pos, max) => {}Callback when scroll position changes

Style Options

OptionTypeDefaultDescription
colorstring'#000'Scrollbar thumb color
backgroundColorstring'#fff'Scrollbar track color
radiusnumber0Scrollbar corner radius
paddingnumber0Padding 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.

Keywords

scrollbar

FAQs

Package last updated on 15 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts