Posts tonen met het label html5. Alle posts tonen
Posts tonen met het label html5. Alle posts tonen

donderdag 22 oktober 2015

HTML5 Canvas, context setTransform explained from skew, rotate, scale and translate

Things are sometimes a little skewed in the world of Adobe. And if you use Adobe products to produce game graphics (like animations) that you'll use on a canvas, (and you don't want the BULK of createJS) you run into trouble quite easily.

So I needed to understand how to get THIS in terms of the canvas context.setTransform() function.



If you want to skip the explanation, my working example is at the bottom of the page.

What are matrices again?

The canvas setTransform function takes a transformation matrix: [[ace],[bdf],[001]]
Now I've had matrices in school for a semester and later dabbled in OpenGL matrices, which are a bit more complicated than 2D matrices, but I always had trouble explaining to myself what happened in either 2D or 3D. So time for some experimentation.

void ctx.setTransform(a, b, c, d, e, f);

They say, it helps to think about these parameters as:
a Horizontal scaling.
b Horizontal skewing.
c Vertical skewing.
d Vertical scaling.
e Horizontal moving.
f Vertical moving.

It helps. If only it wasn't a gross over-simplification.
Because when you start to experiment for real, you'll see the truth is a bit different.
The above is only true, AS LONG AS you do not ROTATE.
But you should be able to set skewing even when rotated, just like that.. That's the beauty of using matrices, I remember that much from school.

So I wrote this little bit of code to experiment and show you (and myself) how simple or difficult it REALLY is to get the results you expect..

If you want to rotate, make sure the angle for x and y are the same. If you want to skew, make the angles for x and y NOT the same. The amount by which they differ is the skewing.

You can visualise this as setting the rotation for the x axis and the rotation for the y axis seperately. Just like you would when you do a non proportional scale and set the length of the X axis and Y axis seperately.
Anyway, it helps me to understand those pesky matrices better and I wanted to clarify what I found to you. So here is the code-breakdown:
It basically sets up a interval and a canvas. Then in the move function draws a stroked rectangle repeatedly from -50 to +50 in x and y.
But before it does that it sets the context transform according to the frame-counter.
The first 45 frames it just rotates and moves a scaled version of the rectangle.
After the first 45 frames it skewes it in the y-direction as well..

The first 45 frames you might achieve the same by doing ctx.translate, ctx.scale and ctx.rotate.
But after 45 frames, you really see the power of setTransform.
Also note, that ctx.translate etc are cumulative and you need to save and restore your context.
With setTransform, you don't. Just set it back to the zero-matrix when you are done and want to draw normally again: ctx.setTransform(1,0,0,1,0,0);

Anyway, here is my code and it's execution:

var counter=0;
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
setInterval(move,25);

function move()
{
    counter++;
    var anglex=45+counter;
    var angley=45+counter;
    if(counter>45) angley=80+counter;
    var scalex=0.5;
    var scaley=0.7;
    var origx=0+counter*2;
    var origy=100;
    var rx=Math.PI*anglex/180;
    var ry=Math.PI*angley/180;
    ctx.setTransform(scalex*Math.cos(rx),scalex*Math.sin(rx),-scaley*Math.sin(ry),scaley*Math.cos(ry),origx,origy);
    ctx.strokeRect(-50,-50,100,100);
}





About skewing

Now skew or slant is a bit of a wrongly used term normally. Speaking Mathematically we mean something else, than what most people know from Adobe's programs.

So I made another example, to mean more what most people THINK skewX and skewY mean: namely skewx and skewy are inverted: If you want the code for that one, just press the edit in fiddle button in the top right hand.
I'm not going to explain that code here, I've kept it very simple.

It's still not exactly how skew works with Adobe, because at low skews in the X the height of the object stays the same with for instance Flash. If you go skew things a lot, it will just give you very unexpected results. This is why: Adobe compensates the length of the axis that is not skewed to make it keep approximately the same height:
I fiddled around a little bit more and ended up with this:

If you are used to adobe products, this will give you something quite a kin to the skewing that you know and love. It's not an exact -pixel perfect- match, mind you, but quite good at low settings (-45/45 degrees) and that's good enough for me right now.


Original article I found (and I found was wrong:)
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform


dinsdag 22 september 2015

LOI Gamedesign

Afgelopen vrijdag gaf ik GameDesign op HBO niveau voor de LOI.
Als onderdeel van deze cursus zou men gamemaker moeten leren.
Aangezien gameDesign voor alle leerlingen niet echt een hoofdvak was, maar meer een verplicht nummer, besloten we de game in Javascript te gaan maken,
Gamemaker is leuk als je wilt gameDesignen, zonder iets te willen programmeren. Maar als je een multimedia/frontend opleiding doet, dan is javascript toch wel een vereiste.

Javascript zat tot nog toe niet in de cursus, hetgeen een extra uitdaging vormde:
naast basic gamedevelopment, moest ik ze ook leren programmeren.
Gelukkig heb ik dat vaker bij de hand gehad, dus op naa JSFiddle.

http://jsfiddle.net/wn8rq0m0/
var a=4;
if(1==3)
{
 a=a+1;
}
console.log("------------");
console.log("de waarde van a ="+ a);

Mijn controle vraag aan de leerlingen om het niveau in te schatten is: "Wat gaat er gebeuren?" en
 "Wat wordt de waarde van a?"
Als je hier een antwoord op wil kunnen geven moet je twee basis-concepten van programmeren snappen. Variabelen en Expressies.

var a=4;
Een variabele (in dit geval a) wordt gedeclareerd.
Dit betekent, dat je javascript verteld, hoe de variabele heet en (eventueel) wat zijn initiele waarde is.
Je kunt het zien als het maken van een doosje (met een naam) waar je iets in kan stoppen, maar het hoeft niet direct.
In dit geval heet het doosje "a" en stoppen we er 4 in.

if(1==3)
Dit is een voorbeeld van een expressie. Je kunt dit zien aan de dubbele ==. We vragen de computer of wat tussen haakjes staat waar is, of niet waar.
Dus 1=3? geeft niet waar.
Het blok code daaronder.
{
 a=a+1;
}
wordt dus overgeslagen. (Bij een ware expressie bijvoorbeeld (1==1) was het WEL uitgevoerd).
Dit is de functie van if. Na if volgt een expressie en een blok code. Is de expressie waar, dan wordt het blok uitgevoerd, anders wordt het overgeslagen.
Op deze manier kan een programma reageren op veranderende omstandigheden. De expressie kan namelijk wat complexer zijn dan 1==3? Want dat is een domme vraag.

console.log("------------");
Dan wordt de javascript gevraagd, een boodschap af te drukken, deze vind je in je console. Dat is een speciale plek voor programmeurs. Afhankelijk van je browser vind je deze op andere plekken, vaak is f12 of ctrl-shift-i een goeie plek om te beginnen.

console.log("de waarde van a ="+ a);
Dit betekent, dat er een boodschap wordt afgedrukt. Het eerste deel van de boodschap is letterlijk en staat dus tussen aanhalingstekens. (In programmeren heet dit een string). Het tweede deel van de boodschap is de waarde van a. Deze worden samengevoegd door een plus-teken.

Het resultaat van het programma is dus:
----------------------
de waarde van a=4

Als je dit snapt, heb je al bijna alles wat nodig is om een spel te programmeren.
Even vooruitspringend naar wat je in 30 stappen gaat leren maken:


Ik zal het nog allemaal uitgebreid beschrijven als ik tijd heb, maar voor nu kun je het proces terughalen met:

donderdag 10 september 2015

7/8 games for the biggest National Dutch TV-show for children since 2007

It seems I am going to disappear for a while again , so I thought I'd take the chance to do a quick post, and explain why this happens..



SINTERKLAASJOURNAAL - NTR

Nobody knows it outside of Holland and Belgium, but it's possibly the biggest National Dutch TV-show for children from 3-9. Won a very prestigious TV prize last year..

