Activate your free membership today | Log-in

Monday, February 8th, 2010

Pseudo 3D tricks from old computer games for all your Canvas needs

Category: Canvas, Fun, Games, JavaScript

It is quite interesting to see how technology moves in circles. With canvas being the new fun toy to play with for creating browser-based games we have to find solutions to fake a 3D environment to be really fast (sure there is Canvas 3D but it is overkill for most games). The trick is to dig into the tricks arsenal of old-school game development on machines full of win like the Commodore 64 or Amiga.

Louis Gorenfeld some very detailed explanations on how to fake 3D including some of the formulas used in the days of 8 bit.

He is also working on some demo code which you can help him with by providing some JS/Canvas demos:

Fake 3D

CODE:
  1. current_x = 160 // Half of a 320 width screen
  2. dx = 0 // Curve amount, constant per segment
  3. ddx = 0 // Curve amount, changes per line
  4.  
  5. for each line of the screen from the bottom to the top:
  6.   if line of screen's Z Map position is below segment.position:
  7.     dx = bottom_segment.dx
  8.   else if line of screen's Z Map position is above segment.position:
  9.     dx = segment.dx
  10.   end if
  11.   ddx += dx
  12.   current_x += ddx
  13.   this_line.x = current_x
  14. end for
  15.  
  16. // Move segments
  17. segment_y += constant * speed // Constant makes sure the segment doesn't move too fast
  18. if segment.position <0 // 0 is nearest
  19.   bottom_segment = segment
  20.   segment.position = zmap.length - 1 // Send segment position to farthest distance
  21.   segment.dx = GetNextDxFromTrack() // Fetch next curve amount from track data
  22. end if

Posted by Chris Heilmann at 7:04 am
4 Comments

++++-
4.6 rating from 23 votes

Thursday, January 28th, 2010

Canvas Benchmark

Category: Browsers, Canvas, Performance

The Freeciv.net crew has benchmarked a path in their canvas game. It is one data point, and tests more than just Canvas itself because a lot of code is running in the game. Thus, it ends up testing the union of a particular JavaScript path and the rendering of the canvas.

Here are the results:

canvasbenchmark

With Bespin we had slightly different results, and the bulk of the bottleneck was in the blitting of the canvas. Optimizations were made to canvas over the initial phase of Bespin so the various browsers would leap frog each other. Good times :)

Posted by Dion Almaer at 6:09 am
26 Comments

++++-
4 rating from 28 votes

Tuesday, January 5th, 2010

Commodore 64 JavaScript Emulator

Category: Canvas, Fun, Games

c64

Tim de Koning has done what folks do best with their winter holidays.... created something fun :)

With JSC64 he has ported the Flash version to give us an emulator for the Commodore 64 in JavaScript.

Tim's work uses Canvas and he provides a jQuery plugin if you are that way inclined. It is fun to check out the source on GitHub.

Posted by Dion Almaer at 6:06 am
10 Comments

++++-
4.7 rating from 29 votes

Tuesday, December 29th, 2009

Hand-Drawn Look in Canvas Drawing App

Category: Canvas

Steve Hanov has produced a Canvas-based drawing tool with a hand-drawn look. Lines can be drawn with a "sloppiness" option, with possible values "Draftsman", "Artist", "Cartoonist", "Child", and - perfect for this time of year - "Drunk" :). The FG Virgil font, applied to text elements, adds to the cartoon vibe.

The techniques are used in a C library for WebSequenceDiagrams, which Steve explained in an older blog post, but here he's done it with Javascript and Canvas.

Posted by Michael Mahemoff at 6:57 am
5 Comments

++++-
4.5 rating from 33 votes

Monday, December 28th, 2009

TransM: Programmable Image Transitions

Category: Canvas, JavaScript, Library

The infamous Christian Effenberger has a new library to share:

transm

TransM.js 1.0 allows you to add programmable image transitions to your webpages. Features 113 transitions, 32 tweenings, individual corners, overlay graphics and event actions. Requires no plugin/extension or any other external resource! It uses unobtrusive javascript to keep your code clean.

You can play around with an inline preview:

transmpreview

And check out the demos.

Posted by Dion Almaer at 6:38 am
10 Comments

++++-
4.6 rating from 27 votes

Friday, December 4th, 2009

Fun Friday Particle System

Category: Canvas

I think we first featured Mr Speaker when we showed his fun Friday platformer game.

This Friday we have a canvas based particle system that is very visual. Check it out.

mrspeakerparticles

Posted by Dion Almaer at 12:39 am
11 Comments

++++-
4.4 rating from 26 votes

Wednesday, November 25th, 2009

Javascript JPEG Encoding

Category: Canvas

Andreas Ritter has managed to encode JPEGs in Javascript. This blog post explains how he did it, shows some benchmarks, and provides a demo and a downloadable library so you can play along at home.

It was surprising that it was that easy to get the first js-encoded jpeg displayed in the browser. Of course I didn't want to stop there. I wanted to optimize things as much as I could to make the encoder fast. This took me several days. I found optimized encoder versions for flash and haxe floating around the net (Faster JPEG Encoding with Flash Player 10) and tried the optimizations used there in my javascript version. As you can seen in the benchmarks below I was quite successful.

Another idea was to use the new web workers to do the heavy lifting in an separate thread, not blocking the gui. This is something flash can't do. So I created a version using a web worker for the encoding.

The API gives you a JPEGEncoder or an alternative JPEGEncoderThreaded. Usage is straightforward:

JAVASCRIPT:
  1.  
  2. var myEncoder = new JPEGEncoder([quality])
  3. var JPEGImage = myEncoder.encode(CanvasPixelArray,[quality])
  4.  

I think the results show that JavaScript is quite fast (at least in Safari and Chrome). A little over 4 seconds for the non-threaded version is a very good [sic] result, when compared to the 3,3 seconds the optimized flash jpeg encoder takes. Please note, that JavaScript has no static types, no byte array, no Vector-class and is not pre-compiled. Taking these facts into account Nitro and V8 are faster than the ActionScript 3 VM.

Comparing the different browsers Nitro and V8 are a magnitude faster than TraceMonkey. Firefox 3.6b2 shows some improvements, but it's still a long way. Probably the Mozilla guys should consider adopting Nitro or V8?

He used the AS3 (ActionScript) JPEG encoder as a starting point. It's worth noting there's also a PNGEncoder there too; that's a port waiting to happen.

Posted by Michael Mahemoff at 1:15 pm
23 Comments

++++-
4.5 rating from 39 votes

Thursday, October 22nd, 2009

Microsoft to help move Canvas 2D API out of HTML5 spec?

Category: Canvas, Microsoft

Eliot Graff of Microsoft wrote to public-html@w3.org:

In his mail describing why he created a separate Canvas 2D API specification, Doug Schepers wrote [1]:

> There is a chance that currently Canvas could be a blocker on progress
> for the HTML5 spec, and at this point, Canvas is so widely implemented
> that I don't think it's at risk, so I hope this isn't disruptive. I am
> available to help with any editing that needs doing, but I hope that
> others will also work with this draft, and step into the editor role.

At Microsoft, we agree with the sentiments expressed by Doug, Maciej [2], and others about creating a separate Canvas 2D API specification. [3] We are prepared to offer editorial resources to aid in the completion of this separate specification. We have looked over Doug's initial document, made some editorial enhancements, and are prepared to follow through in taking feedback and maintaining the specification.

We believe that some sort of accessibility API functionality is needed in the canvas element. However, the exact nature and depth of that functionality presents a dilemma that may block progress on the HTML5 spec. We also think that the Canvas 2D API may be a desirable feature used in other technologies such as SVG.

Starting with Doug Schepers' initial draft, we made changes to get the document to adhere to the W3C PubRules [4], enhance readability, and improve logical flow of the document. In addition, we foresee adding sample code throughout the specification, where appropriate. No normative changes have been made. As with all drafts, the Canvas 2D API specification is still a work in progress. We would like to solicit feedback about the changes that were made (see below TODO) and about further changes that the working group would like to see.

