ada*_*ign 0 animation svg canvas
我有一个图像,我想在我的网页上动画.
目前我正在使用gif.
http://dev.thepenline.com/TEMP/Hol_10_swingingBell.gif
我想对它的动画有更多的控制权,js lib/framework最能帮助我吗?
我应该使用画布还是SVG?哪个(小)库最好用?
我碰巧知道一点raphael.js 但我不知道如何完成这个效果.
我已经为SVG做了一些接近你想要实现的目标:http://live.echo-flow.com/stackoverflow/clock.svg
如果我拿下这个例子,这是相关的代码.
clock.svg:
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 100 100">
<rect x="0" y="0" height="100" width="100" fill="none" stroke="blue" id="border"/>
<script type="text/ecmascript" xlink:href="clock.js"></script>
</svg>
Run Code Online (Sandbox Code Playgroud)
clock.js:
var svgRoot = document.documentElement;
var svgNS = "http://www.w3.org/2000/svg";
var ident = svgRoot.createSVGMatrix();
function Pendulum(thetaZero,period,tempo,fillColor){
//sensible default values
thetaZero = thetaZero || 0;
period = period || 20;
tempo = tempo || 20;
fillColor = fillColor || "red";
if(fillColor == "randomize"){
fillColor = "rgb(" + Math.random() * 254 +
"," + Math.random() * 254 +
"," + Math.random() * 254 + ")";
}
//construct his representation in DOM
var g = document.createElementNS(svgNS,"g");
g.setAttributeNS(null,"transform","translate(50,20) rotate(" + thetaZero + ")");
var rotateTransform = g.transform.baseVal.getItem(1);
var path = document.createElementNS(svgNS,"line");
path.setAttributeNS(null,"x1",0);
path.setAttributeNS(null,"y1",0);
path.setAttributeNS(null,"x2",0);
path.setAttributeNS(null,"y2",40);
path.setAttributeNS(null,"stroke","black");
var c = document.createElementNS(svgNS,"circle");
var styleString = "fill:" +fillColor;
//console.log(styleString);
c.setAttributeNS(null,"cx",0);
c.setAttributeNS(null,"cy",50);
c.setAttributeNS(null,"r",10);
c.setAttributeNS(null,"style",styleString);
c.setAttributeNS(null,"stroke","black");
c.setAttributeNS(null,"opacity",.5);
g.appendChild(path);
g.appendChild(c);
svgRoot.appendChild(g);
this.node=g;
//timeout
var timeStep = 10; //in milliseconds
var timeCount = 0;
var _animateTimeStep = function(){
timeCount++;
var adjustedTimeCount = timeCount/tempo;
//compute theta
//theta(t) = period * sin(t + thetaZero)
var theta = period * Math.sin(adjustedTimeCount + thetaZero);
//set his current rotate transformation to the new theta
rotateTransform.setMatrix(ident.rotate(theta));
}
var timerId = null;
//method definitions
this.go=function(){
timerId = window.setInterval(_animateTimeStep,timeStep);
};
this.stop=function(){
window.clearInterval(timerId);
};
}
//Pendulum(thetaZero,period,tempo,fillColor)
var p1 = new Pendulum(-10,20,40,"red");
p1.go();
var p2 = new Pendulum(20,20,40,"yellow");
p2.go();
var p3 = new Pendulum(-20,20,40,"blue");
p3.go();
var p4 = new Pendulum(10,20,40,"purple");
p4.go();
Run Code Online (Sandbox Code Playgroud)