Adjustable Font Controls

Currently, I'm exploring the benefits of utilizing variable fonts for a school project. As part of this project, I am working on incorporating sliders to adjust the font attributes.

Specifically, I have chosen to use the Gingham variable font for this exercise.

While I have successfully implemented the font-weight slider, I seem to be facing difficulties with the width slider and can't seem to pinpoint the issue. Any insights on what might be going wrong?

Below is the snippet of my HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Variable Fonts</title>
  <link rel="stylesheet" href="css/master.css">
</head>
<body>

  <h1>Adjust the sliders to modify the Variable Font: Gingham</h1>

  <p id="testarea" contenteditable="true">Type Text Here</p>

  <label for="slider_weight">Weight</label>
  <input type="range" id="slider_weight" name="slider" value="300" min="300" max="700" step="10">
  <span id="value_weight"></span>
  <br>

  <label for="slider_width">Width</label>
  <input type="range" id="slider_width" name="slider" value="100" min="10" max="100" step="1">
  <span id="value_width2"></span>

<script src="js/slider.js" charset="utf-8"></script>
</body>
</html>

Here is my JavaScript implementation:

document.getElementById('slider_weight').addEventListener("input", function () {
  var axisValue = document.getElementById('slider_weight').value;

  document.getElementById('testarea').style.fontWeight = axisValue;

  document.getElementById('value_weight').innerText = axisValue;
});

document.getElementById('slider_width').addEventListener("input", function () {
  var axisValue2 = document.getElementById('slider_width').value;

  document.getElementById('testarea').style.fontStretch = axisValue2;

  document.getElementById('value_width2').innerText = axisValue2;
});

Check out the JSFiddle link

Answer №1

To ensure correct rendering of interpolated width or weight values, you must include ranges for each variable design axis in your @font-face rule as demonstrated below:

 @font-face {
  font-family: 'Roboto Flex';
  font-style: normal;
  font-weight: 100 1000;
  font-stretch: 0% 200%;
  src: url(fontfile.woff2) format('woff2');
}

If these ranges are not added, browsers may struggle to render the values correctly.

slider_weight.addEventListener("input", function () {
  var axisValue = slider_weight.value;
  testarea.style.fontWeight = axisValue;
  value_weight.innerText = axisValue;
});

slider_width.addEventListener("input", function () {
  var axisValue2 = slider_width.value;
  testarea.style.fontStretch = axisValue2+'%';
  value_width.innerText = axisValue2;
});
@font-face {
  font-family: 'Roboto Flex';
  font-style: normal;
  font-weight: 100 1000;
  font-stretch: 0% 200%;
  src: url(https://fonts.gstatic.com/s/robotoflex/v9/NaNeepOXO_NexZs0b5QrzlOHb8wCikXpYqmZsWI-__OGfttPZktqc2VdZ80KvCLZaPcSBZtOx2MifRuWR28sPJtUMbsFEK6cRrleUx9Xgbm3WLHa_F4Ep4Fm0PN19Ik5Dntczx0wZGzhPlL1YNMYKbv9_1IQXOw7AiUJVXpRJ6cXW4O8TNGoXjC79QRyaLshNDUf9-EmFw.woff2) format('woff2');
}


html {
  font-family: 'Roboto Flex';
}

#testarea {
  font-size: 2em;
  transition:0.5s;
  font-weight:100;
  font-stretch: 0%;
}
  <p id="testarea" contenteditable="true">Type something ...</p>

  <label for="slider_weight">Weight</label>
  <input type="range" id="slider_weight" name="slider" value="100" min="100" max="1000" step="10">
  <span id="value_weight">100</span>
  <br>

  <label for="slider_width">Width</label>
  <input type="range" id="slider_width" name="slider" value="0" min="0" max="200" step="1">
  <span id="value_width">0</span>

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

What is the best method for presenting nested JSON data in React using a key-value pair format?

This component serves as the product description section with tabs for both description and details. Selecting the description tab displays the product specifications in a tabular format. We are utilizing the Axios library to fetch JSON data from an API. I ...

No further code will run after the jquery .each function has completed its execution

After running my .each function, it seems that no code is executed. I assumed that all code following the each function would be executed once it's completed? Here is the JavaScript file I am using: function onUploadLoad() { $.ajax({ typ ...

Display when moving up and conceal when moving down

Is there a way to make the div appear when scrolling up and down on the page? I'm new to jquery and could use some assistance, please. ...

