OpenLaszlo DHTML CSS 3 demo – to Flash or not to Flash, is no question!

by Raju Bitter on August 25, 2009

Since the OpenLaszlo DHTML runtime has been released, I’ve been wondering if there’ll ever be the time for me to say: “I can imagine that Flash won’t be a necessity for visually very appealing RIAs!” That time has come, thanks to the incredible work on great open source projects like the Firefox browser or Webkit.

Judge for yourself, here are two versions (DHTML/JavaScript and SWF9) of a demo application, that was inspired by Zachary Johnson’s great CSS3 text-shadow demo. This is the OpenLaszlo DHTML/JavaScript version of the demo, and as always with the advanced CSS features, you need a modern browser for it to work (I’ve successfully tested with Firefox 3.5+, Safari 4.x or Webkit Nighly Build, Google Chrome and Opera 10):

And in case you wonder, what the SWF9 version of this application looks like, here it is. Even after so many years of working with OpenLaszlo it’s still surprising to see how well the platform renders applications across runtimes.

Get Adobe Flash player

I believe this demo underlines what I’ve said in my previous blog, that OpenLaszlo can be one of the key technologys to drive adoption of standards like HTML 5 and CSS3 extensions in the RIA space, providing a backup version of an application in Flash for Internet Explorer 6/7 and older browsers. And who would choose Flash, if you can get exact the same visual results with open standards!

Update: Some people have requested the source code for this example. This is the main LZX file, and here is a link to the ZIP file containing the graphic resources as well. I ran tested with OpenLaszlo 4.5.1 and Trunk.


<?xml version="1.0" encoding="utf-8"?>
<!-- * GPL_COPYRIGHT_BEGIN ***********************************************
 * Copyright 2009 Raju Bitter
 * Use is subject to license terms.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * GPL_COPYRIGHT__END ****************************************************** -->
<canvas width="600" height="400" bgcolor="#FFFFFF" proxied="true">

  <!-- Resources -->
  <resource name="spotlightRes" src="spotlight.png" />
  <resource name="openlaszloRes" src="OpenLaszlo-Header.png" />

  <!-- reposition boxes depending on mouse x-pos -->
  <handler name="onmousemove" reference="lz.GlobalMouse"><![CDATA[
    var posX = canvas.getMouse('x');
    var posY = canvas.getMouse('y');
    if (posX>0 &amp;amp;&amp;amp; posY>0 &amp;amp;&amp;amp; posX<canvas.width &amp;amp;&amp;amp; posY<canvas.height) {
      spotlight.setAttribute("x", (posX-spotlight.width/2));
      spotlight.setAttribute("y", (posY-spotlight.height/2));
      // Calculate angel and distance
      var xx = posX - canvas.width/2;
      var yy = (posY - canvas.height/2);
      var distance = Math.sqrt(Math.pow(xx, 2) + Math.pow(yy, 2));
      var angle = Math.atan(yy/xx)/(Math.PI/180);
      if (xx<0){
        angle+=180
      }
      if(xx>=0 &amp;amp;&amp;amp; yy<0) {
        angle+=360
      }
      theText.setAttribute("distance", distance/2);
      theText.setAttribute("angle", angle+180);
    }
  ]]>
  </handler>

  <class name="demotext" extends="text">
      <!-- AS3 import statements -->
      <switch>
        <when property="$as3">
          <passthrough>
                        import flash.filters.DropShadowFilter;
          </passthrough>
        </when>
      </switch>

      <!-- Shadow distance -->
      <attribute name="distance" value="12" />
      <!-- Shadow angle -->
      <attribute name="angle" value="0" />
      <!-- blur X  -->
      <attribute name="blurx" value="5" />
      <!-- blur Y -->
      <attribute name="blury" value="5" />

      <!-- xoffset for CSS DropBox -->
      <attribute name="__xoffset" value="0" />
      <!-- yoffset for CSS DropBox -->
      <attribute name="__yoffset" value="0" />

      <!-- Reference to the Flash DropShadowFilter object -->
      <attribute name="shadowfilter" value="null" />

      <handler name="oninit">
        this.addBoxShadow()
      </handler>

      <method name="addBoxShadow">
        if ($dhtml) {
          // lz.text clipping active, which would cut off the shadow in DHTML
          // Workaround for http://jira.openlaszlo.org/jira/browse/LPP-8415
          this.getMCRef().style.overflow = "visible";
          this.getMCRef().firstChild.style.overflow = "visible";
          this.getMCRef().firstChild.style.clip = "";
          this.cssCalculateOffset();
          var cssString = "#333 "+ __xoffset +"px "+ __yoffset+"px "+ blurx + "px";
          Debug.write(cssString);
          this.getMCRef().style.textShadow = cssString;
        } else {
          /* DropShadowFilter constructor values:
           *  [distance:Number]
           *  [angle:Number]
           *  [color:Number]
           *  [alpha:Number]
           *  [blurX:Number]
           *  [blurY:Number]
           *  [strength:Number]
           *  [quality:Number]
           *  [inner:Boolean]
           *  [knockout:Boolean]
           *  [hideObject:Boolean])
           */
          this.shadowfilter = new flash.filters.DropShadowFilter(this.distance, this.angle,
              0x333333, 0.95, this.blurx, this.blury, 1, 2, false, false, false);
          // Have to remove mask on sprite for SWF8 runtime
          // Workaround for http://jira.openlaszlo.org/jira/browse/LPP-8426
          if ($as2) {
            this.sprite.removeMask();
          }
          updateSWFFilter();
        }
      </method>

      <handler name="onangle">
        if ($as2 || $as3) {
          this.shadowfilter.angle = this.angle;
          this.shadowfilter.distance = this.distance;
          this.shadowfilter.blurX = this.blurx;
          this.shadowfilter.blurY = this.blurx;
          updateSWFFilter();
        } else if ($dhtml) {
          this.cssCalculateOffset();
          var cssString = "#333 "+ __xoffset +"px "+ __yoffset+"px "+ blurx + "px";
          this.getMCRef().style.textShadow = cssString;
        }
      </handler>

      <method name="updateSWFFilter">
          this.sprite.getMCRef().filters = [this.shadowfilter];
      </method>

      <method name="cssCalculateOffset">
        // CSS3 doesn't use angle, but x/y offset. So we need to
        // translate from angle and distance to x and y offset for CSS3.
        // Math.cos and Math.cos are based on radians, not degrees
        var radians = this.angle * Math.PI/180;
        this.__xoffset = Math.round(Math.cos(radians) * this.distance);
        this.__yoffset = Math.round(Math.sin(radians) * this.distance);
      </method>

  </class>

  <demotext align="center" y="${canvas.height/2-this.height}" id="theText" clip="false"
        fontsize="70" fontstyle="bold" fgcolor="#2c657a" opacity="1.0">OpenLaszlo</demotext>

  <view id="wall" resource="openlaszloRes" y="${canvas.height-180}" />

  <view id="spotlight" x="$once{canvas.width/2-this.width/2}" y="$once{canvas.height/2-this.height/2}" resource="spotlightRes" />
