react-canvas-workbench
v1.0.3
Published
Canvas workbench for react
Maintainers
Readme
react-canvas-workbench
react-canvas-workbench is a library that provides extensive and simple functionality for interacting with canvas out of the box
Features
- Out of the box, you can work with layers and elements, move, scale, rotate, etc.
- Low-level access to functionality for advanced customization
- Canvas functionality for drawing has been implemented
- Advanced text manipulation, personal fonts
- Ability to customize ready-made components
TODO:
- [x] More complete readme
- [x] Documentation on usage
- [ ] Unit tests
- [ ] Extended (paid) version
- [ ] ...
Extended (paid) version - Coming Soon
We're hard at work on a premium tier of react-canvas-workbench that will give power users everything they've been asking for - and more
What's planned?
| Benefit | What it unlocks for you | | --- | --- | | Commercial-ready licence | Freely modify, fork, and integrate the library in revenue-generating products for your own use and for client-facing apps | | Expanded toolset | • Larger collection of out-of-the-box canvas components• Extra low-level hooks for fine-grained control• Additional component-level customisation props• Ability to override core helper functions for maximum flexibility | | Scheduled enhancements & fixes | Planned release cadence with regular feature additions, performance tweaks, and bug fixes delivered directly to paid customers | | Feature-request pool access | Create new feature requests or vote on existing ones. The request pool is snap-shotted at intervals, and items with the most votes rise to the top of the roadmap. Have a direct say in what we build next! | | | |
Stay tuned! Watch the repository to be the first to know when the Extended version launches
And you can already support me with a donation, all early sponsors will receive a reward, you can see it on this page
Installation
$ npm install react-canvas-workbench
# or use yarn
$ yarn add react-canvas-workbenchQuick Start
import { ElementsMenu, EditorCanvas, EditorControls } from 'react-canvas-workbench';
export default () => {
return (
<div className={'editor'}>
<div className={'editor-elements'}>
<ElementsMenu />
</div>
<div className={'editor-content'}>
<EditorControls />
<EditorCanvas />
</div>
</div>
);
};You can also start with our example, a guide on how to use it is provided below
Example installation guide
- Copy the contents of the example folder
- Initialize the project by typing yarn init -y
- Type yarn install react react-dom react-canvas-workbench and yarn add --dev parcel
- Replace ../../../src/index with react-canvas-workbench in pages/Home/index.js
- Run the project yarn parcel index.html
Usage
useTranslation:
import { useTranslation } from 'react-canvas-workbench';
...
const { t, addResources, changeLanguage } = useTranslation();useTranslation is built on top of i18n and provides access to the following elements:
t(resource) - equivalent to the i18n function, accepts a path to the desired resource
addResources(language, resource) - allows you to add or override a resource if you want to use custom names or add a new language. You can see the current state of the resource in example/locales/en.json
changeLanguage(language) - switches the current language, works the same way as the similar function in i18n
useCustomization:
import { useCustomization } from 'react-canvas-workbench';
...
const { customization, setCustomizationSettings, changeCustomizationSettings, resetCustomizationSettings } = useCustomization();useCustomization provides access to the following elements for working with customization of built-in components:
customization - the current customization state of all components. The parameters for each component are identical to the parameters found in the component's corresponding customization field
setCustomizationSettings({ updater }) - sets customization settings for all components at once. updater can be either a function (with access to the current state) or a plain object. Customizable components: editorControls, elementsMenu, editorCanvas, colorPicker
changeCustomizationSettings({ component, updater }) - similar to setCustomizationSettings, but applies settings to a specific component only. Customizable components: editorControls, elementsMenu, editorCanvas, colorPicker
resetCustomizationSettings({ component }) - resets customization settings for the specified component. Resettable components: editorControls, elementsMenu, editorCanvas, colorPicker, all
useSettings:
import { useSettings } from 'react-canvas-workbench';
...
const { settings, changeSettings, resetSettings } = useSettings();useSettings provides access to the following elements for managing editor settings:
settings - the current state of the editor settings
changeSettings(updater) - allows updating editor settings. The updater can be either a function with access to the current state or a plain object
Available fields to modify:
{
"snap": 30,
"handleSize": 8,
"rotateHandleSize": 16,
"showMiddleLines": true,
"rotatehandleOffset": 30,
"positionChangeByArrowsShiftMultiplier": 10,
"positionChangeByArrows": {
"ArrowUp": { "x": 0, "y": -1 },
"ArrowRight": { "x": 1, "y": 0 },
"ArrowDown": { "x": 0, "y": 1 },
"ArrowLeft": { "x": -1, "y": 0 }
}
}- resetSettings() - resets the settings to their default values
useFonts:
import { useFonts } from 'react-canvas-workbench';
...
const { fonts, setFonts, addDefaultFonts } = useFonts();useFonts provides access to the following elements for managing editor fonts:
fonts - the current state of loaded fonts. Fonts can either be preloaded (if previously declared and loaded) or dynamically loaded after being added
setFonts(fonts) - sets the list of fonts to be used in the editor. The array should contain objects with the following structure: { family: string, url?: string, loaded: boolean }. The url is required only if loaded is false and the font needs to be loaded before use. For preloaded fonts, simply set loaded: true
addDefaultFonts() - adds 10 basic web-safe fonts that are available on all major platforms: Arial, Times New Roman, Courier New, Georgia, Verdana, Trebuchet MS, Tahoma, Palatino, Comic Sans MS, Impact
useContent:
import { useContent } from 'react-canvas-workbench';
...
const { width, height, elements, selectedElement, selectedElementId, changeWidth, changeHeight,changeElements, deselectElement, selectElementById, changeSelectedElement } = useContent();useContent provides access to the following elements for managing editor content:
width - current canvas width
height - current canvas height
elements - current state of all elements
selectedElement - the currently selected element (can be null)
selectedElementId - the ID of the selected element (can be null)
changeWidth(width) - updates the current canvas width
changeHeight(height) - updates the current canvas height
changeElements(updater) - updates the current elements state. The updater can be a function (with access to the current state) or a plain object. Available fields to update: elements, selectedElement, selectedElementId
selectElementById(elementId) - selects an element from the elements array by its ID
deselectElement() - deselects the currently selected element
changeSelectedElement(updater) - updates the state of the selected element. The updater can be a function (with access to the current state) or a plain object. Available fields to update: elements, selectedElement, selectedElementId
useHistory:
import { useHistory } from 'react-canvas-workbench';
...
const { canUndo, canRedo, list, index, current, undo, redo, reset } = useHistory();useHistory provides access to the following elements for managing the history of element changes:
canUndo - indicates whether an undo action is currently possible
canRedo - indicates whether a redo action is currently possible
list - the current history list of element states
index - the index of the current state within the history list
current - the current state of elements in history
undo() - reverts the last change
redo() - reapplies the last undone change
reset({ elements }) - resets the history state and overrides the initial history snapshot with the provided elements
ColorPicker:
import { ColorPicker } from 'react-canvas-workbench';
export default () => {
return (
<ColorPicker
color={color}
copyHexCallback={() => {}}
onColorChange={(hex) => {}}
/>
);
};color – a variable representing the current color
onColorChange – a function for updating the color state
copyHexCallback – a callback function triggered after the color is copied
ElementsMenu:
import { ElementsMenu } from 'react-canvas-workbench';
export default () => {
return <ElementsMenu />;
};A ready-to-use component that provides simple access to managing elements and layers
EditorControls:
import { EditorControls } from 'react-canvas-workbench';
export default () => {
return <EditorControls />;
};A ready-to-use component that provides simple access to managing the element change history
EditorCanvas:
import { EditorCanvas } from 'react-canvas-workbench';
export default () => {
return <EditorCanvas onPreviewChange={(canvasDataUrl) => {}} />;
};A ready-to-use component that provides direct access to the canvas and visual editing of elements
onPreviewChange – a function for updating the preview image
