Beautiful HTML 5 Web page special effects learning notes (13)_svg to achieve the ball following curve path movement

Keywords: Javascript

Effect:

  • The ball is at the top of the path. As the mouse scrolls over the page, the path can change.
  • svg rendering path, javascript implementation to follow, difficult to get started: simple

Source code:

From codepen: https://codepen.io/sfi0zy/pen/wVJBGB

Learning Notes:

First, there's only one piece of code in the body of HTML 5. This code is to draw a zigzag path and a red circle.

 <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 1000'>
    <g transform='translate(0,-52.36216)'>
        <path id='path' fill='none' stroke='#1E110D' stroke-width='1' stroke-dasharray='500 1204'  stroke-linecap='round' d='m 19.285714,47.36216 c 13.309222,4.27768 25.499666,11.986979 35.06927,22.177986 9.569604,10.191007 16.497733,22.841748 19.93073,36.393444 1.641394,6.47938 2.496163,13.21286 1.857346,19.86631 -0.638818,6.65345 -2.826525,13.2368 -6.836818,18.58414 -4.010293,5.34734 -9.919997,9.37706 -16.501216,10.54498 -3.290609,0.58396 -6.715251,0.45322 -9.930167,-0.4597 -3.214916,-0.91293 -6.213213,-2.61397 -8.589145,-4.9643 -2.10256,-2.07991 -3.735407,-4.73428 -4.16089,-7.66101 -0.212742,-1.46336 -0.117896,-2.97774 0.337052,-4.38476 0.454947,-1.40703 1.275501,-2.7027 2.395267,-3.66852 1.310941,-1.13072 3.011806,-1.78453 4.73991,-1.88818 1.728104,-0.10364 3.47575,0.33338 4.985123,1.18125 1.509374,0.84787 2.781009,2.09816 3.711403,3.55811 0.930395,1.45995 1.523453,3.12538 1.792212,4.8356 0.537518,3.42044 -0.22162,6.95379 -1.655019,10.10557 -1.433399,3.15178 -3.514869,5.96392 -5.716486,8.63623 -4.893104,5.93922 -10.457873,11.32123 -15.049592,17.49641 -4.59172,6.17518 -8.242104,13.38485 -8.521837,21.07501 -0.161495,4.43967 0.823344,8.9037 2.741626,12.91081 1.918282,4.00711 4.759433,7.55625 8.18158,10.3892 6.844293,5.66591 15.908475,8.34342 24.791079,8.12856 2.761508,-0.0668 5.529744,-0.40146 8.174482,-1.1988 2.644738,-0.79734 5.167526,-2.06857 7.251693,-3.88148 2.084167,-1.81291 3.715772,-4.18312 4.472103,-6.83987 0.756331,-2.65676 0.600524,-5.59816 -0.612563,-8.07985 -0.777994,-1.5916 -1.974244,-2.97256 -3.422298,-3.99315 -1.448054,-1.02058 -3.143829,-1.68124 -4.896724,-1.93777 -3.505792,-0.51306 -7.177888,0.63624 -9.907822,2.89484 -2.729933,2.2586 -4.523193,5.56278 -5.148465,9.0503 -0.625271,3.48753 -0.116194,7.13793 1.232452,10.41435 2.168187,5.26743 6.349179,9.43533 10.724749,13.08242 4.375571,3.64708 9.083381,6.95917 12.849341,11.23286 1.88298,2.13684 3.520136,4.51231 4.644588,7.12905 1.124452,2.61674 1.724709,5.48419 1.524403,8.32524 -0.200305,2.84106 -1.228229,5.65056 -3.076945,7.81712 -1.848716,2.16655 -4.539013,3.64665 -7.380421,3.84188 -1.213126,0.0833 -2.447234,-0.0641 -3.588779,-0.48304 -1.141545,-0.41893 -2.187916,-1.11347 -2.976706,-2.0389 -0.78879,-0.92544 -1.312787,-2.08385 -1.433849,-3.29379 -0.121062,-1.20995 0.17057,-2.46591 0.856476,-3.46998 0.485518,-0.71073 1.160024,-1.28822 1.9286,-1.67572 0.768576,-0.3875 1.629191,-0.586 2.489893,-0.59364 1.721403,-0.0153 3.409029,0.7428 4.647057,1.93894 1.238027,1.19614 2.040286,2.80412 2.400976,4.48738 0.360691,1.68326 0.295405,3.43987 -0.03795,5.12876 -0.555518,2.8144 -1.849411,5.45991 -3.597239,7.73467 -1.747828,2.27477 -3.941652,4.18675 -6.343288,5.7557 -4.803273,3.1379 -10.362641,4.89013 -15.885053,6.44602 -5.522411,1.5559 -11.135815,2.97038 -16.267252,5.53673 -5.131438,2.56636 -9.828566,6.44213 -12.192883,11.66973 -1.717261,3.79694 -2.105875,8.1309 -1.401112,12.23809 0.704763,4.10719 2.468677,7.99254 4.837548,11.42097 4.737743,6.85687 11.720295,11.79595 18.706421,16.34094 10.426551,6.78323 21.425334,13.21663 29.285715,22.85715 10.713801,13.14015 14.330856,31.52205 10.248906,47.97765 -4.08195,16.4556 -15.523436,30.76224 -30.248906,39.1652 -2.286321,1.30467 -4.664811,2.4824 -7.195143,3.20823 -2.530333,0.72584 -5.229713,0.9865 -7.806797,0.44978 -2.577084,-0.53671 -5.022486,-1.91619 -6.584585,-4.03498 -0.781049,-1.0594 -1.333685,-2.2916 -1.566933,-3.58695 -0.233247,-1.29536 -0.143062,-2.65253 0.296315,-3.89322 0.754278,-2.12989 2.530274,-3.83051 4.625166,-4.67715 2.094891,-0.84663 4.474737,-0.87526 6.65274,-0.27387 2.178003,0.60138 4.16065,1.8072 5.844386,3.31399 1.683736,1.50678 3.081077,3.30998 4.306279,5.20846 5.753858,8.91576 7.707813,19.93048 6.855602,30.50741 -0.852211,10.57693 -4.352852,20.78584 -8.799516,30.42041 -8.893328,19.26913 -21.749828,36.98934 -26.627514,57.64361 -2.075907,8.7903 -2.619555,18.08363 -0.355232,26.82729 2.264324,8.74367 7.531337,16.89526 15.253122,21.58065 3.860892,2.34269 8.283601,3.7883 12.793577,4.02245 4.509975,0.23414 9.098347,-0.76125 13.032359,-2.97897 3.934012,-2.21771 7.18735,-5.66866 9.031323,-9.79109 1.843973,-4.12244 2.241564,-8.90162 0.959136,-13.23176 -1.365997,-4.61231 -4.577587,-8.57497 -8.583798,-11.2376 -4.00621,-2.66264 -8.765496,-4.07531 -13.559058,-4.47669 -10.012254,-0.83837 -20.192981,2.73819 -27.937173,9.1394 -7.744193,6.40122 -13.091496,15.4795 -15.6264504,25.20175 -2.5349543,9.72225 -2.3398041,20.05398 -0.1407738,29.85767 2.1990302,9.80369 6.3575082,19.10083 11.5840952,27.68167 10.453172,17.16169 25.002002,31.36502 38.729959,46.03933 13.727957,14.67432 27.026515,30.41632 34.104628,49.22304 2.349311,6.24218 3.989986,12.84054 3.866256,19.50903 -0.12373,6.66849 -2.123253,13.43579 -6.397459,18.55587 -2.137102,2.56004 -4.821273,4.67815 -7.8598,6.05233 -3.038526,1.37418 -6.431905,1.99198 -9.749936,1.65779 -3.31803,-0.33419 -6.549733,-1.63453 -9.100087,-3.78318 -2.550354,-2.14865 -4.394238,-5.14964 -5.044688,-8.42041 -0.548076,-2.75598 -0.248002,-5.66363 0.777355,-8.27982 1.025356,-2.61619 2.764617,-4.9378 4.936931,-6.72018 2.44071,-2.00259 5.422247,-3.32591 8.533952,-3.85952 3.111705,-0.5336 6.346808,-0.28317 9.360399,0.65796 6.027181,1.88224 11.027449,6.5295 13.815379,12.19494 2.787931,5.66544 3.444433,12.26189 2.348302,18.48027 -1.09613,6.21838 -3.880658,12.06813 -7.58662,17.18043 C 80.488061,850.81228 69.682455,857.96466 59.085049,864.833 48.487642,871.70134 37.624392,878.72358 30,888.79074 c -1.864243,2.46153 -3.529655,5.11323 -4.532283,8.03372 -1.002627,2.92049 -1.311314,6.13879 -0.467718,9.10912 0.894025,3.14789 3.057056,5.86609 5.775172,7.68828 2.718115,1.82218 5.961592,2.78149 9.224829,3.02602 4.206142,0.31519 8.543478,-0.56158 12.133657,-2.77553 3.59018,-2.21395 6.374564,-5.8081 7.310356,-9.92091 0.467896,-2.05641 0.476315,-4.22084 -0.01718,-6.27126 -0.493495,-2.05042 -1.491961,-3.98257 -2.910699,-5.543 -1.418739,-1.56042 -3.257916,-2.74237 -5.279148,-3.34434 -2.021231,-0.60197 -4.219604,-0.61685 -6.236987,-0.002 -2.785082,0.84868 -5.137597,2.87104 -6.65917,5.35333 -1.521574,2.48228 -2.251166,5.39737 -2.391297,8.30552 -0.280261,5.81628 1.697322,11.46779 3.336181,17.05544 14.355417,48.94448 2.409598,104.78428 -30.714285,143.57148' />
        <circle id='fire-should-be-here' cx='0' cy='0' r='10' fill='#BC3A3A' />
    </g>
