0.231227.0 • Published 4 months ago

elice-canvas-plugin v0.231227.0

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
4 months ago

Elice-canvas-plugin

Elice-canvas-plugin is a canvas drawing library that makes it easier and faster to implement handwriting on the web.

Initially, we will use the web_pen_sdk to implement pen drawing, but we will enhance it to implement features with other handwriting tools.

Getting Started

This example is how to use the libraries in your app. And you need to create instance.

HTML

First create new html file and add elements which include container. Default id is set to elice-smart-pen-container and if you want to customize then set container option in EliceSmartPen.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Elice Smart Pen - Core</title>
</head>
<body>
  <header>
      <h3>Elice Smart Pen</h1>
      <button id="pen-connection" type="button">펜 연결하기</button>
  </header>
  <div id="elice-smart-pen-container"></div>
</body>
</html>

Script

Add script tag which import plugin module. After import module you can initialize plugin by init method in instance.

<script type="module">
    import { EliceSmartPen } from 'elice-canvas-plugin';

    const eliceSmartPen = new EliceSmartPen();
    eliceSmartPen.init();
</script>

Event & Subscribe

This example codes are placed in script tag where you import plugin. So first you need to add pen connection handler at element.

const penConnectionButton = document.getElementById("pen-connection");
penConnectionButton?.addEventListener("click", () => {
void eliceSmartPen.togglePenConnection(true);
});

Then you need to subscribe instance store state to detect pen controller is created. If controller is created, you can create new drawing canvas by createDrawingStage method.

eliceSmartPen.store.subscribe((current, prev) => {
if (current.penController !== prev.penController) {
  const isConnected = Boolean(current.penController);

  if (penConnectionButton) {
    penConnectionButton.innerText = isConnected
      ? "펜 연결중"
      : "펜 연결하기";
  }

  if (current.penController) {
    eliceSmartPen.createDrawingStage();
  }
}
});

You can simply add event listeners by on method.

eliceSmartPen.on(EliceSmartPenEvents.PenUp, () => {
  localStorage.setItem("saved-drawing", eliceSmartPen.extractDrawingStage('json'));
});
eliceSmartPen.on(EliceSmartPenEvents.NeoPUI, (_, payload) => {
  alert(`This is Neo pen PUI data\n${JSON.stringify(payload)}`);
});

Provider

You need to pass instance as prop at EliceSmartPenProvider. It automatically call init method in instance. Finally you can use helpful hooks useEliceSmartPen and useEliceSmartPenState in Component.

import { useState } from 'react';
import { EliceSmartPen, EliceSmartPenProvider } from "elice-canvas-plugin";

function App() {
const [eliceSmartPen] = useState(() => new EliceSmartPen());

return (
  <EliceSmartPenProvider eliceSmartPen={eliceSmartPen}>
    <Tool/>
    <Note/>
  </EliceSmartPenProvider>
);
}

export default App;

Hooks

This plugin provide useEliceSmartPen, useEliceSmartPenState hooks. useEliceSmartPen returns instance that provide properties and method of canvas core logics. useEliceSmartPenState returns states in instance store. You can use this states to create on demand ui, effects, message events.

import React from 'react';
import { useEliceSmartPen, useEliceSmartPenState, EliceSmartPenEvents } from "elice-canvas-plugin";

function Tool() {
  const eliceSmartPen = useEliceSmartPen();
  const penController = useEliceSmartPenState((state => state.penController));

  const handlePenConnection = () => {
      eliceSmartPen.togglePenConnection(true);
  }

  // create drawing
  React.useEffect(() => {
      if(!penController){
          return;
      }

      eliceSmartPen.createDrawingStage();
  }, [penController]);

  React.useEffect(() => {
      // add listeners.
      eliceSmartPen.on(
      EliceSmartPenEvents.PenUp,
      (eliceSmartPen) => {
          console.log('Pen-up event is fired.');
      },
      );

      return () => {
          // remove listeners.
          eliceSmartPen.off(EliceSmartPenEvents.PenUp);
      }
  }, []);

  return (
      <div>
          <button onClick={handlePenConnection}>penController ? Connected : Connect</button>
      </div>
  )
}

export default Tool;

Container

Basically, this plugin find container by getElementById. By default it set to elice-smart-pen-container and also you can customize it. If container is not found, document body is set to container of drawing canvas. Canvas is basically set to absolute position, so you have to set relative position at container.

import { ELICE_SMART_PEN_CONTAINER } from "elice-canvas-plugin";

function Note() {
  return (
    <div
      id={ELICE_SMART_PEN_CONTAINER}
      style={{ position: "relative", width: "30rem", aspectRatio: 1 }}
    />
  )
}

export default Note;

Demos

All demos located in ./playground folder. There you will find Core (HTML, JS), React versions.

To open demo, run:

yarn core

Runs the playground which implement with core. Open http://localhost:3120 to view it in your browser.

yarn react

Runs the playground which implement with core and react. Open http://localhost:3121 to view it in your browser.

Core

All features are based on EliceSmartPen. You need to initialize it and optionally set configuration.

Methods & Properties

After you initialize EliceSmartPen, you have initialized instance in variable with helpful methods and properties:

propertiestype
storeStoreApi<EliceSmartPenState>State manager which used zustand.
optionsEliceSmartPenOptionsInitial configuration.

You can see store detail information in here.

methodstype
init() => voidInitialize configuration of pen, drawing, store.
togglePenConnection(value) => PromiseConnect or disconnect pen.
readDrawingStage(drawing) => voidRedraw passed drawing stage.
createDrawingStage() => voidCreate new drawing stage.
extractDrawingStage() => stringExtract drawing stage result that formatted by json.
clearDrawingStage() => voidClear drawing in current stage.
scanDrawingStage() => PromiseScan drawing stage to find overlapped drawings on inputs. Those drawings are converted to text which implement by ocr and set value of target input.
on(event, callback) => voidAdd event at event stack.
off(event) => voidRemove event which stored in event stack.

React

If you use react, you can use those helpful hooks. Before you use those hooks, you must wrap your app or page component with EliceSmartPenProvider.

hooks
useEliceSmartPenReact hooks that return EliceSmartPen instance.
useEliceSmartPenStoreReact hooks that return states in store.

Event

We support extra helpful custom events. For performance, this listeners are bind with debounce. You can get event types from enum EliceSmartPenEvents.

Eventtype
PenUp(eliceSmartPen) => voidThis event is fired when last of Pen_UP event callback is fired.
NeoPUI(eliceSmartPen, payload) => voidThis event is fired when you point ncode data in note.