Skip to content

Instantly share code, notes, and snippets.

@OctaneInteractive
Last active February 3, 2021 14:12
Show Gist options
  • Save OctaneInteractive/f4a438972a1beea8a2b1b18ea61f2a74 to your computer and use it in GitHub Desktop.
Save OctaneInteractive/f4a438972a1beea8a2b1b18ea61f2a74 to your computer and use it in GitHub Desktop.
Text on a path in Fabric
<!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>
.canvas {
border: 1px solid red;
}
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)
@OctaneInteractive
Copy link
Author

OctaneInteractive commented Feb 3, 2021

In case anyone else is struggling to place text on a path, as I was.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment