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

interactive_canvas

v0.2.4

Published

Make a canvas zoomable a pannable with your mouse.

Downloads

5

Readme

Interactive Canvas

A simple manager for a canvas so that you can zoom with the scroll wheel and pan the canvas by dragging with the mouse.

Requires a callback for how to draw something to the canvas.

Usage

Get a reference to a canvas element, create an InteractiveCanvas object, and start the event listening and render loop. If at some later time you want the events and rendering to be stopped call the stop method.

Simple example of drawing a circle that can be moved around.

import { InteractiveCanvas } from "interactive_canvas";
const canvas = document.getElementById("myCanvas");

function draw(ctx) {
    ctx.beginPath();
    ctx.fillStyle = "red";
    ctx.arc(100, 100, 0, 2*Math.PI);
    ctx.fill();
}

var ic = new InteractiveCanvas(canvas, draw);
ic.start();

More complex example of grid lines, so you need to know the original coordinate system since things need to line up with the origin of the canvas.

import { InteractiveCanvas } from "interactive_canvas";
const CELL_SIZE = 25;
const canvas = document.getElementById("myCanvas");

function draw(ctx) {
    // not uncommon to want to be able to get the original coordinates
    // this will return the point in the transformed coordinate system
    var top_left = ctx.transformedPoint(0, 0);
    var bottom_right = ctx.transformedPoint(canvas.width, canvas.height);

    var start_row = Math.ceil(top_left.x/CELL_SIZE) - 1;
    var end_row = Math.ceil(bottom_right.x/CELL_SIZE) + 1;
    var start_col = Math.ceil(top_left.y/CELL_SIZE) - 1;
    var end_col = Math.ceil(bottom_right.y/CELL_SIZE) + 1;

    var low_x = start_row*CELL_SIZE;
    var low_y = start_col*CELL_SIZE;
    var high_x = end_row*CELL_SIZE;
    var high_y = end_col*CELL_SIZE;

    for (var x=low_x; x<=high_x; x += CELL_SIZE) {
        ctx.beginPath();
        ctx.moveTo(x, low_y);
        ctx.lineTo(x, high_y);
        ctx.stroke();
    }

    for (var y=low_y; y<=high_y; y += CELL_SIZE) {
        ctx.beginPath();
        ctx.moveTo(low_x, y);
        ctx.lineTo(high_x, y);
        ctx.stroke();
    }
}

var ic = new InteractiveCanvas(canvas, draw);
ic.start();