Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Buy the book

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

Buy the book

Creation of a jigsaw puzzle using HTML5 Canvas and KineticJS – step3: jigsaw generation

Here we go with the thir part of the jigsaw puzzle generator made with KineticJS. In the first step we splitted an image into pieces, in the second step we created a jigsaw piece shape, and now it’s time to cut an image into randomly generated pieces.

The code is still uncommented but the concept is:

* Any piece has four sides
* Each side can have a “hill” or a “hole”, with the exception of sides on puzzle borders which are straight
* Starting from the upper left piece, we randomly decide if left and bottom sides will be “hill” or “hole”
* Accordingly, leftmost and upper pieces (if any) will have “hole” to host “hill” or “hill” to host “hole”

This is the code:

And you can see the final result here.

Pick and drag pieces around

Is there something more you would like to see about this topic? Maybe how to rotate and join pieces?

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 5.00 out of 5)
Loading ... Loading ...
Template monster proudly presents HTML Templates in order to meet its customers\' demands.
These HTML Templates can be easily edited with any HTML editor including the simplest one - Notepad.
They include .html files which makes them available for HTML editing.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 6 comments

  1. Harack

    on January 9, 2013 at 3:32 am

    Seems to lag a bit more than it should when dragging pieces.

  2. Vitor

    on January 9, 2013 at 6:41 pm

    Hi! How many marriage purposes do you receive each week, Emanuele? This is one! :D

  3. shark255

    on January 14, 2013 at 9:18 pm

    maybe offtop: do you know any solutions how to use kineticjs with box2d library togeter?

  4. ami

    on February 6, 2013 at 12:22 am

    hi,
    nice, very nice, but if I try to unvisible the stroke, I show a small gaps between the piece, how can I fix it?

  5. angelo

    on April 13, 2013 at 1:31 pm

    your example doesn’t show nothing except a skeleton jigsaw board without pictures and also the lesson/post n.4 it doesn’t scroll at all. I loaded your page on firefox 20.0 and chrome latest version with same result. I suggest to have a look.
    Thanks a lot

  6. angelo

    on April 13, 2013 at 1:32 pm

    Sorry on firefox this example is shown but not in chrome while n.4 doesn’t scroll at all in both browsers.