r/programming • • Dec 20 '10

A Christmas Tree in 1k of Javascript

http://www.romancortes.com/blog/how-i-did-the-1kb-christmas-tree/
1.1k Upvotes

221 comments sorted by

74

u/skelooth Dec 20 '10

Hats off, that is brilliant. Not only is the programming impressive, but the tree looks pretty damn good as well!

4

u/dunker Dec 21 '10

I have to agree, the programming is top notch (coming from a fellow web dev), and the tree looks all soft and fuzzy and Christmasy, I love it!

→ More replies (7)

119

u/jhaluska Dec 20 '10

Now this is what I come to /r/programming for!

23

u/different2une Dec 20 '10

I once read (and executed) a tiny piece of obfuscated C code that when executed printed out "Twelve days of Christmas" ... I was awed!

edit: found it!

5

u/RedHaze Dec 21 '10

Great read! However, even though I read through the entire article I still couldn't fully grasp how this was done. I'm still throughly impressed though.

2

u/dmhouse Dec 21 '10

Is http://research.microsoft.com/en-us/um/people/tball/papers/XmasGift/final.html not clear enough? Which part don't you understand?

1

u/RedHaze Dec 21 '10

Now I feel stupid. I never saw that link there when I first read it through. Thanks.

42

u/ImTheGuyWithTheGun Dec 20 '10 edited Dec 20 '10

Amazing how much smoother this runs in Chrome compared to Firefox. And IE8 - forget about it.

EDIT: Safari 5 seems even worse than Firefox. My system is a core-i5 w/ 6GB on Win7 - should be plenty of horsepower.

21

u/rickmatt Dec 20 '10

I loaded it in Firefox 3.6.13 (on a 64 bit quad core desktop with 6G RAM running Windows 7 Ultimate) and my CPU usage went from 1% to 25%. Closed it and usage dropped to 1% again. Loaded it in Chrome and there was no appreciable increase in usage. WTF?

3

u/whoisrich Dec 20 '10

For anyone wanting to try FireFox 4 there is a portable edition here:

http://portableapps.com/apps/internet/firefox_portable/test

The Reddit comments load so much faster compared to Firefox 3.

8

u/[deleted] Dec 20 '10

I don't know what could cause that but i'm suspecting that hardware acceleration has a lot to do with it. It seems that javascript is one of the things that is fully output with software only. If there was hardware acceleration then it would be blazing fast.

It's like trying to render out a 3d game with software (ie. CPU only). It doesn't matter what computer you use, it's going to get crushed trying to spit out those polygons. But on hardware acceleration it's all offloaded onto the GPU which is specially designed to handle just such things, which is how we enjoy things like that these days.

15

u/ascendant512 Dec 21 '10

I was forced to register because of this thread and some comments in it. The Chrome fanboys here are quite ridiculous.

Chrome does not have hardware-accelerated javascript. Hardware-accelerated javascript does not exist. You are mistaking it with WebGL which is present in Safari, Firefox (3 and 4) and Chrome. WebGL has nothing to do with javascript, it is a hardware accelerated <canvas> element (HTML5). Chrome has barely any more hardware acceleration than Microsoft Powerpoint.

Furthermore, the Christmas tree is not rendered with polygons, it is rendered with sprites. It says so right on the front page.

5

u/[deleted] Dec 21 '10

hint: a sprite is a textured polygon. you can very much take advantage of hardware acceleration using just sprites.

Although i doubt this tree example uses any hardware acceleration.

-2

u/ascendant512 Dec 21 '10

Yeah, thanks for the comment. I know that you can hardware-accelerate sprites by turning them into a textured polygon. Hardware accelerated sprites seem to be both common and rare. The only example I can think of is the mouse pointer.

It's just that usually when you see a game or something that uses sprites, it's not 3D accelerated. Even more so the older it is.

Perhaps someone can write a WebGL canvas version of this toy. It'll probably be faster >_>

2

u/ZorbaTHut Dec 21 '10

Actually, the vast majority of modern 2d games are hardware-accelerated. Software rendering is long since dead. I'd say you've got a 95%+ chance of any 2d game in the last five years being hardware-accelerated.

That's probably an underestimation.

0

u/ascendant512 Dec 21 '10

The vast majority of what you think are modern 2D games are not 2D. They are rendered in a 3D environment with a fixed camera perspective using 3D textured polygons (again, not sprites); not because it's easier to do it that way but because it's much, much faster. The rest of them use non-hardware accelerated java or non-hardware accelerated flash. Which use sprites. Which use the CPU.

You are wrong, go away.

5

u/phort99 Dec 21 '10

