-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efe6aff
commit 0f635ed
Showing
4 changed files
with
85 additions
and
10 deletions.
There are no files selected for viewing
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
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
62 changes: 62 additions & 0 deletions
62
packages/mermaid/src/rendering-util/rendering-elements/shapes/flippedTriangle.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { log } from '$root/logger.js'; | ||
import { labelHelper, updateNodeBounds, getNodeClasses } from './util.js'; | ||
import intersect from '../intersect/index.js'; | ||
import type { Node } from '$root/rendering-util/types.d.ts'; | ||
import { | ||
styles2String, | ||
userNodeOverrides, | ||
} from '$root/rendering-util/rendering-elements/shapes/handdrawnStyles.js'; | ||
import rough from 'roughjs'; | ||
import { createPathFromPoints } from './util.js'; | ||
|
||
export const flippedTriangle = async (parent: SVGAElement, node: Node): Promise<SVGAElement> => { | ||
const { labelStyles, nodeStyles } = styles2String(node); | ||
node.labelStyle = labelStyles; | ||
const { shapeSvg, bbox, label } = await labelHelper(parent, node, getNodeClasses(node)); | ||
|
||
const w = bbox.width + node.padding + 20; | ||
const h = (Math.sqrt(3) / 2) * w + (node.padding ?? 0); | ||
const points = [ | ||
{ x: 0, y: -h }, | ||
{ x: w, y: -h }, | ||
{ x: w / 2, y: 0 }, | ||
]; | ||
|
||
const { cssStyles } = node; | ||
|
||
// @ts-ignore - rough is not typed | ||
const rc = rough.svg(shapeSvg); | ||
const options = userNodeOverrides(node, {}); | ||
if (node.look !== 'handdrawn') { | ||
options.roughness = 0; | ||
options.fillStyle = 'solid'; | ||
} | ||
const pathData = createPathFromPoints(points); | ||
const roughNode = rc.path(pathData, options); | ||
|
||
const polygon = shapeSvg | ||
.insert(() => roughNode, ':first-child') | ||
.attr('transform', `translate(${-w / 2}, ${h / 2})`); | ||
|
||
if (cssStyles) { | ||
polygon.attr('style', cssStyles); | ||
} | ||
|
||
if (nodeStyles) { | ||
polygon.attr('style', nodeStyles); | ||
} | ||
|
||
node.width = w; | ||
node.height = h; | ||
|
||
updateNodeBounds(node, polygon); | ||
|
||
label.attr('transform', `translate(${-bbox.width / 2}, ${-h / 2})`); | ||
|
||
node.intersect = function (point) { | ||
log.info('Triangle intersect', node, points, point); | ||
return intersect.polygon(node, points, point); | ||
}; | ||
|
||
return shapeSvg; | ||
}; |
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