Is there a way to modify the font while typing on an SVG path?

I'm working on a code snippet that adds letters along a path to create a typing effect. However, I want the last part of the text "It's our jam" to be displayed in a different font compared to the rest, as shown in the image at the bottom. Is there a way to achieve this? I've been experimenting with adding a class dynamically, but it modifies the entire tspan, and I can't figure out if it's possible to add a second tspan or something similar?

var input = "";
var draw = SVG().addTo("#drawing").viewbox(0, 0, 300, 140);
var text = draw.text(function (add) {
  add.tspan(input).attr("class", "title");
});

const stringToType =
  "Great design is not just our bread + butter ... it's our jam";

textPath = text.path(
  "M 10 20 C 23 15 45 12 64 20 C 99 36 96 48 135 57 C 172 65 188 61 204 58 L 204 58"
);

var count = 0;

var paused = 0;
var secondPause = 0;
var complete = false;
var restart = false;

var newInput = "";

var timer = setInterval(function () {
  if (!paused) {
    var textToAdd = stringToType[count];

    newInput = newInput + textToAdd;
    textPath.tspan(newInput).attr("class", "title");

    count++;
    if (count === stringToType.length) {
      paused++;
    }
  } else {
    clearInterval(timer);
  }
}, 100);
body {
  font-size: 8px;
}
<div id="drawing"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/3.1.1/svg.min.js"></script>

The desired effect I am aiming for (ignoring the cheesy part): https://i.sstatic.net/oLYl0.png

Answer №1

You have the option of utilizing the tspan tag to format a specific section of your text:

let sentence1 = `Great design is not just our bread + butter... `;
let sentence2 = `it's our jam`;
let pointToSentence1 = 1;
let pointToSentence2 = 1;

let textPathElement = document.querySelector('textPath');

let writeText = setInterval(() => {
    let currentText = sentence1.slice(0,pointToSentence1);
    if(pointToSentence1 >= sentence1.length){
        currentText += '<tspan>';
        currentText += sentence2.slice(0,pointToSentence2);
        currentText += '</tspan>'
        pointToSentence2++;
    } else {
        pointToSentence1++;
    }
    
    textPathElement.innerHTML = currentText;
    
    if(pointToSentence2> sentence2.length){
        clearInterval(writeText);
    }
}, 100)  
<svg viewBox="0 0 300 140" xmlns="http://www.w3.org/2000/svg" fill="orange">
    <style>
        tspan {
            font-weight: bold;
        }
    </style>
    
    <path id="textPath" fill="none" d="M 10 20 C 23 15 45 12 64 20 C 99 36 96 48 135 57 C 172 65 188 61 204 58 L 204 58" />
    
    <text>
        <textPath href="#textPath" font-size="9.2"></textPath>
    </text>
</svg>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An option instead of using a pop-up window

Currently, I have a PHP script and a small form that allows users to upload image files. The user can access this feature by clicking on a button on the parent HTML page, which opens up a Pop Up Window. I understand that not everyone is a fan of 'Pop ...

Why is this <div> element refusing to budge according to my jQuery commands?

Currently embarking on a jQuery coding challenge where I am attempting to maneuver a circle around the page in a square pattern. Numerous methods have been tested with no success thus far. While I would like to showcase my various attempts, it would prove ...

What are the reasons behind the min and max range method not providing accurate results?

