我正在嘗試生成一個svg并且SMIL很難??弄清楚它為什么不起作用。
代碼如下
// targeting the svg itself
const svg = document.querySelector("svg");
// variable for the namespace
const svgns = "http://www.w3.org/2000/svg"
//assigning svg element attribute
svg.setAttribute('class', 'layer1');
svg.setAttribute('xmlns', svgns);
svg.setAttribute('viewBox', '0 0 1280 720');
let bg = document.createElementNS(svgns, 'rect');
bg.setAttribute('class', 'bg');
bg.setAttribute('id', 'bg');
bg.setAttribute("width", '1280');
bg.setAttribute("height", '720');
bg.setAttribute("fill", "#F1C40F");
svg.appendChild(bg);
let pth = document.createElementNS(svgns, 'path');
pth.setAttribute('class', 'pth');
pth.setAttribute('id', 'pth');
pth.setAttribute('d',
'M10,20, L110,20, L110,140, L10,140 ,Z'
);
pth.style.setProperty("stroke-width", ".5");
pth.style.setProperty("stroke", "red");
pth.setAttribute('fill', 'red');
svg.appendChild(pth);
let anim = document.createElementNS(svgns, 'animate');
anim.setAttribute('attributeName', 'd');
anim.setAttribute('attributeType', 'XML');
anim.setAttribute('values', "M10,20, L110,20, L110,140, L10,140 ,Z; M10,20, L210,20, L210,140, L10,140 ,Z; M10,20, L110,20, L110,140, L10,140 ,Z");
anim.setAttribute('keyTimes', '0;0.5;1');
anim.setAttribute('begin', '0s');
anim.setAttribute('end', '1s');
anim.setAttribute('repeatCount', 'indefinite');
svg.appendChild(anim);
pth.appendChild(anim);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<link rel="stylesheet" href="style.css">
</link>
<svg>
<script href="index.js"></script>
<!--
<rect id="bg" width="1280" height="720" fill="#F1C40F"></rect>
<path id="pth" d="M10,20, L110,20, L110,140, L10,140 ,Z" fill="red"
style="stroke-width: 0.5; stroke: red;">
<animate attributeName="d"
attributeType="XML"
values=
"M10,20, L110,20, L110,140, L10,140 ,Z
;M10,20, L210,20, L210,140, L10,140 ,Z
;M10,20, L110,20, L110,140, L10,140 ,Z"
keyTimes="0;.5;1"
begin="0s"
dur="1s"
repeatCount="indefinite"
/>
</path>-->
</svg>
</html>
但是,未注釋的 SVG 可與 SMIL 一起正常作業
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<link rel="stylesheet" href="style.css">
</link>
<svg>
<script href="index.js"></script>
<rect class="bg" id="bg" width="1280" height="720" fill="#F1C40F"></rect>
<path class="pth" id="pth" d="M10,20, L110,20, L110,140, L10,140 ,Z" fill="red"
style="stroke-width: 0.5; stroke: red;">
<animate attributeName="d"
attributeType="XML"
values=
"M10,20, L110,20, L110,140, L10,140 ,Z
;M10,20, L210,20, L210,140, L10,140 ,Z
;M10,20, L110,20, L110,140, L10,140 ,Z"
keyTimes="0;.5;1"
begin="0s"
dur="1s"
repeatCount="indefinite"
/>
</path>
</svg>
</html>
更新
影片效果很好,具有完全相同的d值和 abegin和dur。
// targeting the svg itself
const svg = document.querySelector("svg");
// variable for the namespace
const svgns = "http://www.w3.org/2000/svg"
//assigning svg element attribute
/*
svg.setAttribute('class', 'layer1');
svg.setAttribute('xmlns', svgns);
svg.setAttribute('viewBox', '0 0 1280 720');
*/
let bg = document.createElementNS(svgns, 'rect');
bg.setAttribute('class', 'bg');
bg.setAttribute('id', 'bg');
bg.setAttribute("width", '1280');
bg.setAttribute("height", '720');
bg.setAttribute("fill", "#F1C40F");
svg.appendChild(bg);
let pth = document.createElementNS(svgns, 'path');
pth.setAttribute('class', 'pth');
pth.setAttribute('id', 'pth');
pth.setAttribute('d',
'M10,20, L110,20, L110,140, L10,140 ,Z'
);
pth.style.setProperty("stroke-width", ".5");
pth.style.setProperty("stroke", "red");
pth.setAttribute('fill', 'red');
svg.appendChild(pth);
let anim = document.createElementNS(svgns, 'animate');
anim.setAttribute('attributeName', 'd');
anim.setAttribute('attributeType', 'XML');
anim.setAttribute('values', "M10,20, L110,20, L110,140, L10,140 ,Z; M10,20, L210,20, L210,140, L10,140 ,Z; M10,20, L110,20, L110,140, L10,140 ,Z");
anim.setAttribute('keyTimes', '0;0.5;1');
anim.setAttribute('begin', '2s');
anim.setAttribute('dur', '1s');
anim.setAttribute('repeatCount', 'indefinite');
svg.appendChild(anim);
pth.appendChild(anim);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<svg>
<script href="index.js"></script>-
<!--
<rect id="bg" width="1280" height="720" fill="#F1C40F"></rect>
<path id="pth" d="M10,20, L110,20, L110,140, L10,140 ,Z" fill="red"
style="stroke-width: 0.5; stroke: red;">
<animate attributeName="d"
attributeType="XML"
values="M10,20, L110,20, L110,140, L10,140 ,Z; M10,20, L210,20, L210,140, L10,140 ,Z; M10,20, L110,20, L110,140, L10,140 ,Z"
keyTimes="0;.5;1"
begin="2s"
dur="1s"
repeatCount="indefinite"
/>
</path>-->
</svg>
</html>
uj5u.com熱心網友回復:
d我通過洗掉逗號(不允許在字母之前,謝謝,@ccprog)來清理值,洗掉beginandend屬性并添加dur屬性。就是這樣。
更新
如果您將begin屬性設定為“無限期”,影片將開始,然后您呼叫函式beginElement()on <animate>。這里我加了一個setTimeout()來說明。
// targeting the svg itself
const svg = document.querySelector("svg");
// variable for the namespace
const svgns = "http://www.w3.org/2000/svg"
//assigning svg element attribute
svg.setAttribute('class', 'layer1');
svg.setAttribute('xmlns', svgns);
svg.setAttribute('viewBox', '0 0 1280 720');
let bg = document.createElementNS(svgns, 'rect');
bg.setAttribute('class', 'bg');
bg.setAttribute('id', 'bg');
bg.setAttribute("width", '1280');
bg.setAttribute("height", '720');
bg.setAttribute("fill", "#F1C40F");
svg.appendChild(bg);
let pth = document.createElementNS(svgns, 'path');
pth.setAttribute('class', 'pth');
pth.setAttribute('id', 'pth');
pth.setAttribute('d', 'M 10 20 L 110 20 L 110 140 L 10 140 Z');
pth.style.setProperty("stroke-width", ".5");
pth.style.setProperty("stroke", "red");
pth.setAttribute('fill', 'red');
svg.appendChild(pth);
let anim = document.createElementNS(svgns, 'animate');
anim.setAttribute('attributeName', 'd');
anim.setAttribute('attributeType', 'XML');
anim.setAttribute('values', "M 10 20 L 110 20 L 110 140 L 10 140 Z;M 10 20 L 210 20 L 210 140 L 10 140 Z;M 10 20 L 110 20 L 110 140 L 10 140 Z");
anim.setAttribute('keyTimes', '0;.5;1');
anim.setAttribute('begin', 'indefinite');
anim.setAttribute('dur', '2s');
anim.setAttribute('repeatCount', 'indefinite');
svg.appendChild(anim);
pth.appendChild(anim);
setTimeout(function(){anim.beginElement();}, 2000);
<svg></svg>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453185.html
標籤:javascript 动画 svg svg-动画 微笑
上一篇:為什么修飾符大小沒有被覆寫?
