1
0
Fork 0

chore(core): Fixed a few issues regarding to events

This commit is contained in:
Joost De Cock 2020-07-23 11:54:55 +02:00
parent 01ab30656d
commit f6e919e42f
3 changed files with 43 additions and 41 deletions

View file

@ -17,7 +17,7 @@ function Path(debug = false) {
this.bottomRight = false this.bottomRight = false
this.attributes = new Attributes() this.attributes = new Attributes()
this.ops = [] this.ops = []
Object.defineProperty(this, 'debug', { value: debug }) Object.defineProperty(this, 'debug', { value: debug, configurable: true })
} }
/** Adds the raise method for a path not created through the proxy **/ /** Adds the raise method for a path not created through the proxy **/
@ -295,10 +295,7 @@ Path.prototype.boundary = function () {
/** Returns a deep copy of this */ /** Returns a deep copy of this */
Path.prototype.clone = function () { Path.prototype.clone = function () {
let clone = new Path() let clone = new Path(this.debug).withRaise(this.raise).setRender(this.render)
clone.render = this.render
clone.debug = this.debug
clone.raise = this.raise
if (this.topLeft) clone.topLeft = this.topLeft.clone() if (this.topLeft) clone.topLeft = this.topLeft.clone()
else clone.topLeft = false else clone.topLeft = false
if (this.bottomRight) clone.bottomRight = this.bottomRight.clone() if (this.bottomRight) clone.bottomRight = this.bottomRight.clone()
@ -338,21 +335,21 @@ function pathOffset(path, distance, raise) {
for (let i in path.ops) { for (let i in path.ops) {
let op = path.ops[i] let op = path.ops[i]
if (op.type === 'line') { if (op.type === 'line') {
let segment = offsetLine(current, op.to, distance, path.raise) let segment = offsetLine(current, op.to, distance, path.debug, path.raise)
if (segment) offset.push(segment) if (segment) offset.push(segment)
} else if (op.type === 'curve') { } else if (op.type === 'curve') {
// We need to avoid a control point sitting on top of start or end // We need to avoid a control point sitting on top of start or end
// because that will break the offset in bezier-js // because that will break the offset in bezier-js
let cp1, cp2 let cp1, cp2
if (current.sitsRoughlyOn(op.cp1)) { if (current.sitsRoughlyOn(op.cp1)) {
cp1 = new Path() cp1 = new Path(path.debug)
.withRaise(path.raise) .withRaise(path.raise)
.move(current) .move(current)
.curve(op.cp1, op.cp2, op.to) .curve(op.cp1, op.cp2, op.to)
.shiftAlong(2) .shiftAlong(2)
} else cp1 = op.cp1 } else cp1 = op.cp1
if (op.cp2.sitsRoughlyOn(op.to)) { if (op.cp2.sitsRoughlyOn(op.to)) {
cp2 = new Path() cp2 = new Path(path.debug)
.withRaise(path.raise) .withRaise(path.raise)
.move(op.to) .move(op.to)
.curve(op.cp2, op.cp1, current) .curve(op.cp2, op.cp1, current)
@ -364,7 +361,7 @@ function pathOffset(path, distance, raise) {
{ x: cp2.x, y: cp2.y }, { x: cp2.x, y: cp2.y },
{ x: op.to.x, y: op.to.y } { x: op.to.x, y: op.to.y }
) )
for (let bezier of b.offset(distance)) offset.push(asPath(bezier, raise)) for (let bezier of b.offset(distance)) offset.push(asPath(bezier, path.debug, path.raise))
} else if (op.type === 'close') closed = true } else if (op.type === 'close') closed = true
if (op.to) current = op.to if (op.to) current = op.to
if (!start) start = current if (!start) start = current
@ -374,19 +371,19 @@ function pathOffset(path, distance, raise) {
} }
/** Offsets a line by distance */ /** Offsets a line by distance */
function offsetLine(from, to, distance, raise = false) { function offsetLine(from, to, distance, debug = false, raise = false) {
if (from.x === to.x && from.y === to.y) return false if (from.x === to.x && from.y === to.y) return false
let angle = from.angle(to) - 90 let angle = from.angle(to) - 90
return new Path() return new Path(debug)
.withRaise(raise) .withRaise(raise)
.move(from.shift(angle, distance)) .move(from.shift(angle, distance))
.line(to.shift(angle, distance)) .line(to.shift(angle, distance))
} }
/** Converts a bezier-js instance to a path */ /** Converts a bezier-js instance to a path */
function asPath(bezier, raise = false) { function asPath(bezier, debug = false, raise = false) {
return new Path() return new Path(debug)
.withRaise(raise) .withRaise(raise)
.move(new Point(bezier.points[0].x, bezier.points[0].y)) .move(new Point(bezier.points[0].x, bezier.points[0].y))
.curve( .curve(
@ -398,7 +395,7 @@ function asPath(bezier, raise = false) {
/** Joins path segments together into one path */ /** Joins path segments together into one path */
function joinPaths(paths, closed = false, raise = false) { function joinPaths(paths, closed = false, raise = false) {
let joint = new Path().withRaise(raise).move(paths[0].ops[0].to) let joint = new Path(paths[0].debug).withRaise(paths[0].raise).move(paths[0].ops[0].to)
let current let current
for (let p of paths) { for (let p of paths) {
for (let op of p.ops) { for (let op of p.ops) {
@ -409,7 +406,7 @@ function joinPaths(paths, closed = false, raise = false) {
if (current && !op.to.sitsRoughlyOn(current)) joint.line(op.to) if (current && !op.to.sitsRoughlyOn(current)) joint.line(op.to)
} else { } else {
let err = 'Cannot join a closed path with another' let err = 'Cannot join a closed path with another'
this.raise.error(err) joint.raise.error(err)
throw new Error(err) throw new Error(err)
} }
if (op.to) current = op.to if (op.to) current = op.to
@ -564,15 +561,17 @@ Path.prototype.reverse = function () {
let op = this.ops[i] let op = this.ops[i]
if (op.type === 'line') { if (op.type === 'line') {
if (!op.to.sitsOn(current)) if (!op.to.sitsOn(current))
sections.push(new Path().withRaise(this.raise).move(op.to).line(current)) sections.push(new Path(this.debug).withRaise(this.raise).move(op.to).line(current))
} else if (op.type === 'curve') { } else if (op.type === 'curve') {
sections.push(new Path().withRaise(this.raise).move(op.to).curve(op.cp2, op.cp1, current)) sections.push(
new Path(this.debug).withRaise(this.raise).move(op.to).curve(op.cp2, op.cp1, current)
)
} else if (op.type === 'close') { } else if (op.type === 'close') {
closed = true closed = true
} }
if (op.to) current = op.to if (op.to) current = op.to
} }
let rev = new Path().withRaise(this.raise).move(current) let rev = new Path(this.debug).withRaise(this.raise).move(current)
for (let section of sections.reverse()) rev.ops.push(section.ops[1]) for (let section of sections.reverse()) rev.ops.push(section.ops[1])
if (closed) rev.close() if (closed) rev.close()
@ -630,11 +629,13 @@ Path.prototype.divide = function () {
start = op.to start = op.to
} else if (op.type === 'line') { } else if (op.type === 'line') {
if (!op.to.sitsRoughlyOn(current)) if (!op.to.sitsRoughlyOn(current))
paths.push(new Path().withRaise(this.raise).move(current).line(op.to)) paths.push(new Path(this.debug).withRaise(this.raise).move(current).line(op.to))
} else if (op.type === 'curve') { } else if (op.type === 'curve') {
paths.push(new Path().withRaise(this.raise).move(current).curve(op.cp1, op.cp2, op.to)) paths.push(
new Path(this.debug).withRaise(this.raise).move(current).curve(op.cp1, op.cp2, op.to)
)
} else if (op.type === 'close') { } else if (op.type === 'close') {
paths.push(new Path().withRaise(this.raise).move(current).line(start)) paths.push(new Path(this.debug).withRaise(this.raise).move(current).line(start))
} }
if (op.to) current = op.to if (op.to) current = op.to
} }
@ -768,10 +769,12 @@ Path.prototype.split = function (point) {
if (path.ops[1].type === 'line') { if (path.ops[1].type === 'line') {
if (pointOnLine(path.ops[0].to, path.ops[1].to, point)) { if (pointOnLine(path.ops[0].to, path.ops[1].to, point)) {
firstHalf = divided.slice(0, pi) firstHalf = divided.slice(0, pi)
firstHalf.push(new Path().withRaise(this.raise).move(path.ops[0].to).line(point)) firstHalf.push(new Path(this.debug).withRaise(this.raise).move(path.ops[0].to).line(point))
pi++ pi++
secondHalf = divided.slice(pi) secondHalf = divided.slice(pi)
secondHalf.unshift(new Path().withRaise(this.raise).move(point).line(path.ops[1].to)) secondHalf.unshift(
new Path(this.debug).withRaise(this.raise).move(point).line(path.ops[1].to)
)
} }
} else if (path.ops[1].type === 'curve') { } else if (path.ops[1].type === 'curve') {
let t = pointOnCurve(path.ops[0].to, path.ops[1].cp1, path.ops[1].cp2, path.ops[1].to, point) let t = pointOnCurve(path.ops[0].to, path.ops[1].cp1, path.ops[1].cp2, path.ops[1].to, point)
@ -785,7 +788,7 @@ Path.prototype.split = function (point) {
let split = curve.split(t) let split = curve.split(t)
firstHalf = divided.slice(0, pi) firstHalf = divided.slice(0, pi)
firstHalf.push( firstHalf.push(
new Path() new Path(this.debug)
.withRaise(this.raise) .withRaise(this.raise)
.move(new Point(split.left.points[0].x, split.left.points[0].y)) .move(new Point(split.left.points[0].x, split.left.points[0].y))
.curve( .curve(
@ -797,7 +800,7 @@ Path.prototype.split = function (point) {
pi++ pi++
secondHalf = divided.slice(pi) secondHalf = divided.slice(pi)
secondHalf.unshift( secondHalf.unshift(
new Path() new Path(this.debug)
.withRaise(this.raise) .withRaise(this.raise)
.move(new Point(split.right.points[0].x, split.right.points[0].y)) .move(new Point(split.right.points[0].x, split.right.points[0].y))
.curve( .curve(
@ -827,7 +830,7 @@ Path.prototype.trim = function () {
let intersection = intersections.pop() let intersection = intersections.pop()
let trimmedStart = chunks.slice(0, i) let trimmedStart = chunks.slice(0, i)
let trimmedEnd = chunks.slice(parseInt(j) + 1) let trimmedEnd = chunks.slice(parseInt(j) + 1)
let glue = new Path().withRaise(this.raise) let glue = new Path(this.debug).withRaise(this.raise)
let first = true let first = true
for (let k of [i, j]) { for (let k of [i, j]) {
let ops = chunks[k].ops let ops = chunks[k].ops

View file

@ -5,7 +5,7 @@ function Point(x, y, debug = false) {
this.x = round(x) this.x = round(x)
this.y = round(y) this.y = round(y)
this.attributes = new Attributes() this.attributes = new Attributes()
Object.defineProperty(this, 'debug', { value: debug }) Object.defineProperty(this, 'debug', { value: debug, configurable: true })
} }
/** Adds the raise method for a path not created through the proxy **/ /** Adds the raise method for a path not created through the proxy **/
@ -17,10 +17,8 @@ Point.prototype.withRaise = function (raise = false) {
/** Debug method to validate point data **/ /** Debug method to validate point data **/
Point.prototype.check = function () { Point.prototype.check = function () {
if (this.debug) { if (typeof this.x !== 'number') this.raise.warning('X value of `Point` is not a number')
if (typeof this.x !== 'number') this.raise.warning('X value of `Point` is not a number') if (typeof this.y !== 'number') this.raise.warning('Y value of `Point` is not a number')
if (typeof this.y !== 'number') this.raise.warning('Y value of `Point` is not a number')
}
} }
/** Radians to degrees */ /** Radians to degrees */
@ -108,13 +106,13 @@ Point.prototype.rotate = function (deg, that) {
let x = that.x + radius * Math.cos(this.deg2rad(angle + deg)) * -1 let x = that.x + radius * Math.cos(this.deg2rad(angle + deg)) * -1
let y = that.y + radius * Math.sin(this.deg2rad(angle + deg)) let y = that.y + radius * Math.sin(this.deg2rad(angle + deg))
return new Point(x, y).withRaise(this.raise) return new Point(x, y, this.debug).withRaise(this.raise)
} }
/** returns an identical copy of this point */ /** returns an identical copy of this point */
Point.prototype.copy = function () { Point.prototype.copy = function () {
if (this.debug) this.check() if (this.debug) this.check()
return new Point(this.x, this.y).withRaise(this.raise) return new Point(this.x, this.y, this.debug).withRaise(this.raise)
} }
/** Mirrors this point around X value of that point */ /** Mirrors this point around X value of that point */
@ -127,8 +125,9 @@ Point.prototype.flipX = function (that = false) {
that.check() that.check()
} }
} }
if (that === false || that.x === 0) return new Point(this.x * -1, this.y).withRaise(this.raise) if (that === false || that.x === 0)
else return new Point(that.x + this.dx(that), this.y).withRaise(this.raise) return new Point(this.x * -1, this.y, this.debug).withRaise(this.raise)
else return new Point(that.x + this.dx(that), this.y, this.debug).withRaise(this.raise)
} }
/** Mirrors this point around Y value of that point */ /** Mirrors this point around Y value of that point */
@ -141,8 +140,9 @@ Point.prototype.flipY = function (that = false) {
that.check() that.check()
} }
} }
if (that === false || that.y === 0) return new Point(this.x, this.y * -1).withRaise(this.raise) if (that === false || that.y === 0)
else return new Point(this.x, that.y + this.dy(that)).withRaise(this.raise) return new Point(this.x, this.y * -1, this.debug).withRaise(this.raise)
else return new Point(this.x, that.y + this.dy(that), this.debug).withRaise(this.raise)
} }
/** Shifts this point distance in the deg direction */ /** Shifts this point distance in the deg direction */
@ -234,7 +234,7 @@ Point.prototype.shiftOutwards = function (that, distance) {
/** Returns a deep copy of this */ /** Returns a deep copy of this */
Point.prototype.clone = function () { Point.prototype.clone = function () {
if (this.debug) this.check() if (this.debug) this.check()
let clone = new Point(this.x, this.y).withRaise(this.raise) let clone = new Point(this.x, this.y, this.debug).withRaise(this.raise)
clone.attributes = this.attributes.clone() clone.attributes = this.attributes.clone()
return clone return clone

View file

@ -5,7 +5,7 @@ function Snippet(def, anchor, debug = false) {
this.def = def this.def = def
this.anchor = anchor this.anchor = anchor
this.attributes = new Attributes() this.attributes = new Attributes()
Object.defineProperty(this, 'debug', { value: debug }) Object.defineProperty(this, 'debug', { value: debug, configurable: true })
return this return this
} }
@ -26,9 +26,8 @@ Snippet.prototype.attr = function (name, value, overwrite = false) {
/** Returns a deep copy of this */ /** Returns a deep copy of this */
Snippet.prototype.clone = function () { Snippet.prototype.clone = function () {
let clone = new Snippet(this.def, this.anchor.clone()).withRaise(this.raise) let clone = new Snippet(this.def, this.anchor.clone(), this.debug).withRaise(this.raise)
clone.attributes = this.attributes.clone() clone.attributes = this.attributes.clone()
clone.raise = this.raise
return clone return clone
} }