Adjust the path-clip to properly fill the SVG

Is there a way to adjust the clip-path registration so that the line fills correctly along its path instead of top to bottom? Refer to the screenshots for an example. You can view the entire SVG and see how the animation works on codepen, where it is controlled by scrolling.

Screenshots:

https://i.sstatic.net/3ASqF.png

https://i.sstatic.net/9plWv.png

Issue Screenshot:

The current clip-path setup (For complete code, refer to codepen):

g.setAttribute('style', `clip-path: polygon(0 0%, 100% 0%, 100% ${progress+0.8}%, 0% ${progress+0.8}%);`)

UPDATE: If you have a solution, please demonstrate it using my example on codepen. My issue is specific, so I prefer solutions tailored to my problem.

Answer №1

To enhance your colored lines, you can create a path that mirrors the movement of each line back and forth. Make sure the stroke width is wide enough to cover the line at its widest point.

Then, utilize this new line as a <mask> to showcase your colors effectively.

The concept is outlined in my response to a similar inquiry concerning simulating animated handwriting. The technique used there can be applied to address your issue as well.

Update

Have you reviewed my codepen prior to responding?

Indeed, I have. The method detailed in that resource should work seamlessly for your case. As evidence, here is my own CodePen:

https://codepen.io/PaulLeBeau/pen/RwNOaZg

This serves as a demonstration. I've only established a mask path for a small segment of the paths. To illustrate the idea, I opted for a straightforward CSS animation instead of incorporating an onscroll handler.

The process involved should be fairly transparent. You'll just need to adjust your onscroll handler to determine the stroke-dashoffset based on your scroll progress.

If you're unsure about how the stroke-offset effect operates to animate a line's length, numerous tutorials are available online and here on Stack Overflow. For instance, CSS-Tricks provides a comprehensive guide:

https://css-tricks.com/svg-line-animation-works/

Answer №2

After investing several hours into the issue, I have discovered that finding a solution is not as straightforward as it seems. However, I have managed to come up with a solution that, while effective, still poses its own set of challenges.

https://i.sstatic.net/Vc4hs.gif

It's important to note that the blue polygon serves the purpose of illustrating the shape used for the clip-path of the group element <g id="Line_Orange" >

I chose not to attach this to the scroll event in order to focus on developing the logic behind the clip-path polygon using a basic setup. This allowed me to easily start and stop the setInterval() function to track the x and y values for determining where the required turns should take place.

You can view the complete example here: https://codepen.io/Alexander9111/pen/eYmbajB

Below is the JavaScript code:

    var running = false;
const g = document.querySelector("#Line_Orange");
const g_clone = g.cloneNode(true);
const svg = document.querySelector("svg");
g_clone.id = "Line_Grey";
//svg.appendChild(g_clone);
svg.insertBefore(g_clone, svg.childNodes[0]);

g.setAttribute('clip-path', "polygon(0 0, 0 100, 250 100, 250 0)");