I am in need of a method that can verify if a given value falls within the valid range of -064.000000 to -180.000000 and 142.000000 to 180.000000. The structure of my ranges object is as follows: "ranges": { "range1": { "min& ...

Secure an input field for exclusive attention. React

How can I lock the focus of my input field? I attempted using the following code: onBlur={this.click()} However, it was not successful. What is the correct way to accomplish this? ...

Having trouble decoding invalid JSON received from the Twilio API

For the past 4 hours, I've been struggling to parse a basic JSON from Twilio. The process is as follows: A text message containing a magnet link is sent Twilio forwards the request to my serverless function in the cloud I attempt to parse the reques ...

Error: Material-UI prop type validation failed. Please specify either children, image, src, or component prop for CardMedia component. This error occurred at

I encountered an issue while trying to utilize the CardMedia component from Material-ui. The error message received was 'Failed prop type: Material-UI: Either children, image, src, or component prop must be specified. at CardMedia'. I attempted v ...

Seamless movements to the exact midpoint

I am trying to find a method to create a smooth transition when removing the center class from p.title.class, allowing it to smoothly move to its new position. JSfiddle Here is an excerpt of my HTML: <body> <div class="wrapper-top"> ...

Using JavaScript to send formatted text through POST requests in a Rails application

Within APP A, I have the following formatted text: A beautiful painting because I created it because an artist endorsed it because my cat loves it I can access this formatted text in index.html.erb through product.body_html, which I then load into a Ja ...

The menu icon in the Bootstrap 5 navigation bar is not functioning on a specific webpage

I am facing an issue with my menu icon not working properly on mobile devices or when I try to inspect the page and run it on a responsive device. How can I resolve this problem so that users can click on the menu icon to view the navigation items? &l ...

Clicking the React Todo Delete Button instantly clears out all tasks on the list

I am dealing with 2 files: App.js import React, { Component } from 'react'; import './App.css'; import ToDo from './components/ToDo.js'; class App extends Component { constru ...

Having an issue with the return function in a jQuery $.ajax call

I am currently working with a $.ajax call that is structured like this: <script type="text/javascript"> $(document).ready(function () { $('#add_com_frm').submit(function (e) { e.preventDefault(); var $b ...

Utilizing JSON data for efficient React component rendering

As a newcomer to React, I am currently in the process of constructing a dashboard that showcases data from three different sensors. The information regarding these sensors is stored in a single JSON file where each sensor has its own ID and name. This JSON ...

What could be causing the issue with this function not loading correctly the first time in Node.JS?

I am currently in the process of developing a Twitter bot using Node.JS. One issue I am facing is with a function that utilizes an npm library called "scrapejs" to extract data from Yahoo Finance. The problem arises when this function loads after the initi ...

Using Knex to Generate a Migration Including a Foreign Key

I attempted to use the code from this link in order to create a foreign key: how to do knex.js migration However, an error occurred at this line of code: table.bigInteger('AddressId') .unsigned() .index() .inTable('Address&apos ...

Can CKEditor be integrated with Mutation Observer? If so, how can this be achieved?

Trying to detect changes in the current CKEditor content. The goal is to identify which element's content has been modified when a user writes multiple paragraphs. Not well-versed in JavaScript or jQuery, but managed to come up with this code after s ...

Is there a way to extract the content from a dynamic textbox using a dynamic button in HTML and AJAX?

My current task involves fetching data from a database and updating the records individually. I have created a table with a text input field and a button that will be generated dynamically for each record. The text input field is populated with the previou ...

modifying a mongodb array without actually updating it

Currently, I am facing an issue with updating the collection of users whose purchase dates have expired. When I attempt to save the changes, the user's role gets updated successfully but the purchase history does not reflect the changes. Below is the ...

Nextjs attempting to access local storage without proper initialization

I'm currently working on a feature where I have a sidebar with two buttons, contact and profile. When a user clicks on either of them, the URL updates to: /?section=clickedItem, allowing the user to save the state and return to that specific page upon ...

Unable to load JQuery from a div element

My goal is to create a standard .html file containing the navigation, footer, and other elements that will be used across multiple pages for a small site I'm building. I want to keep it simple and avoid using php or other programming languages. I&apo ...

I'm finding it a bit difficult to arrange the placement of these DIV

Struggling with a layout issue here. I have a total of 4 DIVs that I want to position in a specific way: Initially, I floated DIV 1 to the left and DIV 4 to the right, which worked as expected. However, things get messy as soon as I try to include DIVs 2 ...