我正在实施一个简单的游戏,玩家将在屏幕中间控制一个红色圆圈。圆圈实现为SVG元素。
我希望能够使用SVG动画(SMIL)在SVG viewBox中的任意两个位置之间移动圆圈。每个动画的触发器都是在屏幕上的任意位置左键单击。
我written code,我认为,它应该适用于Firefox和Chrome。在Chrome中,它仅适用于第一个动画,而对于所有后续动画,圆圈只是“传送”。在Firefox中没有动画(控制台中没有错误)。
我的代码中是否存在错误,或者是否存在SMIL过于不成熟的一些已知问题?
我可以让SMIL在这个用例中工作,还是应该使用canvas?
这是我到目前为止的代码:
<!DOCTYPE html>
<html>
<body>
<svg id="canvas" width="800" height="800" onclick="Move()">
<circle id="player1" cx="300" cy="300" r="40" stroke="blue" stroke-width="4" fill="red">
</circle>
</svg>
<script>
function createAnimation(attribute, playerID, duration, from, to) {
var animation = document.createElementNS("http://www.w3.org/2000/svg", "animate")
animation.setAttribute("attributeType", "XML")
animation.setAttribute("attributeName", attribute)
animation.setAttribute("dur", duration)
animation.setAttribute("to", to)
animation.setAttribute("from", from)
animation.setAttribute("fill", "freeze")
animationID = playerID + "animation" + attribute
animation.setAttribute("id", animationID)
player = document.getElementById(playerID)
previous_animation = document.getElementById(animationID)
if (previous_animation != null) {
player.removeChild(previous_animation)
}
player.appendChild(animation)
}
function Move() {
console.log(event.clientX);
console.log(event.clientY);
createAnimation("cx", "player1", "2s", document.getElementById("player1").getAttribute("cx"), event.clientX);
createAnimation("cy", "player1", "2s", document.getElementById("player1").getAttribute("cy"), event.clientY);
}
</script>
</body>
</html>
答案 0 :(得分:3)
有很多问题......
<!DOCTYPE html>
<html>
<body>
<svg id="canvas" width="800" height="800" onclick="Move(evt)">
<circle id="player1" cx="300" cy="300" r="40" stroke="blue" stroke-width="4" fill="red">
</circle>
</svg>
<script>
function createAnimation(attribute, playerID, duration, from, to) {
var animation = document.createElementNS("http://www.w3.org/2000/svg", "animate")
animation.setAttribute("attributeType", "XML")
animation.setAttribute("attributeName", attribute)
animation.setAttribute("dur", duration)
animation.setAttribute("to", to)
animation.setAttribute("from", from)
animation.setAttribute("fill", "freeze")
animationID = playerID + "animation" + attribute
animation.setAttribute("id", animationID)
player = document.getElementById(playerID)
previous_animation = document.getElementById(animationID)
if (previous_animation != null) {
player.removeChild(previous_animation)
}
player.appendChild(animation)
}
function Move(evt) {
console.log(evt.clientX);
console.log(evt.clientY);
createAnimation("cx", "player1", "2s", document.getElementById("player1").cx.animVal.value, evt.clientX);
createAnimation("cy", "player1", "2s", document.getElementById("player1").cy.animVal.value, evt.clientY);
document.getElementById("canvas").setCurrentTime(0);
}
</script>
</body>
</html>