<template> <div> <canvas ref="canvas" :width="canvasWidth" :height="canvasHeight"></canvas> </div> </template>
<script> export default { props: { text: { default: '阿斯顿发斯蒂芬', type: String, required: true }, radius: { type: Number, default: 200 }, canvasWidth: { type: Number, default: 500 }, canvasHeight: { type: Number, default: 600 }, fontSize: { type: Number, default: 32 }, curvature: { type: Number, default: 0.5 }, letterSpacing: { type: Number, default: 0 }, startAngle: { type: Number, default: 0 }, ellipseFactor: { type: Number, default: 1 } }, mounted() { this.drawCurvedText(); }, methods: { drawCurvedText() { const canvas = this.$refs.canvas; const ctx = canvas.getContext('2d');
ctx.font = `${this.fontSize}px Arial`; ctx.textBaseline = 'middle';
const text = this.text; const radius = this.radius; const canvasWidth = this.canvasWidth; const canvasHeight = this.canvasHeight; const textLength = text.length; const letterSpacing = this.letterSpacing; const startAngle = this.startAngle; const ellipseFactor = this.ellipseFactor;
const centerX = canvasWidth / 2; const centerY = canvasHeight / 2;
const angleRange = Math.PI * this.curvature;
const totalSpacing = letterSpacing * (textLength - 1); const angleStep = (angleRange - totalSpacing / radius) / (textLength - 1);
const halfTextLength = Math.floor(textLength / 2);
for (let i = 0; i < textLength; i++) { const char = text[i]; const angle = startAngle + (i - halfTextLength) * (angleStep + letterSpacing / radius);
let x = centerX + radius * Math.sin(angle) * ellipseFactor; let y = centerY - radius * Math.cos(angle);
ctx.save();
ctx.translate(x, y); ctx.rotate(angle);
if (i === halfTextLength) { ctx.rotate(-angle); }
ctx.fillText(char, 0, 0);
ctx.restore(); } } }, watch: { text() { this.drawCurvedText(); }, radius() { this.drawCurvedText(); }, canvasWidth() { this.drawCurvedText(); }, canvasHeight() { this.drawCurvedText(); }, fontSize() { this.drawCurvedText(); }, curvature() { this.drawCurvedText(); }, letterSpacing() { this.drawCurvedText(); }, startAngle() { this.drawCurvedText(); }, ellipseFactor() { this.drawCurvedText(); } } } </script>
<style scoped> canvas { border: 1px solid #000; display: block; margin: 0 auto; } </style>
|