@drauu/core
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -321,3 +321,3 @@ "use strict"; | ||
// src/models/stylus.ts | ||
var StylusModel = class extends BaseModel { | ||
var StylusModel = class _StylusModel extends BaseModel { | ||
constructor() { | ||
@@ -350,4 +350,7 @@ super(...arguments); | ||
getSvgData(points) { | ||
return _StylusModel.getSvgData(points, this.brush); | ||
} | ||
static getSvgData(points, brush) { | ||
const stroke = ae(points, __spreadValues({ | ||
size: this.brush.size, | ||
size: brush.size, | ||
thinning: 0.9, | ||
@@ -361,3 +364,3 @@ simulatePressure: false, | ||
} | ||
}, this.brush.stylusOptions)); | ||
}, brush.stylusOptions)); | ||
if (!stroke.length) | ||
@@ -625,3 +628,3 @@ return ""; | ||
// src/models/draw.ts | ||
var DrawModel = class extends BaseModel { | ||
var DrawModel = class _DrawModel extends BaseModel { | ||
constructor() { | ||
@@ -653,3 +656,3 @@ super(...arguments); | ||
} | ||
this.attr("d", toSvgData(this.points)); | ||
this.attr("d", _DrawModel.toSvgData(this.points)); | ||
return true; | ||
@@ -662,3 +665,3 @@ } | ||
return false; | ||
path.setAttribute("d", toSvgData(simplify(this.points, 1, true))); | ||
path.setAttribute("d", _DrawModel.toSvgData(simplify(this.points, 1, true))); | ||
if (!path.getTotalLength()) | ||
@@ -668,33 +671,34 @@ return false; | ||
} | ||
// https://francoisromain.medium.com/smooth-a-svg-path-with-cubic-bezier-curves-e37b49d46c74 | ||
static line(a2, b2) { | ||
const lengthX = b2.x - a2.x; | ||
const lengthY = b2.y - a2.y; | ||
return { | ||
length: Math.sqrt(lengthX ** 2 + lengthY ** 2), | ||
angle: Math.atan2(lengthY, lengthX) | ||
}; | ||
} | ||
static controlPoint(current, previous, next, reverse) { | ||
const p = previous || current; | ||
const n = next || current; | ||
const smoothing = 0.2; | ||
const o = _DrawModel.line(p, n); | ||
const angle = o.angle + (reverse ? Math.PI : 0); | ||
const length = o.length * smoothing; | ||
const x = current.x + Math.cos(angle) * length; | ||
const y = current.y + Math.sin(angle) * length; | ||
return { x, y }; | ||
} | ||
static bezierCommand(point, i, points) { | ||
const cps = _DrawModel.controlPoint(points[i - 1], points[i - 2], point); | ||
const cpe = _DrawModel.controlPoint(point, points[i - 1], points[i + 1], true); | ||
return `C ${cps.x.toFixed(D)},${cps.y.toFixed(D)} ${cpe.x.toFixed(D)},${cpe.y.toFixed(D)} ${point.x.toFixed(D)},${point.y.toFixed(D)}`; | ||
} | ||
static toSvgData(points) { | ||
return points.reduce( | ||
(acc, point, i, a2) => i === 0 ? `M ${point.x.toFixed(D)},${point.y.toFixed(D)}` : `${acc} ${_DrawModel.bezierCommand(point, i, a2)}`, | ||
"" | ||
); | ||
} | ||
}; | ||
function line(a2, b2) { | ||
const lengthX = b2.x - a2.x; | ||
const lengthY = b2.y - a2.y; | ||
return { | ||
length: Math.sqrt(lengthX ** 2 + lengthY ** 2), | ||
angle: Math.atan2(lengthY, lengthX) | ||
}; | ||
} | ||
function controlPoint(current, previous, next, reverse) { | ||
const p = previous || current; | ||
const n = next || current; | ||
const smoothing = 0.2; | ||
const o = line(p, n); | ||
const angle = o.angle + (reverse ? Math.PI : 0); | ||
const length = o.length * smoothing; | ||
const x = current.x + Math.cos(angle) * length; | ||
const y = current.y + Math.sin(angle) * length; | ||
return { x, y }; | ||
} | ||
function bezierCommand(point, i, points) { | ||
const cps = controlPoint(points[i - 1], points[i - 2], point); | ||
const cpe = controlPoint(point, points[i - 1], points[i + 1], true); | ||
return `C ${cps.x.toFixed(D)},${cps.y.toFixed(D)} ${cpe.x.toFixed(D)},${cpe.y.toFixed(D)} ${point.x.toFixed(D)},${point.y.toFixed(D)}`; | ||
} | ||
function toSvgData(points) { | ||
return points.reduce( | ||
(acc, point, i, a2) => i === 0 ? `M ${point.x.toFixed(D)},${point.y.toFixed(D)}` : `${acc} ${bezierCommand(point, i, a2)}`, | ||
"" | ||
); | ||
} | ||
@@ -764,3 +768,3 @@ // src/models/eraser.ts | ||
const segment = this.pathFragments[i]; | ||
const line2 = { | ||
const line = { | ||
x1: this.svgPointPrevious.x, | ||
@@ -771,3 +775,3 @@ x2: this.svgPointCurrent.x, | ||
}; | ||
if (this.lineLineIntersect(segment, line2)) { | ||
if (this.lineLineIntersect(segment, line)) { | ||
segment.element.remove(); | ||
@@ -774,0 +778,0 @@ erased.push(i); |
@@ -343,3 +343,3 @@ "use strict"; | ||
// src/models/stylus.ts | ||
var StylusModel = class extends BaseModel { | ||
var StylusModel = class _StylusModel extends BaseModel { | ||
constructor() { | ||
@@ -372,4 +372,7 @@ super(...arguments); | ||
getSvgData(points) { | ||
return _StylusModel.getSvgData(points, this.brush); | ||
} | ||
static getSvgData(points, brush) { | ||
const stroke = ae(points, __spreadValues({ | ||
size: this.brush.size, | ||
size: brush.size, | ||
thinning: 0.9, | ||
@@ -383,3 +386,3 @@ simulatePressure: false, | ||
} | ||
}, this.brush.stylusOptions)); | ||
}, brush.stylusOptions)); | ||
if (!stroke.length) | ||
@@ -647,3 +650,3 @@ return ""; | ||
// src/models/draw.ts | ||
var DrawModel = class extends BaseModel { | ||
var DrawModel = class _DrawModel extends BaseModel { | ||
constructor() { | ||
@@ -675,3 +678,3 @@ super(...arguments); | ||
} | ||
this.attr("d", toSvgData(this.points)); | ||
this.attr("d", _DrawModel.toSvgData(this.points)); | ||
return true; | ||
@@ -684,3 +687,3 @@ } | ||
return false; | ||
path.setAttribute("d", toSvgData(simplify(this.points, 1, true))); | ||
path.setAttribute("d", _DrawModel.toSvgData(simplify(this.points, 1, true))); | ||
if (!path.getTotalLength()) | ||
@@ -690,33 +693,34 @@ return false; | ||
} | ||
// https://francoisromain.medium.com/smooth-a-svg-path-with-cubic-bezier-curves-e37b49d46c74 | ||
static line(a2, b2) { | ||
const lengthX = b2.x - a2.x; | ||
const lengthY = b2.y - a2.y; | ||
return { | ||
length: Math.sqrt(lengthX ** 2 + lengthY ** 2), | ||
angle: Math.atan2(lengthY, lengthX) | ||
}; | ||
} | ||
static controlPoint(current, previous, next, reverse) { | ||
const p = previous || current; | ||
const n = next || current; | ||
const smoothing = 0.2; | ||
const o = _DrawModel.line(p, n); | ||
const angle = o.angle + (reverse ? Math.PI : 0); | ||
const length = o.length * smoothing; | ||
const x = current.x + Math.cos(angle) * length; | ||
const y = current.y + Math.sin(angle) * length; | ||
return { x, y }; | ||
} | ||
static bezierCommand(point, i, points) { | ||
const cps = _DrawModel.controlPoint(points[i - 1], points[i - 2], point); | ||
const cpe = _DrawModel.controlPoint(point, points[i - 1], points[i + 1], true); | ||
return `C ${cps.x.toFixed(D)},${cps.y.toFixed(D)} ${cpe.x.toFixed(D)},${cpe.y.toFixed(D)} ${point.x.toFixed(D)},${point.y.toFixed(D)}`; | ||
} | ||
static toSvgData(points) { | ||
return points.reduce( | ||
(acc, point, i, a2) => i === 0 ? `M ${point.x.toFixed(D)},${point.y.toFixed(D)}` : `${acc} ${_DrawModel.bezierCommand(point, i, a2)}`, | ||
"" | ||
); | ||
} | ||
}; | ||
function line(a2, b2) { | ||
const lengthX = b2.x - a2.x; | ||
const lengthY = b2.y - a2.y; | ||
return { | ||
length: Math.sqrt(lengthX ** 2 + lengthY ** 2), | ||
angle: Math.atan2(lengthY, lengthX) | ||
}; | ||
} | ||
function controlPoint(current, previous, next, reverse) { | ||
const p = previous || current; | ||
const n = next || current; | ||
const smoothing = 0.2; | ||
const o = line(p, n); | ||
const angle = o.angle + (reverse ? Math.PI : 0); | ||
const length = o.length * smoothing; | ||
const x = current.x + Math.cos(angle) * length; | ||
const y = current.y + Math.sin(angle) * length; | ||
return { x, y }; | ||
} | ||
function bezierCommand(point, i, points) { | ||
const cps = controlPoint(points[i - 1], points[i - 2], point); | ||
const cpe = controlPoint(point, points[i - 1], points[i + 1], true); | ||
return `C ${cps.x.toFixed(D)},${cps.y.toFixed(D)} ${cpe.x.toFixed(D)},${cpe.y.toFixed(D)} ${point.x.toFixed(D)},${point.y.toFixed(D)}`; | ||
} | ||
function toSvgData(points) { | ||
return points.reduce( | ||
(acc, point, i, a2) => i === 0 ? `M ${point.x.toFixed(D)},${point.y.toFixed(D)}` : `${acc} ${bezierCommand(point, i, a2)}`, | ||
"" | ||
); | ||
} | ||
@@ -786,3 +790,3 @@ // src/models/eraser.ts | ||
const segment = this.pathFragments[i]; | ||
const line2 = { | ||
const line = { | ||
x1: this.svgPointPrevious.x, | ||
@@ -793,3 +797,3 @@ x2: this.svgPointCurrent.x, | ||
}; | ||
if (this.lineLineIntersect(segment, line2)) { | ||
if (this.lineLineIntersect(segment, line)) { | ||
segment.element.remove(); | ||
@@ -796,0 +800,0 @@ erased.push(i); |
{ | ||
"name": "@drauu/core", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
105500
3206