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?

  • Harack

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

  • Vitor

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

  • shark255

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

  • ami

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

  • angelo

    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

  • angelo

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

  • Santhosh

    For those who see only black pattern instead of the images within the pieces change the shapes fill property
    from this

    to this

  • Santhosh

    Thanks for such a great tutorial :) It would be so nice if you can show us how we can shuffle, join and validate the pieces.

  • Sha

    I got this error when i run this code, can you please let me know the reason?Uncaught TypeError: Object # has no method ‘getContext’