简体   繁体   English

如何使用 offset-path CSS 属性从其原点位置偏移 SVG 路径

[英]How to offset SVG path from its origin position using offset-path CSS property

I have a bunch of SVG paths that are text letters.我有一堆文本字母的 SVG 路径。 On scroll I want to animate them along offset-path starting from their original position.在滚动时,我想从它们的原始位置开始沿着offset-path为它们设置动画。 So I give 0x and 0y starting position to offset-path property and then give randomized Line to offset-path along which I want to animate SVG letters, like so:因此,我将0x0y起始位置赋予offset-path属性,然后将随机Line赋予offset-path ,我想沿着它为 SVG 字母设置动画,如下所示:

path.setAttribute("style", "offset-path: path('M" + 0 +" " + 0 + " L " + generateRandomAnimationPathLine() + " " + generateRandomAnimationPathLine() + "')"); 

But once I give all SVG paths offset-path with random L attributes they are already all over the screen with offset-distance set to 0% .但是,一旦我给所有 SVG 路径offset-path加上随机L属性,它们就已经遍布整个屏幕,并且offset-distance设置为0% Why is that?这是为什么? Shouldn't they stay in their origin if offset-distance is set to 0?如果offset-distance设置为 0,它们不应该留在原点吗? Why L in offset-path: path() moves SVG from their origin even though M are set to 0?为什么L in offset-path: path()即使M设置为 0 也会从它们的原点移动 SVG?

You can inspect each SVG letter and check off offset-distance to see that they are already out of origin point.您可以检查每个 SVG 字母并检查offset-distance以查看它们是否已经超出原点。 How can I animate SVG paths starting from their original position?如何从原始位置开始为 SVG 路径设置动画? I'm trying to achieve an effect where as you scroll it slowly destructs the "Hello, World" and when you scroll back up it goes back to original form.我试图实现一种效果,当您滚动它时,它会慢慢破坏“Hello, World”,当您向上滚动时,它又会恢复到原始形式。

Image to explain what I'm trying to achieve.图像来解释我想要实现的目标。 When SVG is on screen, set offset-path to random Lines/L along which I want to animate SVG letters from their original position by changing offset-distance from 0% to 100% relative to the scroll, like so:当 SVG 在屏幕上时,将offset-path设置为随机Lines/L ,我想通过将相对于滚动的offset-distance0%更改为100%来为 SVG 字母的原始位置设置动画,如下所示:

path.style.offsetDistance = element.intersectionRatio * 100 + "%";

在此处输入图片说明

Live example: https://codepen.io/Limpuls/pen/KKzZWEm实例: https : //codepen.io/Limpuls/pen/KKzZWEm

Code:代码:

 <section class="row first">
                <div class="col-lg-12">
                    <div class="svg">
                        <svg viewBox="-145 -90 500 500" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M26 2V30H19.52V18.52H6.80001V30H0.320007V2H6.80001V13.04H19.52V2H26Z" fill="black"/>
                            <path d="M53.2647 19.32C53.2647 19.4 53.2247 19.96 53.1447 21H36.8647C37.158 22.3333 37.8514 23.3867 38.9447 24.16C40.038 24.9333 41.398 25.32 43.0247 25.32C44.1447 25.32 45.1314 25.16 45.9847 24.84C46.8647 24.4933 47.678 23.96 48.4247 23.24L51.7447 26.84C49.718 29.16 46.758 30.32 42.8647 30.32C40.438 30.32 38.2914 29.8533 36.4247 28.92C34.558 27.96 33.118 26.64 32.1047 24.96C31.0914 23.28 30.5847 21.3733 30.5847 19.24C30.5847 17.1333 31.078 15.24 32.0647 13.56C33.078 11.8533 34.4514 10.5333 36.1847 9.6C37.9447 8.64 39.9047 8.16 42.0647 8.16C44.1714 8.16 46.078 8.61333 47.7847 9.52C49.4914 10.4267 50.8247 11.7333 51.7847 13.44C52.7714 15.12 53.2647 17.08 53.2647 19.32ZM42.1047 12.88C40.6914 12.88 39.5047 13.28 38.5447 14.08C37.5847 14.88 36.998 15.9733 36.7847 17.36H47.3847C47.1714 16 46.5847 14.92 45.6247 14.12C44.6647 13.2933 43.4914 12.88 42.1047 12.88Z" fill="black"/>
                            <path d="M57.4191 0.319998H63.6591V30H57.4191V0.319998Z" fill="black"/>
                            <path d="M69.4503 0.319998H75.6903V30H69.4503V0.319998Z" fill="black"/>
                            <path d="M91.7216 30.32C89.4549 30.32 87.4149 29.8533 85.6016 28.92C83.8149 27.96 82.4149 26.64 81.4016 24.96C80.3882 23.28 79.8816 21.3733 79.8816 19.24C79.8816 17.1067 80.3882 15.2 81.4016 13.52C82.4149 11.84 83.8149 10.5333 85.6016 9.6C87.4149 8.64 89.4549 8.16 91.7216 8.16C93.9882 8.16 96.0149 8.64 97.8016 9.6C99.5882 10.5333 100.988 11.84 102.002 13.52C103.015 15.2 103.522 17.1067 103.522 19.24C103.522 21.3733 103.015 23.28 102.002 24.96C100.988 26.64 99.5882 27.96 97.8016 28.92C96.0149 29.8533 93.9882 30.32 91.7216 30.32ZM91.7216 25.2C93.3216 25.2 94.6282 24.6667 95.6416 23.6C96.6816 22.5067 97.2016 21.0533 97.2016 19.24C97.2016 17.4267 96.6816 15.9867 95.6416 14.92C94.6282 13.8267 93.3216 13.28 91.7216 13.28C90.1216 13.28 88.8016 13.8267 87.7616 14.92C86.7216 15.9867 86.2016 17.4267 86.2016 19.24C86.2016 21.0533 86.7216 22.5067 87.7616 23.6C88.8016 24.6667 90.1216 25.2 91.7216 25.2Z" fill="black"/>
                            <path d="M109.819 22.56C110.939 22.56 111.859 22.92 112.579 23.64C113.299 24.3333 113.659 25.2533 113.659 26.4C113.659 26.9333 113.592 27.4667 113.459 28C113.326 28.5333 113.032 29.3333 112.579 30.4L110.299 36.16H106.339L108.099 29.8C107.432 29.5333 106.899 29.1067 106.499 28.52C106.126 27.9067 105.939 27.2 105.939 26.4C105.939 25.2533 106.299 24.3333 107.019 23.64C107.766 22.92 108.699 22.56 109.819 22.56Z" fill="black"/>
                            <path d="M171.976 2L162.816 30H155.856L149.696 11.04L143.336 30H136.416L127.216 2H133.936L140.256 21.68L146.856 2H152.856L159.256 21.84L165.776 2H171.976Z" fill="black"/>
                            <path d="M183.792 30.32C181.525 30.32 179.485 29.8533 177.672 28.92C175.885 27.96 174.485 26.64 173.472 24.96C172.459 23.28 171.952 21.3733 171.952 19.24C171.952 17.1067 172.459 15.2 173.472 13.52C174.485 11.84 175.885 10.5333 177.672 9.6C179.485 8.64 181.525 8.16 183.792 8.16C186.059 8.16 188.085 8.64 189.872 9.6C191.659 10.5333 193.059 11.84 194.072 13.52C195.085 15.2 195.592 17.1067 195.592 19.24C195.592 21.3733 195.085 23.28 194.072 24.96C193.059 26.64 191.659 27.96 189.872 28.92C188.085 29.8533 186.059 30.32 183.792 30.32ZM183.792 25.2C185.392 25.2 186.699 24.6667 187.712 23.6C188.752 22.5067 189.272 21.0533 189.272 19.24C189.272 17.4267 188.752 15.9867 187.712 14.92C186.699 13.8267 185.392 13.28 183.792 13.28C182.192 13.28 180.872 13.8267 179.832 14.92C178.792 15.9867 178.272 17.4267 178.272 19.24C178.272 21.0533 178.792 22.5067 179.832 23.6C180.872 24.6667 182.192 25.2 183.792 25.2Z" fill="black"/>
                            <path d="M205.723 11.32C206.469 10.28 207.469 9.49333 208.723 8.96C210.003 8.42667 211.469 8.16 213.123 8.16V13.92C212.429 13.8667 211.963 13.84 211.723 13.84C209.936 13.84 208.536 14.3467 207.523 15.36C206.509 16.3467 206.003 17.84 206.003 19.84V30H199.763V8.48H205.723V11.32Z" fill="black"/>
                            <path d="M216.833 0.319998H223.073V30H216.833V0.319998Z" fill="black"/>
                            <path d="M250.784 0.319998V30H244.824V27.52C243.278 29.3867 241.038 30.32 238.104 30.32C236.078 30.32 234.238 29.8667 232.584 28.96C230.958 28.0533 229.678 26.76 228.744 25.08C227.811 23.4 227.344 21.4533 227.344 19.24C227.344 17.0267 227.811 15.08 228.744 13.4C229.678 11.72 230.958 10.4267 232.584 9.52C234.238 8.61333 236.078 8.16 238.104 8.16C240.851 8.16 242.998 9.02667 244.544 10.76V0.319998H250.784ZM239.184 25.2C240.758 25.2 242.064 24.6667 243.104 23.6C244.144 22.5067 244.664 21.0533 244.664 19.24C244.664 17.4267 244.144 15.9867 243.104 14.92C242.064 13.8267 240.758 13.28 239.184 13.28C237.584 13.28 236.264 13.8267 235.224 14.92C234.184 15.9867 233.664 17.4267 233.664 19.24C233.664 21.0533 234.184 22.5067 235.224 23.6C236.264 24.6667 237.584 25.2 239.184 25.2Z" fill="black"/>
                            </svg>                       
                    </div>           
                </div>
    </section>

JS: JS:

let thresholdArray = []
for (let i = 10; i < 100; i += 1){    
    thresholdArray.push(i / 100);    
}

let options = {
  root: null,
  rootMargin: "20px",
  threshold: thresholdArray
};

let pathGenerated = false;
let callback = (entries, observer) => {
    entries.forEach(element => {
        element.target.querySelectorAll("path").forEach(path => {
        if (element.isIntersecting) {
            if (!pathGenerated) {           
              path.setAttribute("style", "offset-path: path('M" + 0 +" " + 0 + " L " + generateRandomAnimationPathLine() + " " + generateRandomAnimationPathLine() + "')"); 
            }
            path.style.offsetDistance = element.intersectionRatio * 100 + "%";          
        } else {           
            pathGenerated = false;
            path.style.removeProperty("offset-path");            
        }
        
    });
    }); 
    pathGenerated = true;  
}

let generateRandomAnimationPathLine = (element) => {
    return Math.floor(Math.random() * Math.floor(100));
}

let observer = new IntersectionObserver(callback, options);

document.querySelectorAll('section').forEach(section => {
    console.log(section)
    observer.observe(section);
});

I have to say I find the usage of offset-path for the goal you want to achieve suboptimal.我不得不说我发现使用offset-path来实现你想要实现的目标并不理想。 Support for several properties of the Motion path CSS module is incomplete, and as we have found out, the relation of rotation to the path is counter-intuitive.对 Motion path CSS 模块的几个属性的支持是不完整的,正如我们发现的,旋转与路径的关系是违反直觉的。 SVG itself has a mechanism for shifting and rotating individual letters in a text that can do this without any compatibility issues. SVG 本身有一个机制来移动和旋转文本中的单个字母,可以在没有任何兼容性问题的情况下做到这一点。 This assumes you can use a <text> element instead of letters converted to paths.这假设您可以使用<text>元素而不是转换为路径的字母。

<text> and <tspan> elements accept a number of attributes that take a list of space-separated numbers as argument. <text><tspan>元素接受许多以空格分隔的数字列表作为参数的属性。 The nth number is applied to the nth letter inside the element:第 n 个数字应用于元素内的第 n 个字母:

  • dx and dy shift each letter away from the position it would be rendered at. dxdy将每个字母从它要呈现的位置移开。 The shifting is accumulative.转移是累积的。 In other words: after the first letter is shifted according to the first values, the second letter is rendered next to the shifted first letter, and is shifted away from that position according to the second values.换句话说:在根据第一个值移动第一个字母后,第二个字母在移动的第一个字母旁边呈现,并根据第二个值从位置移开。
  • rotate rotates each letter, but is not accumulative: each number represents the rotation away from its original orientation. rotate旋转每个字母,但不是累积的:每个数字代表远离其原始方向的旋转。

The following example is a bit reduced from your pen, setting the "distance" with a slider instead of using an Observer.以下示例与您的笔略有不同,使用滑块而不是使用观察者设置“距离”。 The maximum distances and rotations are stored in a global object.最大距离和旋转存储在全局对象中。 On each slider change, the list of values in the dx , dy and rotate attributes is recomputed to a fraction of the maximum distance and angle.在每次滑块更改时, dxdyrotate属性中的值列表会重新计算为最大距离和角度的一小部分。 Because of the accumulative nature of the distance values, the shift is "reset" for each letter by subtracting the shift of the previous letter.由于距离值的累积性质,通过减去前一个字母的位移来“重置”每个字母的位移。

 const offsets = []; for (let i = 0; i < 11; i++) { offsets.push({ x: Math.random() * 60 - 30, y: Math.random() * 60 - 30, a: Math.random() * 360 - 180 }); } const text = document.querySelector('.mesg'); document.querySelector('#distance').addEventListener('change', (ev) => { const dist = parseFloat(ev.target.value); const attrs = offsets.reduce((attr, o, i) => { attr.dx.push((i ? ox - offsets[i-1].x : ox) * dist); attr.dy.push((i ? oy - offsets[i-1].y : oy) * dist); attr.rotate.push(oa * dist); return attr; }, {dx: [], dy: [], rotate: []}); for (let [key, value] of Object.entries(attrs)) { text.setAttribute(key, value.join(" ")); } })
 text { font-size: 20px; font-family: sans-serif; }
 <div><input id="distance" type="range" min="0" max="1" step="0.05" value="0"></input></div> <svg viewBox="0 0 200 100" width="100%" height="150"> <text x="10" y="60" class="mesg">Hello World</text> </svg>

There are two misunderstandings.有两个误解。 The first one concerns the way offset-path works.第一个涉及offset-path工作方式。 If you have a path defined along which to move a letter, this is done using a moving coordinate system:如果您定义了沿其移动字母的路径,则这是使用移动坐标系完成的:

  • The origin of the coordinate system is moved to the point on the path indicated by offset-distance .坐标系的原点移动到由offset-distance指示的路径上的点。
  • If nothing else is set for the property offset-rotate , the coordinate system is rotated automatically such that the x-axis points along the tangent to the path at the current used point.如果没有为属性offset-rotate设置任何其他内容,坐标系将自动旋转,以便 x 轴沿当前使用点的路径的切线点。
  • Then, the letter is drawn in the moved and rotated coordinate system.然后,在移动和旋转的坐标系中绘制字母。

Contrary to what your grafic above implies, the letters are not moved away from their original position with an increasing distance, each of them always has a distance from the origin of its unmoved coordinate system, which is rotated and moved along the offset path.与上面的图形所暗示的相反,字母不会随着距离的增加而远离其原始位置,它们中的每一个始终与其未移动坐标系的原点一段距离,该坐标系沿偏移路径旋转和移动。 (Since each path has a separate offset path, this means a separate coordinate system for each letter.) (由于每个路径都有一个单独的偏移路径,这意味着每个字母都有一个单独的坐标系。)

In a sense, for your straight lines, the rotation is applied before there is even a small distance, and that is why the letters end up all over the place.从某种意义上说,对于您的直线,甚至在很小的距离之前就应用了旋转,这就是为什么字母会到处都是。

在此处输入图片说明

As soon as you set只要你设置

path {
  offset-rotate: 0deg;
}

the rotation of the coordinate system is suppressed, and at offset-distance: 0 the letters are displayed at their original place.坐标系的旋转被抑制,并且在offset-distance: 0 ,字母显示在其原始位置。 For greater distances, they still move in different directions, but unrotated.对于更远的距离,它们仍然向不同的方向移动,但不旋转。

If you want to give the letters an increasing rotation dependent on the distance, you would have to compute that in the script.如果你想根据距离给字母一个递增的旋转,你必须在脚本中计算它。 But remember, the center of rotation is not the moved letter position, but the origin of the original coordinate system!但是请记住,旋转的中心不是移动的字母位置,而是原始坐标系的原点! That has the consequence that a linear function that rotates the letter increasingly with declining intersection, combined with an increasing distance, will produce a movement along a spiral .其结果是,一个线性函数随着交叉点的减少而不断旋转字母,再加上距离的增加,将产生沿螺旋的运动。 (Think of the combination of distance and rotation as polar coordinates.) (将距离和旋转的组合视为极坐标。)

The second misunderstanding lies in the value of intersectionRatio the IntersectionObserver gives back.误区之二在于价值intersectionRatio的IntersectionObserver还给。 An intersection of 0% means the object is outside the reference, 100% means is is inside . 0% 的交集表示对象在参考外部,100% 表示在内部 Therefore, if you want to positiion the letters "orderly" as long as the section is visible, you have to compute the offset distance as inverse to the intersection:因此,如果要在截面可见的情况下“有序”定位字母,则必须计算与交点相反的偏移距离:

let callback = (entries, observer) => {
    entries.forEach(element => {
      element.target.querySelectorAll("path").forEach(path => {
        if (element.isIntersecting) {
          if (!pathGenerated) {           
            const x1 = 0;        
            const y1 = 0;        
            const x2 = generateRandomAnimationPathLine();        
            const y2 = generateRandomAnimationPathLine();
            path.style.offsetPath = `path('M ${x1} ${y1} L ${x2} ${y2}')`;
          }
          path.style.offsetDistance = (1 - element.intersectionRatio) * 100 + "%";
        } else {           
          pathGenerated = false;
          path.style.removeProperty("offset-path");            
        }
      });
    }); 
    pathGenerated = true;  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM