aboutsummaryrefslogtreecommitdiffstats
path: root/html/index.html
blob: 04bb11bdc8ae244b2eb3d3fc73cd849fd541011c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html>
<head>
  <title>calligram.me</title>
  <style>
    div {
      width: 100%;
      height: 100%;
      overflow: visible;
      display: block;
      margin: auto;
      position: absolute;
      text-align: center;
    }

    svg {
      width: 100%;
      height: 90%;
      overflow: visible;
      display: flex;
      justify-content: center;
      align-self: stretch;
      position: absolute;
    }
 
    svg path {
      fill:transparent;
      stroke:black;
      stroke-width:.02em;
    }

    svg text {
      font-size:30px;
      fill: brown;
    }
  </style>
</head>
<body>
  <input id="the_text" type="text" value="" placeholder="Place text here...">
  <input id="the_svg" type="text" value="" placeholder="Paste SVG path 'd' field here...">
  <div id="svg-target"></div>
  <script>
    const targetDiv = document.getElementById('svg-target');
    const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    svgNode.setAttributeNS(null, 'viewBox', '0 0 1500 1000');
    svgNode.setAttributeNS(null, 'width', '100%');
    svgNode.setAttributeNS(null, 'preserveAspectRatio', 'xMidyMid meet');
    targetDiv.appendChild(svgNode);
    
    const svgPath = document.createElementNS('http://www.w3.org/2000/svg', 'path'); 
    svgPath.setAttributeNS(null, 'id', 'svgpath');
    svgNode.appendChild(svgPath);
    
    const textNode = document.createElementNS('http://www.w3.org/2000/svg', 'text');
    textNode.setAttributeNS(null, 'text-anchor', 'middle');
    textNode.setAttributeNS(null, 'id', 'moving_text');
    svgNode.appendChild(textNode);
    
    const textPath = document.createElementNS('http://www.w3.org/2000/svg', 'textPath');
    textPath.setAttributeNS(null, 'startOffset', '0%');
    textPath.setAttributeNS(null, 'href', '#svgpath');
    textPath.textContent = "envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope  envelope!";
    textNode.appendChild(textPath);
    
    const animate = document.createElementNS('http://www.w3.org/2000/svg', 'animate');
    animate.setAttributeNS(null, 'attributeName', 'startOffset');
    animate.setAttributeNS(null, 'from', '-50%');
    animate.setAttributeNS(null, 'to', '150%');
    animate.setAttributeNS(null, 'begin', '0s');
    animate.setAttributeNS(null, 'repeatCount', 'indefinite');
    updateSVG("M64 112c-8.8 0-16 7.2-16 16v22.1L220.5 291.7c20.7 17 50.4 17 71.1 0L464 150.1V128c0-8.8-7.2-16-16-16H64zM48 212.2V384c0 8.8 7.2 16 16 16H448c8.8 0 16-7.2 16-16V212.2L322 328.8c-38.4 31.5-93.7 31.5-132 0L48 212.2zM0 128C0 92.7 28.7 64 64 64H448c35.3 0 64 28.7 64 64V384c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V128z");
    textPath.appendChild(animate);
    
    console.log(Object.entries(svgNode));
    
    var input = document.getElementById('the_text');
    input.addEventListener("input", updateValue);
    
    function updateValue(e) {
      textPath.textContent = (e.target.value);
      textPath.appendChild(animate);
    };

    var svg_input = document.getElementById('the_svg');
    svg_input.addEventListener("input",
      function () {
        updateSVG(svg_input.value);
      }
    );

    function updateSVG(_d) {
      svgPath.setAttributeNS(null, 'd', _d);
      let _BB = svgPath.getBBox();
      svgNode.setAttributeNS(null, 'viewBox',
        _BB.x + " " + _BB.y + " " + _BB.width + " " + _BB.height);
      moving_text.style["font-size"] = _BB.width / _BB.height + "em";
      let _dur = svgPath.getTotalLength() / ( 25 * ( _BB.width / _BB.height ) );
      animate.setAttributeNS(null, 'dur', _dur);
    };
  </script>
</body>
</html>