It only airs for a month, but is on EVERY day in that period and is almost instantly the best watched program (among the target group) and stays that way up to the end. Actually it climaxes at the end, because the parents and grandparents tend to join in.

This program maintains a website, during the period that it airs. Kind of a second-screen experience, if that tells you anything. I don't make the website, but I create the games..
So second screen means for me: the games have to be web-apps that run on mobile and table as well and preferably be mobile friendly. It's about 50/50 with the desktops..
Even when it's a platform game, so we have to think about controls!

70 games


I have been making games for Sinterklaas for the past 8 years. Just call me gamePiet!
(Or is that 9? 2007-2015 you do the math.. So 2016 will be 10 years? I can only hope...)
So that means.. somewhere around 60-70 games so far! The games range from small to quite big and encompass almost all casual game types: platform, racing, physics, puzzle and click games, time management, match 3 games, toys and classics like colouring, jigsaw, memory and tetris.
We have to keep them VERY simple and visual, it's for 3-9 years old mostly. For many kids it used to be the first game-experience ever, on grandpa's lap.. I'm kind of hoping it still is.

It's fast.
It's a lot of work.
It's sooooo much fun.


You can see some of these games in my portfolio.
(www.snoepgames.nl, I'd love to publish movies about them all, but it's just so much work.. )

Production madness

What happens is:
I get to sit with the editors and get a sneak peak at the VERY classified scenario.
Then we think of about 13/14 games to fit the scenario. I make an offer for all of them and they decide on 7/8 or sometimes even 9 of them.
And then production starts...

We have 1.5 to 2 months to create all 7/8 games, depending on how long we stay in the negotiation phase. They are in HTML5 these days, used to be Flash, that was a lot easier..
Every game that's somewhat playable, I put on my website (with a password, don't bother)
to be tested by the NTR as soon as it's finished enough to test.
Nobody else can test it, the scenario of the TV-show is one of the best kept secrets in Holland.
And while I make the next game and the next one, the bugs, misconceptions and tips start coming in for triage. So planning is a bit strange if not impossible. I plan about 3 days solid per game and then I'll just have to wing it. The triage is serious business. I couldn't do it without that principle, it's a battlefield.

But I haven't missed a beat yet, even the year when we moved from Flash to HTML5. That was a personal victory, but it's allways been close. It's nuts, but I love it.

Bugs and 'complaints'

Now I won't claim the games are completely bug-free or as elegant or polished as I normally like to make them, when I have more time. It's a bit rough around the edges.. Partially intentional. The style is great for simple and rough animation. But programming can't be too rough..
So last year we introduced something new. If there was a problem with the game, kids could tell us (me), so I could fix it right away. This was a safety measure I built in, because we had a couple of games with extra levels, which is always harder to test in advance.