So, because hardware-accelerated 2D games use some GPU tricks that involve them being rendered in the same manner as 3D graphics, that makes them suddenly 3D? All it is is a different architecture for doing exactly the same thing. Because a shader and a textured quad get involved, suddenly it's 3D?

WebGL is not the only hardware acceleration tech making its way into browsers, canvas is also getting hardware acceleration. IE9 and chrome both use hardware accelerated canvas, from what I understand.

What exactly is the point you're trying to make? You started out arguing that hardware accelerated javascript does not exist. I don't see anybody trying to make the argument that it does.

It seems that javascript is one of the things that is fully output with software only. If there was hardware acceleration then it would be blazing fast.

This comment was probably not talking about hardware-accelerating javascript, it was talking about the output that javascript controls: when the script says "draw this image here," does the CPU set each pixel or does the GPU?

1

u/ascendant512 Dec 21 '10

Oh gosh, you're right

I misread the original comment by tgc1. But then ZorbaTHut came in with implications that more than just <canvas>, <video> and CSS is accelerated. I really should just stop replying to him.

There are also other commenters that chrome's hardware acceleration is responsible for differences in drawing speed for the Christmas tree and I was mixing up posts. That's really what made me lose it in the first place.

→ More replies (0)

5

u/ZorbaTHut Dec 21 '10

So what exactly is the difference between a 2D game and a game "rendered in a 3D environment with a fixed camera perspective using 3D textured polygons"?

If it looks like a duck and quacks like a duck, it's a duck. I'd call that a 2D game.

-3

u/ascendant512 Dec 21 '10

At this point, I suspect you're just being deliberately dense, but to help other redditors who might be confused:

It's a 3D game if it uses 3D rendering APIs such as Direct3D (a part of DirectX) or OpenGL and the accelerated 3D rendering pathways in the GPU for its rendering engine.

Real 2D games usually are stuff like solitaire and Linux games that use GTK and QT.

→ More replies (0)

1

u/altrego99 Dec 21 '10

The vast majority of what you think are modern 2D games are not 2D

I like Duke Nukem Manhattan project, which falls in this category :) Awesome game!

3

u/ZorbaTHut Dec 21 '10

Hardware Acceleration is Coming to Chrome

You're somewhat out of date. They're trying to get hardware acceleration working for things as simple as webpage layouts. No, the javascript isn't accelerated, but the actual compositing of the sprites certainly would be. And that tree uses a shitload of sprites with alpha channels.

0

u/ascendant512 Dec 21 '10

Take a closer look at the link you posted. It still has nothing to do with javascript, and everything to do with what I said it did. The examples at the bottom of this description and the description itself say that the acceleration's focus is 1. HTML5 and 2. CSS3.

This is in addition to the original hardware acceleration which copies parts of a page already rendered by the CPU directly into the GPU framebuffer. The CPU still does the javascript, and the javascript still does the semitransparent sprites.

4

u/ZorbaTHut Dec 21 '10

Read your own comment again.

Chrome does not have hardware-accelerated javascript. Hardware-accelerated javascript does not exist. You are mistaking it with WebGL which is present in Safari, Firefox (3 and 4) and Chrome. WebGL has nothing to do with javascript, it is a hardware accelerated <canvas> element (HTML5). Chrome has barely any more hardware acceleration than Microsoft Powerpoint.

You're right that there's no hardware-accelerated javascript. Everything else in that paragraph is wrong. Chrome's hardware acceleration isn't WebGL, this acceleration isn't based on <canvas>, and Chrome is working on having significantly more hardware acceleration than Microsoft Powerpoint.

Hardware acceleration doesn't imply polygons. You can do it with sprites also. That's what the Chrome team is working on.

And just to stave off the inevitable, while it might not be accelerating Javascript itself, it certainly can accelerate the sprites that are placed by Javascript.

-2

u/ascendant512 Dec 21 '10 edited Dec 21 '10

If you don't bother reading Google's own explanation of how Chrome's hardware acceleration works, then what is the point in reasoning with you? It clearly states:

  • Layer has 3D or perspective transform CSS properties

  • Layer is used by <video> element using accelerated video decoding

  • Layer is used by a <canvas> element with a 3D context

  • Layer uses a CSS animation for its opacity or uses an animated webkit transform

And

Even with hardware acceleration enabled, pages that don't contain a <video> or WebGL elements and don't make use of 3D CSS transformations/animation use the software path.

In the example of this article (regular html and javascript) the new hardware-accelerated features in Chrome would never even be invoked.

And finally, Microsoft PowerPoint (an example I used to illustrate a common and not hotly debated piece of software) features GPU acceleration for animations, transitions, and video decoding. I can't even think of something that Chrome does on top of that either in current builds or future plans.

In short, you are a rabid fanboy.

EDIT: formatting

→ More replies (3)

1

