다이어그램 편집기: JavaScript 라이브러리

다이어그램 모델과 그리기: 순서도, 조직도, 네트워크도, 전기 회로도. 도형, 자동 경로 커넥터, 그룹, 페이지, 템플릿, SVG·PNG·PDF 내보내기. 모델에는 DOM이 필요 없습니다.

  • @openlibsdev/diagram-editor
  • v1.0.0
  • ES module
  • MIT

설치

번들러 또는 Node 방식 가져오기로:

npm install @openlibsdev/diagram-editor
import DiagramEditor from "@openlibsdev/diagram-editor";

또는 빌드 없이 페이지에서 바로(jsDelivr가 의존성을 해결합니다):

<script type="module">
import DiagramEditor from "https://cdn.jsdelivr.net/npm/@openlibsdev/diagram-editor@1.0.0/+esm";
</script>

사용법

import DiagramEditor from "@openlibsdev/diagram-editor";

const d = DiagramEditor.createDiagram();
const a = DiagramEditor.addNode(d, "terminator", 40, 40, { text: "Start" });
const b = DiagramEditor.addNode(d, "rect", 40, 160, { text: "Step" });
DiagramEditor.addEdge(d, a.id, b.id);
const svg = DiagramEditor.toSVG(d);               // a string
const json = DiagramEditor.serialize(d);

API

DiagramEditor.createDiagram()

빈 다이어그램을 만듭니다.

DiagramEditor.createProject()

페이지가 있는 프로젝트를 만듭니다.

DiagramEditor.addNode(d, type, x, y, props)

지정한 점에 도형을 추가합니다.

DiagramEditor.addEdge(d, from, to, props)

두 도형을 연결합니다.

DiagramEditor.remove(d, ids)

항목을 삭제합니다.

DiagramEditor.move(d, ids, dx, dy)

항목을 이동합니다.

DiagramEditor.align(d, ids, how)

항목을 정렬합니다.

DiagramEditor.distribute(d, ids, axis)

항목을 균등하게 배치합니다.

DiagramEditor.group(d, ids)

항목을 그룹으로 묶습니다.

DiagramEditor.layoutTree(d, opts)

도형을 트리 형태로 배치합니다.

DiagramEditor.route(d, e)

커넥터 경로를 다시 계산합니다.

DiagramEditor.toSVG(d, opts)

SVG로 된 다이어그램.

DiagramEditor.toCanvas(d, opts)

<canvas>로 된 다이어그램.

DiagramEditor.toBlob(d, format, opts)

파일로 된 다이어그램: svg, png, jpeg, webp, pdf.

DiagramEditor.serialize(x)

다이어그램 또는 프로젝트 → JSON.

DiagramEditor.parse(data)

JSON → 다이어그램 또는 프로젝트.

DiagramEditor.registerShape(type, shape)

도형 유형을 추가합니다.

DiagramEditor.SHAPES

도형 유형.

DiagramEditor.TEMPLATES

미리 만든 다이어그램.

DiagramEditor.findNamedColor(text, lang)

