Skip to content

Commit ea400fc

Browse files
committedJul 9, 2021
restore the old fillstyle after drawing thick stroked lines
refuse to draw lines with NaN or bigger than MaxInt skip points in a path when they are the same as the previous point add Line.is_invalid() add Point.isEquals()
1 parent 00de1ea commit ea400fc

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@
4545
"node": ">=0.8"
4646
},
4747
"license": "MIT"
48-
}
48+
}

‎src/context.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -889,13 +889,30 @@ export class Context {
889889
*/
890890
stroke() {
891891
let flat_path = flatten_path(this.path)
892-
let stroke_path = path_to_stroked_path(flat_path,this.lineWidth)
892+
let stroke_path = path_to_stroked_path(flat_path,this.lineWidth/2)
893893
const lines = pathToLines(stroke_path)
894+
const old_fillStyle = this.fillStyle
895+
this.fillStyle = this.strokeStyle
894896
this.imageSmoothingEnabled ? this.fill_aa(lines) : this.fill_noaa(lines);
895-
// this.strokeStyle = 'red'
896-
// this.lineWidth = 1
897-
// pathToLines(this.path).forEach((line)=> this.drawLine(line));
898-
// pathToLines(stroke_path).forEach(line => this.drawLine(line))
897+
this.fillStyle = old_fillStyle
898+
899+
if(this.debug) {
900+
this.save()
901+
let old_ss = this.strokeStyle
902+
let old_lw = this.lineWidth
903+
this.strokeStyle = 'red'
904+
this.lineWidth = 1
905+
console.log("path is",this.path)
906+
pathToLines(this.path).forEach((line) => this.drawLine(line));
907+
console.log("flat path is",flat_path)
908+
pathToLines(flat_path).forEach((line) => this.drawLine(line));
909+
console.log("stroke path is",stroke_path)
910+
pathToLines(stroke_path).forEach(line => this.drawLine(line))
911+
console.log("final lines are",lines)
912+
this.strokeStyle = old_ss
913+
this.lineWidth = old_lw
914+
this.restore()
915+
}
899916
}
900917

901918
/**
@@ -913,6 +930,7 @@ export class Context {
913930
* @memberof Context
914931
*/
915932
drawLine(line) {
933+
if(line.is_invalid()) return console.error('cannot draw line',line)
916934
this.imageSmoothingEnabled?this.drawLine_aa(line):this.drawLine_noaa(line)
917935
}
918936

@@ -1322,7 +1340,7 @@ function path_to_stroked_path(path, w) {
13221340
let path_start = 0
13231341

13241342
function project(A,B,scale) {
1325-
// console.log("projecting",A,B)
1343+
if(A.equals(B)) console.log("same points!",A,B)
13261344
let delta_unit = A.subtract(B).unit()
13271345
let C_unit = delta_unit.rotate(toRad(90))
13281346
let D_unit = delta_unit.rotate(toRad(-90))
@@ -1365,6 +1383,7 @@ function path_to_stroked_path(path, w) {
13651383
if(cmd[0] === PATH_COMMAND.LINE) {
13661384
const A = curr
13671385
const B = cmd[1]
1386+
if(A.equals(B)) return console.log("can't project the same paths",i,cmd,A,B)
13681387
// console.log(i,"====",B)
13691388
let next = path[i+1]
13701389
//if first
@@ -1384,6 +1403,7 @@ function path_to_stroked_path(path, w) {
13841403
return
13851404
}
13861405
const C = next[1]
1406+
if(C.equals(B)) return console.log("can't project the same paths",i,cmd,A,B)
13871407
// console.log(i,A,B,C)
13881408
// console.log("next",next)
13891409
let BA = A.subtract(B)

‎src/line.js

+12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ export class Line {
6666
Math.pow(this.start.x - this.end.x, 2) + Math.pow(this.start.y - this.end.y, 2)
6767
);
6868
}
69+
70+
is_invalid() {
71+
if(Number.isNaN(this.start.x)) return true
72+
if(Number.isNaN(this.end.x)) return true
73+
if(Number.isNaN(this.start.y)) return true
74+
if(Number.isNaN(this.end.y)) return true
75+
if(this.start.x > Number.MAX_SAFE_INTEGER) return true
76+
if(this.start.y > Number.MAX_SAFE_INTEGER) return true
77+
if(this.end.x > Number.MAX_SAFE_INTEGER) return true
78+
if(this.end.y > Number.MAX_SAFE_INTEGER) return true
79+
return false
80+
}
6981
}
7082

7183
/** @ignore */

‎src/point.js

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export class Point {
6767
this.y*scalar
6868
)
6969
}
70+
equals(pt) {
71+
return this.x === pt.x && this.y === pt.y;
72+
}
7073
}
7174

7275
export const toRad = (deg) => Math.PI/180*deg

0 commit comments

Comments
 (0)