@drauu/core
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -22,7 +22,18 @@ interface Unsubscribe { | ||
dasharray?: string; | ||
/** | ||
* @expiremental | ||
* @default true | ||
*/ | ||
pressure?: boolean; | ||
draw?: { | ||
/** | ||
* Read the presure from the given sensor and change the weight of the stroke | ||
* | ||
* @expiremental | ||
* @default false | ||
*/ | ||
pressure?: boolean; | ||
/** | ||
* Simplify the points of the lines | ||
* | ||
* @expiremental | ||
* @default false | ||
*/ | ||
simplify?: boolean; | ||
}; | ||
rectangle?: { | ||
@@ -29,0 +40,0 @@ /** |
@@ -16,3 +16,3 @@ (() => { | ||
// src/utils.ts | ||
// src/utils/index.ts | ||
function numSort(a, b) { | ||
@@ -32,2 +32,76 @@ return a - b; | ||
// src/utils/simpify.ts | ||
function getSqDist(p1, p2) { | ||
const dx = p1.x - p2.x; | ||
const dy = p1.y - p2.y; | ||
return dx * dx + dy * dy; | ||
} | ||
function getSqSegDist(p, p1, p2) { | ||
let x = p1.x; | ||
let y = p1.y; | ||
let dx = p2.x - x; | ||
let dy = p2.y - y; | ||
if (dx !== 0 || dy !== 0) { | ||
const t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy); | ||
if (t > 1) { | ||
x = p2.x; | ||
y = p2.y; | ||
} else if (t > 0) { | ||
x += dx * t; | ||
y += dy * t; | ||
} | ||
} | ||
dx = p.x - x; | ||
dy = p.y - y; | ||
return dx * dx + dy * dy; | ||
} | ||
function simplifyRadialDist(points, sqTolerance) { | ||
let prevPoint = points[0]; | ||
const newPoints = [prevPoint]; | ||
let point; | ||
for (let i = 1, len = points.length; i < len; i++) { | ||
point = points[i]; | ||
if (getSqDist(point, prevPoint) > sqTolerance) { | ||
newPoints.push(point); | ||
prevPoint = point; | ||
} | ||
} | ||
if (prevPoint !== point && point) | ||
newPoints.push(point); | ||
return newPoints; | ||
} | ||
function simplifyDPStep(points, first, last, sqTolerance, simplified) { | ||
let maxSqDist = sqTolerance; | ||
let index = 0; | ||
for (let i = first + 1; i < last; i++) { | ||
const sqDist = getSqSegDist(points[i], points[first], points[last]); | ||
if (sqDist > maxSqDist) { | ||
index = i; | ||
maxSqDist = sqDist; | ||
} | ||
} | ||
if (maxSqDist > sqTolerance) { | ||
if (index - first > 1) | ||
simplifyDPStep(points, first, index, sqTolerance, simplified); | ||
simplified.push(points[index]); | ||
if (last - index > 1) | ||
simplifyDPStep(points, index, last, sqTolerance, simplified); | ||
} | ||
} | ||
function simplifyDouglasPeucker(points, sqTolerance) { | ||
const last = points.length - 1; | ||
const simplified = [points[0]]; | ||
simplifyDPStep(points, 0, last, sqTolerance, simplified); | ||
simplified.push(points[last]); | ||
return simplified; | ||
} | ||
function simplify(points, tolerance, highestQuality = false) { | ||
if (points.length <= 2) | ||
return points; | ||
const sqTolerance = tolerance !== void 0 ? tolerance * tolerance : 1; | ||
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance); | ||
points = simplifyDouglasPeucker(points, sqTolerance); | ||
return points; | ||
} | ||
// src/models/base.ts | ||
@@ -120,2 +194,3 @@ var BaseModel = class { | ||
this.index = 0; | ||
this.count = 0; | ||
} | ||
@@ -131,4 +206,6 @@ onStart(point) { | ||
return false; | ||
if (this.points[this.points.length - 1] !== point) | ||
if (this.points[this.points.length - 1] !== point) { | ||
this.points.push(point); | ||
this.count += 1; | ||
} | ||
if (this.pressure) { | ||
@@ -145,2 +222,6 @@ while (this.points.length - this.index >= SEGMENT_LENGTH) { | ||
} else { | ||
if (this.simplify && this.count > 5) { | ||
this.points = simplify(this.points, 1, true); | ||
this.count = 0; | ||
} | ||
this.attr("d", toSvgData(this.points)); | ||
@@ -151,2 +232,4 @@ } | ||
onEnd() { | ||
if (!this.pressure && this.simplify) | ||
this.attr("d", toSvgData(simplify(this.points, 1, true))); | ||
const path = this.el; | ||
@@ -163,4 +246,9 @@ this.el = null; | ||
get pressure() { | ||
return !!this.brush.pressure; | ||
var _a; | ||
return !!((_a = this.brush.draw) == null ? void 0 : _a.pressure); | ||
} | ||
get simplify() { | ||
var _a; | ||
return !!((_a = this.brush.draw) == null ? void 0 : _a.simplify); | ||
} | ||
}; | ||
@@ -167,0 +255,0 @@ function line(a, b) { |
@@ -29,3 +29,3 @@ var __defProp = Object.defineProperty; | ||
// src/utils.ts | ||
// src/utils/index.ts | ||
function numSort(a, b) { | ||
@@ -45,2 +45,76 @@ return a - b; | ||
// src/utils/simpify.ts | ||
function getSqDist(p1, p2) { | ||
const dx = p1.x - p2.x; | ||
const dy = p1.y - p2.y; | ||
return dx * dx + dy * dy; | ||
} | ||
function getSqSegDist(p, p1, p2) { | ||
let x = p1.x; | ||
let y = p1.y; | ||
let dx = p2.x - x; | ||
let dy = p2.y - y; | ||
if (dx !== 0 || dy !== 0) { | ||
const t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy); | ||
if (t > 1) { | ||
x = p2.x; | ||
y = p2.y; | ||
} else if (t > 0) { | ||
x += dx * t; | ||
y += dy * t; | ||
} | ||
} | ||
dx = p.x - x; | ||
dy = p.y - y; | ||
return dx * dx + dy * dy; | ||
} | ||
function simplifyRadialDist(points, sqTolerance) { | ||
let prevPoint = points[0]; | ||
const newPoints = [prevPoint]; | ||
let point; | ||
for (let i = 1, len = points.length; i < len; i++) { | ||
point = points[i]; | ||
if (getSqDist(point, prevPoint) > sqTolerance) { | ||
newPoints.push(point); | ||
prevPoint = point; | ||
} | ||
} | ||
if (prevPoint !== point && point) | ||
newPoints.push(point); | ||
return newPoints; | ||
} | ||
function simplifyDPStep(points, first, last, sqTolerance, simplified) { | ||
let maxSqDist = sqTolerance; | ||
let index = 0; | ||
for (let i = first + 1; i < last; i++) { | ||
const sqDist = getSqSegDist(points[i], points[first], points[last]); | ||
if (sqDist > maxSqDist) { | ||
index = i; | ||
maxSqDist = sqDist; | ||
} | ||
} | ||
if (maxSqDist > sqTolerance) { | ||
if (index - first > 1) | ||
simplifyDPStep(points, first, index, sqTolerance, simplified); | ||
simplified.push(points[index]); | ||
if (last - index > 1) | ||
simplifyDPStep(points, index, last, sqTolerance, simplified); | ||
} | ||
} | ||
function simplifyDouglasPeucker(points, sqTolerance) { | ||
const last = points.length - 1; | ||
const simplified = [points[0]]; | ||
simplifyDPStep(points, 0, last, sqTolerance, simplified); | ||
simplified.push(points[last]); | ||
return simplified; | ||
} | ||
function simplify(points, tolerance, highestQuality = false) { | ||
if (points.length <= 2) | ||
return points; | ||
const sqTolerance = tolerance !== void 0 ? tolerance * tolerance : 1; | ||
points = highestQuality ? points : simplifyRadialDist(points, sqTolerance); | ||
points = simplifyDouglasPeucker(points, sqTolerance); | ||
return points; | ||
} | ||
// src/models/base.ts | ||
@@ -133,2 +207,3 @@ var BaseModel = class { | ||
this.index = 0; | ||
this.count = 0; | ||
} | ||
@@ -144,4 +219,6 @@ onStart(point) { | ||
return false; | ||
if (this.points[this.points.length - 1] !== point) | ||
if (this.points[this.points.length - 1] !== point) { | ||
this.points.push(point); | ||
this.count += 1; | ||
} | ||
if (this.pressure) { | ||
@@ -158,2 +235,6 @@ while (this.points.length - this.index >= SEGMENT_LENGTH) { | ||
} else { | ||
if (this.simplify && this.count > 5) { | ||
this.points = simplify(this.points, 1, true); | ||
this.count = 0; | ||
} | ||
this.attr("d", toSvgData(this.points)); | ||
@@ -164,2 +245,4 @@ } | ||
onEnd() { | ||
if (!this.pressure && this.simplify) | ||
this.attr("d", toSvgData(simplify(this.points, 1, true))); | ||
const path = this.el; | ||
@@ -176,4 +259,9 @@ this.el = null; | ||
get pressure() { | ||
return !!this.brush.pressure; | ||
var _a; | ||
return !!((_a = this.brush.draw) == null ? void 0 : _a.pressure); | ||
} | ||
get simplify() { | ||
var _a; | ||
return !!((_a = this.brush.draw) == null ? void 0 : _a.simplify); | ||
} | ||
}; | ||
@@ -180,0 +268,0 @@ function line(a, b) { |
{ | ||
"name": "@drauu/core", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/index.mjs", |
Sorry, the diff of this file is not supported yet
56539
1907