pushStyle() and popStyle()

38 views
Skip to first unread message

apeace

unread,
Apr 27, 2009, 11:26:04 PM4/27/09
to Processing.js
Apologies if this is a repeat...didn't find anything on this.

Has there been any thought about implementing the pushStyle() and
popStyle() functions? I noticed that push/popMatrix() already exist,
but they only save/restore a few key values. The rest are in the
'private' state vars in buildProcessing().

Would it be out of the question to store style values on the p object,
like is done with some of the keypress/click values? I realize that
it's not as desirable for these values to be visible, like the key/
click ones, but it'd make it easy to implement push/popStyle().

For fun, I decided to try implementing the two functions using only
one new private state variable (which I called styleStack). The
results were disturbing:

p.pushStyle = function pushStyle() {
p.pushMatrix();
var styleVars = ['doFill', 'doStroke', 'curTint', 'curRectMode',
'curEllipseMode', 'curColorMode', 'redRange', 'blueRange',
'greenRange',
'opacityRange', 'curTextFont', 'curTextSize'];
var current = {};
for(i in styleVars){
var q1 = q2 = '';
eval('var val = '+styleVars[i]);
if(typeof val == 'string'){
q1="'\\'";
q2="\\''";
}
eval('current[styleVars[i]]='+q1+styleVars[i]+q2);
}
styleStack.push(current);
};

p.popStyle = function popStyle() {
p.popMatrix();
var old = styleStack.pop();
for(i in old)
eval(i+'='+eval('eval(old[i])'));
};


Yipe! Keep out of the reach of children...

Are my programming skills simply lacking, or is there no better way to
do it without making the style variables visible?

Peace!

apeace

unread,
May 1, 2009, 8:21:17 PM5/1/09
to Processing.js
Um...just looked at that code again. Was I drinking?


p.pushStyle = function PushStyle() {
p.pushMatrix();
var current = {'doFill':doFill, 'doStroke':doStroke,
'curTint':curTint,
'curRectMode':curRectMode, 'curColorMode':curColorMode,
'redRange':redRange, 'blueRange':blueRange,
'greenRange':greenRange,
'opacityRange':opacityRange, 'curTextFont':curTextFont,
'curTextSize':curTextSize};
styleStack.push(current);
};


p.popStyle = function popStyle() {
p.popMatrix();
var old = styleStack.pop();
doFill = old.doFill;
doStroke = old.doStroke;
curTint = old.curTint;
curRectMode = old.curRectmode;
curColorMode = old.curColorMode;
redRange = old.redRange;
blueRange = old.blueRange;
greenRange = old.greenRange;
opacityRange = old.opacityRange;
curTextFont = old.curTextFont;
curTextSize = old.curTextSize;
};


Still, there must be a cleaner way. Clearly my JS-ing is failing as I
get more senile...

Any other solutions?

Al MacDonald

unread,
May 1, 2009, 10:56:28 PM5/1/09
to proces...@googlegroups.com
lol, cool

hey does your method work for nested styles?
ie:

pushStyle();
    style1=?
    pushStyle();
        style2=?
       pushStyle();
            style3=?
       popStyle();
       ?=style2
    popStyle();
    ?=style1
popStyle();


apeace wrote:
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Processing.js" group.
To post to this group, send email to proces...@googlegroups.com
To unsubscribe from this group, send email to processingjs...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/processingjs?hl=en
-~----------~----~----~----~------~----~------~--~---

  

No virus found in this incoming message. Checked by AVG - www.avg.com Version: 8.0.238 / Virus Database: 270.12.12/2090 - Release Date: 05/01/09 06:17:00


apeace

unread,
May 2, 2009, 10:59:04 PM5/2/09
to Processing.js
Yep. Check source here:
http://www.andrewpeace.com/projects/pushpopstyle/test.html

Just don't forget to add var styleStack = new Array(); to the private
variables.

Still...I feel like one of the more skilled Processing.js developers
could write it better.

Also, I haven't tested this, but it just occured to me that there
might be errors if you tried to popStyle() without ever doing a
pushStyle(). Not sure what the Processing spec says about that...

Peace!
> > ------------------------------------------------------------------------
>
> > No virus found in this incoming message.
> > Checked by AVG -www.avg.com
> > Version: 8.0.238 / Virus Database: 270.12.12/2090 - Release Date: 05/01/09 06:17:00
>
> --
> *Alistair MacDonald <http://twitter.com/F1LT3R>*
> *Hyper-Metrix <http://hyper-metrix.com>*
> 617.584.1420
> hyper-metrix.com <http://hyper-metrix.com> <http://hyper-metrix.com>

apeace

unread,
May 2, 2009, 11:44:24 PM5/2/09
to Processing.js
Just checked, it did in fact break. Fixed it like so:

p.popStyle = function popStyle() {
p.popMatrix();
var old = styleStack.pop();
if(old) {
doFill = old.doFill;
doStroke = old.doStroke;
curTint = old.curTint;
curRectMode = old.curRectmode;
curColorMode = old.curColorMode;
redRange = old.redRange;
blueRange = old.blueRange;
greenRange = old.greenRange;
opacityRange = old.opacityRange;
curTextFont = old.curTextFont;
curTextSize = old.curTextSize;
}
};

http://processing.org/reference/popStyle_ This page doesn't say what
should happen when popStyle() is called and no styles have been
"pushed", but I think it makes most sense for it to simply do nothing.
I updated the demo at the above URL to call popStyle() a few extra
times, just to show there are no errors.

Anybody lemmie know if you've got a better push/popStyle(). There must
be one...
Reply all
Reply to author
Forward
0 new messages