)
}
}
)
(
}
{
)
)
(
)
(
(
{
}
)
(
)
}
)
)
{
(
(
)
)
}
)
(
}

Wobbling Ball with SVG

  1. const el = document.body.appendChild(
  2.   document.createElement('div'));
  3.  
  4. el.innerHTML = `
  5.   <svg width="100%" height="100%">
  6.     <circle 
  7.      id="circ" 
  8.      cx="0" cy="0" r="50"
  9.      fill="red" style="will-change: transform;"/>
  10.   </svg>
  11.   <style>
  12.     svg, div, body, html {
  13.       overflow: visible; 
  14.       height: 100%; 
  15.       width: 100%;
  16.       margin: 0; padding: 0;
  17.     }
  18.   </style>
  19.   `;
  20.  
  21. let w = window.innerWidth,
  22.     h = window.innerHeight,
  23.     x = w / 2,
  24.     y = h / 2,
  25.     vx = vy = dx = dy = 0,
  26.     damp = 0.99, div = 8, ticks = 0, 
  27.     wobbleChance = 0.03,
  28.     startTick = 50;
  29.  
  30. function loop() {
  31.   w = window.innerWidth;
  32.   h = window.innerHeight;
  33.  
  34.   if (x > w){
  35.     vx *= -1;
  36.     dx *= -1;
  37.     x = w;
  38.   } else if (x < 0){
  39.     vx *= -1;
  40.     dx *= -1;
  41.     x = 0;
  42.   }
  43.  
  44.   if (y > h) {
  45.     vy *= -1;
  46.     dy *= -1;
  47.     y = h;
  48.   } else if (y < 0) {
  49.     vy *= -1;
  50.     dy *= -1;
  51.     y = 0
  52.   } 
  53.  
  54.   if (
  55.     Math.random() < wobbleChance || 
  56.     ticks === startTick) {
  57.       dx += Math.random() * 10 - 5;
  58.       dy += Math.random() * 10 - 5;
  59.   }
  60.  
  61.   dx *= damp;
  62.   dy *= damp;
  63.  
  64.   vx += (dx - vx) / div;
  65.   vy += (dy - vy) / div;
  66.  
  67.   x += vx;
  68.   y += vy;
  69.  
  70.   circ.setAttribute('transform', `translate(${x} ${y})`);
  71.  
  72.   ticks++;
  73.   window.requestAnimationFrame(loop);
  74. }
  75. loop();
  76.  
  77. function resize() {
  78.   const radius = Math.min(w, h) * .05;
  79.  
  80.   // `window.circ` is the global id (⌐■_■)
  81.   circ.r.baseVal.value = radius;
  82. }
  83. resize();
  84. window.addEventListener('resize', resize);

A wobbling ball with svg.

Drawing Gears With SVG

  1. const el = document.body.appendChild(
  2.   document.createElement('div'));
  3.  
  4. el.innerHTML = `
  5.   <svg id="svg" width="100%" height="100%" viewBox="0 0 550 496">
  6.     <path id='path' d="M 10 10 L 100 100" stroke="black" fill='none' vector-effect="non-scaling-stroke"/>
  7.   </svg>
  8.   <style>
  9.     svg, div, body, html {
  10.       overflow: visible; 
  11.       height: 100%; 
  12.       width: 100%;
  13.       margin: 0; padding: 0;
  14.     }
  15.   </style>
  16.   `;
  17.  
  18. // a variation on something from my old 
  19. // site https://actionsnippet.com/?p=1175
  20.  
  21. const TWO_PI = Math.PI * 2;
  22. let cmds = '';
  23.  
  24. // x, y, max radius, notch number
  25. drawVerts(calcGear(200, 200, 50, 6));
  26. drawVerts(calcGear(400, 200, 30, 3));
  27. drawVerts(calcGear(300, 350, 30, 20));
  28. drawVerts(calcGear(400, 400, 30, 2));
  29. dotVerts(drawVerts(calcGear(150, 400, 30, 6)));
  30.  
  31. // `<path id=...` becomes a global o_Ö
  32. window.path.setAttribute('d', cmds);
  33.  
  34. function calcGear(x, y, maxRad, notches) {
  35.   let verts = [],
  36.       step = TWO_PI / (notches * 8),
  37.       mod = 0, r;
  38.   for (let i = 0; i <= TWO_PI; i += step) {
  39.     r = (parseInt(mod) % 2 + 1) * maxRad;
  40.     mod += .25;
  41.     verts.push(x + r * Math.cos(i));
  42.     verts.push(y + r * Math.sin(i));
  43.   }
  44.   return verts;
  45. }
  46.   
  47. function drawVerts(verts) {
  48.   cmds += `M ${verts[0]} ${verts[1]} L`;
  49.     for (let i = 2; i < verts.length; i += 2) {
  50.     cmds += ` ${verts[i]} ${verts[i + 1]} `
  51.     }
  52.   cmds += 'z ';
  53.   return verts;
  54. }
  55.  
  56. function dotVerts(verts, rad = 3) {
  57.   let dots = '';
  58.   for (let i = 2; i < verts.length; i+=2) {
  59.     dots += `
  60.       <circle 
  61.         cx="${verts[i]}" cy="${verts[i + 1]}"
  62.         r="${rad}"
  63.         fill="red" />
  64.     `
  65.   }
  66.   // another global o_Ö 
  67.   window.svg.innerHTML += dots;
  68. }

Using yesterdays technique, this is a variation of an old snippet from ActionSnippet.com

Quick SVG

  1. const el = document.body.appendChild(
  2.     document.createElement('div'));
  3. el.innerHTML = `<svg style='overlow:visible'>
  4.   <circle cx="100" cy="20" r="20" fill="red" />
  5.   <rect x="100" y="60" width="20" height="20" fill="blue" />
  6. </svg>`;

Defining SVG like this in a template string is a fast and powerful way to start doing SVG that is controlled or generated by JavaScript. Here is another example:

  1. const scribble = document.body.appendChild(
  2.     document.createElement('div'));
  3. const d = (iter = 30) => {
  4.   // make sure the iter is odd
  5.   if (iter % 2 == 0) iter += 1;
  6.   // random cubic beziér
  7.   let path = 'M ';
  8.   for (let i = 0; i < iter; i++) {
  9.     const cmd = i == 1 ? 'C ' : ' '
  10.     path += cmd + Math.random() * 200 + ' ' + Math.random() * 200;
  11.   }
  12.   return path + 'z';
  13. }
  14. scribble.innerHTML = `<svg style='overlow:visible' viewBox="0 0 200 200">
  15.   <path d="${d()}" 
  16.     stroke="#295896" 
  17.     stroke-width="3"
  18.     fill="#ccc" 
  19.     fill-rule="even-odd"
  20.     vector-effect="non-scaling-stroke" />
  21. </svg>
  22. <style>
  23.   svg, div, body, html {
  24.     overflow: visible; 
  25.     height: 100%; 
  26.     width: 100%;
  27.     margin: 0; padding: 0;
  28.   }
  29. </style>
  30. `;

You’ll notice a somewhat hacky style tag as well… this is used to quickly fill the page with the SVG.

snippet.zone ~ 2021-24 /// {s/z}