const polygon = document.querySelector("#polygon_mask");
let segment_num = 0;
var temp_arr = [];
var polygon_points_arr = [80, 0, 80, 0];
var polygon_points_str = "80 0 80 0";
const polygon_segments = [
  {
    n: 0, dir: 1, progress: "y", boost: 1, init_x_y: [80,0], index: [3,5],
    points:[80, 0, 80, 250, 250, 250, 250, 0]
  }, ...

The heart of the JavaScript lies in this array:

const polygon_segments = [
  {
    n: 0, dir: 1, progress: "y", boost: 1, init_x_y: [80,0], index: [3,5],
    points:[80, 0, 80, 250, 250, 250, 250, 0]
  }, ...

Each segment of the polygon is represented in this array, which accounts for the increasing complexity of the polygon.

To further clarify, refer to this diagram:

https://i.sstatic.net/RpwmV.png

Additionally, this diagram shows how the number of points in the polygon grows:

https://i.sstatic.net/oThvX.jpg

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

Validation of user input alongside input masking using jQuery

One issue that I am encountering is that form inputs with assigned masks (as placeholders) are not being validated as empty by jQuery validation. I am using: https://github.com/RobinHerbots/jquery.inputmask https://github.com/1000hz/bootstrap-validator ...

Is Axios the sole option for API calls when utilizing Next.js with SSG and SSR?

Can someone clarify the best practice for data fetching in Next.js? Should we avoid using axios or other methods in our functional components, and instead rely on SSG/SSR functions? I'm new to Next.js and seeking guidance. ...

Utilizing a TypeScript function to trigger an action from within a Chart.js option callback

Currently, I am utilizing a wrapper for Chart.js that enables an animation callback to signify when the chart has finished drawing. The chart options in my code are set up like this: public chartOptions: any = { animation: { duration: 2000, ...

A comprehensive guide on troubleshooting the toggleComplete functionality for React Todo applications

When you click on an item in the to-do list, it should show a strikethrough to indicate completion. However, when I try clicking on an item, nothing happens. Here is my toggleComplete function and where I am attempting to implement it: class ToDoForm exten ...

How can I create a dropdown menu that is dependent on another dropdown menu using Ajax in my Laravel application?

I have two dropdown fields that are dependent on each other - Class & Section. I am trying to Select * from sections where class_id=selected Class Id. Although I attempted to achieve this using java script, it doesn't seem to work for me. Here are ...

Inject Custom ASP Control Script into the DOM dynamically upon registration

During a postback, I am loading my ascx control when a dropdown change event occurs. Parent C#: private void ddlChange() { MyControl myCtr = (CallScript)Page.LoadControl("~/Controls/MyControl.ascx"); myCtr.property = "something"; // setting publ ...

What is the best way to add numerous images to a Laravel platform via ajax using pure javascript?

I am currently working on a form where users can upload one or more photos to the server using ajax. Sometimes, users may not upload any photos at all. How can I efficiently send the data from the file input to the server in the background? Below is the r ...

Exploring vuelidate: demonstrating personalized validation messages alongside pre-built validators

I'm currently utilizing the vuelidate library to validate my forms. I've been attempting to use the built-in validators along with a custom message, as shown below. However, I have encountered issues with it not functioning properly. For referenc ...

I am utilizing Vuex to store all of the product details and handle API request queries efficiently

Question regarding Vue/Vuex efficiency and best practices. I am working with an API that returns a paginated data-set of 50 products, which I am storing in a Vuex store. When displaying all products, I iterate over the Vuex store. Now, for individual pr ...

Creating Pop-up Dialog Boxes in WebForms Using jQuery

I have implemented a Plugin in Webforms using jQuery UI dialog. Since I have a Content Place holder on my Form, I had to modify the code as shown below: <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <link rel="stylesheet" ...

issue with data binding in ons-dialog

Utilizing an ons-dialog to display a popup prompting the user to sign up for a newsletter. I have set a 3-second timeout to prevent users from immediately closing the prompt without reading it or waiting. I aim to initially show the dialog with the ' ...

Unusual occurrence with the nth-child selector

For the fiddle, click here - http://jsfiddle.net/ashwyn/a45ha/ To see the HTML code, take a look below: <div class="parent"> <div class="a">Class A</div> <div class="b">Class B1</div> <div class="b">Class B ...

Deleting various classes (jQuery)

Is there a more effective alternative to rewriting the following code snippet? $('element').removeClass('class1').removeClass('class2'); I am unable to utilize removeClass(); because it would eliminate ALL classes, and that ...

Create a dynamic AppBar Toolbar in React Admin that changes based on the current page

Recently delving into the world of React, I've been facing some challenges when it comes to loading IconButtons onto the toolBar of an AppBar (from Material UI). For instance, I'm looking to have two specific IconButtons displayed on pageA and d ...

The object[] | object[] type does not have a call signature for the methods 'find()' and 'foreach()'

Here are two array variables with the following structure: export interface IShop { name: string, id: number, type: string, } export interface IHotel { name: string, id: number, rooms: number, } The TypeScript code is as shown below ...

Executing the npm run test command with the unsafe-perm flag within the lifecycle phase

My React/Redux app is working fine, but whenever I run the command below: npm run test An error occurs as shown below: 6 info lifecycle [email protected]~test: [email protected] 7 verbose lifecycle [email protected]~test: unsafe-perm in li ...

Creating a mapping strategy from API call to parameters in getStaticPaths

I am attempting to map parameters in Next.js within the context of getStaticPaths, but I am facing issues with it not functioning as expected. The current setup appears to be working without any problems. https://i.stack.imgur.com/LeupH.png The problem a ...

The Gulp task is failing to generate the CSS file

Greetings! I have a task set up in my gulpfile.js as shown below: const gulp = require('gulp'); const sass = require('gulp-sass'); gulp.task('sass', function(){ return gulp.src('sass/*.scss') .pipe(sass()) ...

Display images with sequential animation and a delay, combining fade-in and slide-up effects

I am attempting to create a cycling image display with specific effects: There should be a one-second delay before the first image is shown. The first image will appear with a fade-in and slide-up effect. Image #1 will remain visible for 5 seconds before ...

Maintaining the integrity of a list within a for loop

I have a challenge where I need to display 3 elements in cards on each row from a list of elements. The issue with my current code is that it only displays the first two elements and then the loop stops. Here is the code snippet using ReactJS and Materia ...