u/[deleted] Dec 21 '10

Just to clear up, I didn't mean that the browser renders polys, just an example of cpu vs. gpu usage. And I was only wondering if it were the case. I'm not even using chrome, fyi so i'm by no means a fan boy. Matter of fact I have yet to install and try it.

But anyway, thank you for the further info. I know about open GL but not WebGL. I'm going to check that out.

3

u/ascendant512 Dec 21 '10

I'm sorry for calling you a fanboy.

It is probably impossible to hardware accelerate JS, at least not without severely cutting its usefulness. However, it's not really that bad. All the fast javascript engines use JIT (which attempts to make it run as fast as natively compiled code if you don't know), and JS by its nature as a general purpose scripting language is more suited to a general purpose processor anyway.

1

u/techdawg667 Dec 21 '10

then the speed advantage of chrome over firefox is ever more awesome now!

0

u/ascendant512 Dec 21 '10

do you mean the speed advantage of firefox over chrome? :D

1

u/techdawg667 Dec 21 '10

that graph is almost 2 months old o.o

1

u/funnynickname Dec 21 '10

I've got a single core 2.8ghz with 3 gigs and firefox just pinned at 100% then crashed. Chrome renders it flawlessly and fast. I don't believe it is hardware acceleration. Chrome is just seriously optimized for this stuff, etc. Translates the code in to machine code very efficiently.

2

u/mdot Dec 21 '10

So there is some way that Chrome can invoke hardware acceleration that the others cannot?

Not trying to smart ass, because I honestly want to know the answer. But hardware acceleration doesn't explain different CPU usage, using different browsers, on the same machine.

3

u/[deleted] Dec 21 '10

Yes, they each need to implement hardware acceleration, its not just something that comes for free.

1

u/mdot Dec 21 '10

Ok, I understand that you can't get something for nothing.

However, is it then, your contention, that Chrome is the only browser that explicitly makes use of hardware acceleration? Seems pretty far-fetched to me.

1

u/[deleted] Dec 21 '10

I didn't say chrome is the only one doing it. They're all doing it, but it's a recent development so everyone has different bits done, and they each have a ways to go.

Also, "hardware acceleration" is a pretty blanket term for a lot of things. It could mean webGL, it could mean compositing using GL, it could mean rendering the page using tiles, etc.

1

u/mdot Dec 21 '10

Thanks for the info...TIL.

1

u/[deleted] Dec 21 '10

Well I can't confirm, it's just a hunch of mine. Not much else can explain the difference in performance, other than maybe a bigger memory, buffer or cache allocation for javascript. Not really sure what else could account for the performance difference.

I'd like to know too btw if anyone else can chime in.

1

u/ImTheGuyWithTheGun Dec 21 '10

I might just be ignorant, but I attribute it simply to Chrome's V8 javascript engine, which tends to be faster than other browser's engines. From the wiki:

V8 increases performance by compiling JavaScript to native machine code before executing it, rather than to a bytecode or interpreting it. Further performance increases were achieved by employing optimization techniques such as inline caching. With these features, JavaScript applications running within V8 have an effective speed comparable to a compiled binary

1

u/[deleted] Dec 21 '10

Ah! there it is. The proper info. Thank you for posting! That makes a hell of a lot more sense.

1

u/TouchedByAnAnvil Dec 21 '10

i'm suspecting that hardware acceleration has a lot to do with it.

I'm suspecting you don't know what you're talking about.

Hardware accelaration only works for large amounts of very similar calculations (eg rendering triangles)

Javascript is interpreted code, it needs to run on a general purpose CPU. There is no hardware accelarated javascript. There are only faster software implementations.

2

u/wefarrell Dec 21 '10

For js, chrome is several orders of magnitude faster than ff.

The ff beta is just as fast as chrome though.

4

u/abadidea Dec 21 '10

iirc the Mozilla Javascript team has said they're on track for catching up to Chrome. Chrome's JS has done to FF what Firefox did to the IE interface team several years ago: embarrass them into trying harder.

1

u/Psythik Dec 21 '10

Runs great in Firefox 4b8 with my GPU in 3D mode.

15

u/Seandroid Dec 20 '10

This worked PERFECTLY on Safari 5 on my Mac.... it was smooth as butter.

9

u/causticmango Dec 21 '10

Must be specific to Windows. On OS X, Safari is noticeably smoother (for this demo, at least).

2

u/[deleted] Dec 21 '10

Not surprising, Safari probably uses HW acceleration for canvas on OS X, but not on Windows.

7

u/MironV Dec 20 '10

It actually runs fastest for me in IE9.

-2

u/imadork32 Dec 20 '10

Please be careful what you post on the web. Some helpless soul might read this, upgrade, and then start using IE* all over again. The only reason anyone would write this is if they were trol---oh wait, I see what you did there!

