开发者

How to create jigsaw puzzle from an image using javascript

开发者 https://www.devze.com 2023-01-23 11:31 出处:网络
I googled it but didn\'t find a good answer. Specifically, I want to learn: to slice an image into curved pieces

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 a开发者_高级运维ssume that i need this to reassemble)

thanks.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消