지정한 언어의 색 이름으로 이름 있는 색을 찾습니다(“진한 파랑” → #00008b).

DiagramEditor.listNamedColors(lang)

이름 있는 색을 지정한 언어의 이름과 함께 나열합니다.

DiagramEditor.ALIGNS

텍스트 정렬: 왼쪽, 가운데, 오른쪽.

DiagramEditor.APP

라이브러리의 이름, 버전, 출시일: {name, version, created}.

DiagramEditor.ARROWS

선 끝 모양: 화살표, 삼각형, 마름모, 원, ER의 까마귀 발.

DiagramEditor.BAND

컨테이너 제목 띠의 높이(픽셀).

DiagramEditor.DASHES

선 스타일: 실선, 파선, 점선(대시 패턴으로).

DiagramEditor.DOTS

선의 연결점 모드: 자동, 켜기, 끄기.

DiagramEditor.EDGE_DEFAULTS

새 선의 기본 속성: 끝 모양, 종류, 화살표, 색, 두께, 스타일, 텍스트.

DiagramEditor.FORMATS

내보내기 형식과 그 MIME 유형.

DiagramEditor.IMAGE_LIMIT

다이어그램에 보관하는 가장 큰 그림(data URL의 바이트 수, 3 MB).

DiagramEditor.LINE_KINDS

선 종류: 직선, 꺾은선(직각), 곡선.

DiagramEditor.MIN_SIZE

도형의 최소 너비와 높이(픽셀).

DiagramEditor.NODE_DEFAULTS

새 도형의 기본 속성: 텍스트, 채우기, 불투명도, 테두리, 글꼴, 정렬.

DiagramEditor.PAGE_DEFAULTS

페이지 기본 설정: 배경, 투명도, 격자, 맞춤, 격자 크기, 여백, 연결점.

DiagramEditor.SHAPE_SETS

편집기가 제공하는 세트별 도형: 기본, 순서도, UML, ER, BPMN, 네트워크, 전기, 논리.

DiagramEditor.SIDES

선이 붙을 수 있는 변: 자동, 위, 오른쪽, 아래, 왼쪽.

DiagramEditor.addBend(d, e, x, y)

선이 점 가까이 지나는 곳에 꺾임을 추가하고 그 인덱스를, 없으면 -1을 반환합니다.

DiagramEditor.anchor(n, side)

도형 윤곽의 한 변 가운데 점(회전 반영).

DiagramEditor.bounds(d, ids)

지정한 항목(또는 전체)을 둘러싼 상자. 빈 다이어그램이면 null.

DiagramEditor.clone(d)

다이어그램의 깊은 복사본.

DiagramEditor.contents(d, c)

컨테이너 안에 있는 도형.

DiagramEditor.cssFont(it)

항목의 CSS 글꼴 문자열(스타일, 굵기, 크기, 패밀리).

DiagramEditor.decorationBox(d)

setDecorations로 설정한 페이지 장식(예: 도면 틀)의 상자, 또는 null.

DiagramEditor.distToSegment(p, a, b)

점에서 선분까지의 거리.

DiagramEditor.drawPrimitives(ctx, prims)

canvas 2D 컨텍스트에 프리미티브(primitives()의 결과)를 그립니다.

DiagramEditor.duplicate(d, ids, dx, dy)

항목을 복사해 (dx, dy)만큼 옮겨 붙여 넣고 새 id를 반환합니다.

DiagramEditor.edgeAt(d, x, y, tol)

점 가까이 지나는 맨 위의 선, 또는 null.

DiagramEditor.edgeEnds(d, e)

선이 실제로 끝나는 위치: [시작, 끝]. 각각 점, 변, 핀, 비어 있는지 여부를 가집니다.

DiagramEditor.electricShape(W, H, o)

크기와 그림(선, 핀, 라벨 위치)으로 전기 기호 도형을 만듭니다(registerShape용).

DiagramEditor.exportBox(d, ids)

내보낼 영역: 지정한 항목과 바깥 라벨, 또는 장식을 포함한 다이어그램 전체.

DiagramEditor.extract(d, ids)

다이어그램의 일부(도형과 그 사이의 선)를 다른 곳에 삽입할 조각으로 복사합니다.

DiagramEditor.find(d, id)

id로 찾은 도형 또는 선.

DiagramEditor.findEdge(d, id)

id로 찾은 선, 또는 null.

DiagramEditor.findNode(d, id)

id로 찾은 도형, 또는 null.

DiagramEditor.insert(d, part, dx, dy)

extract로 만든 조각을 (dx, dy)만큼 옮겨 삽입합니다. 그룹은 새 이름을 받습니다. 새 id를 반환합니다.

DiagramEditor.itemsInRect(d, box)

사각형 안에 완전히 들어가는 도형과 선의 id.

DiagramEditor.junctions(d)

연결점을 그리는 위치(T자 연결과 세 개 이상의 전선이 교차하는 곳).

DiagramEditor.labelPlace(n)

기호 바깥 라벨의 위치: 아래, 위 또는 오른쪽.

DiagramEditor.labelText(n)

기호 라벨의 텍스트: 참조 번호, 값, 텍스트를 한 줄씩.

DiagramEditor.loadFonts(d)

다이어그램이 쓰는 모든 패밀리의 글꼴 파일을 불러와, 그리기와 측정에 실제 글꼴을 쓰게 합니다.

DiagramEditor.loadImages(d)

그리기 전에 다이어그램의 모든 그림을 불러옵니다.

DiagramEditor.markup(d, opts)

바깥 <svg> 없이 SVG 요소로 나타낸 다이어그램(직접 만든 SVG에 넣는 용도).

DiagramEditor.moveSegment(d, e, k, dx, dy)

그려진 선의 한 구간을 (dx, dy)만큼 옮깁니다.

DiagramEditor.namedColorTexts(lang)

지정한 언어의 색상 필드 라벨(색 선택기의 텍스트). 해당 언어가 없으면 영어.

DiagramEditor.nearestPin(n, x, y, toward)

점에 가장 가까운 도형 핀의 인덱스, 또는 -1.

DiagramEditor.nodeAt(d, x, y, pad)

점 아래의 맨 위 도형(컨테이너는 제목 띠와 테두리에서만).

DiagramEditor.normalize(d)

임의의 객체 → 유효한 다이어그램: 알 수 없는 값은 기본값으로, id는 고유하게, 끊어진 선은 제거.

DiagramEditor.normalizeProject(p)

임의의 객체 → 페이지가 있는 유효한 프로젝트.

DiagramEditor.order(d, ids, how)

도형을 그리는 순서를 바꿉니다: 맨 앞으로, 맨 뒤로, 앞으로, 뒤로.

DiagramEditor.orthoThrough(a, sa, vias, b, sb)

한 점에서 중간 점들을 거쳐 다른 점으로 가는 직각 꺾은선. 지정한 변을 따라 나가고 들어옵니다.

DiagramEditor.pins(n)

보이는 그대로의 도형 핀(전기 및 논리 기호): [{x, y, side}].

DiagramEditor.primitives(d, opts)

그리기 프리미티브(경로, 텍스트, 그림) 목록으로 나타낸 다이어그램. canvas나 SVG에 바로 쓸 수 있습니다.

DiagramEditor.removeBend(e, i)

인덱스로 지정한 선의 꺾임을 제거합니다.

DiagramEditor.resetBends(e)

선의 수동 꺾임을 모두 제거합니다(자동 배선으로 돌아감).

DiagramEditor.rotate(d, ids, dir)

도형을 각자의 중심을 기준으로 4분의 1 바퀴 회전합니다(1: 시계 방향, -1: 반시계 방향).

DiagramEditor.scalePath(d, sx, sy)

SVG 경로 데이터(M L H V Q C A Z)를 sx와 sy로 크기 조정합니다.

DiagramEditor.setCornerJoins(on)

전선 모서리의 연결점을 켜거나 끕니다.

DiagramEditor.setDecorations(fn)

페이지 장식(예: 도면 틀과 표제란)을 그리는 함수를 설정합니다.

DiagramEditor.setProjectHook(fn)

열리는 모든 프로젝트마다 호출되는 함수를 설정합니다(데이터 이전이나 보완용).

DiagramEditor.setRefText(fn)

라벨의 참조 번호 형식을 정하는 함수를 설정합니다.

DiagramEditor.sheetPlan(box)

다이어그램을 실제 크기의 A4 용지로 나누는 방식: 방향, 열, 행, 타일 크기.

DiagramEditor.snap(d, v)

맞춤이 켜져 있으면 좌표를 격자에 맞춰 반올림합니다.

DiagramEditor.splitWire(d, edgeId, x, y)

전선을 한 점에서 연결점으로 나눕니다. 연결점 도형 또는 null을 반환합니다.

DiagramEditor.template(name, texts)

템플릿 이름으로 고르는 기성 다이어그램(직접 넣은 텍스트 포함).

DiagramEditor.textBox(n)

도형의 텍스트가 놓이는 상자와 그 줄, 정렬.

DiagramEditor.textFontData

텍스트와 글꼴 블록의 데이터: 글꼴 목록, 스타일 불러오기, 글꼴 파일 추가, 언어별 블록 라벨, 기본값.

DiagramEditor.ungroup(d, ids)

지정한 항목의 그룹을 해제하고 그룹에서 빠진 도형 수를 반환합니다.

DiagramEditor.visualBox(n)

도형이 그림에서 차지하는 모든 것: 상자와 바깥 라벨.

DiagramEditor.wirePoint(d, x, y, tol, except)

주어진 거리 안에서 가장 가까운 전선 위의 점, 또는 null(놓은 전선 끝이 여기에 붙습니다).

DiagramEditor.withContents(d, ids)

id와 그중 각 컨테이너의 내용.

DiagramEditor.withGroups(d, ids)

id와 그 그룹의 다른 구성원.

DiagramEditor.wrapText(text, it, maxWidth)

텍스트를 지정한 너비보다 넓지 않은 줄로 나눕니다(단어는 나누지 않음).

의존성

라이브러리가 불러오는 것과 그 라이선스(모두 라이선스 페이지에 있습니다).

이름버전라이선스파일
@openlibsdev/font-forge1.0.0MITGitHub
@openlibsdev/image-forge1.0.0MITGitHub
@imagemagick/magick-wasm0.0.35Apache-2.0 라이선스 · 소스 코드
@imagemagick/magick-wasm0.0.35Apache-2.0 라이선스 · 소스 코드
@simonwep/pickr1.10.2MIT 라이선스 · 소스 코드
@simonwep/pickr1.10.2MIT 라이선스 · 소스 코드
dejavu-fonts-ttf2.37.3Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain 라이선스 · 소스 코드
imagetracerjs1.2.6Unlicense 라이선스 · 소스 코드
jspdf2.5.2MIT 라이선스 · 소스 코드