We had 2,2 MILLION unique visitors in a month, that year. Games were played up to 60.000 times in a single day. That is an amazing amount of traffic. (There are about 2,5 million families in the Netherlands!). So about 80% of the Dutch population of kids..
And we got 23 'complaints':

  • 4 of these 'complaints' were actually compliments. (Those meant the world to me, especially the one from the grandpa and grandchild, you know who you are!)
  • 7 were little things I could fix in minutes.
  • 12 were just general internet abuse ( like 'testing123 and BlahBlah') and a lot of stuff about the so-called 'zwarte piet' issue.
    (I have no fixed opinion about it, other than both sides should refrain from violence and abuse. It's a heated debate, too heated to take either side very seriously)

It was amazing.. The absence of complaints was overwhelming.

Trust and Focus


The thing that makes it work, despite the obvious obstacles?
  Trust, focus, positivity.. Sounds soppy, I know, but I mean it.

I get a lot of freedom, more than with any customer.. That takes a lot of trust.
But else this would be completely impossible, given the timeframe.
There is a lot of trust, both ways. I trust the editors to test the games, for my part I make an offer, that has so much specifications in it, it takes me more than three full days to write and I know at least 4 games are going to be dropped anyway so that's 40% of the work out of the window and I haven't even gotten a 'go' yet.
But this way, when we do produce... We all know what to expect going in. I try to accomodate as much evolving insight as I dare during production, but there is little room for it.
And most of all, we trust each other to stay positive, because with this kind of pressure, you can kill it if you don't stay positive. It has to be fun all-round, every day, or you'll never make it.
It almost went wrong twice, but both times we fixed it in the nick of time.
This is only possible, because at that moment Sinterklaas is truly the most important thing in the world.
For all of us.


Around 10th of November the games start going live. One every three or four days.
If anything is wrong, I need to fix it quickly, so I'm on call all day.
We've cut it as close as 20 minutes away from LIVE.. I never want to relive that moment..

And 5th of December it's all over.
I take a deep breath and relax a couple of days,
and then start hoping they will ask me again next year.

donderdag 2 januari 2014

Hybrid Platform. HTML5 and Flash Games.

2013 saw the birth of the “Hybrid Game Platform”. This platform is basically a compatibility layer written simultaneously in Flash and Javascript.

This means, you can write game code once and export as Flash and HTML5. Now this may seem unimpressive, as adobe claims the same thing with createJS. But the fact is, that there needs to be a lot of conversion, before your game works again.

With the Hybrid platform we are talking the same language in both cases. The main advantage of this is, your choice of platform is now a lot wider.
Mobile platforms have a very limited support for Flash, but older browsers don't support HTML5.
The Hybrid platform analyses the browsers capabilities and chooses the right techniques to display.

This is best practice anyway, but used to mean, you had two versions of the software that had to be written.
And if you change anything (like texts, logo's etc) you have to change it twice. This lead to more cost.
With the deployment of the hybrid platform, this cost is literally cut in half, because you only have to do everything once.
Also, by keeping Flash as an option, you can then convert the Flash-apps to Mobile Apps quite easily.
All round flexibility for a very low price.

The Hybride Platform has been succesfully deployed for three different clients in 2013.
(Sinterklaasjournaal (just HTML5, no flash fallback), AanZee vakantiehuizen, VillaXL).

I will add links when these go live 15th of  januari 2014!

Hybrid Site Platform
I'm now working on a similar platform for sites.


zondag 6 oktober 2013

Icons in scrolling menu

As an experiment I am writing this on my iPad.
Testing things in a touch environment can be a bit trying if you are used to working in a desktop environment like me.
So I've been playing around with jsfiddle directly on the iPad. It takes some getting used to, but it's possible. As a proof of concept I made this:

http://jsfiddle.net/RfDv6/7/

dinsdag 24 september 2013

Sinterklaas journaal games

For the Dutch site Sinterklaasjournaal, I'll be creating 7 pure HTML 5 games in the next 6 weeks.
So my deadline is somewhere around 30th of octobre.

The games need to be scalable (lineair like Flash) and respond to touch. On the Ipad sound will be just a music. On all other devices sound will be implemented.
I will create a preloader, splashpage and game per game.
Some games will have levels.

This may seem like a gargantuan task, but I have the benefit of having made many a flash game, even in the time when this wasn't easy. AS1 was buggy as hell and there was no real way to debug it.
Even so, I made a game in a couple of days in as3.

Now I face the same kind of technical difficulties: Dom-sprites are fine, but if you have to scale them lineairly aswell, you cannot use the background property,because background scaling is not supported in all browsers.
So I'll use a div and another clipping div as sprites.
This will give me very limited performance, so where ever possible, I'll not use spritesheets at all.

Primitive, but the result should work just about anywhere, because I'll use only div, overflow hidden, top and left.. I use Jquery to navigate around the DOM (and for little else).

I'll be posting examples and general findings here, in the coming six weeks or if I really don't have the time to post, later after I'm finished.

For now, my finding is that with this technique my Phone (HTC Desire, with Android 2.2.2) is the bottleneck performance wise. But I have succesfully had 20 moving sprites, depthsorted live at 25 fps, with multitouch enabled, this gives me hope.