Photo uploading in ASP.NET MVC - encountering null HttpPostedFileBase issue

QUESTION: I'm having an issue where the Photo1 value is null in the controller post method despite uploading it. Can someone help with this? This is my model class: class ProductVM{ public string Name { get; set;} public string Color {get; ...

How to center an inline element using CSS and a background color

I'm struggling to center a block of text in a div with fixed dimensions both horizontally and vertically. I also want the text to have a semi-transparent black background and be able to stretch across two lines if needed. The issue I'm facing is ...

Column with a bottom aligned div

My current layout in Bootstrap 4 consists of four columns, but I am facing an issue where one of the columns with longer text is causing misalignment of the "buttons" at the end. https://i.sstatic.net/lWFgQ.png Below is the HTML code: < ...

Utilize Node Package to Dispatch Firebase Push Notifications

I am currently experimenting with a NodeJS module for sending Push Notifications. I have been exploring the 'node-sender' module but have not been able to find an option for sending notifications to a web browser for testing purposes: https://w ...

Utilizing JQuery for Redirection

I need help with a specific issue on my website. Visit this link On the webpage, there is a button labeled "get a quote". When users click on this button, I want them to be redirected to google.com. I attempted to achieve this using the following JavaSc ...

Replace all occurrences of a specific string throughout a extensive document in Node.js

I'm facing a challenge with handling large files in memory. I need to go through each line, replace any double quotes found, and update the file itself. Currently, I am reading the file line by line, storing it in an array, and then overwriting the sa ...

Failing to hide a div on hover: Hoverintent's shortcomings

When I hover over the div with the unique identifier id="navbar", it doesn't seem to trigger any actions. I have included the following script in my document head: <script type="text/javascript" src="https://ajax.googleapi ...

Guide on implementing two submission options in an HTML form using JavaScript

Currently, I am working on a form that includes two buttons for saving inputted data to different locations. However, I am facing an issue with the functionality of the form when it comes to submitting the data. Since only one submit function can be activa ...

Navigating through dynamic elements using Selenium

I'm having trouble extracting boxer information from the flashcore.com website using Selenium. The code I've written doesn't seem to be working properly. Can anyone point out where the error might be? The expectation is that Selenium should ...

Horizontal rule spans the full width of the content, appearing within an ordered list

Check out this example to see the issue with the horizontal rule not extending all the way across under the number. I've tried using list-style-position:inside;, but that interferes with positioning the number correctly due to the left image being flo ...

What is the best way to add margins to the elements in a row without disrupting the row layout?

I'm trying to figure out how to add margin between elements in my code snippet without breaking the row. You can view the fiddle here. Although a similar question has been asked on Stack Overflow here, I wasn't able to find a solution that works ...

Tips for invoking an Android function from an AngularJS directive?

I am facing an issue with my HTML that uses an AngularJS directive. This HTML file is being used in an Android WebView, and I want to be able to call an Android method from this directive (Learn how to call Android method from JS here). Below is the code ...

The vertical scrolling functionality of the MUI DataGrid in Safari may occasionally fail to work

Utilizing the <Box> component from MUI core and the <DataGrid> component from MUIX, along with some other components, I have created a data grid that has the following appearance: https://i.sstatic.net/Gc8sP.png When the number of rows exceed ...

Using Angular $resources in the query() function often results in the error message "Undefined is not a function

When I try to call the query() function using ngResource, I keep getting an error message saying "Undefined is not a function." [demo.js] var app = angular.module('StarterApp', ['ngResource']); app.factory("resourceFactory", function ...

Exploring the similarities between nested objects and arrays in JavaScript when comparing and determining key equality

In my scenario, I am dealing with two nested objects named obj1 and obj2. The goal is to compare these objects recursively and then return another object in which each nested key will have a boolean flag indicating equality. For instance, consider an obj1 ...

What method can be used to verify if Handlebar.js is integrated into the application?

I am struggling to verify the presence of Handlebar JS on the application's HTML page. No matter what I do, it keeps throwing an error saying "Uncaught Reference Error - Handlebars is not defined". Can anyone provide guidance on how to fulfill this ta ...

Issue in Jquery: Unable to load the corresponding pages when toggling the checkbox on and off

I am facing an issue with a checkbox and calling different php pages based on the status of the checkbox. Currently, the code works only for checked checkboxes. I'm not sure why it's not working for unchecked checkboxes as well. <script type ...