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

chicken-scratch-native

v1.0.0

Published

A simple set of tools to create a no-frills react native drawing app

Downloads

2

Readme

chicken-scratch

Table of Contents

chicken-scratch-native is a React Native library designed to provide a ultra-minimalistic drawing interface for applications. It features customizable drawing tools, including pen color and width selectors, undo/redo functionality, and the ability to export drawings as SVG strings. This library is perfect for applications requiring user-generated drawings or annotations.

Installation

To install the chicken-scratch-native package, run:

npm install chicken-scratch-native

Usage

To use chicken-scratch-native in your project, wrap your app's component tree with the ChickenScratchProvider and then use the Canvas component where you want the drawing area to be. You can also use the button components provided by the library for drawing controls.

Basic Setup

Before creating a canvas, wrap your component (or application) with the ChickenScratchProvider. Next, include the Canvas component where you want the drawing area:

import React from "react";
import { View } from "react-native";
import { Canvas } from "chicken-scratch-native";
function Drawing() {
	return (
		<ChickenScratchContext>
			<View>
				<Canvas width={300} height={500} backgroundColor="#fff" />
			</View>
		</ChickenScratchContext>
	);
}
export default Drawing;

Buttons

Chicken scratch comes with a set of default buttons to help quick start development. To create custom ways to interact with canvas states, please see Using ChickenScratchContext

import React from "react";
import { View } from "react-native";
import {
	Canvas,
	UndoButton,
	RedoButton,
	ClearCanvasButton,
	ColorSelectorButton,
	WidthSelectorSlider,
	ToggleEraseStrokeButton,
} from "chicken-scratch-native";
function DrawingControls() {
	return (
		<View>
            <Canvas width={200} height={200}>
			<UndoButton />
            <RedoButton />
            <ClearCanvasButton />
            <ColorSelectorButton />
			<WidthSelectorSlider min={1} max={10} />
            <ToggleEraseStrokeButton />
		</View>
	);
}

API Reference

Canvas

| Prop | Type | Description | | ----------------- | ------ | -------------------------------------------------------------------------------------------------- | | width | number | Width of the canvas. | | height | number | Height of the canvas. | | fluid | bool | If set, the canvas will fill the container in the X direction (requires a height to still be set). | | fill | bool | If set, the canvas will fill in both X and Y directions (requires neither height nor width). | | backgroundColor | string | Background color of the canvas. |

ChickenScratchContext

This context provides access to drawing functions, canvas manipulation methods, and drawing properties that you can use throughout your application.

Properties

| Property | Type | Description | | ----------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------ | | strokeCollectionInitialized | number | A value that changes whenever the stroke collection is initialized or overwritten (begins at 0). Useful for detecting changes. | | strokes | | The current collection of drawn strokes. | | currentStroke | | The stroke currently being drawn. |

Functions

| Function | Description | | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | | overwriteStrokeCollection(strokeCollection: StrokeCollection) | Overwrites the current stroke collection with a new one. If no stroke collection is set, then one will be created. | | draw(x: number, y: number) | Begins or continues drawing a stroke at the specified coordinates. | | endDraw() | Finalizes the current stroke and adds it to the collection. | | removeStroke(x: number, y: number) | Removes a stroke at the specified coordinates. | | clear() | Clears the canvas of all strokes. | | setStateErase() | Sets the canvas state to erasing. | | setStateDraw() | Sets the canvas state to drawing. | | getCurrentState() | Returns the current canvas state, indicating whether the user is drawing or erasing. | | undo() | Reverts the last action (draw, erase, or clear). | | redo() | Reapplies an action that was previously undone. | | getCurrentStrokeColor() | Returns the current stroke color. | | setCurrentStrokeColor(color: string) | Sets the current stroke color. | | getCurrentStrokeWidth() | Returns the current stroke width. | | setCurrentStrokeWidth(width: number) | Sets the current stroke width. | | getCanvasWidth() | Returns the width of the canvas. | | getCanvasHeight() | Returns the height of the canvas. | | getSvgString() | Returns an SVG string representation of the current drawing. | | render() | Forces the canvas to re-render, useful for applying changes that don't automatically trigger a re-render. |

Using ChickenScratchContext

To interact with the drawing functionality, your components need to access ChickenScratchContext. Here's an example of using some context functions

import React, { useContext } from "react";
import { View, Button } from "react-native";
import { ChickenScratchContext } from "chicken-scratch-native";

// Note: to work properly, DrawingControls must be placed somewhere in the <ChickenScratchProvider> tree along side the canvas
const DrawingControls = () => {
	const { undo, redo, clear } = useContext(ChickenScratchContext);

	return (
		<View>
			<Button title="Undo" onPress={undo} />
			<Button title="Redo" onPress={redo} />
			<Button title="Clear" onPress={clear} />
		</View>
	);
};

Limitations

Feature Set

Being ultra-minimalistic, chicken-scratch-native lacks many advanced features found in more comprehensive drawing libraries, such as layer management, shapes, brush stroke, fill, text, and image import/export capabilities. It is primarily useful for situations where user input is required in the form of a sketch, such as a signature or whiteboard-style drawing game. In the future, some of these features may be added to improve quality of life for developers

Output

Currently, chicken-scratch-native can only output an SVG string of a user's drawing. This choice prioritizes ease of integration and scalability of output across different platforms and screen sizes. However, it may limit how output can be manipulated or used. Further updates may expand the list of available outputs

Performance

Chicken-scratch-native employs a rendering mechanism that triggers re-renders for each frame during user interaction with the Canvas component. This approach, while ensuring up-to-date visual feedback, can be resource-intensive. Despite this, our testing on a range of devices, including lower-end Android models, has shown that the library maintains a respectable performance level with minimal impact on usability.

The library currently utilizes a linear interpolation method to connect points, which simplifies implementation but may result in less smooth curves and edges in certain drawings. This effect is most noticeable in fast or complex movements.

License

This project is licensed under the MIT License - see the LICENSE file for details.