2

u/jhaks Dec 21 '10

It was surprising to me but apparently the latest release/platform preview of IE 9 is faster than Chrome on the SunSpider bench.

1

u/16807 Dec 21 '10

In all seriousness, I tried this in IE9 expecting its hardware acceleration and inseparable ties to Windows to be worth a damn. By far, it runs the slowest among browsers I've tried.

2

u/[deleted] Dec 21 '10

Ran slow in my IE9 (hw accel off) and fast in my Opera11.

7

u/krische Dec 20 '10

It was about the same speed wise for me in Firefox 4 beta 7 and Opera 11 as it was in Chrome.

6

u/ImTheGuyWithTheGun Dec 20 '10

I'm using FF 3.6.9. I know FF 4 supposedly has a much faster JS-engine -- good to hear that it's true. But in 3.6, it runs at a fraction of the framerate compared to chrome.

1

u/pytechd Dec 20 '10

FF 4 also has hardware acceleration that helps a ton.

2

u/ascendant512 Dec 21 '10

Hardware acceleration does not affect javascript. It affects HTML5 and CSS.

5

u/longmover79 Dec 20 '10

Smoothest/Fastest award goes to Safari followed by Chrome then FF a significant last (on mine at least)

3

u/ImTheGuyWithTheGun Dec 20 '10

Safari is a slide-show for me (dual core on Win7). Firefox is better, but still jittery. Chrome is silky smooth.

6

u/cruzin Dec 20 '10

Safari and Chrome look the same for me: smooth, no jitters. I'm using OS X, not Win7 though.

5

u/[deleted] Dec 20 '10

Seconded, Safari and Chrome are pretty much neck-to-neck, with Safari being slightly faster (in my highly unscientific side-by-side comparison (OSX, i7)).

1

u/[deleted] Dec 21 '10

Safari for Windows is absolutely not on par with the Mac version in many respects. Sounds like the JS engine is one of them.

1

u/[deleted] Dec 21 '10

Firefox 4 is much better in regard to JS speed, there's no question. Sites like Yahoo News made FF 3.6 freak out a bit (specifically the Linux) while it was loading, and the same site is very smooth in FF 4.

2

u/IllBeBack Dec 20 '10

In IE8 all I get is a javascript error.

4

u/imadork32 Dec 20 '10

What did you expect?

2

u/IllBeBack Dec 20 '10

I thought maybe it would work, just excruciatingly slowly.

4

u/imadork32 Dec 21 '10

I was kinda expecting that myself, but trust me I had anything short of a shockface when I saw that error.

1

u/[deleted] Dec 21 '10

IE 8 entirely lacks canvas support, doesn't it?

2

u/[deleted] Dec 21 '10

Interesting. Safari 5 under OS X 10.6 is noticeably smoother than either Firefox or Chrome. And I have a pretty slow machine (2.16 GHz Core 1 Duo, Radeon X1600).

2

u/boran_blok Dec 21 '10

It runs fine in opera, but one core is pegged at 100% tho.

1

u/FractalP Dec 20 '10

Latest Firefox beta on a MacBook runs rather less smooth than it does on Opera 11 or Chrome. In fact, I'd say Opera runs slightly better than Chrome, although it isn't quite up to date.

1

u/rmart Dec 21 '10

That's weird, it works just fine in Safari on my Mac. Then again, I've got an i7 CPU so maybe it's just a case of Safari running it well when there's enough horsepower.

7

u/[deleted] Dec 20 '10 edited Oct 02 '26

[removed] — view removed comment

12

u/mrkite77 Dec 20 '10

chrome has incredible javascript performance. I made a 68k emulator in javascript that had no problem running at 1mhz on chrome.

6

u/feureau Dec 21 '10

I made a 68k emulator in javascript

... whoat?!

68k emulator in javascript

@_@

68k emulator in javascript

WANT!!!

1

u/mrkite77 Dec 21 '10

It's just the CPU, I did it to run Magnetic Scrolls adventure games in the browser (they're basically just 68K code with some special A-Line traps).

1

u/feureau Dec 21 '10

Still, ... Cool stuff! ^

1

u/Concise_Pirate Dec 21 '10

Hats off to ye.

1

u/irascible Dec 21 '10

Do you mean your emulator was 68k big, or you were emulating a motorola 68000?.. and if it was the motorola... are you talking about emulating for a mac game? and if so... have you ever played the game Hellcats over the Pacific? and if so... we should talk because I would kill to have a working version of that game on the PC again.

2

u/mrkite77 Dec 21 '10