</svg>

viewbox:

First look at how to write svg in css.

svg {
    display: block;
    width: 300px;
    height: 3000px;
    margin: 0 auto;
    background: #F2C78C;
    box-shadow: 0 0 1rem rgba(0, 0, 0, .5);
    transform: rotateX(180deg);
}

It is specified here that the width is 300 PX and the height is 3000px. The latter two parameters of the viewbox seem to be the unit of width and height. Width 300 px, is a hundred units, then a unit is 3px, height 3000px, 1000 units, a unit is 1px. This unit will be used in the drawing of subsequent graphics.

These are the last two parameters. The first two parameters specify the coordinates of the origin, that is, the lower left corner. Generally set to 0 and 0.

g:

Element g is a container for assembling objects. The transformation added to the g element applies to all its child elements. Attributes added to g elements are inherited by all their children. Reference resources:

https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/g

path d:

No matter what strokedasharray and strokeoffset are. Let's first see how the path is drawn. Basic Grammar of Path Drawing https://www.runoob.com/svg/svg-path.html . Then c, which means cubic Bessel curve. The principle of Bessel curve is quite clear here: https://www.jianshu.com/p/0c9b4b681724 . Of course, it's impossible to adjust the value by itself. It's definitely a tool. https://svg.wxeditor.com/ For example, this path online editor. Bessel curve is the pen tool. In fact, the pen tool in ps is also the drawing method of Bessel curve.