Our updated version is published at http://dev.w3.org/html5/canvas-api/canvas-2d-api.html.

[1] http://lists.w3.org/Archives/Public/public-canvas-api/2009JulSep/0002.html
[2] http://lists.w3.org/Archives/Public/public-canvas-api/2009JulSep/0007.html
[3] http://lists.w3.org/Archives/Public/public-html/2009Aug/0628.html
[3] http://www.w3.org/2005/07/pubrules

Appreciate the help from Microsoft, but the snark in me wants to say:

How about giving "resources" to implement the bugger in IE!!!!!!

Posted by Dion Almaer at 12:32 am
30 Comments

++---
2.9 rating from 41 votes

Thursday, October 15th, 2009

HTML5 Canvas FTW!

Category: Canvas, Presentation

Dmitry Baranovskiy, of Raphaël fame (can't forget the umlauts), has posted an excellent presentation on the Canvas tag from Web Directions South ’09:

Posted by Brad Neuberg at 6:30 am
9 Comments

++---
2.6 rating from 49 votes

Monday, October 12th, 2009

Crazy Times: Rendering HTML…. in Canvas

Category: Canvas, HTML

It's still early work but James Urquhart has gotten HTML rendering inside the Canvas tag. In his demo, he renders the following HTML into the Canvas element:

HTML:
  1. &lt;html&gt;
  2.   &lt;head&gt;
  3.     &lt;title&gt;&lt;/title&gt;
  4.   &lt;/head&gt;
  5.   &lt;body&gt;
  6.     &lt;p class="woo" id="render" style="display:none;"&gt;
  7.       Rendering &lt;b&gt;HTML&lt;/b&gt;...
  8.     &lt;/p&gt;
  9.     &lt;p&gt;&lt;span&gt;In &lt;b&gt;Canvas&lt;/b&gt;&lt;/span&gt;!&lt;/p&gt;
  10.     &lt;p&gt;0_0&lt;/p&gt;
  11.   &lt;/body&gt;
  12. &lt;/html&gt;

It's still pretty simple, but this renders as:

canvas_html_rendering

Much of the real work involves regular expressions plus using the Canvas measureText and fillText functions. Right now he is parsing the following tags: HTML, BODY, P, B, and SPAN.

Run the demo yourself (FF 3.5+ and Webkit based browsers only). All the code is open source and available on GitHub.

James jokes that we might one day see HTML Canvas running in HTML Canvas running in HTML Canvas.... turtles all the way down.

ALLTHEWAYDOWN

Posted by Brad Neuberg at 6:30 am
21 Comments

++---
2.4 rating from 32 votes

Thursday, October 8th, 2009

Play Kings Quest, Space Quest, and More… Using Canvas Tag

Category: Canvas, Games

Martin Kool and Sjoerd Visscher have gotten many of the old Sierra Online games running on the Canvas tag, including many of the Kings Quest series, Space Quest series, and Leisure Suit Larry.

gr

lsl

Martin Kool describes some of the long process it took to get these into the shape they are:

In 2003, I created Good Old Adventures. It was a chat environment set in the Sierra worlds without actual gameplay. All images were manually crafted, copy/pasting from WinAGI Game Development Studio. A time consuming process.

sq
Four years later I wrote a few conversion tools in .NET to extract Sierra's in-game images and objects from the original games, and display them properly on the web. The "only thing" left to do then was to export and understand all in-game logics.

I was able to convert the logic to javascript, but there was one big problem: Sierra's code used GOTO statements, and those could jump anywhere in the code, even inside a nested "if"-statement. It seemed unsolvable in javascript.

I sat down together with Sjoerd Visscher, one of my collegues at Q42, and we came up with a solution. Using a decompilation approach to get rid of nested "if"-statements and putting the whole shebang inside a huge switch/case statement, we could mimic line numbers and GOTO's while maintaining performance. World domination was within reach :-D

code

It was halfway 2008 when I started to work on it, and Sarien.net is the result.

Posted by Brad Neuberg at 6:00 am
10 Comments

++++-
4 rating from 35 votes

Monday, October 5th, 2009

toDataURL, Canvas, and SVG

Category: Canvas, SVG

I'm a fan of all the new ways of drawing on the web. I consider myself a Canvas evangelist, an SVG evangelist, and an evangelist for the new CSS Animation work going on. When asked "SVG or Canvas" I've long felt the right answer is: "Yes" :) Canvas is great at pixels, SVG is great at vectors, 'nuff said. Give me my scriptable image tag (Canvas) AND my easy scene graph (SVG).

Along these lines, I was delighted to attend a recent presentation by Samuli Kaipiainen and Matti Paksula from the University of Helsinki at SVG Open 2009. In their paper (which I recommend reading) they show all sorts of cool results concerning Canvas and SVG, but two results jumped out at me in particular.

Samulimatti

First, they showed apps built with SVG that should have used Canvas and vice versa to identify when to use one or the other. They showed a Jigsaw Puzzle program written in Canvas that should have used SVG, as the Canvas version took 2800 (!) lines of JavaScript essentially recreating the scene graph and rotations that SVG gives you for free:

jigsaw_screenshot

They then showed a Space Invaders game written with SVG that obviously should have used Canvas instead: there's no mouse interaction, it's keyboard driven, and it needs rapid pixel-based updates since it's a game:

spaceinvaders_screenshot

Second, both developers point out a nifty trick and snippet of code that I'm sure will become a regular part of peoples' work. They convert a Canvas image into a form that can be brought right into SVG by turning it into a data URL:

JAVASCRIPT:
  1. function importCanvas(sourceCanvas, targetSVG) {
  2. // get base64 encoded png from Canvas
  3. var image = sourceCanvas.toDataURL("image/png");
  4.  
  5. // Create new SVG Image element.  Must also be careful with the namespaces.
  6. var svgimg = document.createElementNS("http://www.w3.org/2000/svg", "image");
  7. svgimg.setAttributeNS("http://www.w3.org/1999/xlink", 'xlink:href', image);
  8.  
  9. // Append image to SVG
  10. targetSVG.appendChild(svgimg);
  11. }

This would be useful for drawing a fancy background with Canvas, for example, and then pulling that onto an SVG element so you get an easy scene graph and scalable graphics. Samuli and Matti report that this technique works on all browsers (but not IE unfortunately, even with ExCanvas, as that currently doesn't support toDataURL yet).

Samuli and Matti end their paper with the following quote:

Based on these findings, there really is no vs. situation. The right technique needs to be selected for the job, and we hope this paper gives a good starting point for that. When the right choices are made, SVG and Canvas can actually benefit from each other...

Yes, there's a limit on each standard's capabilitiness. For pixel flare and other "demo effects", go with Canvas. For intrinsic shapes and user interaction, go wth SVG. Graphical user interfaces on the web are an especially delicious application for SVG.

As a final note, Matti and Samuli identify a gap that HTML 5 needs to fill: it would be great if you could go the other direction and turn an SVG image into it's rendered pixels to be passed to the Canvas tag, basically a toDataURL for SVG that can be passed into Canvas's fromDataURL.

Posted by Brad Neuberg at 6:30 am
21 Comments

+++--
3.7 rating from 27 votes

Thursday, October 1st, 2009

ChemDoodle Web Components

Category: Canvas, HTML

Via jzornig comes news of a cool set of components written with HTML 5 and the Canvas tag:

chemdoodle1

ChemDoodle Web Components are pure javascript objects derived from ChemDoodle™ to solve common chemistry related tasks on the web. These components are powerful, fully customizable, easy to implement, and are free under the open source GPL license.

The page shows example usage of the components. To use the 3D Rotator component, for example, you just provide some JavaScript to configure it:

JAVASCRIPT:
  1. var rotate3D = new RotatorCanvas('rotate3D', 200, 200, true);
  2. rotate3D.specs.atoms_useJMOLColors = true;
  3. rotate3D.specs.atoms_circles = true;
  4. rotate3D.specs.bonds_symmetrical = true;
  5. rotate3D.specs.backgroundColor = '#E4FFC2';
  6. rotate3D.yIncrement = -Math.PI / 360;
  7. rotate3D.loadMolecule(readMOL(threed));
  8. rotate3D.startRotation();

Posted by Brad Neuberg at 6:30 am
2 Comments

++++-
4 rating from 17 votes

Monday, September 28th, 2009

Berts Breakdown

Category: Canvas, Fun, Games

We usually post these on Friday, but who says a little fun on Monday is wrong? Paul Brunt has put together a nifty game using the Canvas tag. It's pretty impressive. He is also using Chrome Frame to have the game work on IE as well.

bert3

Play it now

Posted by Brad Neuberg at 7:00 am
8 Comments

++++-
4.3 rating from 36 votes

Thursday, September 24th, 2009

Content Aware Image Resizing… in JavaScript

Category: Canvas

Content aware image resizing is a nifty technique where you can make an image smaller by literally understanding the content better and remapping it.

original_pix

Stéphane Roucheray has put together a fancy demo (Firefox 3.5 only) that uses JavaScript and the Canvas tag plus createImageData to do all of this in the browser versus C++:

For this demo, a sub part of the Content Aware Image Resizing algorithm has been implemented. The width of the image can be reduced interactively without modifying its height. This implementation uses seam carving to re-size the image, subtracting the less visible vertical lines. It is a four step iterative algorithm. One iteration is one pixel width re-size. First an image is loaded into the Canvas context and then the iteration starts :

  1. A grayscale version of the image has to be calculated
  2. The edges of the image (Sobel convolution is used in our case) and its energy matrix is computed
  3. The seam of least energy (1 pixel vertical line from the bottom to the top of the energy matrix) is detected
  4. Then the pixel of the detected seam is removed from the original image and the result is re-injected as a source image to step 1

Each of the previous steps stores a whole matrix of data at the source image size. While these matrices are not all images but actually artifacts of the algorithm, storing them in an ImageData object is more convenient than using simple Arrays. This is why the createImageData method of the Canvas context is used. One of the benefits of this process is to allow showing the intermediate computations made under the hood.

As Chris Blizzard mentions in the blog post, this opens up lots of interesting possibilities:

This demo shows that’s possible to do more intelligent image resizing than just flattening an image’s pixels with CSS. Having computational and image manipulation capabilities directly in the browser opens up a new range of possibilities of how image data can be displayed to users. This is only one small demonstration of that.

Posted by Brad Neuberg at 1:30 pm
8 Comments

++++-
4.5 rating from 39 votes

Wednesday, August 26th, 2009

Canvas via Silverlight again

Category: Canvas

The infamous excanvas has had a Silverlight bridge for some time. It is faster, yet has some artifacts and most people stick with good ole VML.

David Anson has put his hat in the ring by creating a new prototype canvas implementation in Silverlight.

Thanks to Silverlight's HTML Bridge, I had no trouble creating a Silverlight object that looks just like a <canvas> to JavaScript code running on a web page. So similar, in fact, that I'm able to run some pretty cool sample applications on my own <canvas> simply by tweaking the HTML to instantiate a Silverlight <canvas> instead of the browser's <canvas>. And as a nice side effect, Internet Explorer "magically" gains support for the tag!

I did not attempt to implement the complete canvas specification. Instead, I implemented just enough to support the first 5 (of 6 total) Mozilla sample pages as well as the handful of applications shown above. Specifically, I've implemented everything that's not in italics in the feature list at the beginning of this post. Thinking about what it would take to add the stuff that's not implemented: text and pixel-level manipulation are both directly supported by Silverlight and should be pretty easy. Shadows seem like a natural fit for Silverlight's pixel shader support (though I haven't played around with it yet). All that's left is layer compositing, which does worry me just a little... I haven't thought about it much, but this seems like another job for WriteableBitmap, perhaps.

David gets a ton of the examples going though, and I am still hopeful that someone will continue work on this kind of shim to make a fast implementation. I need to go off and test Bespin.

Posted by Dion Almaer at 6:18 am
6 Comments

+++--
3.5 rating from 24 votes

Next Page »