A motorola 68000, although it's a slightly stripped down 68k, without an eXtend flag, so none of the opcodes that rely on that flag are implemented (abcd, addx, subx, roxl, etc). (Like I said in the other reply, this was specifically to run Magnetic Scrolls text adventure games.. like The Pawn. They were originally written on the Amiga, the game logic itself targeted this theoretical stripped down 68k cpu to make it portable. So, for example, there was a light-weight 68k emulator built into the Apple II version of The Pawn, which is rather impressive).

As far as your Hellcats over the Pacific game goes, you can get everything to need to run it in emulation here: http://macintoshgarden.org/games/hellcats-over-the-pacific

1

u/ToraxHunt Dec 20 '10

Each browser has stronger areas where they render faster. Chrome just happens to be good at this kind of thing. There are areas where each IE8-IE9 and Firefox are faster.

10

u/pkkid Dec 20 '10

Could you be a bit more specific please?

(curious minds want to know)

-2

u/signoff Dec 21 '10
  • Chrome = good at this kind of thing

  • IE8-IE9 = good at other kind of thing

  • Firefox = good at other kind of thing

5

u/pkkid Dec 21 '10

I was actually looking for more concrete references to the claims made and not just a "because I said so" answer that has no merit. But thanks for your input, you helped greatly in this matter. :-/

3

u/ToraxHunt Dec 21 '10

Sure, one example is canvas tag surface rendering using your GPU.

http://www.geek.com/articles/news/ie9-html-5-rendering-test-makes-chrome-6-look-slow-20100624/

Thus, IE9 currently has an advantage on certain scenarios on the canvas tag. As another poster pointed out below, Firefox is better at some scripting scenarios. Like I said, each has strong points, and anyone who throws a blanket over a browser and says "This browser is all crap" is pretty much guaranteed to be ignorant of the facts. I use several browsers depending on what I need to do, and I suggest you do the same. Don't discard any particular browser because of anti-fanboyism.

1

u/Evansbee Dec 21 '10

I'm sorry, but I don't think using several browsers is a feasible suggestion. I can't see my workflow being helped by minor javascript rendering improvements.

Is what you're proposing something as asinine as, "I'm going to facebook, time to open safari, jump to twitter which means chrome, on to reddit which is best in IE9"?

1

u/signoff Dec 21 '10

opera has faster DOM API. not that their javascript runtime is faster.

chrome has faster javascript runtime (except in 64bit OS).

firefox is better overall because it's open sauce unlike other company backed browsers. those proprietary browsers are made for profit be it open sauce or not.

1

u/minikomi Dec 21 '10

And like any good BBQ host knows - when the sauce ain't open, somethin's a cookin!

-2

u/Bloodhound01 Dec 21 '10

there are none that i know of, chrome is incredibly fast at pretty much everything it does. It blows every other browser away at the moment. Its actually crazy how much more advanced it is then other browsers yet people don't seem to understand this. Chrome is like 2 generations ahead of any other browser.

0

u/ascendant512 Dec 21 '10

This comment really takes the cake.

Chrome is definitely not 2 generations ahead of any other browser. It's not even 2 generations ahead of Internet Explorer, the worst of the current set of browsers.

Chrome is not the most customizable, it isn't the most secure, it's not the most resource-efficient, there are even other browsers that look better than it. The only thing it was the best at for any period of time was javascript and html rendering performance.

And even then, it wasn't leading the pack by an enormous margin. It's been in a very close race with WebKit (Safari and Konqueror) for a good while. Finally, see if this doesn't blow your mind: Firefox 4 beta 7's javascript performance is faster than both!

3

u/abadidea Dec 21 '10

but Chrome is Webkit...

1

u/ascendant512 Dec 21 '10

Haha, whoops. Well, I meant to be comparing the javascript engines.

  • V8 in Chrome

  • Carakan in Opera

  • -monkey in Firefox

  • SquirrelFish in Safari

  • and whatever abortion IE uses

→ More replies (0)

2

u/joanthens Dec 21 '10

very close race with webkit? so it's racing against itself?

1

u/[deleted] Dec 21 '10

SHHH he's an expert (at telling people that Chrome is not as good as they perceive it to be).

→ More replies (1)

1

u/[deleted] Dec 21 '10

I have both the Firefox 4 beta and Chromium installed, on Linux.

Firefox 4's performance does not equal Chromium's performance for this demo.

26

u/TheMagoozer Dec 20 '10

Well done - good creative thinking!

12

u/insert_silence Dec 20 '10

11

u/[deleted] Dec 20 '10

All the other entries are pretty dull in comparison?

3

u/[deleted] Dec 20 '10

Is this one going to be as badly judged as the last one?

10

u/jtreminio Dec 20 '10

Does look awesome.

Here's a fun trick for all: You can make that wireframe tree spin either left or right, using naught but your mind!

1

