SVG 0.5

use 要素で星空を倍にして、星空とロケットを動かすアニメーションを作りました。 アニメーションを再度見るには、ブラウザの再読み込みを行ってください。

実行結果

生成されたコード


ソース


svg05.html

SVG.js ライブラリと JavaScript のソースファイルを指定します。
<script type="text/javascript" src="js/svg.min.js"></script>
<script type="text/javascript" src="js/svg05.js"></script>
実行結果のところには "svg" という id の div 要素と "code" という id の pre 要素を用意しました。中身は svg05.js の中で作って入れます。
<h2>実行結果</h2>
<div id="svg">
</div>
<h3>生成されたコード</h3>
<pre id="code" class="brush: xml; class-name: 'code'">
</pre>

svg05.js

ロケットの大きさから配置する位置を計算してから、ロケットに炎を追加しました。 星空の背景は星空とは別に rect 要素として追加し、アニメーションで色を変えました。 星空は use 要素で2枚配置し、アニメーションでロケットと反対方向に移動させました。 ロケットは use 要素で配置し、アニメーションで上昇・下降させました。

window.onload = function() {
    // Generate svg element
    //<svg width="598" height="428" style="border: solid 1px lightgray">
    const width = 598;
    const height = 428;
    const max = 200;
    const size = 2;
    const xmax = width - size - 1;
    const ymax = height - size - 1;
    var draw = SVG().addTo('#svg').size(width, height).css('border', 'solid 1px lightgray');
    //<defs>
    //<g id="stars">
    var stars = draw.defs().group({id: 'stars'});
    //  <ellipse cx="?" cy="?" rx="1" ry="1" 
    //    fill="#ffffff" stroke="none"/>
    for (i = 0; i < max; i++) {
        var x = Math.floor(Math.random() * xmax + size / 2);
        var y = Math.floor(Math.random() * ymax + size / 2);
        stars.ellipse(size, size).cx(x).cy(y).fill('#ffffff').stroke('none');
    }
    //</g>
    //<g id="rocket">
    var rocket = draw.defs().group({id: 'rocket'});

    // Generate polygon, rect, ellipse elements
    //  <polygon points="92,0 22,96 162,96" 
    //    style="fill:#752424;stroke:#000000;stroke-width:2"/>
    rocket.polygon('92,0 22,96 162,96').fill('#752424').stroke({color: '#000000', width: 2})
    //  <polygon points="21,194 0,273 42,273" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.polygon('21,194 0,273 42,273').fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <polygon points="162,193 142,273 183,273" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.polygon('162,193 142,273 183,273').fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <polygon points="93,189 52,259 134,259" 
    //    style="fill:#1f1f1f;stroke:#000000;stroke-width:2"/>
    rocket.polygon('93,189 52,259 134,259').fill('#1f1f1f').stroke({color: '#000000', width: 2})
    //  <rect x="22" y="97" width="141" height="142" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.rect(141, 142).move(22, 97).fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <polygon points="93,192 73,273 114,273" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.polygon('93,192 73,273 114,273').fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <ellipse cx="93" cy="145" rx="41" ry="43" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.ellipse().attr({cx: 93, cy: 145, rx: 41, ry: 43}).fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <ellipse cx="93" cy="145" rx="33" ry="34" 
    //    style="fill:#217dbb;stroke:#000000;stroke-width:2"/>
    rocket.ellipse().attr({cx: 93, cy: 145, rx: 33, ry: 34}).fill('#217dbb').stroke({color: '#000000', width: 2})
    //  <ellipse cx="37" cy="220" rx="7" ry="8" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.ellipse().attr({cx: 37, cy: 220, rx: 7, ry: 8}).fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <ellipse cx="64" cy="221" rx="7" ry="7" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.ellipse().attr({cx: 64, cy: 221, rx: 7, ry: 7}).fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <ellipse cx="122" cy="221" rx="6" ry="7" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.ellipse().attr({cx: 122, cy: 221, rx: 6, ry: 7}).fill('#6e6e6e').stroke({color: '#000000', width: 2})
    //  <ellipse cx="146" cy="220" rx="5" ry="6" 
    //    style="fill:#6e6e6e;stroke:#000000;stroke-width:2"/>
    rocket.ellipse().attr({cx: 146, cy: 220, rx: 5, ry: 6}).fill('#6e6e6e').stroke({color: '#000000', width: 2})
    var x = Math.floor((width - rocket.width()) / 2);
    var y = height - rocket.height();
    //<ellipse cx="93" cy="351" rx="28" ry="73" 
    //    style="fill:#ff0f0f;stroke-width:0"/>
    rocket.ellipse().attr({cx: 93, cy: 351, rx: 28, ry: 73}).fill('#ff0f0f').stroke('none')
    //<ellipse cx="94" cy="337" rx="18" ry="51" 
    //    style="fill:#ff930f;stroke-width:0"/>
    rocket.ellipse().attr({cx: 94, cy: 337, rx: 18, ry: 51}).fill('#ff930f').stroke('none')
    //<ellipse cx="94" cy="316" rx="9" ry="25" 
    //    style="fill:#ffef0f;stroke-width:0"/>
    rocket.ellipse().attr({cx: 94, cy: 316, rx: 9, ry: 25}).fill('#ffef0f').stroke('none')

    //</g>
    //</defs>
    //<rect x="0" y="0" width="598" height="428" 
    //    fill="#000000" stroke="none"/>
    var sky = draw.rect(width, height).fill('#ffffff').stroke('none')
    sky.animate({duration: 2000}).fill('#000000');
    sky.animate({delay: 1000, duration: 2000}).fill('#ffffff');
    //<use xlink:href="#stars" x="0" y="0"/>
    var use1 = draw.use(stars).move(0, 0);
    use1.animate({duration: 2000}).move(0, height);
    use1.animate({delay: 1000, duration: 2000}).move(0, 0);
    var use2 = draw.use(stars).move(0, -height);
    use2.animate({duration: 2000}).move(0, 0);
    use2.animate({delay: 1000, duration: 2000}).move(0, -height);
    //<use xlink:href="#rocket" x="?" y="?"/>
    var use3 = draw.use(rocket).move(x, y);
    use3.animate({duration: 2000}).move(x, y / 2);
    use3.animate({delay: 1000, duration: 2000}).move(x, y);
    //</svg>
    var svgNode = document.getElementById('svg').children[0];
    var codeNode = document.getElementById('code')
    // Get generated SVG code
    var svgText = new XMLSerializer().serializeToString(svgNode);
    // Show the code with additional new lines
    codeNode.textContent = svgText.replace(/></g, '>\n<');
}

参考文献

このプログラムは以下の情報またはソフトウェアを参照しています。