Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Smart Graphics – Part 2

Posted on 27.01.2013 by Jon Lennart Posted in Developers log, News

In the previous article we introduced some general graphical concepts, things like double buffering, per scene rendering, sprites and alpha blending. Time to get our hands dirty and jump into it – how can a Delphi developer leverage his skills under HTML5? Well let’s find out!

Getting started

Fire up the Smart IDE, click “new project”. You will be presented by a small range of different project types: canvas, console and visual. Since we will be playing around with graphics – click canvas and let’s get cracking.

Now that a new project is ready we need to familiarize ourselves with the HTML5 canvas. This is not your father’s TCanvas so you will really benefit from studying the RTL classes and reading up on a javascript canvas tutorial. One of the great benefits of Smart Mobile Studio is that you can easily follow pretty much any javascript graphics tutorial since the canvas object maps directly.

First, let’s setup the project. We will create a canvas game project. This type of project contains an automatic update routine which forces a redraw of the screen within a short interval. It’s perfect for games and graphical demos and all you really have to do is populate the paint method.

project_new

Getting to know the canvas

Now that you have a brand new project up and running, it’s time to investigate what we have to work with. On top of your project unit (called project12 in this example) you will find “w3graphics” in the uses list. Right click this unit and select “open file at cursor”. This opens up the graphics unit so you can see the source. Now scroll down to TW3Canvas and have a look at the methods.  It is also handy to have a look at the unit structure tab (right side of the IDE), you will recognize all the functions you have in Delphi and more. The most notable being:

  • Rectangle
  • Moveto
  • Lineto
  • Arc
  • Lineto

These are functions you already know. If you are wondering why they are post-fixed with F – this is because they take floating point parameters rather than plain integers. Have a look at the methods and their parameters and get to know the API (this is a very handy time to have a look at the official javascript canvas documentation if you prefer pure docs rather than exploring).

By examining the TW3Canvas class you get to know the methods

By examining the TW3Canvas class you get to know the methods

Right, flip back to the project source unit, let’s draw a simple line, so we populate the paint method with the following code:

procedure TApplication.PaintView(Canvas: TW3Canvas);
begin
  // 1. Clear background
  Canvas.FillStyle := 'rgb(0, 0, 99)';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);

  // 2. Draw our framerate on the screen
  Canvas.Font := '10pt verdana';
  Canvas.FillStyle := 'rgb(255, 255, 255)';
  Canvas.FillTextF('FPS:' + IntToStr(GameView.FrameRate), 10, 20, MAX_INT);

  // 3. Draw our line
  canvas.StrokeStyle:="#FFFFFF";
  canvas.beginpath;
  canvas.MoveToF(0,0);
  canvas.LineToF(100,100);
  canvas.stroke;
end;

The first two sections (marked 1 and 2) are automatically added when you create the project, so only the section marked 3 is the actual text we added here. Let’s go through the commands.

The result of our simple path operation

The result of our simple path operation

Strokestyle

Strokestyle determines the color and style of what you want to draw. In this case we have used a simple HTML color code for white. If we want to use opacity when we draw, we would use the other color format: rgba(255,255,255,0.5) for instance (note: use the ColorToWebStr function in w3system.pas to generate an RGBA string). You can also use a gradient or a pattern as strokestyle (more about that later). So yeah, it’s pretty cool.

Beginpath

Like we mentioned in our first article, all HTML5 drawing is path based. You want to draw all the graphics in one-go for optimal speed. So to setup a path you issue a call to beginpath. This simply tells the canvas that whatever drawing functions you issue ahead should be collected.

Moveto and Lineto
These functions are the same here as they are in delphi. You use moveto to fix the cursor at a point on the canvas. And you use lineto to draw from that point. You can use the full spectrum of functions in the canvas to generate complex and beautiful paths.

Stroke

Stroke closes the path and draws whatever shape you have defined using the different canvas methods. Stroke means that you draw and outline shape. The alternative would be a filled shape, a polygon, in which you would call the fill method rather than stroke.

A more advanced example

In the first installment of this article we showed a slightly more complex example, which used a bezier curve and a quadric curve. This is taken straight from javascript. Let’s alter our code to match that (postfix with F is the only real change you need here). Remember to keep section 1 and 2 (as marked above) the same:

  canvas.beginpath;

  canvas.MoveToF(0,0);
  canvas.LineToF(100,100);

// quadratic curve
canvas.quadraticCurveToF(230, 200, 250, 120);

// bezier curve
canvas.bezierCurveToF(290, -40, 300, 200, 400, 150);

// line 2
canvas.lineToF(500, 90);

canvas.lineWidth := 5;
canvas.strokeStyle := 'blue';
canvas.stroke();

The end result is exactly the same in Smart Mobile Studio as it is under “pure” javascript. Again the only real difference is the postfixing with F:

Straight port from javascript

Straight port from javascript

Now that you have some general idea about how the modern HTML5 canvas works, that it’s really not that different from the old TCanvas (except magnitudes cooler) take a while to investigate. In the demo folder that ships with Smart Mobile Studio you will find plenty of examples that uses the classical drawing methods. And you will also find a lot of powerful demonstrations which uses more advanced features.

In the next installment we will cover two important aspects of graphics programming: offscreen bitmaps and double buffering. So get ready to move stuff around!

Click here to download the test project: Project12.opp

« Smart Graphics
Smart Graphics – Part 3 »

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS