Last active
February 3, 2021 14:12
-
-
Save OctaneInteractive/f4a438972a1beea8a2b1b18ea61f2a74 to your computer and use it in GitHub Desktop.
Text on a path in Fabric
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
<!doctype html> | |
<html> | |
<head> | |
<title>Text on a path, using Fabric!</title> | |
<link href="styles.css" rel="stylesheet"> | |
<script src="https://unpkg.com/fabric@4.3.0/dist/fabric.js"></script> | |
<script src="text-on-a-path.js"></script> | |
</head> | |
<body> | |
<canvas ref="canvas" id="canvas" class="canvas"></canvas> | |
</body> | |
</html> |
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
.canvas { | |
border: 1px solid red; | |
} |
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
const parameters = { | |
width: 800, | |
height: 200 | |
} | |
let reference = document.getElementById('canvas') | |
let canvas = new fabric.Canvas(reference, { | |
selection: false, | |
preserveObjectStacking: true | |
}) | |
canvas.setWidth(parameters.width) | |
canvas.setHeight(parameters.height) | |
let pathForText = new fabric.Path(`M 0 0 Q 390 200 780 0`, { | |
left: 10, | |
top: 50, | |
fill: '', | |
stroke: 'red', | |
strokeWidth: 1 | |
}) | |
canvas.add(pathForText) | |
let text = new fabric.Text('Here is some text on a path, and I hope you find it useful!', { | |
fill: 'red', | |
fontFamily: 'Verdana', | |
fontSize: 26, | |
left: pathForText.left, | |
path: pathForText, | |
textAlign: 'center', // While this doesn't work with text on a path, `text-anchor="middle" startOffset="50%"` would accomplish the same in SVG. | |
top: pathForText.top, | |
width: parameters.width | |
}) | |
canvas.add(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case anyone else is struggling to place text on a path, as I was.