Created
September 21, 2021 16:05
-
-
Save baku89/4be0a63a9fd07fe2aed6ed9ce61a25c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
{ | |
"id": "9mpi7vhkir", | |
"label": "Cmd Pen Tool", | |
"icon": "⌘", | |
"parameters": [ | |
{ | |
"name": "strokeColor", | |
"type": "color", | |
"default": "#282a2e" | |
}, | |
{ | |
"name": "strokeWidth", | |
"type": "float", | |
"default": 2 | |
} | |
] | |
} | |
*/ | |
let path, guideExtend, guideRadius, guideB, guideC, p0, p1 | |
let r = 0 | |
function press() { | |
if (!path) { | |
path = new Path() | |
path.strokeWidth = strokeWidth | |
path.strokeColor = strokeColor | |
path.moveTo(mouse) | |
} | |
p1 = mouse | |
updateGuide() | |
} | |
function drag() { | |
const dir = p1.subtract(p0).normalize() | |
r = Math.max(0, dir.dot(mouse.subtract(p1))) | |
updateGuide() | |
} | |
function move() { | |
p1 = mouse | |
updateGuide() | |
} | |
function release() { | |
extendPath(path, path) | |
p0 = p1 | |
r = 0 | |
} | |
function extendPath(path, pathToExtend) { | |
const t0 = path.getTangentAt(path.length) | |
if (!p0) return | |
if (!t0) { | |
pathToExtend.lineTo(p1) | |
return | |
} | |
const t1 = p0.subtract(p1).normalize() | |
const b = p0.add(t0.multiply(r)) | |
const c = p0.add(t1.multiply(r)) | |
pathToExtend.cubicCurveTo(b, c, p0) | |
pathToExtend.lineTo(p1) | |
if (guideRadius) guideRadius.remove() | |
guideRadius = Guide.add(new Path) | |
guideRadius.moveTo(b) | |
guideRadius.lineTo(p0) | |
guideRadius.lineTo(c) | |
if (guideB) guideB.remove() | |
if (guideC) guideC.remove() | |
if (r > 0) { | |
guideB = Guide.addPoint(b, 'stroke') | |
guideC = Guide.addPoint(c, 'stroke') | |
} | |
} | |
function updateGuide() { | |
if (guideExtend) guideExtend.remove() | |
guideExtend = new Path() | |
guideExtend.moveTo(p0) | |
guideExtend.strokeColor = strokeColor | |
guideExtend.strokeWidth = strokeWidth | |
extendPath(path, guideExtend) | |
} | |
function end() { | |
p0 = null | |
p1 = 0 | |
path = null | |
guideExtend?.remove() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment