Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create jigsaw puzzle from an image using javascript

I googled it but didn't find a good answer. Specifically, I want to learn:

  • to slice an image into curved pieces
  • to create individual objects from those pieces (i assume that i need this to reassemble)

thanks.

like image 221
mdikici Avatar asked Nov 01 '10 08:11

mdikici


People also ask

Can I get a picture turned into a puzzle?

Pictorem offers personalized puzzles in a variety of different sizes with either artwork or a favorite image printed on a photo puzzle. These custom jigsaw puzzles can showcase favorite memories and make an excellent personalized gift for Christmas, a wedding anniversary, or as a gift, the whole family can enjoy.


2 Answers

There are several pieces to this puzzle. :)

The first piece is SVG and its Canvas. That's what you'll need to draw, because otherwise you can't make a curved piece out of a picture. Only rectangles are possible with standard HTML/CSS.

The second piece is an algorithm for generating jigsaw pieces from the picture. Google should help you with that if you can't figure one out by yourself (though it doesn't seem very complicated).

The rest should be straightforward.

Added: A quick Google search gave just such a jigsaw engine in the first result. Check out the source of that.

like image 117
Vilx- Avatar answered Sep 27 '22 20:09

Vilx-


I'll assume the image you want to saw to pieces is a raster image with a resolution that you will use for the puzzle pieces, call that /picture/. Also, I assume you have the edges along which you wish to saw in a second raster image with the same dimensions, call that /raster/. Then your problem amounts to determining all connected areas in the raster. Each pixel of the raster gets annotated with the id of the jigsaw piece it belongs to, initially 'none', -1 or whatever. Then your algorithm scans across all pixels in the raster, skipping pixels that already belong to a piece. For each unassigned piece it executes a flood fill, "coloring" the pixels with the pieces id (e.g. number). In a second scan, after allocating an image for each piece, you add the corresponding pixels of the image to the piece. As part of your first pass you can maintain for each piece id the bounding box. That allows you to allocate the the images for the pieces to their proper dimensions.

You need a suitable convention to deal with border pixels: e.g. border pixels to the right belong to the piece if they have the same x-position, but are above they also belong to the piece.

like image 21
Marko Avatar answered Sep 27 '22 22:09

Marko