u/[deleted] Dec 21 '10

If you're having trouble doing it, try turning your head sideways.

1

u/protonfish Dec 21 '10 edited Dec 21 '10

I don't think so. But I did stare at it for a while turning my head so touché.

To make it actually spin backward you could change the line D+=.02 to D = D -.02

Edit: Whoa, that also makes the snow fall up...

1

u/UsernameTaken123 Dec 21 '10

Ya that's what I noticed too, I thought it only worked with the ballet girl.

10

u/UncleOxidant Dec 20 '10

It's like Bob Ross is doing Javascript from the grave!

4

u/murki Dec 21 '10

always too soon :(

1

u/lamintak Dec 21 '10

Your comment reminded me of this T-shirt

9

u/krische Dec 20 '10

Wow, it looks amazing. I can't believe it was done with only 1,000 characters.

37

u/ImTheGuyWithTheGun Dec 20 '10

Well, to be fair, he didn't comment his code very well ;)

2

u/scott Dec 21 '10

it's what they all say.... a picture's worth a thousand (err) characters.

8

u/[deleted] Dec 20 '10

works slow in any current browser except in Chrome

Bullocks, works great in Opera 11

7

u/[deleted] Dec 21 '10

Opera rocks. Even my netbook handles it.

1

u/thealliedhacker Dec 21 '10

What he meant was IE and FF suck and JS.
Works in anything else.

13

u/abeliangrape Dec 20 '10

Can some explain to me how using cos(angle+11) for sin(angle) saves space?

32

u/cnk Dec 20 '10 edited Dec 20 '10

Look at the code:

M=Math
T=M.sin

Then he uses that trick for "cos" twice in the code:

T(R-11) //7 characters

The other way to write that is: M.cos(R) //8 characters

Assigning cos to a variable is 7 additional chars (C=M.cos), using it that way is C(a) 4 chars, he uses cos twice in the code: that would be 7+4+4 total: 15. Using T(a-11) twice is 14 chars.

Also: assigning to a variable uses an additional semicolon which isn't necessary using the sin approximation

15

u/majoris Dec 20 '10 edited Dec 20 '10

Paraphrasing his code, here is how it looks with and without the trick:

M=Math;T=M.sin;T(a);T(a);T(a);T(a);T(a);T(a-11);T(a-11)
M=Math;T=M.sin;S=M.cos;T(a);T(a);T(a);T(a);T(a);S(a);S(a)

edit: who gave me upvotes without checking my solution!

6

u/cnk Dec 20 '10

in the 2nd line you don't need the "-11"s

1

u/abeliangrape Dec 20 '10

So it works when you end up using one of sin or cosine way less.

4

u/Douglas77 Dec 21 '10

11 = 3.5 * PI

(with an error of 0.0044, that's 1/4 degree)

EDIT: Misunderstood your question.

5

u/[deleted] Dec 20 '10

I think this alone may have caused me to switch from Firefox.

3

u/[deleted] Dec 21 '10

It runs great in Chrome and Opera. Give those a try.

4

u/Oddish Dec 21 '10

Although not as elite, I prefer this one:

http://js1k.com/2010-xmas/demo/865

8

u/InsideDown Dec 21 '10

Hey, that's mine! Never thought something I did would be randomly posted on reddit. Glad you liked it. (Nowhere near as impressive as Roman's though).

4

u/abadidea Dec 21 '10

I'm a dummy and thought that the static image was the result. "What are people talking about? This thing renders instantly..."

1

u/[deleted] Dec 21 '10

I agree, the link to the real thing should be more prominent.

10

u/ercax Dec 20 '10

I didn't know that I could boil tea on my cpu.

24

u/skelooth Dec 20 '10

You mean you aren't running a tea cooled rig? It's been all the rage since the crumpet towers came out.

3

u/abadidea Dec 21 '10

Clearly, you don't play Dwarf Fortress.

(And people think that graphics are what's driving the hardware market.)

1

u/ercax Dec 21 '10

Dwarf Fortress? No! Just guessing from what I read; if I start playing that thing I will have no life. It looks too good.

2

u/abadidea Dec 21 '10

I am a fierce advocate and evangelist for Dwarf Fortress. It is literally the only reason I'll be looking for more than 1.6ghz on my next computer. It is a masterwork piece of simulation.

But I do too have a life. Because my fiance and my best friend play it too.

2

u/ours Dec 21 '10

It looks too good.

Not literately.

1

u/[deleted] Dec 21 '10

Yeah, so much for thinking that Flash CPU usage is excessive.

6

u/10goto10 Dec 20 '10

I love stuff like this. The fact that all of this is contained in 1K really blows my mind. Reminds me of being amazed of what the guys in the demo scene used to pull off.

7

u/[deleted] Dec 20 '10

They're still going strong.

Here for your delight is 3.5 minutes of terraformed environments, music, light effects synced to music... in 4k.

Elevated by RGBA and TBC

6

u/10goto10 Dec 20 '10

I know, I visit several demoscene parties a year. I didn't want to imply there's nothing happening anymore, but modern PC demos just don't have the same appeal to me. Maybe because I lost oversight on what the limits really are that the coders have to work with. On the C64 I knew the limits of the machine by heart, which made it easy to recognize a spectacular effect.

I guess Javascript and the "browser environment" is just closer to home for me, seeing it is my daily work environment. That's why I like it so much!

0

u/[deleted] Dec 20 '10

[deleted]

2

u/[deleted] Dec 21 '10

It was, but then they zipped it up into 4k.

3

u/[deleted] Dec 20 '10

"Used to"?

1

u/10goto10 Dec 21 '10

Yeah.

1

u/[deleted] Dec 21 '10

Why the past tense?

1

u/10goto10 Dec 21 '10

See this comment. I think I gradually became less impressed with PC demos after 1995.

0

u/[deleted] Dec 21 '10

Perhaps you should phrase yourself to make it clearer that it's a matter of subjective interest on your part, not that people aren't still doing amazing things, because claiming so would be ridiculous.

Also, people are still doing even more amazing things with the C=64, too.

19

u/naich Dec 20 '10 edited Dec 20 '10

27

u/[deleted] Dec 20 '10 edited Dec 20 '10

should be this

edit: ok now my comment looks stupid. to clarify: naich asked for a mirror...

2

u/naich Dec 21 '10

Sorry, I thought I changed it quickly enough that no-one would see it.

1

u/allholy1 Dec 20 '10

huh?

15

u/[deleted] Dec 20 '10

when i posted that comment, the comment of naich above asked for a mirror because the site linked to by the title was slow at that time. I provided that link, then he edited his comment to what it is now.

0

u/[deleted] Dec 20 '10

[deleted]

1

u/[deleted] Dec 21 '10

climb in his window, snatch his people up?

2

u/[deleted] Dec 20 '10

My puter's CPU is screaming when viewing that page.

5

u/666pool Dec 20 '10

Mine was too in Firefox. Do you have chrome installed? It ran much smoother for me.

1

u/IllBeBack Dec 20 '10

I have both installed, and man, Chrome's Javascript execution is far faster than Firefox's. And I'm using the latest beta of Firefox 4.

1

u/[deleted] Dec 21 '10

I haven't installed chrome, but I probably should try it out.

1

u/666pool Dec 21 '10

A lot of people are switching, but I still prefer Firefox. I haven't found a replacement for ad block plus that I like for Chrome :(

1

u/theinternet Dec 20 '10

Thank you! I was wondering why the tree wasn't spinning.

2

u/[deleted] Dec 20 '10

For 1k that is incredible. Well done.

2

u/pkkid Dec 20 '10

Wow, I thought I knew Javascript pretty well.. :-/

2

u/pkkid Dec 20 '10

I would love to see a version of this source thats not all obfuscated.

0

u/[deleted] Dec 21 '10

[deleted]

1

u/pkkid Dec 21 '10

Thank you very much. :)

2

u/Jackofallnutz Dec 20 '10

Wow, that is awesome! Glad I'm learning Javascript right now, this just adds to the excitement.

2

u/ajnabee4u Dec 20 '10

Brilliant! Post like this make me wanna learn programming.

2

u/LieutenantClone Dec 21 '10

That is absolutely beautiful. You really don't get the full effect without running it and seeing how well the 3D part works. The 2D image does not do it justice whatsoever.

2

u/andash Dec 21 '10

Haha, I think I noticed this effect on the spinning tree animation.

2

u/[deleted] Dec 21 '10

outstanding.

2

u/progfu Dec 21 '10

Christmas tree made of pure awesomeness.

3

u/jukea Dec 20 '10

The leaves sprites are now diamonds !

2

u/ConstipatedAsshole Dec 20 '10

Is it changing directions by itself? or is that some optical illusion.

6

u/[deleted] Dec 20 '10

hmm, mine doesn't change...

don't think its an optical illusion, because the direction of movement and front/backside are clearly visible from the christmas bulbs.

3

u/hobophobe Dec 20 '10

It is an optical illusion. One of the interesting things about that form (assuming you mean the GIF in the article, not the tree itself) of it is that individual branches can appear to move in opposite directions simultaneously, because the whole isn't a solid form.

1

u/zitler Dec 20 '10

Aaauughhh why did I not pay attention in math class! I could be shitting diamond christmas trees made in JavaScript by now...

1

u/[deleted] Dec 20 '10

Amazing stuff.

Just finished my first year of college so I haven't hit my CS classes; everything I see looks so damn intimidating.

→ More replies (2)

1

u/Francesscaconti Dec 20 '10

Don't understand it but really love it!!!

1

u/blondin Dec 20 '10

good job!

1

u/a1phanumeric Dec 20 '10

Nice, except it took me about 20seconds to go back from the demo page. OSX, Firefox with Firebug (disabled though...) and the web developer toolbar.

1

u/Narcolepzzzzzzzzzzzz Dec 20 '10

Opera Mobile 10.1 on android can't render it but Dolphin HD can. Slow but it works.

1

u/causticmango Dec 21 '10

Sweet.

Odd observation; it's a little jerky on Chrome on OS X, Safari much smoother. Though, Chrome is generally faster with JS -- what gives? Both seem to use the same CPU, so it doesn't seem like Chrome is brute forcing some rendering that Safari is off-loading.

Then again, Chrome is 32 bit and Safari is 64 ... guess that's the diff?

1

u/[deleted] Dec 21 '10

It appears identical to me on OS X in Chrome and Safari, so I don't know. What's your CPU speed? It takes a core to 100% in either browser, so perhaps that 100% is not enough for Chrome on your system but it is for Safari. Hmm. I have a 2.53 ghz i5.

1

u/beej71 Dec 21 '10

What, no environment mapping on the ornaments? For shame!

;-)