</canvas>

And if you built some other cool demos with CSS3 or HTML5 support, please ping me!

Share:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • E-mail this story to a friend!
  • Ping.fm
  • Twitter

{ 4 trackbacks }

How long is Adobe Flash going to be around?
September 23, 2009 at 22:17
November OpenLaszlo Newsletter
December 3, 2009 at 19:21
Showcase Of Really Cool Live CSS Demos
July 7, 2010 at 23:27
Announcing Mobile OpenLaszlo – OpenLaszlo based W3C Widgets, native Smartphone Apps and OpenScreen Project Apps
July 15, 2010 at 11:34

{ 4 comments… read them below or add one }

Antun Karlovac August 25, 2009 at 20:18

That’s a cool demo Raju. FYI the moving shadow behind OpenLaszlo didn’t appear in Firefox 3 for me. It works correctly in Safari.

Did you write your own WordPress embed code for embedding the DHTML version of the application in this blog post?

-Antun

Mike August 25, 2009 at 21:11

One of the reasons for the success of Flash is that Adobe knows designers and makes good tools creating content. In addition to using the timeline in the Flash IDE, a designer can work in Photoshop or Illustrator and import assets as layers — a huge time and effort saver. Until someone develops an alternative to this workflow and integration I think designers will continue to prefer the Flash platform. If you’re building RIAs, Flash assets can also be used with Flex or AIR.

Also, I’ve rarely encountered a cross-platform issue with the Flash Player, which is one of the many reasons that developers use it. The DHTML/CSS version above doesn’t display for me in IE7 (I just get the spinning “Powered by…”) but the Flash below it does.

Raju Bitter August 25, 2009 at 23:43

@Antun: Text-shadow based on CSS is only available in Firefox 3.5+. And the OpenLaszlo app is simply embedded through an iFrame plugin in Wordpress.

@Mike: On the application not showing up in IE: I haven’t it stated it there, but IE doesn’t support CSS text-shadow support. I mentioned that many times in the previous blog posts on the subject. The genereal idea is to have a Flash version for IE, and the open standards based versions for Webkit, Firefox, Safari, Google Chrome, and the next version of Opera supporting these features.

You have some good points, Adobe wouldn’t be successful with just providing a runtime plug-in for the browser. The company has the some of the best products for an integrated workflow of design, UI engineering and – to a lesser extend – coding. Flex Builder isn’t really a great product, I think Java or .NET/WPF/Silverlight IDEs are much more advanced. I bought Flex Builder 3, and there’s no refactoring, not even source code formatting. And the Flex compiler is painfully slow, I heard of large projects having a compile time of up to almost 10 minutes.

Microsoft, IBM or Sun are far ahead of Adobe when it comes to engineering languages, compilers and browser plug-ins delivering state-of-the-art performance. You can simply see that when you compare Silverlight and JavaFX performance with that of Flash. To be fair, the JavaFX user experience launching an application sucks compared to Flash. But Microsoft is going to take huge share of the Flash business with Silverlight.

Flash and cross-platform issues? There are more than enough issues, let me just point to the sad FP-40 bug, with no unicode support for Flash under Linux. The last company I worked for wanted to deploy Flash communication portals on Linux netbooks for cable Internet customers in Germany. Due to the fact that you can’t even enter the @ char with a German keyboard, the whole business is on hold, until FP-40 is fixed. Based on what I read, the problem shows up on combinations of Windows with Firefox as well.

United Internet/GMX – Europe’s leading web mail provider – built a Flex webmail client, launched it – and had so many problems with Flash and input, that they took the application offline. In the end they developed their own open source Ajax framework for an RIA based webmail client: Qooxdoo.

Raju Bitter September 2, 2009 at 13:12

As I agree with Mike’s comment on the bad user experience with Internet Explorer, I’ve modified the application for browsers without support for text-shadow. Before the DHTML version of the app never loaded in IE, showing the loader spinning. The updated version displays without the text-shadow, displaying a message that the user’s brower doesn’t support the CSS feature used here.

That message will be displayed for older versions of Firefox, Safari, Chrome and Opera as well.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: OpenLaszlo CSS3 text-shadow demo

Next post: How long is Adobe Flash going to be around?