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

@curtishughes/pixel-editor

v4.0.1

Published

A web based pixel editor that utilizes the html canvas element to display pixels and handle user input. The goal of this package was provide a minimal interface to the canvas that would allow consumers to create their own tools and UI elements.

Downloads

14

Readme

Pixel Editor

A web based pixel editor that utilizes the html canvas element to display pixels and handle user input. The goal of this package was provide a minimal interface to the canvas that would allow consumers to create their own tools and UI elements.

Features

  • Configurable width and height
  • Undo/Redo/Clear
  • Mobile/Desktop event handling and pointer positions
  • Custom UI/tool compatibility

Default Tool Set

Even though the editor implementation empowers the consumer to create their own tools, the project has included some default tools:

  • Pencil
  • Rectangle
  • Line

Installation

yarn add @curtishughes/pixel-editor
npm install @curtishughes/pixel-editor

Usage

Pixel Editor is not coupled with any specific framework. However, I have included some examples of how it can be used with a few of the popular frontend frameworks:

React

import React, { useRef, useEffect, useState } from 'react';
import { PixelEditor, Pencil } from '@curtishughes/pixel-editor';

function App() {
  const editorRef = useRef<HTMLCanvasElement>(null);
  const [editor, setEditor] = useState<PixelEditor>();

  useEffect(() => {
    if (editorRef.current) {
      setEditor(new PixelEditor(editorRef.current, 64, 64, new Pencil('black')));
    }
  }, []);

  return (
    <>
      <canvas ref={editorRef} />
      <button onClick={() => { if (editor) editor.tool = new Pencil() }}>Eraser</button>
      <button onClick={() => { if (editor) editor.tool = new Pencil('black') }}>Pencil</button>
      <button onClick={() => { if (editor) editor.undo() }}>Undo</button>
      <button onClick={() => { if (editor) editor.redo() }}>Redo</button>
    </>
  );
}

export default App;

Vue

<template>
  <div>
    <canvas ref="editor" />
    <button @click="editor.tool = eraser">Eraser</button>
    <button @click="editor.tool = pencil">Pencil</button>
    <button @click="() => editor.undo()">Undo</button>
    <button @click="() => editor.redo()">Redo</button>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import { PixelEditor, Pencil } from '@curtishughes/pixel-editor';

@Component
export default class Editor extends Vue {
  private editor!: PixelEditor;

  private pencil = new Pencil('black');

  private eraser = new Pencil();

  mounted() {
    const canvas = this.$refs.editor as HTMLCanvasElement;
    this.editor = new PixelEditor(canvas, 64, 64, this.pencil);
  }
}
</script>

Creating Custom Tools

Custom tools will need to implement the Tool interface in order to handle user events:

export interface Tool {
  handlePointerDown: (position: Point, editor: PixelEditor) => void;
  handlePointerMove: (position: Point, editor: PixelEditor) => void;
  handlePointerUp: (position: Point, editor: PixelEditor) => void;
}

Once implemented, a custom tool can be used by assigning the editor's tool property to an instance of the custom tool:

editor.tool = new CustomTool();

Please refer to the default tool implementations for specific examples: Pencil, Rectangle, Line