Very clear writeup on the workings, too! Well done!

1

u/NeoSniper Dec 21 '10

That's awesome. Is there an optimized version for Firefox?

1

u/chewbacca81 Dec 21 '10

used to do the xmas tree in QuickBasic, back in the day. you kids and your 24-bit color!

1

u/OlmecJones Dec 21 '10

That's frigging awesome!

1

u/kidnees Dec 20 '10

Your code is the most awesome real accomplishment I have ever seen in all of reddit. Congratulations. I hope you are amply rewarded in life for your skill.

For non-programmers: there is no image of a tree. It is all purely created real time by code when yr page loads.

-2

u/skizmo Dec 20 '10

Great... a slow christmas tree that fucks up my cpu.

0

u/[deleted] Dec 22 '10

i'm feeling hard pressed to determine how this is superior to a pre-rendered gif.

-3

u/onlyvotes Dec 20 '10

Ho ho ho flash is completely fucked.

-2

u/[deleted] Dec 20 '10

This would be so much better, and leaner, if it were done using CSS3.

-4

u/rjcarr Dec 20 '10

A small niggle:

"and works slow in any current browser except in Chrome"

Why does everyone forget about safari? It worked great for me in safari on a laptop. Do people forget that Apple created Canvas and WebKit, both of which Chrome uses?

