Archiv der Kategorie ‘hacks‘

 
 

zQuery AS Weirdness

I was on the train the other day with my laptop and I though – hey, it would be pretty easy to write a hacky piece of code that will add method chaining to any AS object – and it would also be interesting if you could get at composed objects like the graphics class via a method call so that you could function chain those as well – obviously inspired by jQuery… but not really much like it… and significantly less useful (warning this is just an experimental hack):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
// slow
var t = getTimer();
 
// untyped to stress js style
var rect = $(new Sprite)
           .add("click", function(e){ e.target.rotation+=10 })
           .appendTo(this)             
           .rotation(45).x(100).y(100)
           .graphics()
           .beginFill(0)
           .drawRect(-50,-50,100,100);
 
var line = $(new Sprite)
           .prependTo(this)
           .attr({x:30, y:30, rotation : 10})
           .graphics()
           .lineStyle(0, 0xFF0000)
           .lineTo(100,100)
trace(getTimer() - t);
 
t = getTimer();
 
// fast
var line2;
with(line2 = addChild(new Sprite)){
	with(graphics) lineStyle(0,0x0000FF),
	lineTo(100,100);
	rotation = 10, x = 200, y = 30;
}
 
var rect2;
with(rect2 = addChild(new Sprite)){
	with(graphics) beginFill(0xFF0000), 
	drawRect(-50,-50,100,100);
	rotation = 45, x = 300, y = 100;
}
trace(getTimer() - t);
 
// zQuery joke function
function $(object:*):Object{ 
  var i:XML, access:String, nm:String;
  var wrapper:Object = {};
  var structure:XML = describeType(object);
  for each(i in structure.method){
	  nm = i.@name;
	  (function(nm):void{
	    wrapper[nm] = function():Object{
		  object[nm].apply(object, arguments);
		  return wrapper;
	    }
	   })(nm);
  }
  for each(i in structure.accessor){
	 access = i.@access;
	 nm = i.@name;
	 if (access == "readonly"){
		 (function(nm):void{
		     var o = $(object[nm]);
		     wrapper[nm] = function():*{
		       return o
			 }
		 })(nm);
	 }else if (access == "readwrite"){
		 (function(nm):void{
			 wrapper[nm] = function(value:*):Object{
				 object[nm] = value;
				 return wrapper;
			 }
		 })(nm);
	 }
  }
  wrapper.add = wrapper.addEventListener;
  wrapper.remove = wrapper.removeEventListener;
  wrapper.appendTo = function(target):Object{
	  target.addChild(object);
	  return wrapper;
  }
  wrapper.prependTo = function(target):Object{
	  target.addChildAt(object, 0);
	  return wrapper;
  }
  wrapper.attr = function(props):Object{
	  for (var i:String in props){
		 object[i] = props[i];
	  }
	  return wrapper;
  }
  return wrapper;
}

A few things worth noting. I did a little speed test against with statements which is pretty funny, the zQuery version takes 9ms and the with statements take less than 1ms. It might not be obvious, but the rect and the line vars are set to reference graphics objects not the sprites – there are a bunch of different ways to handle that…. but I’d never use this anywhere for real so I may not bother fixing it up. The attr() function is actually a useful utility to have hanging around – I’ve mentioned variations on it on actionsnippet many times.

A long time ago I saw this somewhere as3Query I’ve messed with it a bit – sort of tries to bring jQuery style syntax to AS. Worth checking out, not sure I’d use it on a real AS project though…

Great CDATA Trick – (Multiple Line String Literal)

One thing I like about php strings is that you can easily write them on multiple lines:

1
2
3
4
5
6
$theString = "
<!DOCTYPE html>
  <html>
    <head>
      <meta charset='utf-8' /> 
etc....";

It always bugged me that you couldn’t do this in actionscript. Turns out you can – I did some googling the other day in an attempt to find a way – and to my surprise I found this question on stackoverflow note the answer by Nick Sonneveld.

Here’s how it works:

txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();

It’s usually the case that you want to use condenseWhite to remove extra whitespace – however being able to preserve returns and extra spacing may also be desirable.

I used this because I was manually copy and pasting information from an html version of a word doc – clicking a button in an air app I wrote to remove SOME of the html elements and attributes like and class=”MSsomething” and then pasting the final result into some weird timeline based flash templates (that someone else created). This little snippet saved me some serious time. The air app also did a lot of the work with some regular expressions. Converting things like:

<p>.           stuff</p>

to…

<li>stuff</li>

A funny side note: My syntax highlighter did NOT dig this AS syntax, so to get it to show up in a readable way, I wrote some php directly in the post (I have Exec-PHP installed);

1
2
3
4
5
6
7
8
9
10
11
<?php
echo htmlentities("txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();
");
?>

Implicit 2D Div Plot

For those of you who kept up with my old site actionsnippet, you may remember a period where I was plotting lots of implicit 2D and 3D equations. Things like:

Circle…
x^2 + y^2 = 1

The Kusner-Schmitt (Tetrahedral Implicit Surface)
(x^2 + 3) (y^2 + 3) (z^2 + 3) – 32 (x y z + 1) = 0

My personal 2D favorite… the Bicuspid:
(x^2 – a^2)(x – a)^2 + (y^2 – a^2)^2 = 0

With a little tweaking the bicuspid looks like this when plotted:


Click to view original actionsnippet post…

Anyway, I was on the subway the other day with my laptop and I wrote this snippet that plots a donut shape using the implicit equation for a circle. The plot is boring looking, but I was curious to see if I could choke any browsers by drawing the plot with 1000s of divs. No such luck, ie6 in virtual box is the slowest running at around 450-500ms – ei9 in virtualBox was faster than firefox on osx… pretty crazy.

Den ganzen Beitrag lesen…