Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@drauu/core

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@drauu/core - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

58

dist/index.d.ts

@@ -5,5 +5,22 @@ interface Brush {

/**
* @default 4
* @default 'transparent'
*/
smoothness?: number;
fill?: string;
draw?: {
/**
* @default 4
*/
smoothness?: number;
/**
* WIP
* @default 0
*/
pressure?: number;
};
rectangle?: {
/**
* @default 0
*/
radius?: number;
};
}

@@ -14,3 +31,3 @@ interface Point {

}
declare type DrawingMode = 'draw' | 'line';
declare type DrawingMode = 'draw' | 'line' | 'rectangle' | 'ellipse';
interface Options {

@@ -22,11 +39,14 @@ el?: string | SVGSVGElement;

declare abstract class BaseModel {
declare abstract class BaseModel<T extends SVGElement> {
private drauu;
protected event: MouseEvent | TouchEvent;
protected point: Point;
event: MouseEvent | TouchEvent;
point: Point;
start: Point;
el: T | null;
constructor(drauu: Drauu);
get brush(): Brush;
onStart(point: Point): SVGElement | undefined;
onMove(point: Point): boolean;
onEnd(point: Point): SVGElement | boolean | undefined;
get brush(): Brush;
get shiftPressed(): boolean;
getMousePosition(event: MouseEvent | TouchEvent): {

@@ -37,5 +57,15 @@ x: number;

protected createElement<K extends keyof SVGElementTagNameMap>(name: K): SVGElementTagNameMap[K];
protected attr(name: keyof T, value: string | number): void;
private _setEvent;
/**
* @internal
*/
_eventDown(event: MouseEvent | TouchEvent): SVGElement | undefined;
/**
* @internal
*/
_eventMove(event: MouseEvent | TouchEvent): boolean;
/**
* @internal
*/
_eventUp(event: MouseEvent | TouchEvent): boolean | SVGElement | undefined;

@@ -49,7 +79,9 @@ }

brush: Brush;
shiftPressed: boolean;
private _models;
private tempNode;
private undoStack;
private _currentNode;
private _undoStack;
private _disposables;
constructor(options?: Options);
get model(): BaseModel;
get model(): BaseModel<SVGElement>;
mount(selector: string | SVGSVGElement): void;

@@ -60,5 +92,7 @@ unmounted(): void;

private eventMove;
private eventDown;
private eventUp;
private eventStart;
private eventEnd;
private eventKeyboard;
private commit;
clear(): void;
cancel(): void;

@@ -65,0 +99,0 @@ }

@@ -6,2 +6,5 @@ (() => {

}
function numSort(a, b) {
return a - b;
}

@@ -14,6 +17,5 @@ // src/models/base.ts

this.point = void 0;
this.start = void 0;
this.el = null;
}
get brush() {
return this.drauu.brush;
}
onStart(point) {

@@ -28,2 +30,8 @@ return void 0;

}
get brush() {
return this.drauu.brush;
}
get shiftPressed() {
return this.drauu.shiftPressed;
}
getMousePosition(event) {

@@ -38,4 +46,5 @@ const rect = this.drauu.el.getBoundingClientRect();

createElement(name) {
var _a;
const el = document.createElementNS("http://www.w3.org/2000/svg", name);
el.setAttribute("fill", "transparent");
el.setAttribute("fill", (_a = this.brush.fill) != null ? _a : "transparent");
el.setAttribute("stroke", this.brush.color);

@@ -46,2 +55,5 @@ el.setAttribute("stroke-width", this.brush.size.toString());

}
attr(name, value) {
this.el.setAttribute(name, value.toString());
}
_setEvent(event) {

@@ -53,2 +65,3 @@ this.event = event;

this._setEvent(event);
this.start = this.point;
return this.onStart(this.point);

@@ -71,15 +84,14 @@ }

this.smoothBuffer = [];
this.path = null;
this.strPath = "";
}
onStart(point) {
this.path = this.createElement("path");
this.el = this.createElement("path");
this.smoothBuffer = [];
this.appendToBuffer(point);
this.strPath = `M${decimal(point.x)} ${decimal(point.y)}`;
this.path.setAttribute("d", this.strPath);
return this.path;
this.attr("d", this.strPath);
return this.el;
}
onMove(point) {
if (!this.path)
if (!this.el)
return false;

@@ -91,4 +103,4 @@ this.appendToBuffer(point);

onEnd() {
const path = this.path;
this.path = null;
const path = this.el;
this.el = null;
if (!path)

@@ -101,4 +113,4 @@ return false;

get smoothness() {
var _a;
return (_a = this.brush.smoothness) != null ? _a : 4;
var _a, _b;
return (_b = (_a = this.brush.draw) == null ? void 0 : _a.smoothness) != null ? _b : 4;
}

@@ -121,3 +133,3 @@ appendToBuffer(point) {

}
this.path.setAttribute("d", this.strPath + tmpPath);
this.el.setAttribute("d", this.strPath + tmpPath);
}

@@ -146,26 +158,113 @@ getAveragePoint(offset) {

// src/models/ellipse.ts
var EllipseModel = class extends BaseModel {
onStart(point) {
this.el = this.createElement("ellipse");
this.attr("cx", point.x);
this.attr("cy", point.y);
return this.el;
}
onMove(point) {
if (!this.el || !this.start)
return false;
let dx = Math.abs(point.x - this.start.x);
let dy = Math.abs(point.y - this.start.y);
if (this.shiftPressed) {
const d = Math.min(dx, dy);
dx = d;
dy = d;
}
this.attr("rx", dx);
this.attr("ry", dy);
return true;
}
onEnd() {
const path = this.el;
this.el = null;
if (!path)
return false;
if (!path.getTotalLength())
return false;
return true;
}
};
// src/models/line.ts
var LineModel = class extends BaseModel {
constructor() {
super(...arguments);
this.line = null;
onStart(point) {
this.el = this.createElement("line");
this.attr("x1", point.x);
this.attr("y1", point.y);
this.attr("x2", point.x);
this.attr("y2", point.y);
return this.el;
}
onMove(point) {
if (!this.el)
return false;
let { x, y } = point;
if (this.shiftPressed) {
const dx = point.x - this.start.x;
const dy = point.y - this.start.y;
if (dy !== 0) {
let slope = dx / dy;
slope = Math.round(slope);
if (Math.abs(slope) <= 1) {
x = this.start.x + dy * slope;
y = this.start.y + dy;
} else {
x = this.start.x + dx;
y = this.start.y;
}
}
}
this.attr("x2", x);
this.attr("y2", y);
return true;
}
onEnd() {
const path = this.el;
this.el = null;
if (!path)
return false;
if (!path.getTotalLength())
return false;
return true;
}
};
// src/models/rect.ts
var RectModel = class extends BaseModel {
onStart(point) {
this.line = this.createElement("line");
this.line.setAttribute("x1", point.x.toString());
this.line.setAttribute("y1", point.y.toString());
this.line.setAttribute("x2", point.x.toString());
this.line.setAttribute("y2", point.y.toString());
return this.line;
var _a;
this.el = this.createElement("rect");
if ((_a = this.brush.rectangle) == null ? void 0 : _a.radius) {
this.attr("rx", this.brush.rectangle.radius);
this.attr("ry", this.brush.rectangle.radius);
}
this.attr("x", point.x);
this.attr("y", point.y);
return this.el;
}
onMove(point) {
if (!this.line)
if (!this.el || !this.start)
return false;
this.line.setAttribute("x2", point.x.toString());
this.line.setAttribute("y2", point.y.toString());
const [x1, x2] = [this.start.x, point.x].sort(numSort);
const [y1, y2] = [this.start.y, point.y].sort(numSort);
let dx = x2 - x1;
let dy = y2 - y1;
if (this.shiftPressed) {
const d = Math.min(dx, dy);
dx = d;
dy = d;
}
this.attr("x", x1);
this.attr("y", y1);
this.attr("width", dx);
this.attr("height", dy);
return true;
}
onEnd() {
const path = this.line;
this.line = null;
const path = this.el;
this.el = null;
if (!path)

@@ -183,3 +282,5 @@ return false;

draw: new DrawModel(drauu),
line: new LineModel(drauu)
line: new LineModel(drauu),
rectangle: new RectModel(drauu),
ellipse: new EllipseModel(drauu)
};

@@ -193,4 +294,6 @@ }

this.el = null;
this.shiftPressed = false;
this._models = createModels(this);
this.undoStack = [];
this._undoStack = [];
this._disposables = [];
this.brush = options.brush || { color: "black", size: 2 };

@@ -213,10 +316,29 @@ this.mode = options.mode || "draw";

throw new Error("[drauu] can only mount to a SVG element");
this.el.addEventListener("mousedown", this.eventDown.bind(this), false);
this.el.addEventListener("touchstart", this.eventDown.bind(this), false);
this.el.addEventListener("mousemove", this.eventMove.bind(this), false);
this.el.addEventListener("touchmove", this.eventMove.bind(this), false);
this.el.addEventListener("mouseup", this.eventUp.bind(this), false);
this.el.addEventListener("touchend", this.eventUp.bind(this), false);
const el = this.el;
const start = this.eventStart.bind(this);
const move = this.eventMove.bind(this);
const end = this.eventEnd.bind(this);
const keyboard = this.eventKeyboard.bind(this);
el.addEventListener("mousedown", start, false);
el.addEventListener("touchstart", start, false);
el.addEventListener("mousemove", move, false);
el.addEventListener("touchmove", move, false);
el.addEventListener("mouseup", end, false);
el.addEventListener("touchend", end, false);
window.addEventListener("keydown", keyboard, false);
window.addEventListener("keyup", keyboard, false);
this._disposables.push(() => {
el.removeEventListener("mousedown", start, false);
el.removeEventListener("touchstart", start, false);
el.removeEventListener("mousemove", move, false);
el.removeEventListener("touchmove", move, false);
el.removeEventListener("mouseup", end, false);
el.removeEventListener("touchend", end, false);
window.removeEventListener("keydown", keyboard, false);
window.removeEventListener("keyup", keyboard, false);
});
}
unmounted() {
this._disposables.forEach((fn) => fn());
this._disposables.length = 0;
}

@@ -227,3 +349,3 @@ undo() {

return false;
this.undoStack.push(el.lastElementChild.cloneNode(true));
this._undoStack.push(el.lastElementChild.cloneNode(true));
el.lastElementChild.remove();

@@ -233,5 +355,5 @@ return true;

redo() {
if (!this.undoStack.length)
if (!this._undoStack.length)
return false;
this.el.appendChild(this.undoStack.pop());
this.el.appendChild(this._undoStack.pop());
return true;

@@ -245,24 +367,36 @@ }

}
eventDown(event) {
eventStart(event) {
event.stopPropagation();
event.preventDefault();
this.tempNode = this.model._eventDown(event);
if (this.tempNode)
this.el.appendChild(this.tempNode);
this._currentNode = this.model._eventDown(event);
if (this._currentNode)
this.el.appendChild(this._currentNode);
}
eventUp(event) {
eventEnd(event) {
const result = this.model._eventUp(event);
if (!result)
if (!result) {
this.cancel();
else
} else {
if (result instanceof Element && result !== this._currentNode)
this._currentNode = result;
this.commit();
}
}
eventKeyboard(event) {
this.shiftPressed = event.shiftKey;
this.model.onMove(this.model.point);
}
commit() {
this.undoStack.length = 0;
this.tempNode = void 0;
this._undoStack.length = 0;
this._currentNode = void 0;
}
clear() {
this._undoStack.length = 0;
this.cancel();
this.el.innerHTML = "";
}
cancel() {
if (this.tempNode) {
this.el.removeChild(this.tempNode);
this.tempNode = void 0;
if (this._currentNode) {
this.el.removeChild(this._currentNode);
this._currentNode = void 0;
}

@@ -269,0 +403,0 @@ }

@@ -19,2 +19,5 @@ var __defProp = Object.defineProperty;

}
function numSort(a, b) {
return a - b;
}

@@ -27,6 +30,5 @@ // src/models/base.ts

this.point = void 0;
this.start = void 0;
this.el = null;
}
get brush() {
return this.drauu.brush;
}
onStart(point) {

@@ -41,2 +43,8 @@ return void 0;

}
get brush() {
return this.drauu.brush;
}
get shiftPressed() {
return this.drauu.shiftPressed;
}
getMousePosition(event) {

@@ -51,4 +59,5 @@ const rect = this.drauu.el.getBoundingClientRect();

createElement(name) {
var _a;
const el = document.createElementNS("http://www.w3.org/2000/svg", name);
el.setAttribute("fill", "transparent");
el.setAttribute("fill", (_a = this.brush.fill) != null ? _a : "transparent");
el.setAttribute("stroke", this.brush.color);

@@ -59,2 +68,5 @@ el.setAttribute("stroke-width", this.brush.size.toString());

}
attr(name, value) {
this.el.setAttribute(name, value.toString());
}
_setEvent(event) {

@@ -66,2 +78,3 @@ this.event = event;

this._setEvent(event);
this.start = this.point;
return this.onStart(this.point);

@@ -84,15 +97,14 @@ }

this.smoothBuffer = [];
this.path = null;
this.strPath = "";
}
onStart(point) {
this.path = this.createElement("path");
this.el = this.createElement("path");
this.smoothBuffer = [];
this.appendToBuffer(point);
this.strPath = `M${decimal(point.x)} ${decimal(point.y)}`;
this.path.setAttribute("d", this.strPath);
return this.path;
this.attr("d", this.strPath);
return this.el;
}
onMove(point) {
if (!this.path)
if (!this.el)
return false;

@@ -104,4 +116,4 @@ this.appendToBuffer(point);

onEnd() {
const path = this.path;
this.path = null;
const path = this.el;
this.el = null;
if (!path)

@@ -114,4 +126,4 @@ return false;

get smoothness() {
var _a;
return (_a = this.brush.smoothness) != null ? _a : 4;
var _a, _b;
return (_b = (_a = this.brush.draw) == null ? void 0 : _a.smoothness) != null ? _b : 4;
}

@@ -134,3 +146,3 @@ appendToBuffer(point) {

}
this.path.setAttribute("d", this.strPath + tmpPath);
this.el.setAttribute("d", this.strPath + tmpPath);
}

@@ -159,26 +171,113 @@ getAveragePoint(offset) {

// src/models/ellipse.ts
var EllipseModel = class extends BaseModel {
onStart(point) {
this.el = this.createElement("ellipse");
this.attr("cx", point.x);
this.attr("cy", point.y);
return this.el;
}
onMove(point) {
if (!this.el || !this.start)
return false;
let dx = Math.abs(point.x - this.start.x);
let dy = Math.abs(point.y - this.start.y);
if (this.shiftPressed) {
const d = Math.min(dx, dy);
dx = d;
dy = d;
}
this.attr("rx", dx);
this.attr("ry", dy);
return true;
}
onEnd() {
const path = this.el;
this.el = null;
if (!path)
return false;
if (!path.getTotalLength())
return false;
return true;
}
};
// src/models/line.ts
var LineModel = class extends BaseModel {
constructor() {
super(...arguments);
this.line = null;
onStart(point) {
this.el = this.createElement("line");
this.attr("x1", point.x);
this.attr("y1", point.y);
this.attr("x2", point.x);
this.attr("y2", point.y);
return this.el;
}
onMove(point) {
if (!this.el)
return false;
let { x, y } = point;
if (this.shiftPressed) {
const dx = point.x - this.start.x;
const dy = point.y - this.start.y;
if (dy !== 0) {
let slope = dx / dy;
slope = Math.round(slope);
if (Math.abs(slope) <= 1) {
x = this.start.x + dy * slope;
y = this.start.y + dy;
} else {
x = this.start.x + dx;
y = this.start.y;
}
}
}
this.attr("x2", x);
this.attr("y2", y);
return true;
}
onEnd() {
const path = this.el;
this.el = null;
if (!path)
return false;
if (!path.getTotalLength())
return false;
return true;
}
};
// src/models/rect.ts
var RectModel = class extends BaseModel {
onStart(point) {
this.line = this.createElement("line");
this.line.setAttribute("x1", point.x.toString());
this.line.setAttribute("y1", point.y.toString());
this.line.setAttribute("x2", point.x.toString());
this.line.setAttribute("y2", point.y.toString());
return this.line;
var _a;
this.el = this.createElement("rect");
if ((_a = this.brush.rectangle) == null ? void 0 : _a.radius) {
this.attr("rx", this.brush.rectangle.radius);
this.attr("ry", this.brush.rectangle.radius);
}
this.attr("x", point.x);
this.attr("y", point.y);
return this.el;
}
onMove(point) {
if (!this.line)
if (!this.el || !this.start)
return false;
this.line.setAttribute("x2", point.x.toString());
this.line.setAttribute("y2", point.y.toString());
const [x1, x2] = [this.start.x, point.x].sort(numSort);
const [y1, y2] = [this.start.y, point.y].sort(numSort);
let dx = x2 - x1;
let dy = y2 - y1;
if (this.shiftPressed) {
const d = Math.min(dx, dy);
dx = d;
dy = d;
}
this.attr("x", x1);
this.attr("y", y1);
this.attr("width", dx);
this.attr("height", dy);
return true;
}
onEnd() {
const path = this.line;
this.line = null;
const path = this.el;
this.el = null;
if (!path)

@@ -196,3 +295,5 @@ return false;

draw: new DrawModel(drauu),
line: new LineModel(drauu)
line: new LineModel(drauu),
rectangle: new RectModel(drauu),
ellipse: new EllipseModel(drauu)
};

@@ -206,4 +307,6 @@ }

this.el = null;
this.shiftPressed = false;
this._models = createModels(this);
this.undoStack = [];
this._undoStack = [];
this._disposables = [];
this.brush = options.brush || { color: "black", size: 2 };

@@ -226,10 +329,29 @@ this.mode = options.mode || "draw";

throw new Error("[drauu] can only mount to a SVG element");
this.el.addEventListener("mousedown", this.eventDown.bind(this), false);
this.el.addEventListener("touchstart", this.eventDown.bind(this), false);
this.el.addEventListener("mousemove", this.eventMove.bind(this), false);
this.el.addEventListener("touchmove", this.eventMove.bind(this), false);
this.el.addEventListener("mouseup", this.eventUp.bind(this), false);
this.el.addEventListener("touchend", this.eventUp.bind(this), false);
const el = this.el;
const start = this.eventStart.bind(this);
const move = this.eventMove.bind(this);
const end = this.eventEnd.bind(this);
const keyboard = this.eventKeyboard.bind(this);
el.addEventListener("mousedown", start, false);
el.addEventListener("touchstart", start, false);
el.addEventListener("mousemove", move, false);
el.addEventListener("touchmove", move, false);
el.addEventListener("mouseup", end, false);
el.addEventListener("touchend", end, false);
window.addEventListener("keydown", keyboard, false);
window.addEventListener("keyup", keyboard, false);
this._disposables.push(() => {
el.removeEventListener("mousedown", start, false);
el.removeEventListener("touchstart", start, false);
el.removeEventListener("mousemove", move, false);
el.removeEventListener("touchmove", move, false);
el.removeEventListener("mouseup", end, false);
el.removeEventListener("touchend", end, false);
window.removeEventListener("keydown", keyboard, false);
window.removeEventListener("keyup", keyboard, false);
});
}
unmounted() {
this._disposables.forEach((fn) => fn());
this._disposables.length = 0;
}

@@ -240,3 +362,3 @@ undo() {

return false;
this.undoStack.push(el.lastElementChild.cloneNode(true));
this._undoStack.push(el.lastElementChild.cloneNode(true));
el.lastElementChild.remove();

@@ -246,5 +368,5 @@ return true;

redo() {
if (!this.undoStack.length)
if (!this._undoStack.length)
return false;
this.el.appendChild(this.undoStack.pop());
this.el.appendChild(this._undoStack.pop());
return true;

@@ -258,24 +380,36 @@ }

}
eventDown(event) {
eventStart(event) {
event.stopPropagation();
event.preventDefault();
this.tempNode = this.model._eventDown(event);
if (this.tempNode)
this.el.appendChild(this.tempNode);
this._currentNode = this.model._eventDown(event);
if (this._currentNode)
this.el.appendChild(this._currentNode);
}
eventUp(event) {
eventEnd(event) {
const result = this.model._eventUp(event);
if (!result)
if (!result) {
this.cancel();
else
} else {
if (result instanceof Element && result !== this._currentNode)
this._currentNode = result;
this.commit();
}
}
eventKeyboard(event) {
this.shiftPressed = event.shiftKey;
this.model.onMove(this.model.point);
}
commit() {
this.undoStack.length = 0;
this.tempNode = void 0;
this._undoStack.length = 0;
this._currentNode = void 0;
}
clear() {
this._undoStack.length = 0;
this.cancel();
this.el.innerHTML = "";
}
cancel() {
if (this.tempNode) {
this.el.removeChild(this.tempNode);
this.tempNode = void 0;
if (this._currentNode) {
this.el.removeChild(this._currentNode);
this._currentNode = void 0;
}

@@ -282,0 +416,0 @@ }

{
"name": "@drauu/core",
"version": "0.0.1",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"unpkg": "dist/index.global.js",
"jsdelivr": "dist/index.global.js",
"funding": "https://github.com/sponsors/antfu",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/antfu/drauu/issues"
},
"homepage": "https://github.com/antfu/drauu#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/antfu/drauu.git"
},
"keywords": [],
"files": [
"dist"
],
"scripts": {
"dev": "nr build --watch",
"build": "tsup src/index.ts --format esm,cjs,iife --dts --no-splitting --clean"
}
}
"name": "@drauu/core",
"version": "0.0.2",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"unpkg": "dist/index.global.js",
"jsdelivr": "dist/index.global.js",
"funding": "https://github.com/sponsors/antfu",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/antfu/drauu/issues"
},
"homepage": "https://github.com/antfu/drauu#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/antfu/drauu.git"
},
"keywords": [],
"files": [
"dist"
],
"scripts": {
"dev": "nr build --watch",
"build": "tsup src/index.ts --format esm,cjs,iife --dts --no-splitting --clean"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc