MyScript.js
function update(){
myCircle.transition().duration(1000)
.style("fill",function(){
if(toggle)
return "red";
else
return "blue";
});
toggle = 1 - toggle;
}
// an update loop...
Beautiful.css
circle.m { fill: red; }
circle.f { fill: blue; }
MyScript.js
function update(){
myCircle.transition().duration(1000)
.attr("class",function(){
if(toggle)
return "m";
else
return "f";
});
toggle = 1 - toggle;
}
// an update loop...
object.transition().duration(1000).attr("class","B") // which is actually only a class switch
object.transition.duration(1000)
.attr("attribute_0",valueB_0)
...
.attr("attribute_n",valueB_n); // long to write and hard to maintain
It seems like you are trying to transition something that isn't really a transition. What it looks like you want is for something to change discretely after some amount of time. For that you can use the .end(function(d,i) { ... }) of the transition to change your class at the end of the transition.The code you have now will continuously set the fill color to the same color until the very end. transitions don't make sense on class names (you don't want to automatically transition between two class names, you just want to switch). It comes down to the difference between continuous and discrete values, you interpolate between continuous values and you toggle between discrete ones.
Maybe I misunderstand your purpose, but does this make sense?
d3.transition.prototype.classTransition = function (className) {
// find the css rules on the className and nodename + className
// adapted from http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
var getCSSRules = function(ruleName,p) {
var cssStyleRules = new Array();
var elems = ruleName.split(".");
if( elems && elems[0].length > 0 ){
var rules = getCSSRules("."+elems[1],p+1);
for(var i in rules){
cssStyleRules.push(rules[i]);
}
}
if(document.styleSheets){
var found = false;
for(var styleSheetNum = 0; styleSheetNum < document.styleSheets.length && !found ; styleSheetNum++){
var styleSheet = document.styleSheets[styleSheetNum];
var ruleNum = 0;
var cssRule=false;
do {
if (styleSheet.cssRules) {
cssRule = styleSheet.cssRules[ruleNum];
} else {
cssRule = styleSheet.rules[ruleNum];
}
if (cssRule){
if (cssRule.selectorText.toLowerCase()==ruleName) {
cssStyleRules.push(cssRule);
found = true;
}
}
ruleNum++;
} while (cssRule && !found)
}
}
return cssStyleRules;
}
// retreive the set of style attributes
var rulesToAttributeArray = function rulesToAttributeArray(cssStyleRules) {
var attributes = {};
for(var cssStyleRule in cssStyleRules){
var attribute = true;
for(var i = 0;attribute;i++){
attribute = cssStyleRules[cssStyleRule].style[i];
if(attribute && !attributes.attribute){
attributes[attribute] = cssStyleRules[cssStyleRule].style[attribute];
}
}
}
return attributes
};
// if a function is given -- more tests should be required here
if(typeof className === 'function'){
className = className.call(this);
}
// apply on each element of the transition group
this.each(function(){
var localClassName = this.nodeName+"."+className;
var rules = rulesToAttributeArray(getCSSRules(localClassName,0));
// get the attributes and apply them
for(var attribute in rules){
d3.select(this).transition().style(attribute,rules[attribute]);
}
});
return this;
}
// a continuous transition on a class switch
circle.transition()
.duration(5000)
.classTransition(function(){
if(draw){