Ultimately, that's the effect of deleting strokedasharray and strokeoffset. Draw a long curved curve with the path, which is the path of the ball's movement. So why is it not that the ball moves on the path, but that the path grows behind the ball? The reason is strokedasharray and strokeoffset.

strokedasharray:

Simply put, it is a tool for drawing dotted lines. One parameter represents the length of the solid part, and the second parameter represents the length of the blank part. Of course, more parameters can be added. Let the dotted lines overlap. https://www.runoob.com/svg/svg-stroke.html

strokeoffset:

That is the offset of line. If the 50-100 grid is solid and the 101-150 grid is blank, then the offset is set to 10, that is, the 40-90 grid is solid and the 91-140 grid is dotted.

Rolling effect:

Control with js. The update function is executed every time the page is scrolled with the mouse. body's offset Height has been set to 3000 before, so d in this example is always a fixed value. Page YOffset is the distance between the top left of the current user's view and the top left of the page.

Then offset is used to calculate where the top of the solid part of the path should move, and then attach the ball to the top of the solid part of the path.

const path = document.getElementById('path');
const fire = document.getElementById('fire-should-be-here');

function update() {
    const d = document.body.offsetHeight / path.getTotalLength();
    console.log("offset == " + document.body.offsetHeight,path.getTotalLength(),d);
    const offset = Math.floor(window.pageYOffset / d) +450;
    const firePoint = path.getPointAtLength(1704 + 500 - offset);
    console.log(offset,firePoint.x,firePoint.y);
    
    path.style.strokeDashoffset = offset ;
    fire.setAttribute('cx', firePoint.x);
    fire.setAttribute('cy', firePoint.y);
}

update();

window.addEventListener('scroll', update);

 

Posted by javiqq on Wed, 28 Aug 2019 01:57:54 -0700