HTML5 canvas TextPath 教程
要用 Konva
创建沿路径排列的文本,可以实例化一个 Konva.TextPath()
对象。
有关完整的属性和方法列表,请参阅 TextPath API 参考。
- Vanilla
- React
- Vue
import Konva from 'konva'; const stage = new Konva.Stage({ container: 'container', width: window.innerWidth, height: window.innerHeight }); const layer = new Konva.Layer(); stage.add(layer); const textPath = new Konva.TextPath({ x: 0, y: 50, fill: '#333', fontSize: 16, fontFamily: 'Arial', text: '天下皆舞台,男女尽是演员。', data: 'M10,10 C0,0 10,150 100,100 S300,150 400,50', }); layer.add(textPath);
import { Stage, Layer, TextPath } from 'react-konva'; const App = () => { return ( <Stage width={window.innerWidth} height={window.innerHeight}> <Layer> <TextPath x={0} y={50} fill="#333" fontSize={16} fontFamily="Arial" text="天下皆舞台,男女尽是演员。" data="M10,10 C0,0 10,150 100,100 S300,150 400,50" /> </Layer> </Stage> ); }; export default App;
<template> <v-stage :config="stageSize"> <v-layer> <v-text-path :config="textPathConfig" /> </v-layer> </v-stage> </template> <script setup> const stageSize = { width: window.innerWidth, height: window.innerHeight }; const textPathConfig = { x: 0, y: 50, fill: '#333', fontSize: 16, fontFamily: 'Arial', text: "天下皆舞台,男女尽是演员。", data: 'M10,10 C0,0 10,150 100,100 S300,150 400,50' }; </script>