5

u/[deleted] Dec 21 '10

They forgot about Opera too. It works great in Opera.

1

u/[deleted] Dec 21 '10

It's a bit jerky for me in Opera on Linux. It's Opera 10, though.

1

u/jchrome Dec 20 '10

Safari and Chrome do share the (fantastic) Webkit rendering engine (so they both can enjoy the latest HTML 5 and CSS3 stuff), but they do not share the same JavaScript engines. Chrome's JS engine was developed by Google and is far superior to what anyone else has come up with yet. This particular demo is all about JS performance, which FYI is probably why several people down-voted you for your comment.

2

u/rjcarr Dec 20 '10

But it's JS mostly dealing with canvas. If google writes their own implementation of canvas then you'd have a point, but I'm pretty sure they don't.

2

u/ImTheGuyWithTheGun Dec 20 '10

While I don't think you should be downvoted, I also think Safari's success might be limited to Macs. On my fairly-capable Win7 system, Safari5 performs terribly on this demo. Then again, I find Safari on PCs to be sluggish in general. I'm sure it is a different story on a Mac...

1

u/[deleted] Dec 21 '10

Safari for Windows pretty much sucks, it's true. It's nothing like the OS X version.

-4

u/thetanlevel10 Dec 21 '10

Hmm, that's strange. I tried out a ton of browsers to see which loaded the fastest, and ole IE6 won more than 50% of the time. try it for yourself, maybe you'll see why over 40% of the internet is still on that browser.

2

u/[deleted] Dec 21 '10

Even if that's true, that is not the reason that it still has that size of user base.

91

u/[deleted] Dec 21 '10

[removed] — view removed comment

11

u/ryanjkirk Dec 21 '10

I did the thing directly with "obfuscated" code

That shows (aside from your demo, of course) true command of a language. Even traditionally, when one is able to write a speech in a foreign language, you know you've mastered it.

0

u/TheRedTeam Dec 21 '10

You do know pastebin exists right? ;)

Anyway, this was pretty damn awesome so kudos!