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

pizza_canvas

v1.0.1

Published

A javascript library to build pizza with canvas

Downloads

9

Readme

pizza_canvas

pizza_canvas is an open source project made with javascript that allows an application to build a pizza with canvas interface and interactions.

Designed for

Websites and applications who wants to deliver better experiences for your customers and any project that wants to implement a pizza interface with a simple way to integrate using an easy api for that.

Features

  • Loads a tray image as background
  • Sets a number of pizza slices
  • Sets an image and custom data to a specific slice
  • Remove an image and custom data from a specific slice
  • Preview a slice image on cursor position on hover
  • Identify a slice index by mouse position
  • Get all slices data
  • Get slice data by index
  • Clear all images and data

Installation

This is a pure javascript library available through the npm registry and no dependency is required.

$ npm install pizza_canvas

Usage

<canvas width="364" height="364" id="pizza"></canvas>
<script src="node_modules/pizza_canvas/src/pizza-canvas.js"></script>
<script>
    const pizza = new PizzaCanvas({
        canvasId: "pizza",
        background: "#e4e4e4",
        slices: 4,
        padding: 50,
        trayBorder: 12,
    });
</script>

You can set any size to your canvas and the library will adapt to this size.

To view a full example, just open the project library and run example/index.html in your browser.

  • In the example was used a library from jquery.ui to make a drag and drop experience but you can use any library to reproduce this approach.

API

  • Before call any function, be sure to create an instance of library:
    const pizza = new PizzaCanvas({
        //The canvas identifier;
        canvasId: "canvas_id",        
        //A style to the canvas if necessary;
        canvasBorder: "1px solid black",
        //A background to the canvas if necessary;
        canvasBackground: "#e4e4e4",
        //The initial number of slices to the pizza;        
        slices: 1,
        //The space between pizza and canvas border
        padding: 0,
        //The space between tray border and flavor slices;
        trayBorder: 0,
    });
  • Loading a tray image as background:
    pizza.setTrayAsset('path/to/yours/tray_image.png')
  • Setting the number of pizza slices:
    pizza.setSlices(8)
  • This event will be fired when a slice is clicked, then you can pass a callback function as a parameter where you will receive an index slice clicked or undefined if clicked out of some slice inside canvas:
    pizza.onSliceClick(function(index){
        console.log("pizza clicked on slice:", index);
    })
  • You can call any user interaction event like, mousemove, mouseclick, touch or any event according the library you are using, then, you can get the coordinate X and Y from this event and call fireSlicePosition to check if it is inside some slice, and if true, it will return the index slice, otherwise returns undefined:
    function mousemoved(event) {
        const index = pizza.fireSlicePosition(
            event.clientX, 
            event.clientY
        );
        console.log("mouse hover slice:", index)
    }
  • Using yours interaction event combined with 'fireSlicePosition' api, you will receive a slice index, then you can preview some image to this slice:
    const sliceIndex = 0;
    pizza.previewSlice(sliceIndex, 'path/to/yours/pizza_flavor.png')
  • Using yours interaction event combined with 'fireSlicePosition' api, you will receive a slice index, then if it is undefined, you can call 'clearPreview' to remove the preview image:
    pizza.clearPreview()
  • Using yours interaction event combined with 'fireSlicePosition' api, normally a drop event, or using 'onSliceClick' api, you will receive the slice index, then you can get the dropped or selected data and set to canvas:
    const sliceIndex = 0;
    const image = 'path/to/yours/pizza_flavor.png';
    const data = {
        id: 1,
        name: 'pepperoni',
    };
    pizza.setSliceAsset(sliceIndex, image, data)
  • Using yours interaction event combined with 'fireSlicePosition' api, or using 'onSliceClick' api, you will receive the slice index, then you can remove the slice data from canvas:
    const sliceIndex = 0;
    pizza.removeSlice(sliceIndex)
  • You can call 'getData' api any time to return all setted slices data:
    const data = pizza.getData();
    console.log('All setted flavors:', data)
  • Using yours interaction event combined with 'fireSlicePosition' api, or using 'onSliceClick' api, you will receive the slice index, then you can call 'getSliceData' api any time to return the selected data:
    const sliceIndex = 0;
    const data = pizza.getSliceData(sliceIndex);
    console.log('Selected slice data:', data)
  • You can call 'clear' api any time to clear all images and data from canvas:
    pizza.clear()

License

MIT